From 0a7e966d5a093e8c2d3b3834d25feb93f5fca156 Mon Sep 17 00:00:00 2001 From: tjpcc Date: Tue, 10 Oct 2023 16:50:06 -0600 Subject: add NewTLSServer for gopher and finger --- gopher/serve.go | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) (limited to 'gopher') 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) -- cgit v1.2.3