github.com/sagernet/sing-box@v1.9.0-rc.20/common/tls/server.go (about) 1 package tls 2 3 import ( 4 "context" 5 "net" 6 "os" 7 8 "github.com/sagernet/sing-box/common/badtls" 9 C "github.com/sagernet/sing-box/constant" 10 "github.com/sagernet/sing-box/log" 11 "github.com/sagernet/sing-box/option" 12 aTLS "github.com/sagernet/sing/common/tls" 13 ) 14 15 func NewServer(ctx context.Context, logger log.Logger, options option.InboundTLSOptions) (ServerConfig, error) { 16 if !options.Enabled { 17 return nil, nil 18 } 19 if options.ECH != nil && options.ECH.Enabled { 20 return NewECHServer(ctx, logger, options) 21 } else if options.Reality != nil && options.Reality.Enabled { 22 return NewRealityServer(ctx, logger, options) 23 } else { 24 return NewSTDServer(ctx, logger, options) 25 } 26 } 27 28 func ServerHandshake(ctx context.Context, conn net.Conn, config ServerConfig) (Conn, error) { 29 ctx, cancel := context.WithTimeout(ctx, C.TCPTimeout) 30 defer cancel() 31 tlsConn, err := aTLS.ServerHandshake(ctx, conn, config) 32 if err != nil { 33 return nil, err 34 } 35 readWaitConn, err := badtls.NewReadWaitConn(tlsConn) 36 if err == nil { 37 return readWaitConn, nil 38 } else if err != os.ErrInvalid { 39 return nil, err 40 } 41 return tlsConn, nil 42 }