Support logging requests

Resolves #6.
This commit is contained in:
Trevor Slocum 2020-11-12 09:56:59 -08:00
parent 335a90c5c5
commit 7820dd9723
10 changed files with 182 additions and 156 deletions

View file

@ -106,6 +106,11 @@ Cache duration (in seconds). Set to `0` to disable caching entirely. This is an
out-of-spec feature. See [PROPOSALS.md](https://gitlab.com/tslocum/twins/blob/master/PROPOSALS.md) out-of-spec feature. See [PROPOSALS.md](https://gitlab.com/tslocum/twins/blob/master/PROPOSALS.md)
for more information. for more information.
##### Log
Path to log file. Requests are logged in [Apache format](https://httpd.apache.org/docs/2.2/logs.html#combined),
excluding IP address and query.
##### SymLinks ##### SymLinks
When enabled, symbolic links may be accessed. This attribute is disabled by default. When enabled, symbolic links may be accessed. This attribute is disabled by default.
@ -169,6 +174,7 @@ hosts:
default: # Default host configuration default: # Default host configuration
paths: # Default path attributes paths: # Default path attributes
- -
log: /srv/log/gemini.log
symlinks: true # Follow symbolic links symlinks: true # Follow symbolic links
gemini.rocks: gemini.rocks:
cert: /srv/gemini.rocks/data/cert.crt cert: /srv/gemini.rocks/data/cert.crt

View file

@ -49,6 +49,9 @@ type pathConfig struct {
// FastCGI server address // FastCGI server address
FastCGI string FastCGI string
// Log file
Log string
r *regexp.Regexp r *regexp.Regexp
cmd []string cmd []string
cache int64 cache int64
@ -162,6 +165,9 @@ func readconfig(configPath string) error {
if defaultPath.FastCGI != "" && serve.FastCGI == "" { if defaultPath.FastCGI != "" && serve.FastCGI == "" {
serve.FastCGI = defaultPath.FastCGI serve.FastCGI = defaultPath.FastCGI
} }
if defaultPath.Log != "" && serve.Log == "" {
serve.Log = defaultPath.Log
}
} }
} else if len(defaultHost.Paths) > 1 { } else if len(defaultHost.Paths) > 1 {
log.Fatal("only one path may be defined for the default host") log.Fatal("only one path may be defined for the default host")

2
go.mod
View file

@ -6,6 +6,6 @@ require (
github.com/h2non/filetype v1.1.0 github.com/h2non/filetype v1.1.0
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/yookoala/gofast v0.4.1-0.20201013050739-975113c54107 github.com/yookoala/gofast v0.4.1-0.20201013050739-975113c54107
golang.org/x/tools v0.0.0-20201110030525-169ad6d6ecb2 // indirect golang.org/x/tools v0.0.0-20201112171726-b38955972a18 // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
) )

4
go.sum
View file

@ -34,8 +34,8 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200908211811-12e1bf57a112/go.mod h1:Cj7w3i3Rnn0Xh82ur9kSqwfTHTeVxaDqrfMjpcNT6bE= golang.org/x/tools v0.0.0-20200908211811-12e1bf57a112/go.mod h1:Cj7w3i3Rnn0Xh82ur9kSqwfTHTeVxaDqrfMjpcNT6bE=
golang.org/x/tools v0.0.0-20201110030525-169ad6d6ecb2 h1:5GmCe1Mc5HsGGl6E0kOVQRzVp+AgZf4Ffw4DadiVpd4= golang.org/x/tools v0.0.0-20201112171726-b38955972a18 h1:zCVX0Qx6zEiwi5lM2jprfSFA6i6GWMXmY8o0VxPyCfo=
golang.org/x/tools v0.0.0-20201110030525-169ad6d6ecb2/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.0.0-20201112171726-b38955972a18/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

View file

@ -16,6 +16,8 @@ func init() {
var verbose bool var verbose bool
func main() { func main() {
log.SetFlags(0)
configFile := flag.String("config", "", "path to configuration file") configFile := flag.String("config", "", "path to configuration file")
flag.BoolVar(&verbose, "verbose", false, "print request and response information") flag.BoolVar(&verbose, "verbose", false, "print request and response information")
flag.Parse() flag.Parse()
@ -45,7 +47,5 @@ func main() {
log.Fatalf("failed to read configuration file at %s: %v\nSee CONFIGURATION.md for information on configuring twins", *configFile, err) log.Fatalf("failed to read configuration file at %s: %v\nSee CONFIGURATION.md for information on configuring twins", *configFile, err)
} }
log.Printf("twins running on %s:%d", config.hostname, config.port)
listen(config.Listen) listen(config.Listen)
} }

View file

@ -2,14 +2,13 @@ package main
import ( import (
"bytes" "bytes"
"log"
"net" "net"
"net/url" "net/url"
"os/exec" "os/exec"
"strings" "strings"
) )
func serveCommand(c net.Conn, serve *pathConfig, request *url.URL, command []string) { func serveCommand(c net.Conn, serve *pathConfig, request *url.URL, command []string) (int, int64) {
var args []string var args []string
if len(command) > 0 { if len(command) > 0 {
args = command[1:] args = command[1:]
@ -20,8 +19,7 @@ func serveCommand(c net.Conn, serve *pathConfig, request *url.URL, command []str
if request.RawQuery != "" { if request.RawQuery != "" {
requestQuery, err := url.QueryUnescape(request.RawQuery) requestQuery, err := url.QueryUnescape(request.RawQuery)
if err != nil { if err != nil {
writeStatus(c, statusBadRequest) return writeStatus(c, statusBadRequest), -1
return
} }
cmd.Stdin = strings.NewReader(requestQuery + "\n") cmd.Stdin = strings.NewReader(requestQuery + "\n")
} }
@ -30,14 +28,11 @@ func serveCommand(c net.Conn, serve *pathConfig, request *url.URL, command []str
err := cmd.Run() err := cmd.Run()
if err != nil { if err != nil {
writeStatus(c, statusProxyError) return writeStatus(c, statusProxyError), -1
return
} }
writeSuccess(c, serve, geminiType, int64(buf.Len())) writeSuccess(c, serve, geminiType, int64(buf.Len()))
c.Write(buf.Bytes()) c.Write(buf.Bytes())
if verbose { return statusSuccess, int64(buf.Len())
log.Printf("< %s\n", command)
}
} }

View file

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"io" "io"
"io/ioutil" "io/ioutil"
"log"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
@ -55,8 +54,4 @@ func serveFastCGI(c net.Conn, connFactory gofast.ConnFactory, u *url.URL, filePa
gofast.SimpleClientFactory(connFactory, 0), gofast.SimpleClientFactory(connFactory, 0),
). ).
ServeHTTP(newResponseWriter(c), r) ServeHTTP(newResponseWriter(c), r)
if verbose {
log.Printf("< exec %s\n", filePath)
}
} }

View file

@ -13,7 +13,7 @@ import (
"github.com/h2non/filetype" "github.com/h2non/filetype"
) )
func serveDirList(c net.Conn, serve *pathConfig, request *url.URL, dirPath string) { func serveDirList(c net.Conn, serve *pathConfig, request *url.URL, dirPath string) int {
var ( var (
files []os.FileInfo files []os.FileInfo
numDirs int numDirs int
@ -37,8 +37,7 @@ func serveDirList(c net.Conn, serve *pathConfig, request *url.URL, dirPath strin
return nil return nil
}) })
if err != nil { if err != nil {
writeStatus(c, statusTemporaryFailure) return writeStatus(c, statusTemporaryFailure)
return
} }
// List directories first // List directories first
sort.Slice(files, func(i, j int) bool { sort.Slice(files, func(i, j int) bool {
@ -91,6 +90,7 @@ func serveDirList(c net.Conn, serve *pathConfig, request *url.URL, dirPath strin
} }
c.Write([]byte(modified + " - " + formatFileSize(info.Size()) + newLine + newLine)) c.Write([]byte(modified + " - " + formatFileSize(info.Size()) + newLine + newLine))
} }
return statusSuccess
} }
func serveFile(c net.Conn, serve *pathConfig, filePath string) { func serveFile(c net.Conn, serve *pathConfig, filePath string) {

View file

@ -3,15 +3,12 @@ package main
import ( import (
"crypto/tls" "crypto/tls"
"io" "io"
"log"
"net" "net"
"net/url" "net/url"
"strings" "strings"
) )
func serveProxy(c net.Conn, request *url.URL, proxyURL string) { func serveProxy(c net.Conn, request *url.URL, proxyURL string) int {
original := proxyURL
tlsConfig := &tls.Config{} tlsConfig := &tls.Config{}
if strings.HasPrefix(proxyURL, "gemini://") { if strings.HasPrefix(proxyURL, "gemini://") {
proxyURL = proxyURL[9:] proxyURL = proxyURL[9:]
@ -21,8 +18,7 @@ func serveProxy(c net.Conn, request *url.URL, proxyURL string) {
} }
proxy, err := tls.Dial("tcp", proxyURL, tlsConfig) proxy, err := tls.Dial("tcp", proxyURL, tlsConfig)
if err != nil { if err != nil {
writeStatus(c, statusProxyError) return writeStatus(c, statusProxyError)
return
} }
defer proxy.Close() defer proxy.Close()
@ -33,7 +29,5 @@ func serveProxy(c net.Conn, request *url.URL, proxyURL string) {
// Forward response // Forward response
io.Copy(c, proxy) io.Copy(c, proxy)
if verbose { return statusSuccess
log.Printf("< %s\n", original)
}
} }

280
server.go
View file

@ -14,6 +14,7 @@ import (
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"unicode/utf8" "unicode/utf8"
) )
@ -51,15 +52,14 @@ var slashesRegexp = regexp.MustCompile(`[^\\]\/`)
var newLine = "\r\n" var newLine = "\r\n"
func writeHeader(c net.Conn, code int, meta string) { var logLock sync.Mutex
fmt.Fprintf(c, "%d %s%s", code, meta, newLine)
if verbose { func writeHeader(c net.Conn, code int, meta string) int {
log.Printf("< %d %s\n", code, meta) fmt.Fprintf(c, "%d %s%s", code, meta, newLine)
} return code
} }
func writeStatus(c net.Conn, code int) { func writeStatus(c net.Conn, code int) int {
var meta string var meta string
switch code { switch code {
case statusTemporaryFailure: case statusTemporaryFailure:
@ -74,9 +74,10 @@ func writeStatus(c net.Conn, code int) {
meta = "Proxy request refused" meta = "Proxy request refused"
} }
writeHeader(c, code, meta) writeHeader(c, code, meta)
return code
} }
func writeSuccess(c net.Conn, serve *pathConfig, contentType string, size int64) { func writeSuccess(c net.Conn, serve *pathConfig, contentType string, size int64) int {
meta := contentType meta := contentType
if serve.Type != "" { if serve.Type != "" {
meta = serve.Type meta = serve.Type
@ -91,6 +92,7 @@ func writeSuccess(c net.Conn, serve *pathConfig, contentType string, size int64)
} }
writeHeader(c, statusSuccess, meta) writeHeader(c, statusSuccess, meta)
return statusSuccess
} }
func scanCRLF(data []byte, atEOF bool) (advance int, token []byte, err error) { func scanCRLF(data []byte, atEOF bool) (advance int, token []byte, err error) {
@ -123,7 +125,7 @@ func replaceWithUserInput(command []string, request *url.URL) []string {
return newCommand return newCommand
} }
func servePath(c *tls.Conn, request *url.URL, serve *pathConfig) { func servePath(c *tls.Conn, request *url.URL, serve *pathConfig) (int, int64) {
resolvedPath := request.Path resolvedPath := request.Path
requestSplit := strings.Split(request.Path, "/") requestSplit := strings.Split(request.Path, "/")
pathSlashes := len(slashesRegexp.FindAllStringIndex(serve.Path, -1)) pathSlashes := len(slashesRegexp.FindAllStringIndex(serve.Path, -1))
@ -142,8 +144,7 @@ func servePath(c *tls.Conn, request *url.URL, serve *pathConfig) {
if !serve.HiddenFiles { if !serve.HiddenFiles {
for _, piece := range requestSplit { for _, piece := range requestSplit {
if len(piece) > 0 && piece[0] == '.' { if len(piece) > 0 && piece[0] == '.' {
writeStatus(c, statusTemporaryFailure) return writeStatus(c, statusTemporaryFailure), -1
return
} }
} }
} }
@ -159,8 +160,7 @@ func servePath(c *tls.Conn, request *url.URL, serve *pathConfig) {
for i := range requestSplit[pathSlashes:] { for i := range requestSplit[pathSlashes:] {
info, err := os.Lstat(path.Join(root, strings.Join(requestSplit[pathSlashes:pathSlashes+i+1], "/"))) info, err := os.Lstat(path.Join(root, strings.Join(requestSplit[pathSlashes:pathSlashes+i+1], "/")))
if err != nil || info.Mode()&os.ModeSymlink == os.ModeSymlink { if err != nil || info.Mode()&os.ModeSymlink == os.ModeSymlink {
writeStatus(c, statusTemporaryFailure) return writeStatus(c, statusTemporaryFailure), -1
return
} }
} }
} }
@ -169,12 +169,10 @@ func servePath(c *tls.Conn, request *url.URL, serve *pathConfig) {
} }
if serve.Proxy != "" { if serve.Proxy != "" {
serveProxy(c, request, serve.Proxy) return serveProxy(c, request, serve.Proxy), -1
return
} else if serve.FastCGI != "" { } else if serve.FastCGI != "" {
if filePath == "" { if filePath == "" {
writeStatus(c, statusNotFound) return writeStatus(c, statusNotFound), -1
return
} }
contentType := geminiType contentType := geminiType
@ -184,37 +182,32 @@ func servePath(c *tls.Conn, request *url.URL, serve *pathConfig) {
writeSuccess(c, serve, contentType, -1) writeSuccess(c, serve, contentType, -1)
serveFastCGI(c, config.fcgiPools[serve.FastCGI], request, filePath) serveFastCGI(c, config.fcgiPools[serve.FastCGI], request, filePath)
return return statusSuccess, -1
} else if serve.cmd != nil { } else if serve.cmd != nil {
requireInput := serve.Input != "" || serve.SensitiveInput != "" requireInput := serve.Input != "" || serve.SensitiveInput != ""
if requireInput { if requireInput {
newCommand := replaceWithUserInput(serve.cmd, request) newCommand := replaceWithUserInput(serve.cmd, request)
if newCommand != nil { if newCommand != nil {
serveCommand(c, serve, request, newCommand) return serveCommand(c, serve, request, newCommand)
return
} }
} }
serveCommand(c, serve, request, serve.cmd) return serveCommand(c, serve, request, serve.cmd)
return
} }
if filePath == "" { if filePath == "" {
writeStatus(c, statusNotFound) return writeStatus(c, statusNotFound), -1
return
} }
fi, err := os.Stat(filePath) fi, err := os.Stat(filePath)
if err != nil { if err != nil {
writeStatus(c, statusNotFound) return writeStatus(c, statusNotFound), -1
return
} }
mode := fi.Mode() mode := fi.Mode()
hasTrailingSlash := len(request.Path) > 0 && request.Path[len(request.Path)-1] == '/' hasTrailingSlash := len(request.Path) > 0 && request.Path[len(request.Path)-1] == '/'
if mode.IsDir() { if mode.IsDir() {
if !hasTrailingSlash { if !hasTrailingSlash {
writeHeader(c, statusRedirectPermanent, request.String()+"/") return writeHeader(c, statusRedirectPermanent, request.String()+"/"), -1
return
} }
_, err := os.Stat(path.Join(filePath, "index.gmi")) _, err := os.Stat(path.Join(filePath, "index.gmi"))
@ -222,11 +215,9 @@ func servePath(c *tls.Conn, request *url.URL, serve *pathConfig) {
_, err := os.Stat(path.Join(filePath, "index.gemini")) _, err := os.Stat(path.Join(filePath, "index.gemini"))
if err != nil { if err != nil {
if serve.ListDirectory { if serve.ListDirectory {
serveDirList(c, serve, request, filePath) return serveDirList(c, serve, request, filePath), -1
return
} }
writeStatus(c, statusNotFound) return writeStatus(c, statusNotFound), -1
return
} }
filePath = path.Join(filePath, "index.gemini") filePath = path.Join(filePath, "index.gemini")
} else { } else {
@ -234,78 +225,17 @@ func servePath(c *tls.Conn, request *url.URL, serve *pathConfig) {
} }
} else if hasTrailingSlash && len(request.Path) > 1 { } else if hasTrailingSlash && len(request.Path) > 1 {
r := request.String() r := request.String()
writeHeader(c, statusRedirectPermanent, r[:len(r)-1]) return writeHeader(c, statusRedirectPermanent, r[:len(r)-1]), -1
return
} }
serveFile(c, serve, filePath) serveFile(c, serve, filePath)
return statusSuccess, fi.Size()
} }
func serveConn(c *tls.Conn) { func handleRequest(c *tls.Conn, request *url.URL, requestData string) (int, int64, string) {
var requestData string
scanner := bufio.NewScanner(c)
if !config.SaneEOL {
scanner.Split(scanCRLF)
}
if scanner.Scan() {
requestData = scanner.Text()
}
if err := scanner.Err(); err != nil {
writeStatus(c, statusBadRequest)
return
}
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 verbose {
log.Printf("> %s\n", requestData)
}
if len(requestData) > urlMaxLength || !utf8.ValidString(requestData) {
writeStatus(c, statusBadRequest)
return
}
request, err := url.Parse(requestData)
if err != nil {
writeStatus(c, statusBadRequest)
return
}
requestHostname := request.Hostname()
if requestHostname == "" || strings.ContainsRune(requestHostname, ' ') {
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) {
writeStatus(c, statusProxyRequestRefused)
}
if request.Path == "" { if request.Path == "" {
// Redirect to / // Redirect to /
writeHeader(c, statusRedirectPermanent, requestData+"/") return writeHeader(c, statusRedirectPermanent, requestData+"/"), -1, ""
return
} }
pathBytes := []byte(request.Path) pathBytes := []byte(request.Path)
@ -315,6 +245,7 @@ func serveConn(c *tls.Conn) {
} }
var matchedHost bool var matchedHost bool
requestHostname := request.Hostname()
for hostname := range config.Hosts { for hostname := range config.Hosts {
if requestHostname != hostname { if requestHostname != hostname {
continue continue
@ -331,58 +262,146 @@ func serveConn(c *tls.Conn) {
requireInput := serve.Input != "" || serve.SensitiveInput != "" requireInput := serve.Input != "" || serve.SensitiveInput != ""
if request.RawQuery == "" && requireInput { if request.RawQuery == "" && requireInput {
if serve.Input != "" { if serve.Input != "" {
writeHeader(c, statusInput, serve.Input) return writeHeader(c, statusInput, serve.Input), -1, ""
return
} else if serve.SensitiveInput != "" { } else if serve.SensitiveInput != "" {
writeHeader(c, statusSensitiveInput, serve.SensitiveInput) return writeHeader(c, statusSensitiveInput, serve.SensitiveInput), -1, ""
return
} }
} }
if matchedRegexp || matchedPrefix { if matchedRegexp || matchedPrefix {
servePath(c, request, serve) status, size := servePath(c, request, serve)
return return status, size, serve.Log
} }
} }
break break
} }
if matchedHost { if matchedHost {
writeStatus(c, statusNotFound) return writeStatus(c, statusNotFound), -1, ""
} else {
writeStatus(c, statusProxyRequestRefused)
} }
return writeStatus(c, statusProxyRequestRefused), -1, ""
} }
func handleConn(c *tls.Conn) { func handleConn(c *tls.Conn) {
if verbose { t := time.Now()
t := time.Now() var request *url.URL
defer func() { var logPath string
d := time.Since(t) status := 0
if d > time.Second { size := int64(-1)
d = d.Round(time.Second)
} else { defer func() {
d = d.Round(time.Millisecond) if !verbose && logPath == "" {
} return
log.Printf("took %s", d) }
}()
} 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() defer c.Close()
c.SetReadDeadline(time.Now().Add(readTimeout)) c.SetReadDeadline(time.Now().Add(readTimeout))
serveConn(c) var requestData string
scanner := bufio.NewScanner(c)
if !config.SaneEOL {
scanner.Split(scanCRLF)
}
if scanner.Scan() {
requestData = scanner.Text()
}
if err := scanner.Err(); err != nil {
status = writeStatus(c, statusBadRequest)
return
}
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) {
status = writeStatus(c, statusBadRequest)
return
}
var err error
request, err = url.Parse(requestData)
if err != nil {
status = writeStatus(c, statusBadRequest)
return
}
requestHostname := request.Hostname()
if requestHostname == "" || strings.ContainsRune(requestHostname, ' ') {
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) {
status = writeStatus(c, statusProxyRequestRefused)
return
}
status, size, logPath = handleRequest(c, request, requestData)
} }
func getCertificate(info *tls.ClientHelloInfo) (*tls.Certificate, error) { func logEntry(request *url.URL, status int, size int64, elapsed time.Duration) []byte {
host := config.Hosts[info.ServerName] hostFormatted := "-"
if host != nil { if request.Hostname() != "" {
return host.cert, nil hostFormatted = request.Hostname()
if request.Port() != "" {
hostFormatted += ":" + request.Port()
} else {
hostFormatted += ":1965"
}
} }
for _, host := range config.Hosts { timeFormatted := time.Now().Format("02/Jan/2006 03:04:05")
return host.cert, nil sizeFormatted := "-"
if size >= 0 {
sizeFormatted = strconv.FormatInt(size, 10)
} }
return nil, nil
return []byte(fmt.Sprintf(`%s - - - [%s] "GET %s Gemini" %d %s %.4f`, hostFormatted, timeFormatted, request.Path, status, sizeFormatted, elapsed.Seconds()))
} }
func handleListener(l net.Listener) { func handleListener(l net.Listener) {
@ -396,6 +415,17 @@ func handleListener(l net.Listener) {
} }
} }
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) { func listen(address string) {
tlsConfig := &tls.Config{ tlsConfig := &tls.Config{
ClientAuth: tls.RequestClientCert, ClientAuth: tls.RequestClientCert,