server.go 889 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package tls
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "net"
  6. "github.com/sagernet/sing-box/common/badtls"
  7. C "github.com/sagernet/sing-box/constant"
  8. "github.com/sagernet/sing-box/log"
  9. "github.com/sagernet/sing-box/option"
  10. )
  11. func NewServer(ctx context.Context, logger log.Logger, options option.InboundTLSOptions) (ServerConfig, error) {
  12. if !options.Enabled {
  13. return nil, nil
  14. }
  15. return NewSTDServer(ctx, logger, options)
  16. }
  17. func ServerHandshake(ctx context.Context, conn net.Conn, config ServerConfig) (Conn, error) {
  18. tlsConn := config.Server(conn)
  19. ctx, cancel := context.WithTimeout(ctx, C.TCPTimeout)
  20. defer cancel()
  21. err := tlsConn.HandshakeContext(ctx)
  22. if err != nil {
  23. return nil, err
  24. }
  25. if stdConn, isSTD := tlsConn.(*tls.Conn); isSTD {
  26. var badConn badtls.TLSConn
  27. badConn, err = badtls.Create(stdConn)
  28. if err == nil {
  29. return badConn, nil
  30. }
  31. }
  32. return tlsConn, nil
  33. }