outbound.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package shadowtls
  2. import (
  3. "context"
  4. "net"
  5. "os"
  6. "github.com/sagernet/sing-box/adapter"
  7. "github.com/sagernet/sing-box/adapter/outbound"
  8. "github.com/sagernet/sing-box/common/dialer"
  9. "github.com/sagernet/sing-box/common/tls"
  10. C "github.com/sagernet/sing-box/constant"
  11. "github.com/sagernet/sing-box/log"
  12. "github.com/sagernet/sing-box/option"
  13. "github.com/sagernet/sing-shadowtls"
  14. "github.com/sagernet/sing/common"
  15. M "github.com/sagernet/sing/common/metadata"
  16. N "github.com/sagernet/sing/common/network"
  17. )
  18. func RegisterOutbound(registry *outbound.Registry) {
  19. outbound.Register[option.ShadowTLSOutboundOptions](registry, C.TypeShadowTLS, NewOutbound)
  20. }
  21. type Outbound struct {
  22. outbound.Adapter
  23. client *shadowtls.Client
  24. }
  25. func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowTLSOutboundOptions) (adapter.Outbound, error) {
  26. outbound := &Outbound{
  27. Adapter: outbound.NewAdapterWithDialerOptions(C.TypeShadowTLS, tag, []string{N.NetworkTCP}, options.DialerOptions),
  28. }
  29. if options.TLS == nil || !options.TLS.Enabled {
  30. return nil, C.ErrTLSRequired
  31. }
  32. if options.Version == 0 {
  33. options.Version = 1
  34. }
  35. if options.Version == 1 {
  36. options.TLS.MinVersion = "1.2"
  37. options.TLS.MaxVersion = "1.2"
  38. }
  39. tlsConfig, err := tls.NewClient(ctx, logger, options.Server, common.PtrValueOrDefault(options.TLS))
  40. if err != nil {
  41. return nil, err
  42. }
  43. var tlsHandshakeFunc shadowtls.TLSHandshakeFunc
  44. switch options.Version {
  45. case 1, 2:
  46. tlsHandshakeFunc = func(ctx context.Context, conn net.Conn, _ shadowtls.TLSSessionIDGeneratorFunc) error {
  47. return common.Error(tls.ClientHandshake(ctx, conn, tlsConfig))
  48. }
  49. case 3:
  50. if idConfig, loaded := tlsConfig.(tls.WithSessionIDGenerator); loaded {
  51. tlsHandshakeFunc = func(ctx context.Context, conn net.Conn, sessionIDGenerator shadowtls.TLSSessionIDGeneratorFunc) error {
  52. idConfig.SetSessionIDGenerator(sessionIDGenerator)
  53. return common.Error(tls.ClientHandshake(ctx, conn, tlsConfig))
  54. }
  55. } else {
  56. stdTLSConfig, err := tlsConfig.STDConfig()
  57. if err != nil {
  58. return nil, err
  59. }
  60. tlsHandshakeFunc = shadowtls.DefaultTLSHandshakeFunc(options.Password, stdTLSConfig)
  61. }
  62. }
  63. outboundDialer, err := dialer.New(ctx, options.DialerOptions, options.ServerIsDomain())
  64. if err != nil {
  65. return nil, err
  66. }
  67. client, err := shadowtls.NewClient(shadowtls.ClientConfig{
  68. Version: options.Version,
  69. Password: options.Password,
  70. Server: options.ServerOptions.Build(),
  71. Dialer: outboundDialer,
  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 *Outbound) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  82. ctx, metadata := adapter.ExtendContext(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 *Outbound) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  93. return nil, os.ErrInvalid
  94. }