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 /finger | |
parent | 04449ed66e4272ee08bc1dd00f6bdefb2be51a43 (diff) |
add NewTLSServer for gopher and finger
Diffstat (limited to 'finger')
-rw-r--r-- | finger/serve.go | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/finger/serve.go b/finger/serve.go index f959373..cb8f0fd 100644 --- a/finger/serve.go +++ b/finger/serve.go @@ -2,6 +2,7 @@ package finger import ( "context" + "crypto/tls" "fmt" "io" "net" @@ -41,6 +42,31 @@ func NewServer( return fs, nil } +func NewTLSServer( + ctx context.Context, + hostname string, + network string, + address string, + handler types.Handler, + baseLog logging.Logger, + tlsConfig *tls.Config, +) (types.Server, error) { + fs := &fingerServer{handler: handler} + + hostname = internal.JoinDefaultPort(hostname, "79") + address = internal.JoinDefaultPort(address, "79") + + var err error + fs.Server, err = internal.NewServer(ctx, hostname, network, address, baseLog, fs.handleConn) + if err != nil { + return nil, err + } + + fs.Listener = tls.NewListener(fs.Listener, tlsConfig) + + return fs, nil +} + func (fs *fingerServer) handleConn(conn net.Conn) { request, err := ParseRequest(conn) if err != nil { @@ -51,6 +77,11 @@ func (fs *fingerServer) handleConn(conn net.Conn) { request.Server = fs request.RemoteAddr = conn.RemoteAddr() + if tlsconn, ok := conn.(*tls.Conn); ok { + state := tlsconn.ConnectionState() + request.TLSState = &state + } + defer func() { if r := recover(); r != nil { _ = fs.LogError("msg", "panic in handler", "err", r) |