socks.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/common"
  11. E "github.com/sagernet/sing/common/exceptions"
  12. M "github.com/sagernet/sing/common/metadata"
  13. N "github.com/sagernet/sing/common/network"
  14. "github.com/sagernet/sing/common/uot"
  15. "github.com/sagernet/sing/protocol/socks"
  16. )
  17. var _ adapter.Outbound = (*Socks)(nil)
  18. type Socks struct {
  19. myOutboundAdapter
  20. client *socks.Client
  21. resolve bool
  22. uotClient *uot.Client
  23. }
  24. func NewSocks(router adapter.Router, logger log.ContextLogger, tag string, options option.SocksOutboundOptions) (*Socks, error) {
  25. var version socks.Version
  26. var err error
  27. if options.Version != "" {
  28. version, err = socks.ParseVersion(options.Version)
  29. } else {
  30. version = socks.Version5
  31. }
  32. if err != nil {
  33. return nil, err
  34. }
  35. outbound := &Socks{
  36. myOutboundAdapter: myOutboundAdapter{
  37. protocol: C.TypeSocks,
  38. network: options.Network.Build(),
  39. router: router,
  40. logger: logger,
  41. tag: tag,
  42. },
  43. client: socks.NewClient(dialer.New(router, options.DialerOptions), options.ServerOptions.Build(), version, options.Username, options.Password),
  44. resolve: version == socks.Version4,
  45. }
  46. uotOptions := common.PtrValueOrDefault(options.UDPOverTCPOptions)
  47. if uotOptions.Enabled {
  48. outbound.uotClient = &uot.Client{
  49. Dialer: outbound.client,
  50. Version: uotOptions.Version,
  51. }
  52. }
  53. return outbound, nil
  54. }
  55. func (h *Socks) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  56. ctx, metadata := adapter.AppendContext(ctx)
  57. metadata.Outbound = h.tag
  58. metadata.Destination = destination
  59. switch N.NetworkName(network) {
  60. case N.NetworkTCP:
  61. h.logger.InfoContext(ctx, "outbound connection to ", destination)
  62. case N.NetworkUDP:
  63. if h.uotClient != nil {
  64. h.logger.InfoContext(ctx, "outbound UoT connect packet connection to ", destination)
  65. return h.uotClient.DialContext(ctx, network, destination)
  66. }
  67. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  68. default:
  69. return nil, E.Extend(N.ErrUnknownNetwork, network)
  70. }
  71. if h.resolve && destination.IsFqdn() {
  72. addrs, err := h.router.LookupDefault(ctx, destination.Fqdn)
  73. if err != nil {
  74. return nil, err
  75. }
  76. return N.DialSerial(ctx, h.client, network, destination, addrs)
  77. }
  78. return h.client.DialContext(ctx, network, destination)
  79. }
  80. func (h *Socks) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  81. ctx, metadata := adapter.AppendContext(ctx)
  82. metadata.Outbound = h.tag
  83. metadata.Destination = destination
  84. if h.uotClient != nil {
  85. h.logger.InfoContext(ctx, "outbound UoT packet connection to ", destination)
  86. return h.uotClient.ListenPacket(ctx, destination)
  87. }
  88. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  89. return h.client.ListenPacket(ctx, destination)
  90. }
  91. func (h *Socks) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  92. return NewConnection(ctx, h, conn, metadata)
  93. }
  94. func (h *Socks) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  95. return NewPacketConnection(ctx, h, conn, metadata)
  96. }