sing-box/common/tls/server.go

38 lines
889 B
Go
Raw Normal View History

2022-09-10 02:27:00 +00:00
package tls
import (
"context"
2022-09-30 03:27:18 +00:00
"crypto/tls"
"net"
2022-09-10 02:27:00 +00:00
2022-09-30 03:27:18 +00:00
"github.com/sagernet/sing-box/common/badtls"
C "github.com/sagernet/sing-box/constant"
2022-09-10 02:27:00 +00:00
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
)
func NewServer(ctx context.Context, logger log.Logger, options option.InboundTLSOptions) (ServerConfig, error) {
2022-11-13 03:24:37 +00:00
if !options.Enabled {
return nil, nil
}
return NewSTDServer(ctx, logger, options)
2022-09-10 02:27:00 +00:00
}
2022-09-30 03:27:18 +00:00
func ServerHandshake(ctx context.Context, conn net.Conn, config ServerConfig) (Conn, error) {
tlsConn := config.Server(conn)
ctx, cancel := context.WithTimeout(ctx, C.TCPTimeout)
defer cancel()
err := tlsConn.HandshakeContext(ctx)
if err != nil {
return nil, err
}
if stdConn, isSTD := tlsConn.(*tls.Conn); isSTD {
var badConn badtls.TLSConn
badConn, err = badtls.Create(stdConn)
if err == nil {
return badConn, nil
}
}
return tlsConn, nil
}