socks.go 3.1 KB

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