twins/server.go

448 lines
10 KiB
Go
Raw Normal View History

package main
import (
"bufio"
"bytes"
"crypto/tls"
2020-11-04 20:48:55 +00:00
"crypto/x509"
"fmt"
"log"
"net"
"net/url"
"os"
"path"
2020-11-05 04:18:59 +00:00
"regexp"
"strconv"
"strings"
2020-11-12 17:56:59 +00:00
"sync"
2020-10-30 00:17:23 +00:00
"time"
"unicode/utf8"
)
2020-11-04 20:48:55 +00:00
const (
readTimeout = 30 * time.Second
urlMaxLength = 1024
2020-11-10 04:10:53 +00:00
geminiType = "text/gemini; charset=utf-8"
2020-11-16 17:17:42 +00:00
logTimeFormat = "2006-01-02 15:04:05"
2020-11-04 20:48:55 +00:00
)
const (
statusInput = 10
statusSensitiveInput = 11
statusSuccess = 20
statusRedirectTemporary = 30
statusRedirectPermanent = 31
statusTemporaryFailure = 40
statusUnavailable = 41
statusCGIError = 42
statusProxyError = 43
statusPermanentFailure = 50
statusNotFound = 51
statusGone = 52
statusProxyRequestRefused = 53
statusBadRequest = 59
)
2020-10-30 00:17:23 +00:00
2020-11-05 04:18:59 +00:00
var slashesRegexp = regexp.MustCompile(`[^\\]\/`)
2020-11-05 20:57:28 +00:00
var newLine = "\r\n"
2020-11-12 17:56:59 +00:00
var logLock sync.Mutex
2020-10-30 00:17:23 +00:00
2020-11-12 17:56:59 +00:00
func writeHeader(c net.Conn, code int, meta string) int {
fmt.Fprintf(c, "%d %s%s", code, meta, newLine)
return code
}
2020-11-12 17:56:59 +00:00
func writeStatus(c net.Conn, code int) int {
var meta string
switch code {
2020-11-04 20:48:55 +00:00
case statusTemporaryFailure:
meta = "Temporary failure"
2020-11-04 20:48:55 +00:00
case statusProxyError:
2020-10-30 00:17:23 +00:00
meta = "Proxy error"
2020-11-04 20:48:55 +00:00
case statusBadRequest:
meta = "Bad request"
2020-11-04 20:48:55 +00:00
case statusNotFound:
meta = "Not found"
2020-11-04 20:48:55 +00:00
case statusProxyRequestRefused:
meta = "Proxy request refused"
}
writeHeader(c, code, meta)
2020-11-12 17:56:59 +00:00
return code
}
2020-11-12 17:56:59 +00:00
func writeSuccess(c net.Conn, serve *pathConfig, contentType string, size int64) int {
2020-11-10 04:10:53 +00:00
meta := contentType
if serve.Type != "" {
meta = serve.Type
}
if !config.DisableSize && size >= 0 {
meta += fmt.Sprintf("; size=%d", size)
}
2020-11-10 05:12:22 +00:00
if serve.cache != cacheUnset {
meta += fmt.Sprintf("; cache=%d", serve.cache)
2020-11-10 04:10:53 +00:00
}
writeHeader(c, statusSuccess, meta)
2020-11-12 17:56:59 +00:00
return statusSuccess
2020-11-10 04:10:53 +00:00
}
func scanCRLF(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.IndexByte(data, '\r'); i >= 0 {
// We have a full newline-terminated line.
return i + 1, data[0:i], nil
}
// If we're at EOF, we have a final, non-terminated line. Return it.
if atEOF {
return len(data), data, nil
}
// Request more data.
return 0, nil, nil
}
2020-11-05 04:18:59 +00:00
func replaceWithUserInput(command []string, request *url.URL) []string {
newCommand := make([]string, len(command))
copy(newCommand, command)
for i, piece := range newCommand {
if strings.Contains(piece, "$USERINPUT") {
2020-11-05 04:18:59 +00:00
requestQuery, err := url.QueryUnescape(request.RawQuery)
if err == nil {
newCommand[i] = strings.ReplaceAll(piece, "$USERINPUT", requestQuery)
}
}
}
return newCommand
}
2020-11-12 17:56:59 +00:00
func servePath(c *tls.Conn, request *url.URL, serve *pathConfig) (int, int64) {
2020-11-05 04:18:59 +00:00
resolvedPath := request.Path
requestSplit := strings.Split(request.Path, "/")
pathSlashes := len(slashesRegexp.FindAllStringIndex(serve.Path, -1))
if len(serve.Path) > 0 {
if serve.Path[0] == '/' {
pathSlashes++ // Regexp does not match starting slash
}
if serve.Path[len(serve.Path)-1] != '/' {
pathSlashes++
}
}
if len(requestSplit) >= pathSlashes {
resolvedPath = strings.Join(requestSplit[pathSlashes:], "/")
2020-11-05 04:18:59 +00:00
}
if !serve.HiddenFiles {
for _, piece := range requestSplit {
if len(piece) > 0 && piece[0] == '.' {
2020-11-12 17:56:59 +00:00
return writeStatus(c, statusTemporaryFailure), -1
}
}
}
var filePath string
if serve.Root != "" {
root := serve.Root
if root[len(root)-1] != '/' {
root += "/"
}
if !serve.SymLinks {
for i := range requestSplit[pathSlashes:] {
info, err := os.Lstat(path.Join(root, strings.Join(requestSplit[pathSlashes:pathSlashes+i+1], "/")))
if err != nil || info.Mode()&os.ModeSymlink == os.ModeSymlink {
2020-11-12 17:56:59 +00:00
return writeStatus(c, statusTemporaryFailure), -1
}
}
}
filePath = path.Join(root, resolvedPath)
}
2020-11-05 04:18:59 +00:00
if serve.Proxy != "" {
2020-11-12 17:56:59 +00:00
return serveProxy(c, request, serve.Proxy), -1
2020-11-05 04:18:59 +00:00
} else if serve.FastCGI != "" {
if filePath == "" {
2020-11-12 17:56:59 +00:00
return writeStatus(c, statusNotFound), -1
}
2020-11-10 04:10:53 +00:00
contentType := geminiType
2020-11-05 04:18:59 +00:00
if serve.Type != "" {
contentType = serve.Type
}
2020-11-10 04:10:53 +00:00
writeSuccess(c, serve, contentType, -1)
2020-10-30 00:17:23 +00:00
2020-11-05 04:18:59 +00:00
serveFastCGI(c, config.fcgiPools[serve.FastCGI], request, filePath)
2020-11-12 17:56:59 +00:00
return statusSuccess, -1
2020-11-05 04:18:59 +00:00
} else if serve.cmd != nil {
requireInput := serve.Input != "" || serve.SensitiveInput != ""
if requireInput {
newCommand := replaceWithUserInput(serve.cmd, request)
if newCommand != nil {
2020-11-12 17:56:59 +00:00
return serveCommand(c, serve, request, newCommand)
2020-11-05 04:18:59 +00:00
}
}
2020-11-12 17:56:59 +00:00
return serveCommand(c, serve, request, serve.cmd)
2020-11-05 04:18:59 +00:00
}
if filePath == "" {
2020-11-12 17:56:59 +00:00
return writeStatus(c, statusNotFound), -1
2020-11-05 04:18:59 +00:00
}
fi, err := os.Stat(filePath)
if err != nil {
2020-11-12 17:56:59 +00:00
return writeStatus(c, statusNotFound), -1
}
mode := fi.Mode()
hasTrailingSlash := len(request.Path) > 0 && request.Path[len(request.Path)-1] == '/'
if mode.IsDir() {
if !hasTrailingSlash {
2020-11-12 17:56:59 +00:00
return writeHeader(c, statusRedirectPermanent, request.String()+"/"), -1
}
_, err := os.Stat(path.Join(filePath, "index.gmi"))
if err != nil {
_, err := os.Stat(path.Join(filePath, "index.gemini"))
if err != nil {
2020-11-16 17:17:42 +00:00
if serve.List {
2020-11-12 17:56:59 +00:00
return serveDirList(c, serve, request, filePath), -1
}
2020-11-12 17:56:59 +00:00
return writeStatus(c, statusNotFound), -1
}
filePath = path.Join(filePath, "index.gemini")
} else {
filePath = path.Join(filePath, "index.gmi")
}
} else if hasTrailingSlash && len(request.Path) > 1 {
r := request.String()
2020-11-12 17:56:59 +00:00
return writeHeader(c, statusRedirectPermanent, r[:len(r)-1]), -1
}
2020-11-10 04:10:53 +00:00
serveFile(c, serve, filePath)
2020-11-12 17:56:59 +00:00
return statusSuccess, fi.Size()
}
func handleRequest(c *tls.Conn, request *url.URL, requestData string) (int, int64, string) {
if request.Path == "" {
// Redirect to /
return writeHeader(c, statusRedirectPermanent, requestData+"/"), -1, ""
}
pathBytes := []byte(request.Path)
strippedPath := request.Path
if strippedPath[0] == '/' {
strippedPath = strippedPath[1:]
}
var matchedHost bool
requestHostname := request.Hostname()
for hostname := range config.Hosts {
if requestHostname != hostname {
continue
}
matchedHost = true
for _, serve := range config.Hosts[hostname].Paths {
matchedRegexp := serve.r != nil && serve.r.Match(pathBytes)
matchedPrefix := serve.r == nil && strings.HasPrefix(request.Path, serve.Path)
if !matchedRegexp && !matchedPrefix {
continue
}
requireInput := serve.Input != "" || serve.SensitiveInput != ""
if request.RawQuery == "" && requireInput {
if serve.Input != "" {
return writeHeader(c, statusInput, serve.Input), -1, ""
} else if serve.SensitiveInput != "" {
return writeHeader(c, statusSensitiveInput, serve.SensitiveInput), -1, ""
}
}
if matchedRegexp || matchedPrefix {
status, size := servePath(c, request, serve)
return status, size, serve.Log
}
}
break
}
if matchedHost {
return writeStatus(c, statusNotFound), -1, ""
}
return writeStatus(c, statusProxyRequestRefused), -1, ""
2020-11-05 04:18:59 +00:00
}
2020-11-12 17:56:59 +00:00
func handleConn(c *tls.Conn) {
t := time.Now()
var request *url.URL
var logPath string
status := 0
size := int64(-1)
defer func() {
if !verbose && logPath == "" {
return
}
entry := logEntry(request, status, size, time.Since(t))
if verbose {
log.Println(string(entry))
}
if logPath == "" {
return
}
logLock.Lock()
defer logLock.Unlock()
f, err := os.OpenFile(logPath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
log.Printf("ERROR: Failed to open log file at %s: %s", logPath, err)
return
}
defer f.Close()
if _, err = f.Write(entry); err != nil {
log.Printf("ERROR: Failed to write to log file at %s: %s", logPath, err)
return
}
f.Write([]byte("\n"))
}()
defer c.Close()
c.SetReadDeadline(time.Now().Add(readTimeout))
var requestData string
scanner := bufio.NewScanner(c)
2020-11-05 20:57:28 +00:00
if !config.SaneEOL {
scanner.Split(scanCRLF)
}
if scanner.Scan() {
requestData = scanner.Text()
}
if err := scanner.Err(); err != nil {
2020-11-12 17:56:59 +00:00
status = writeStatus(c, statusBadRequest)
return
}
2020-11-04 20:48:55 +00:00
state := c.ConnectionState()
certs := state.PeerCertificates
var clientCertKeys [][]byte
for _, cert := range certs {
pubKey, err := x509.MarshalPKIXPublicKey(cert.PublicKey)
if err != nil {
continue
}
clientCertKeys = append(clientCertKeys, pubKey)
}
if len(requestData) > urlMaxLength || !utf8.ValidString(requestData) {
2020-11-12 17:56:59 +00:00
status = writeStatus(c, statusBadRequest)
return
}
2020-11-12 17:56:59 +00:00
var err error
request, err = url.Parse(requestData)
if err != nil {
2020-11-12 17:56:59 +00:00
status = writeStatus(c, statusBadRequest)
return
}
requestHostname := request.Hostname()
if requestHostname == "" || strings.ContainsRune(requestHostname, ' ') {
2020-11-12 17:56:59 +00:00
status = writeStatus(c, statusBadRequest)
return
}
var requestPort int
if request.Port() != "" {
requestPort, err = strconv.Atoi(request.Port())
if err != nil {
requestPort = 0
}
}
if request.Scheme == "" {
request.Scheme = "gemini"
}
if request.Scheme != "gemini" || (requestPort > 0 && requestPort != config.port) {
2020-11-12 17:56:59 +00:00
status = writeStatus(c, statusProxyRequestRefused)
return
}
2020-11-12 17:56:59 +00:00
status, size, logPath = handleRequest(c, request, requestData)
}
2020-11-12 17:56:59 +00:00
func logEntry(request *url.URL, status int, size int64, elapsed time.Duration) []byte {
hostFormatted := "-"
2020-11-16 17:17:42 +00:00
pathFormatted := "-"
sizeFormatted := "-"
if request != nil {
if request.Path != "" {
pathFormatted = request.Path
}
if request.Hostname() != "" {
hostFormatted = request.Hostname()
if request.Port() != "" {
hostFormatted += ":" + request.Port()
} else {
hostFormatted += ":1965"
}
2020-10-30 00:17:23 +00:00
}
}
2020-11-12 17:56:59 +00:00
if size >= 0 {
sizeFormatted = strconv.FormatInt(size, 10)
}
2020-11-16 17:17:42 +00:00
return []byte(fmt.Sprintf(`%s - - - [%s] "GET %s Gemini" %d %s %.4f`, hostFormatted, time.Now().Format(logTimeFormat), pathFormatted, status, sizeFormatted, elapsed.Seconds()))
2020-11-12 17:56:59 +00:00
}
2020-11-05 04:18:59 +00:00
2020-11-12 17:56:59 +00:00
func handleListener(l net.Listener) {
for {
conn, err := l.Accept()
if err != nil {
log.Fatal(err)
}
2020-11-05 04:18:59 +00:00
2020-11-12 17:56:59 +00:00
go handleConn(conn.(*tls.Conn))
}
2020-11-05 04:18:59 +00:00
}
func getCertificate(info *tls.ClientHelloInfo) (*tls.Certificate, error) {
host := config.Hosts[info.ServerName]
if host != nil {
return host.cert, nil
}
for _, host := range config.Hosts {
return host.cert, nil
}
return nil, nil
}
func listen(address string) {
tlsConfig := &tls.Config{
ClientAuth: tls.RequestClientCert,
GetCertificate: getCertificate,
}
listener, err := tls.Listen("tcp", address, tlsConfig)
if err != nil {
log.Fatalf("failed to listen on %s: %s", address, err)
}
handleListener(listener)
}