socks.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package outbound
  2. import (
  3. "context"
  4. "net"
  5. "github.com/sagernet/sing-box/adapter"
  6. "github.com/sagernet/sing-box/common/dialer"
  7. C "github.com/sagernet/sing-box/constant"
  8. "github.com/sagernet/sing-box/log"
  9. "github.com/sagernet/sing-box/option"
  10. "github.com/sagernet/sing-dns"
  11. "github.com/sagernet/sing/common"
  12. E "github.com/sagernet/sing/common/exceptions"
  13. M "github.com/sagernet/sing/common/metadata"
  14. N "github.com/sagernet/sing/common/network"
  15. "github.com/sagernet/sing/common/uot"
  16. "github.com/sagernet/sing/protocol/socks"
  17. )
  18. var _ adapter.Outbound = (*Socks)(nil)
  19. type Socks struct {
  20. myOutboundAdapter
  21. client *socks.Client
  22. resolve bool
  23. uotClient *uot.Client
  24. }
  25. func NewSocks(router adapter.Router, logger log.ContextLogger, tag string, options option.SocksOutboundOptions) (*Socks, error) {
  26. var version socks.Version
  27. var err error
  28. if options.Version != "" {
  29. version, err = socks.ParseVersion(options.Version)
  30. } else {
  31. version = socks.Version5
  32. }
  33. if err != nil {
  34. return nil, err
  35. }
  36. outboundDialer, err := dialer.New(router, options.DialerOptions)
  37. if err != nil {
  38. return nil, err
  39. }
  40. outbound := &Socks{
  41. myOutboundAdapter: myOutboundAdapter{
  42. protocol: C.TypeSOCKS,
  43. network: options.Network.Build(),
  44. router: router,
  45. logger: logger,
  46. tag: tag,
  47. dependencies: withDialerDependency(options.DialerOptions),
  48. },
  49. client: socks.NewClient(outboundDialer, options.ServerOptions.Build(), version, options.Username, options.Password),
  50. resolve: version == socks.Version4,
  51. }
  52. uotOptions := common.PtrValueOrDefault(options.UDPOverTCP)
  53. if uotOptions.Enabled {
  54. outbound.uotClient = &uot.Client{
  55. Dialer: outbound.client,
  56. Version: uotOptions.Version,
  57. }
  58. }
  59. return outbound, nil
  60. }
  61. func (h *Socks) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  62. ctx, metadata := adapter.ExtendContext(ctx)
  63. metadata.Outbound = h.tag
  64. metadata.Destination = destination
  65. switch N.NetworkName(network) {
  66. case N.NetworkTCP:
  67. h.logger.InfoContext(ctx, "outbound connection to ", destination)
  68. case N.NetworkUDP:
  69. if h.uotClient != nil {
  70. h.logger.InfoContext(ctx, "outbound UoT connect packet connection to ", destination)
  71. return h.uotClient.DialContext(ctx, network, destination)
  72. }
  73. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  74. default:
  75. return nil, E.Extend(N.ErrUnknownNetwork, network)
  76. }
  77. if h.resolve && destination.IsFqdn() {
  78. destinationAddresses, err := h.router.LookupDefault(ctx, destination.Fqdn)
  79. if err != nil {
  80. return nil, err
  81. }
  82. return N.DialSerial(ctx, h.client, network, destination, destinationAddresses)
  83. }
  84. return h.client.DialContext(ctx, network, destination)
  85. }
  86. func (h *Socks) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  87. ctx, metadata := adapter.ExtendContext(ctx)
  88. metadata.Outbound = h.tag
  89. metadata.Destination = destination
  90. if h.uotClient != nil {
  91. h.logger.InfoContext(ctx, "outbound UoT packet connection to ", destination)
  92. return h.uotClient.ListenPacket(ctx, destination)
  93. }
  94. if h.resolve && destination.IsFqdn() {
  95. destinationAddresses, err := h.router.LookupDefault(ctx, destination.Fqdn)
  96. if err != nil {
  97. return nil, err
  98. }
  99. packetConn, _, err := N.ListenSerial(ctx, h.client, destination, destinationAddresses)
  100. if err != nil {
  101. return nil, err
  102. }
  103. return packetConn, nil
  104. }
  105. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  106. return h.client.ListenPacket(ctx, destination)
  107. }
  108. // TODO
  109. // Deprecated
  110. func (h *Socks) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  111. if h.resolve {
  112. return NewDirectConnection(ctx, h.router, h, conn, metadata, dns.DomainStrategyUseIPv4)
  113. } else {
  114. return NewConnection(ctx, h, conn, metadata)
  115. }
  116. }
  117. // TODO
  118. // Deprecated
  119. func (h *Socks) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  120. if h.resolve {
  121. return NewDirectPacketConnection(ctx, h.router, h, conn, metadata, dns.DomainStrategyUseIPv4)
  122. } else {
  123. return NewPacketConnection(ctx, h, conn, metadata)
  124. }
  125. }