outbound.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package shadowsocks
  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. "github.com/sagernet/sing-box/common/mux"
  9. C "github.com/sagernet/sing-box/constant"
  10. "github.com/sagernet/sing-box/log"
  11. "github.com/sagernet/sing-box/option"
  12. "github.com/sagernet/sing-box/transport/sip003"
  13. "github.com/sagernet/sing-shadowsocks2"
  14. "github.com/sagernet/sing/common"
  15. "github.com/sagernet/sing/common/bufio"
  16. E "github.com/sagernet/sing/common/exceptions"
  17. "github.com/sagernet/sing/common/logger"
  18. M "github.com/sagernet/sing/common/metadata"
  19. N "github.com/sagernet/sing/common/network"
  20. "github.com/sagernet/sing/common/uot"
  21. )
  22. func RegisterOutbound(registry *outbound.Registry) {
  23. outbound.Register[option.ShadowsocksOutboundOptions](registry, C.TypeShadowsocks, NewOutbound)
  24. }
  25. type Outbound struct {
  26. outbound.Adapter
  27. logger logger.ContextLogger
  28. dialer N.Dialer
  29. method shadowsocks.Method
  30. serverAddr M.Socksaddr
  31. plugin sip003.Plugin
  32. uotClient *uot.Client
  33. multiplexDialer *mux.Client
  34. }
  35. func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowsocksOutboundOptions) (adapter.Outbound, error) {
  36. method, err := shadowsocks.CreateMethod(ctx, options.Method, shadowsocks.MethodOptions{
  37. Password: options.Password,
  38. })
  39. if err != nil {
  40. return nil, err
  41. }
  42. outboundDialer, err := dialer.New(ctx, options.DialerOptions, options.ServerIsDomain())
  43. if err != nil {
  44. return nil, err
  45. }
  46. outbound := &Outbound{
  47. Adapter: outbound.NewAdapterWithDialerOptions(C.TypeShadowsocks, tag, options.Network.Build(), options.DialerOptions),
  48. logger: logger,
  49. dialer: outboundDialer,
  50. method: method,
  51. serverAddr: options.ServerOptions.Build(),
  52. }
  53. if options.Plugin != "" {
  54. outbound.plugin, err = sip003.CreatePlugin(ctx, options.Plugin, options.PluginOptions, router, outbound.dialer, outbound.serverAddr)
  55. if err != nil {
  56. return nil, err
  57. }
  58. }
  59. uotOptions := common.PtrValueOrDefault(options.UDPOverTCP)
  60. if !uotOptions.Enabled {
  61. outbound.multiplexDialer, err = mux.NewClientWithOptions((*shadowsocksDialer)(outbound), logger, common.PtrValueOrDefault(options.Multiplex))
  62. if err != nil {
  63. return nil, err
  64. }
  65. }
  66. if uotOptions.Enabled {
  67. outbound.uotClient = &uot.Client{
  68. Dialer: (*shadowsocksDialer)(outbound),
  69. Version: uotOptions.Version,
  70. }
  71. }
  72. return outbound, nil
  73. }
  74. func (h *Outbound) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  75. ctx, metadata := adapter.ExtendContext(ctx)
  76. metadata.Outbound = h.Tag()
  77. metadata.Destination = destination
  78. if h.multiplexDialer == nil {
  79. switch N.NetworkName(network) {
  80. case N.NetworkTCP:
  81. h.logger.InfoContext(ctx, "outbound connection to ", destination)
  82. case N.NetworkUDP:
  83. if h.uotClient != nil {
  84. h.logger.InfoContext(ctx, "outbound UoT connect packet connection to ", destination)
  85. return h.uotClient.DialContext(ctx, network, destination)
  86. } else {
  87. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  88. }
  89. }
  90. return (*shadowsocksDialer)(h).DialContext(ctx, network, destination)
  91. } else {
  92. switch N.NetworkName(network) {
  93. case N.NetworkTCP:
  94. h.logger.InfoContext(ctx, "outbound multiplex connection to ", destination)
  95. case N.NetworkUDP:
  96. h.logger.InfoContext(ctx, "outbound multiplex packet connection to ", destination)
  97. }
  98. return h.multiplexDialer.DialContext(ctx, network, destination)
  99. }
  100. }
  101. func (h *Outbound) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  102. ctx, metadata := adapter.ExtendContext(ctx)
  103. metadata.Outbound = h.Tag()
  104. metadata.Destination = destination
  105. if h.multiplexDialer == nil {
  106. if h.uotClient != nil {
  107. h.logger.InfoContext(ctx, "outbound UoT packet connection to ", destination)
  108. return h.uotClient.ListenPacket(ctx, destination)
  109. } else {
  110. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  111. }
  112. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  113. return (*shadowsocksDialer)(h).ListenPacket(ctx, destination)
  114. } else {
  115. h.logger.InfoContext(ctx, "outbound multiplex packet connection to ", destination)
  116. return h.multiplexDialer.ListenPacket(ctx, destination)
  117. }
  118. }
  119. func (h *Outbound) InterfaceUpdated() {
  120. if h.multiplexDialer != nil {
  121. h.multiplexDialer.Reset()
  122. }
  123. }
  124. func (h *Outbound) Close() error {
  125. return common.Close(common.PtrOrNil(h.multiplexDialer))
  126. }
  127. var _ N.Dialer = (*shadowsocksDialer)(nil)
  128. type shadowsocksDialer Outbound
  129. func (h *shadowsocksDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  130. ctx, metadata := adapter.ExtendContext(ctx)
  131. metadata.Outbound = h.Tag()
  132. metadata.Destination = destination
  133. switch N.NetworkName(network) {
  134. case N.NetworkTCP:
  135. var outConn net.Conn
  136. var err error
  137. if h.plugin != nil {
  138. outConn, err = h.plugin.DialContext(ctx)
  139. } else {
  140. outConn, err = h.dialer.DialContext(ctx, N.NetworkTCP, h.serverAddr)
  141. }
  142. if err != nil {
  143. return nil, err
  144. }
  145. return h.method.DialEarlyConn(outConn, destination), nil
  146. case N.NetworkUDP:
  147. outConn, err := h.dialer.DialContext(ctx, N.NetworkUDP, h.serverAddr)
  148. if err != nil {
  149. return nil, err
  150. }
  151. return bufio.NewBindPacketConn(h.method.DialPacketConn(outConn), destination), nil
  152. default:
  153. return nil, E.Extend(N.ErrUnknownNetwork, network)
  154. }
  155. }
  156. func (h *shadowsocksDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  157. ctx, metadata := adapter.ExtendContext(ctx)
  158. metadata.Outbound = h.Tag()
  159. metadata.Destination = destination
  160. outConn, err := h.dialer.DialContext(ctx, N.NetworkUDP, h.serverAddr)
  161. if err != nil {
  162. return nil, err
  163. }
  164. return h.method.DialPacketConn(outConn), nil
  165. }