server.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package tls
  2. import (
  3. "context"
  4. "net"
  5. "os"
  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. aTLS "github.com/sagernet/sing/common/tls"
  11. )
  12. func NewServer(ctx context.Context, logger log.Logger, options option.InboundTLSOptions) (ServerConfig, error) {
  13. if !options.Enabled {
  14. return nil, nil
  15. }
  16. if options.ECH != nil && options.ECH.Enabled {
  17. if options.ECH.PQSignatureSchemesEnabled || options.ECH.DynamicRecordSizingDisabled {
  18. return NewECHServer(ctx, logger, options)
  19. }
  20. } else if options.Reality != nil && options.Reality.Enabled {
  21. return NewRealityServer(ctx, logger, options)
  22. }
  23. return NewSTDServer(ctx, logger, options)
  24. }
  25. func ServerHandshake(ctx context.Context, conn net.Conn, config ServerConfig) (Conn, error) {
  26. ctx, cancel := context.WithTimeout(ctx, C.TCPTimeout)
  27. defer cancel()
  28. tlsConn, err := aTLS.ServerHandshake(ctx, conn, config)
  29. if err != nil {
  30. return nil, err
  31. }
  32. readWaitConn, err := badtls.NewReadWaitConn(tlsConn)
  33. if err == nil {
  34. return readWaitConn, nil
  35. } else if err != os.ErrInvalid {
  36. return nil, err
  37. }
  38. return tlsConn, nil
  39. }