go-gemini/server.go

462 lines
12 KiB
Go
Raw Normal View History

2020-10-24 13:15:32 -06:00
package gemini
2020-09-25 17:06:56 -06:00
import (
"context"
2020-09-25 17:06:56 -06:00
"crypto/tls"
2020-11-05 13:27:12 -07:00
"errors"
2020-09-25 17:06:56 -06:00
"log"
"net"
"runtime"
2020-09-25 17:06:56 -06:00
"strings"
"sync"
"sync/atomic"
2020-09-25 17:06:56 -06:00
"time"
2021-01-14 18:42:12 -07:00
"git.sr.ht/~adnano/go-gemini/certificate"
2020-09-25 17:06:56 -06:00
)
2021-02-14 13:50:41 -07:00
// A Server defines parameters for running a Gemini server. The zero value for
// Server is a valid configuration.
2020-09-25 17:06:56 -06:00
type Server struct {
2021-02-14 13:50:41 -07:00
// Addr optionally specifies the TCP address for the server to listen on,
// in the form "host:port". If empty, ":1965" (port 1965) is used.
// See net.Dial for details of the address format.
2020-09-25 17:06:56 -06:00
Addr string
2021-02-14 13:50:41 -07:00
// ReadTimeout is the maximum duration for reading the entire
// request.
//
// A ReadTimeout of zero means no timeout.
ReadTimeout time.Duration
// WriteTimeout is the maximum duration before timing out
// writes of the response.
2021-02-14 13:50:41 -07:00
//
// A WriteTimeout of zero means no timeout.
WriteTimeout time.Duration
2021-02-14 13:50:41 -07:00
// Certificates contains one or more certificates to present to the
// other side of the connection.
2021-01-14 18:42:12 -07:00
Certificates certificate.Dir
2020-10-31 22:05:00 -06:00
// GetCertificate, if not nil, will be called to retrieve a new certificate
// if the current one is expired or missing.
GetCertificate func(hostname string) (tls.Certificate, error)
2020-09-25 17:06:56 -06:00
2021-02-14 13:50:41 -07:00
// ErrorLog specifies an optional logger for errors accepting connections,
// unexpected behavior from handlers, and underlying file system errors.
2020-11-03 14:11:31 -07:00
// If nil, logging is done via the log package's standard logger.
ErrorLog *log.Logger
2021-02-14 13:50:41 -07:00
// registered handlers
handlers map[handlerKey]Handler
hosts map[string]bool
hmu sync.Mutex
listeners map[*net.Listener]struct{}
conns map[*net.Conn]struct{}
done int32
mu sync.Mutex
}
2021-02-08 10:50:50 -07:00
type handlerKey struct {
2020-10-21 11:22:26 -06:00
scheme string
hostname string
}
2021-02-08 10:50:50 -07:00
// Handle registers the handler for the given pattern.
// If a handler already exists for pattern, Handle panics.
2020-10-28 14:02:04 -06:00
//
2021-01-14 20:12:07 -07:00
// The pattern must be in the form of "hostname" or "scheme://hostname".
2020-10-31 18:32:38 -06:00
// If no scheme is specified, a scheme of "gemini://" is implied.
// Wildcard patterns are supported (e.g. "*.example.com").
// To handle any hostname, use the wildcard pattern "*".
2021-02-14 13:50:41 -07:00
func (srv *Server) Handle(pattern string, handler Handler) {
srv.hmu.Lock()
defer srv.hmu.Unlock()
2020-10-21 11:22:26 -06:00
if pattern == "" {
2020-10-24 13:15:32 -06:00
panic("gemini: invalid pattern")
2020-10-11 16:57:04 -06:00
}
2021-02-08 10:50:50 -07:00
if handler == nil {
2021-02-14 23:15:23 -07:00
panic("gemini: nil handler")
2020-10-11 16:57:04 -06:00
}
2021-02-14 13:50:41 -07:00
if srv.handlers == nil {
srv.handlers = map[handlerKey]Handler{}
srv.hosts = map[string]bool{}
}
2020-10-21 11:22:26 -06:00
split := strings.SplitN(pattern, "://", 2)
2021-02-08 10:50:50 -07:00
var key handlerKey
2020-10-21 11:22:26 -06:00
if len(split) == 2 {
key.scheme = split[0]
key.hostname = split[1]
} else {
key.scheme = "gemini"
key.hostname = split[0]
}
2021-02-14 13:50:41 -07:00
if _, ok := srv.handlers[key]; ok {
2020-10-28 12:59:45 -06:00
panic("gemini: multiple registrations for " + pattern)
}
2021-02-14 13:50:41 -07:00
srv.handlers[key] = handler
srv.hosts[key.hostname] = true
}
2021-02-08 10:50:50 -07:00
// HandleFunc registers the handler function for the given pattern.
2021-02-14 13:50:41 -07:00
func (srv *Server) HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
srv.Handle(pattern, HandlerFunc(handler))
2020-09-25 17:06:56 -06:00
}
// ListenAndServe listens for requests at the server's configured address.
2021-02-14 13:50:41 -07:00
// ListenAndServe listens on the TCP network address srv.Addr and then calls
// Serve to handle requests on incoming connections.
//
// If srv.Addr is blank, ":1965" is used.
//
// ListenAndServe always returns a non-nil error. After Shutdown or Close, the
// returned error is ErrServerClosed.
func (srv *Server) ListenAndServe() error {
if atomic.LoadInt32(&srv.done) == 1 {
return ErrServerClosed
}
2021-02-14 13:50:41 -07:00
addr := srv.Addr
2020-09-25 17:06:56 -06:00
if addr == "" {
addr = ":1965"
}
ln, err := net.Listen("tcp", addr)
if err != nil {
return err
}
defer ln.Close()
2021-02-14 13:50:41 -07:00
return srv.Serve(tls.NewListener(ln, &tls.Config{
2020-10-28 12:59:45 -06:00
ClientAuth: tls.RequestClientCert,
MinVersion: tls.VersionTLS12,
2021-02-14 13:50:41 -07:00
GetCertificate: srv.getCertificate,
2020-10-28 12:59:45 -06:00
}))
2020-09-25 17:06:56 -06:00
}
func (srv *Server) trackListener(l *net.Listener) {
srv.mu.Lock()
defer srv.mu.Unlock()
if srv.listeners == nil {
srv.listeners = make(map[*net.Listener]struct{})
}
srv.listeners[l] = struct{}{}
}
func (srv *Server) deleteListener(l *net.Listener) {
srv.mu.Lock()
defer srv.mu.Unlock()
delete(srv.listeners, l)
}
2021-02-14 13:50:41 -07:00
// Serve accepts incoming connections on the Listener l, creating a new
// service goroutine for each. The service goroutines read requests and
// then calls the appropriate Handler to reply to them.
//
// Serve always returns a non-nil error and closes l. After Shutdown or Close,
// the returned error is ErrServerClosed.
func (srv *Server) Serve(l net.Listener) error {
defer l.Close()
srv.trackListener(&l)
defer srv.deleteListener(&l)
if atomic.LoadInt32(&srv.done) == 1 {
return ErrServerClosed
}
2020-09-25 17:06:56 -06:00
var tempDelay time.Duration // how long to sleep on accept failure
for {
rw, err := l.Accept()
if err != nil {
if atomic.LoadInt32(&srv.done) == 1 {
return ErrServerClosed
}
2020-09-25 17:06:56 -06:00
// If this is a temporary error, sleep
if ne, ok := err.(net.Error); ok && ne.Temporary() {
if tempDelay == 0 {
tempDelay = 5 * time.Millisecond
} else {
tempDelay *= 2
}
if max := 1 * time.Second; tempDelay > max {
tempDelay = max
}
2021-02-14 13:50:41 -07:00
srv.logf("gemini: Accept error: %v; retrying in %v", err, tempDelay)
2020-09-25 17:06:56 -06:00
time.Sleep(tempDelay)
continue
}
// Otherwise, return the error
return err
}
tempDelay = 0
2021-02-14 13:50:41 -07:00
go srv.respond(rw)
2020-09-25 17:06:56 -06:00
}
}
func (srv *Server) closeListenersLocked() error {
var err error
for ln := range srv.listeners {
if cerr := (*ln).Close(); cerr != nil && err == nil {
err = cerr
}
delete(srv.listeners, ln)
}
return err
}
// Close immediately closes all active net.Listeners and connections.
// For a graceful shutdown, use Shutdown.
//
// Close returns any error returned from closing the Server's
// underlying Listener(s).
func (srv *Server) Close() error {
srv.mu.Lock()
defer srv.mu.Unlock()
if !atomic.CompareAndSwapInt32(&srv.done, 0, 1) {
return ErrServerClosed
}
err := srv.closeListenersLocked()
// Close active connections
for conn := range srv.conns {
(*conn).Close()
delete(srv.conns, conn)
}
return err
}
func (srv *Server) numConns() int {
srv.mu.Lock()
defer srv.mu.Unlock()
return len(srv.conns)
}
// shutdownPollInterval is how often we poll for quiescence
// during Server.Shutdown. This is lower during tests, to
// speed up tests.
// Ideally we could find a solution that doesn't involve polling,
// but which also doesn't have a high runtime cost (and doesn't
// involve any contentious mutexes), but that is left as an
// exercise for the reader.
var shutdownPollInterval = 500 * time.Millisecond
// Shutdown gracefully shuts down the server without interrupting any
// active connections. Shutdown works by first closing all open
// listeners and then waiting indefinitely for connections
// to close and then shut down.
// If the provided context expires before the shutdown is complete,
// Shutdown returns the context's error, otherwise it returns any
// error returned from closing the Server's underlying Listener(s).
//
// When Shutdown is called, Serve, ListenAndServe, and
// ListenAndServeTLS immediately return ErrServerClosed. Make sure the
// program doesn't exit and waits instead for Shutdown to return.
//
// Once Shutdown has been called on a server, it may not be reused;
// future calls to methods such as Serve will return ErrServerClosed.
func (srv *Server) Shutdown(ctx context.Context) error {
if !atomic.CompareAndSwapInt32(&srv.done, 0, 1) {
return ErrServerClosed
}
srv.mu.Lock()
err := srv.closeListenersLocked()
srv.mu.Unlock()
// Wait for active connections to close
ticker := time.NewTicker(shutdownPollInterval)
defer ticker.Stop()
for {
if srv.numConns() == 0 {
return err
}
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
}
}
}
// getCertificate retrieves a certificate for the given client hello.
2021-02-14 13:50:41 -07:00
func (srv *Server) getCertificate(h *tls.ClientHelloInfo) (*tls.Certificate, error) {
cert, err := srv.lookupCertificate(h.ServerName, h.ServerName)
if err != nil {
// Try wildcard
2020-10-31 13:11:05 -06:00
wildcard := strings.SplitN(h.ServerName, ".", 2)
if len(wildcard) == 2 {
// Use the wildcard pattern as the hostname.
hostname := "*." + wildcard[1]
2021-02-14 13:50:41 -07:00
cert, err = srv.lookupCertificate(hostname, hostname)
}
// Try "*" wildcard
if err != nil {
// Use the server name as the hostname
// since "*" is not a valid hostname.
2021-02-14 13:50:41 -07:00
cert, err = srv.lookupCertificate("*", h.ServerName)
2020-10-31 13:11:05 -06:00
}
}
return cert, err
}
2020-10-31 13:11:05 -06:00
// lookupCertificate retrieves the certificate for the given hostname,
// if and only if the provided pattern is registered.
// If no certificate is found in the certificate store or the certificate
// is expired, it calls GetCertificate to retrieve a new certificate.
2021-02-14 13:50:41 -07:00
func (srv *Server) lookupCertificate(pattern, hostname string) (*tls.Certificate, error) {
srv.hmu.Lock()
_, ok := srv.hosts[pattern]
srv.hmu.Unlock()
if !ok {
2020-11-05 13:27:12 -07:00
return nil, errors.New("hostname not registered")
}
2021-02-14 13:50:41 -07:00
cert, ok := srv.Certificates.Lookup(hostname)
2020-11-05 20:30:13 -07:00
if !ok || cert.Leaf != nil && cert.Leaf.NotAfter.Before(time.Now()) {
2021-02-14 13:50:41 -07:00
if srv.GetCertificate != nil {
cert, err := srv.GetCertificate(hostname)
2020-10-28 12:59:45 -06:00
if err == nil {
2021-02-14 13:50:41 -07:00
if err := srv.Certificates.Add(hostname, cert); err != nil {
srv.logf("gemini: Failed to write new certificate for %s: %s", hostname, err)
2020-11-03 14:11:31 -07:00
}
2020-10-28 12:59:45 -06:00
}
return &cert, err
}
2020-11-05 13:27:12 -07:00
return nil, errors.New("no certificate")
2020-10-28 12:59:45 -06:00
}
2020-11-05 13:27:12 -07:00
return &cert, nil
2020-10-28 12:59:45 -06:00
}
func (srv *Server) trackConn(conn *net.Conn) {
srv.mu.Lock()
defer srv.mu.Unlock()
if srv.conns == nil {
srv.conns = make(map[*net.Conn]struct{})
}
srv.conns[conn] = struct{}{}
}
func (srv *Server) deleteConn(conn *net.Conn) {
srv.mu.Lock()
defer srv.mu.Unlock()
delete(srv.conns, conn)
}
2020-10-21 14:28:50 -06:00
// respond responds to a connection.
2021-02-14 13:50:41 -07:00
func (srv *Server) respond(conn net.Conn) {
defer conn.Close()
defer func() {
if err := recover(); err != nil && err != ErrAbortHandler {
const size = 64 << 10
buf := make([]byte, size)
buf = buf[:runtime.Stack(buf, false)]
srv.logf("gemini: panic serving %v: %v\n%s", conn.RemoteAddr(), err, buf)
}
}()
srv.trackConn(&conn)
defer srv.deleteConn(&conn)
2021-02-14 13:50:41 -07:00
if d := srv.ReadTimeout; d != 0 {
conn.SetReadDeadline(time.Now().Add(d))
}
2021-02-14 13:50:41 -07:00
if d := srv.WriteTimeout; d != 0 {
conn.SetWriteDeadline(time.Now().Add(d))
}
w := NewResponseWriter(conn)
req, err := ReadRequest(conn)
2020-10-21 14:28:50 -06:00
if err != nil {
w.Status(StatusBadRequest)
w.Flush()
return
}
// Store information about the TLS connection
if tlsConn, ok := conn.(*tls.Conn); ok {
state := tlsConn.ConnectionState()
req.TLS = &state
}
// Store remote address
req.RemoteAddr = conn.RemoteAddr()
2021-02-14 23:15:23 -07:00
h := srv.handler(req)
if h == nil {
w.Status(StatusNotFound)
w.Flush()
return
2020-10-21 14:28:50 -06:00
}
2021-02-14 23:15:23 -07:00
h.ServeGemini(w, req)
w.Flush()
2020-10-21 14:28:50 -06:00
}
2021-02-14 23:15:23 -07:00
func (srv *Server) handler(r *Request) Handler {
srv.hmu.Lock()
defer srv.hmu.Unlock()
2021-02-14 13:50:41 -07:00
if h, ok := srv.handlers[handlerKey{r.URL.Scheme, r.URL.Hostname()}]; ok {
2020-10-21 14:28:50 -06:00
return h
}
wildcard := strings.SplitN(r.URL.Hostname(), ".", 2)
if len(wildcard) == 2 {
2021-02-14 13:50:41 -07:00
if h, ok := srv.handlers[handlerKey{r.URL.Scheme, "*." + wildcard[1]}]; ok {
2020-10-21 14:28:50 -06:00
return h
}
}
2021-02-14 13:50:41 -07:00
if h, ok := srv.handlers[handlerKey{r.URL.Scheme, "*"}]; ok {
return h
}
return nil
2020-10-21 14:28:50 -06:00
}
2021-02-14 13:50:41 -07:00
func (srv *Server) logf(format string, args ...interface{}) {
if srv.ErrorLog != nil {
srv.ErrorLog.Printf(format, args...)
2020-11-03 14:11:31 -07:00
} else {
log.Printf(format, args...)
}
}
2021-02-08 10:50:50 -07:00
// A Handler responds to a Gemini request.
//
// ServeGemini should write the response header and data to the ResponseWriter
2021-02-14 13:50:41 -07:00
// and then return. Returning signals that the request is finished; it is not
// valid to use the ResponseWriter after or concurrently with the completion
// of the ServeGemini call.
//
// Handlers should not modify the provided Request.
//
// If ServeGemini panics, the server (the caller of ServeGemini) assumes that
// the effect of the panic was isolated to the active request. It recovers
// the panic, logs a stack trace to the server error log, and closes the
// network connection. To abort a handler so the client sees an interrupted
2021-02-14 13:50:41 -07:00
// response but the server doesn't log an error, panic with the value
// ErrAbortHandler.
2021-02-08 10:50:50 -07:00
type Handler interface {
2021-02-09 07:45:10 -07:00
ServeGemini(ResponseWriter, *Request)
2020-09-25 17:06:56 -06:00
}
2021-02-08 10:50:50 -07:00
// The HandlerFunc type is an adapter to allow the use of ordinary functions
// as Gemini handlers. If f is a function with the appropriate signature,
// HandlerFunc(f) is a Handler that calls f.
2021-02-09 07:45:10 -07:00
type HandlerFunc func(ResponseWriter, *Request)
2021-02-08 10:50:50 -07:00
// ServeGemini calls f(w, r).
2021-02-09 07:45:10 -07:00
func (f HandlerFunc) ServeGemini(w ResponseWriter, r *Request) {
f(w, r)
2020-09-28 00:05:37 -06:00
}