diff options
Diffstat (limited to 'gopher')
-rw-r--r-- | gopher/serve.go | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/gopher/serve.go b/gopher/serve.go index 67c889a..79ed7f8 100644 --- a/gopher/serve.go +++ b/gopher/serve.go @@ -2,13 +2,14 @@ package gopher import ( "context" + "crypto/tls" "errors" "fmt" "io" "net" - "tildegit.org/tjp/sliderule/internal/types" "tildegit.org/tjp/sliderule/internal" + "tildegit.org/tjp/sliderule/internal/types" "tildegit.org/tjp/sliderule/logging" ) @@ -42,6 +43,32 @@ func NewServer( return gs, nil } +// NewTLSServer builds a gopher server which serves gopher over tls-encrypted connections. +func NewTLSServer( + ctx context.Context, + hostname string, + network string, + address string, + handler types.Handler, + baseLog logging.Logger, + tlsConfig *tls.Config, +) (types.Server, error) { + gs := &gopherServer{handler: handler} + + hostname = internal.JoinDefaultPort(hostname, "70") + address = internal.JoinDefaultPort(address, "70") + + var err error + gs.Server, err = internal.NewServer(ctx, hostname, network, address, baseLog, gs.handleConn) + if err != nil { + return nil, err + } + + gs.Listener = tls.NewListener(gs.Listener, tlsConfig) + + return gs, nil +} + func (gs *gopherServer) handleConn(conn net.Conn) { var response *types.Response request, err := ParseRequest(conn) @@ -52,6 +79,11 @@ func (gs *gopherServer) handleConn(conn net.Conn) { request.RemoteAddr = conn.RemoteAddr() request.Host = gs.Host + if tlsconn, ok := conn.(*tls.Conn); ok { + state := tlsconn.ConnectionState() + request.TLSState = &state + } + defer func() { if r := recover(); r != nil { err := fmt.Errorf("%s", r) |