package finger import ( "context" "crypto/tls" "fmt" "io" "net" "tildegit.org/tjp/sliderule/internal" "tildegit.org/tjp/sliderule/internal/types" "tildegit.org/tjp/sliderule/logging" ) type fingerServer struct { internal.Server handler types.Handler } func (fs fingerServer) Protocol() string { return "FINGER" } // NewServer builds a finger server. func NewServer( ctx context.Context, hostname string, network string, address string, handler types.Handler, baseLog logging.Logger, ) (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 } 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 { _, _ = fmt.Fprint(conn, err.Error()+"\r\n") return } 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) _, _ = fmt.Fprint(conn, "Error handling request.\r\n") } }() response := fs.handler.Handle(fs.Ctx, request) if response == nil { response = Error("No result found.") } defer response.Close() _, _ = io.Copy(conn, response.Body) }