outbound.go 3.8 KB

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