shadowsocks.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. "github.com/sagernet/sing-box/common/mux"
  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-box/transport/sip003"
  12. "github.com/sagernet/sing-shadowsocks"
  13. "github.com/sagernet/sing-shadowsocks/shadowimpl"
  14. "github.com/sagernet/sing/common"
  15. "github.com/sagernet/sing/common/bufio"
  16. E "github.com/sagernet/sing/common/exceptions"
  17. M "github.com/sagernet/sing/common/metadata"
  18. N "github.com/sagernet/sing/common/network"
  19. "github.com/sagernet/sing/common/uot"
  20. )
  21. var _ adapter.Outbound = (*Shadowsocks)(nil)
  22. type Shadowsocks struct {
  23. myOutboundAdapter
  24. dialer N.Dialer
  25. method shadowsocks.Method
  26. serverAddr M.Socksaddr
  27. plugin sip003.Plugin
  28. uot bool
  29. multiplexDialer N.Dialer
  30. }
  31. func NewShadowsocks(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowsocksOutboundOptions) (*Shadowsocks, error) {
  32. method, err := shadowimpl.FetchMethod(options.Method, options.Password)
  33. if err != nil {
  34. return nil, err
  35. }
  36. outbound := &Shadowsocks{
  37. myOutboundAdapter: myOutboundAdapter{
  38. protocol: C.TypeShadowsocks,
  39. network: options.Network.Build(),
  40. router: router,
  41. logger: logger,
  42. tag: tag,
  43. },
  44. dialer: dialer.New(router, options.DialerOptions),
  45. method: method,
  46. serverAddr: options.ServerOptions.Build(),
  47. uot: options.UoT,
  48. }
  49. if options.Plugin != "" {
  50. outbound.plugin, err = sip003.CreatePlugin(options.Plugin, options.PluginOptions, router, outbound.dialer, outbound.serverAddr)
  51. if err != nil {
  52. return nil, err
  53. }
  54. }
  55. if !options.UoT {
  56. outbound.multiplexDialer, err = mux.NewClientWithOptions(ctx, (*shadowsocksDialer)(outbound), common.PtrValueOrDefault(options.MultiplexOptions))
  57. if err != nil {
  58. return nil, err
  59. }
  60. }
  61. return outbound, nil
  62. }
  63. func (h *Shadowsocks) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  64. ctx, metadata := adapter.AppendContext(ctx)
  65. metadata.Outbound = h.tag
  66. metadata.Destination = destination
  67. if h.multiplexDialer == nil {
  68. switch N.NetworkName(network) {
  69. case N.NetworkTCP:
  70. h.logger.InfoContext(ctx, "outbound connection to ", destination)
  71. case N.NetworkUDP:
  72. if h.uot {
  73. h.logger.InfoContext(ctx, "outbound UoT packet connection to ", destination)
  74. tcpConn, err := (*shadowsocksDialer)(h).DialContext(ctx, N.NetworkTCP, M.Socksaddr{
  75. Fqdn: uot.UOTMagicAddress,
  76. Port: destination.Port,
  77. })
  78. if err != nil {
  79. return nil, err
  80. }
  81. return uot.NewClientConn(tcpConn), nil
  82. }
  83. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  84. }
  85. return (*shadowsocksDialer)(h).DialContext(ctx, network, destination)
  86. } else {
  87. switch N.NetworkName(network) {
  88. case N.NetworkTCP:
  89. h.logger.InfoContext(ctx, "outbound multiplex connection to ", destination)
  90. case N.NetworkUDP:
  91. h.logger.InfoContext(ctx, "outbound multiplex packet connection to ", destination)
  92. }
  93. return h.multiplexDialer.DialContext(ctx, network, destination)
  94. }
  95. }
  96. func (h *Shadowsocks) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  97. ctx, metadata := adapter.AppendContext(ctx)
  98. metadata.Outbound = h.tag
  99. metadata.Destination = destination
  100. if h.multiplexDialer == nil {
  101. if h.uot {
  102. h.logger.InfoContext(ctx, "outbound UoT packet connection to ", destination)
  103. tcpConn, err := (*shadowsocksDialer)(h).DialContext(ctx, N.NetworkTCP, M.Socksaddr{
  104. Fqdn: uot.UOTMagicAddress,
  105. Port: destination.Port,
  106. })
  107. if err != nil {
  108. return nil, err
  109. }
  110. return uot.NewClientConn(tcpConn), nil
  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 *Shadowsocks) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  120. return NewEarlyConnection(ctx, h, conn, metadata)
  121. }
  122. func (h *Shadowsocks) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  123. return NewPacketConnection(ctx, h, conn, metadata)
  124. }
  125. func (h *Shadowsocks) Close() error {
  126. return common.Close(h.multiplexDialer)
  127. }
  128. var _ N.Dialer = (*shadowsocksDialer)(nil)
  129. type shadowsocksDialer Shadowsocks
  130. func (h *shadowsocksDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  131. switch N.NetworkName(network) {
  132. case N.NetworkTCP:
  133. var outConn net.Conn
  134. var err error
  135. if h.plugin != nil {
  136. outConn, err = h.plugin.DialContext(ctx)
  137. } else {
  138. outConn, err = h.dialer.DialContext(ctx, N.NetworkTCP, h.serverAddr)
  139. }
  140. if err != nil {
  141. return nil, err
  142. }
  143. return h.method.DialEarlyConn(outConn, destination), nil
  144. case N.NetworkUDP:
  145. outConn, err := h.dialer.DialContext(ctx, N.NetworkUDP, h.serverAddr)
  146. if err != nil {
  147. return nil, err
  148. }
  149. return &bufio.BindPacketConn{PacketConn: h.method.DialPacketConn(outConn), Addr: destination}, nil
  150. default:
  151. return nil, E.Extend(N.ErrUnknownNetwork, network)
  152. }
  153. }
  154. func (h *shadowsocksDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  155. outConn, err := h.dialer.DialContext(ctx, N.NetworkUDP, h.serverAddr)
  156. if err != nil {
  157. return nil, err
  158. }
  159. return h.method.DialPacketConn(outConn), nil
  160. }