shadowsocksr.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //go:build with_shadowsocksr
  2. package outbound
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "net"
  8. "github.com/sagernet/sing-box/adapter"
  9. "github.com/sagernet/sing-box/common/dialer"
  10. C "github.com/sagernet/sing-box/constant"
  11. "github.com/sagernet/sing-box/log"
  12. "github.com/sagernet/sing-box/option"
  13. "github.com/sagernet/sing-box/transport/clashssr/obfs"
  14. "github.com/sagernet/sing-box/transport/clashssr/protocol"
  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/Dreamacro/clash/transport/shadowsocks/core"
  20. "github.com/Dreamacro/clash/transport/shadowsocks/shadowstream"
  21. "github.com/Dreamacro/clash/transport/socks5"
  22. )
  23. var _ adapter.Outbound = (*ShadowsocksR)(nil)
  24. type ShadowsocksR struct {
  25. myOutboundAdapter
  26. dialer N.Dialer
  27. serverAddr M.Socksaddr
  28. cipher core.Cipher
  29. obfs obfs.Obfs
  30. protocol protocol.Protocol
  31. }
  32. func NewShadowsocksR(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowsocksROutboundOptions) (*ShadowsocksR, error) {
  33. outbound := &ShadowsocksR{
  34. myOutboundAdapter: myOutboundAdapter{
  35. protocol: C.TypeShadowsocksR,
  36. network: options.Network.Build(),
  37. router: router,
  38. logger: logger,
  39. tag: tag,
  40. },
  41. dialer: dialer.New(router, options.DialerOptions),
  42. serverAddr: options.ServerOptions.Build(),
  43. }
  44. var cipher string
  45. var err error
  46. switch options.Method {
  47. case "none":
  48. cipher = "dummy"
  49. default:
  50. cipher = options.Method
  51. }
  52. outbound.cipher, err = core.PickCipher(cipher, nil, options.Password)
  53. if err != nil {
  54. return nil, err
  55. }
  56. var (
  57. ivSize int
  58. key []byte
  59. )
  60. if cipher == "dummy" {
  61. ivSize = 0
  62. key = core.Kdf(options.Password, 16)
  63. } else {
  64. streamCipher, ok := outbound.cipher.(*core.StreamCipher)
  65. if !ok {
  66. return nil, fmt.Errorf("%s is not none or a supported stream cipher in ssr", cipher)
  67. }
  68. ivSize = streamCipher.IVSize()
  69. key = streamCipher.Key
  70. }
  71. obfs, obfsOverhead, err := obfs.PickObfs(options.Obfs, &obfs.Base{
  72. Host: options.Server,
  73. Port: int(options.ServerPort),
  74. Key: key,
  75. IVSize: ivSize,
  76. Param: options.ObfsParam,
  77. })
  78. if err != nil {
  79. return nil, E.Cause(err, "initialize obfs")
  80. }
  81. protocol, err := protocol.PickProtocol(options.Protocol, &protocol.Base{
  82. Key: key,
  83. Overhead: obfsOverhead,
  84. Param: options.ProtocolParam,
  85. })
  86. if err != nil {
  87. return nil, E.Cause(err, "initialize protocol")
  88. }
  89. outbound.obfs = obfs
  90. outbound.protocol = protocol
  91. return outbound, nil
  92. }
  93. func (h *ShadowsocksR) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  94. switch network {
  95. case N.NetworkTCP:
  96. h.logger.InfoContext(ctx, "outbound connection to ", destination)
  97. conn, err := h.dialer.DialContext(ctx, network, h.serverAddr)
  98. if err != nil {
  99. return nil, err
  100. }
  101. conn = h.cipher.StreamConn(h.obfs.StreamConn(conn))
  102. writeIv, err := conn.(*shadowstream.Conn).ObtainWriteIV()
  103. if err != nil {
  104. return nil, err
  105. }
  106. conn = h.protocol.StreamConn(conn, writeIv)
  107. err = M.SocksaddrSerializer.WriteAddrPort(conn, destination)
  108. if err != nil {
  109. return nil, E.Cause(err, "write request")
  110. }
  111. return conn, nil
  112. case N.NetworkUDP:
  113. conn, err := h.ListenPacket(ctx, destination)
  114. if err != nil {
  115. return nil, err
  116. }
  117. return &bufio.BindPacketConn{PacketConn: conn, Addr: destination}, nil
  118. default:
  119. return nil, E.Extend(N.ErrUnknownNetwork, network)
  120. }
  121. }
  122. func (h *ShadowsocksR) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  123. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  124. outConn, err := h.dialer.DialContext(ctx, N.NetworkUDP, h.serverAddr)
  125. if err != nil {
  126. return nil, err
  127. }
  128. packetConn := h.cipher.PacketConn(bufio.NewUnbindPacketConn(outConn))
  129. packetConn = h.protocol.PacketConn(packetConn)
  130. packetConn = &ssPacketConn{packetConn, outConn.RemoteAddr()}
  131. return packetConn, nil
  132. }
  133. func (h *ShadowsocksR) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  134. return NewConnection(ctx, h, conn, metadata)
  135. }
  136. func (h *ShadowsocksR) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  137. return NewPacketConnection(ctx, h, conn, metadata)
  138. }
  139. type ssPacketConn struct {
  140. net.PacketConn
  141. rAddr net.Addr
  142. }
  143. func (spc *ssPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
  144. packet, err := socks5.EncodeUDPPacket(socks5.ParseAddrToSocksAddr(addr), b)
  145. if err != nil {
  146. return
  147. }
  148. return spc.PacketConn.WriteTo(packet[3:], spc.rAddr)
  149. }
  150. func (spc *ssPacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
  151. n, _, e := spc.PacketConn.ReadFrom(b)
  152. if e != nil {
  153. return 0, nil, e
  154. }
  155. addr := socks5.SplitAddr(b[:n])
  156. if addr == nil {
  157. return 0, nil, errors.New("parse addr error")
  158. }
  159. udpAddr := addr.UDPAddr()
  160. if udpAddr == nil {
  161. return 0, nil, errors.New("parse addr error")
  162. }
  163. copy(b, b[len(addr):])
  164. return n - len(addr), udpAddr, e
  165. }