shadowtls.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package outbound
  2. import (
  3. "context"
  4. "net"
  5. "os"
  6. "github.com/sagernet/sing-box/adapter"
  7. "github.com/sagernet/sing-box/common/dialer"
  8. "github.com/sagernet/sing-box/common/tls"
  9. C "github.com/sagernet/sing-box/constant"
  10. "github.com/sagernet/sing-box/log"
  11. "github.com/sagernet/sing-box/option"
  12. "github.com/sagernet/sing-shadowtls"
  13. "github.com/sagernet/sing/common"
  14. M "github.com/sagernet/sing/common/metadata"
  15. N "github.com/sagernet/sing/common/network"
  16. )
  17. var _ adapter.Outbound = (*ShadowTLS)(nil)
  18. type ShadowTLS struct {
  19. myOutboundAdapter
  20. client *shadowtls.Client
  21. }
  22. func NewShadowTLS(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowTLSOutboundOptions) (*ShadowTLS, error) {
  23. outbound := &ShadowTLS{
  24. myOutboundAdapter: myOutboundAdapter{
  25. protocol: C.TypeShadowTLS,
  26. network: []string{N.NetworkTCP},
  27. router: router,
  28. logger: logger,
  29. tag: tag,
  30. dependencies: withDialerDependency(options.DialerOptions),
  31. },
  32. }
  33. if options.TLS == nil || !options.TLS.Enabled {
  34. return nil, C.ErrTLSRequired
  35. }
  36. if options.Version == 0 {
  37. options.Version = 1
  38. }
  39. if options.Version == 1 {
  40. options.TLS.MinVersion = "1.2"
  41. options.TLS.MaxVersion = "1.2"
  42. }
  43. tlsConfig, err := tls.NewClient(router, options.Server, common.PtrValueOrDefault(options.TLS))
  44. if err != nil {
  45. return nil, err
  46. }
  47. var tlsHandshakeFunc shadowtls.TLSHandshakeFunc
  48. switch options.Version {
  49. case 1, 2:
  50. tlsHandshakeFunc = func(ctx context.Context, conn net.Conn, _ shadowtls.TLSSessionIDGeneratorFunc) error {
  51. return common.Error(tls.ClientHandshake(ctx, conn, tlsConfig))
  52. }
  53. case 3:
  54. if idConfig, loaded := tlsConfig.(tls.WithSessionIDGenerator); loaded {
  55. tlsHandshakeFunc = func(ctx context.Context, conn net.Conn, sessionIDGenerator shadowtls.TLSSessionIDGeneratorFunc) error {
  56. idConfig.SetSessionIDGenerator(sessionIDGenerator)
  57. return common.Error(tls.ClientHandshake(ctx, conn, tlsConfig))
  58. }
  59. } else {
  60. stdTLSConfig, err := tlsConfig.Config()
  61. if err != nil {
  62. return nil, err
  63. }
  64. tlsHandshakeFunc = shadowtls.DefaultTLSHandshakeFunc(options.Password, stdTLSConfig)
  65. }
  66. }
  67. client, err := shadowtls.NewClient(shadowtls.ClientConfig{
  68. Version: options.Version,
  69. Password: options.Password,
  70. Server: options.ServerOptions.Build(),
  71. Dialer: dialer.New(router, options.DialerOptions),
  72. TLSHandshake: tlsHandshakeFunc,
  73. Logger: logger,
  74. })
  75. if err != nil {
  76. return nil, err
  77. }
  78. outbound.client = client
  79. return outbound, nil
  80. }
  81. func (h *ShadowTLS) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  82. ctx, metadata := adapter.AppendContext(ctx)
  83. metadata.Outbound = h.tag
  84. metadata.Destination = destination
  85. switch N.NetworkName(network) {
  86. case N.NetworkTCP:
  87. return h.client.DialContext(ctx)
  88. default:
  89. return nil, os.ErrInvalid
  90. }
  91. }
  92. func (h *ShadowTLS) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  93. return nil, os.ErrInvalid
  94. }
  95. func (h *ShadowTLS) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  96. return NewConnection(ctx, h, conn, metadata)
  97. }
  98. func (h *ShadowTLS) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  99. return os.ErrInvalid
  100. }