server.go 845 B

12345678910111213141516171819202122232425262728293031323334
  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. return newSTDServer(ctx, logger, options)
  13. }
  14. func ServerHandshake(ctx context.Context, conn net.Conn, config ServerConfig) (Conn, error) {
  15. tlsConn := config.Server(conn)
  16. ctx, cancel := context.WithTimeout(ctx, C.TCPTimeout)
  17. defer cancel()
  18. err := tlsConn.HandshakeContext(ctx)
  19. if err != nil {
  20. return nil, err
  21. }
  22. if stdConn, isSTD := tlsConn.(*tls.Conn); isSTD {
  23. var badConn badtls.TLSConn
  24. badConn, err = badtls.Create(stdConn)
  25. if err == nil {
  26. return badConn, nil
  27. }
  28. }
  29. return tlsConn, nil
  30. }