diff options
author | tjpcc <tjp@ctrl-c.club> | 2023-10-10 16:50:06 -0600 |
---|---|---|
committer | tjpcc <tjp@ctrl-c.club> | 2023-10-10 16:50:06 -0600 |
commit | 0a7e966d5a093e8c2d3b3834d25feb93f5fca156 (patch) | |
tree | 5144343c23caa608a5458d24df3e073a678df7fa /gopher | |
parent | 04449ed66e4272ee08bc1dd00f6bdefb2be51a43 (diff) |
add NewTLSServer for gopher and finger
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) |