protocol.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package mux
  2. import (
  3. "encoding/binary"
  4. "io"
  5. "net"
  6. C "github.com/sagernet/sing-box/constant"
  7. "github.com/sagernet/sing/common"
  8. "github.com/sagernet/sing/common/buf"
  9. E "github.com/sagernet/sing/common/exceptions"
  10. M "github.com/sagernet/sing/common/metadata"
  11. N "github.com/sagernet/sing/common/network"
  12. "github.com/sagernet/sing/common/rw"
  13. "github.com/sagernet/smux"
  14. "github.com/hashicorp/yamux"
  15. )
  16. var Destination = M.Socksaddr{
  17. Fqdn: "sp.mux.sing-box.arpa",
  18. Port: 444,
  19. }
  20. const (
  21. ProtocolSMux Protocol = iota
  22. ProtocolYAMux
  23. )
  24. type Protocol byte
  25. func ParseProtocol(name string) (Protocol, error) {
  26. switch name {
  27. case "", "smux":
  28. return ProtocolSMux, nil
  29. case "yamux":
  30. return ProtocolYAMux, nil
  31. default:
  32. return ProtocolYAMux, E.New("unknown multiplex protocol: ", name)
  33. }
  34. }
  35. func (p Protocol) newServer(conn net.Conn) (abstractSession, error) {
  36. switch p {
  37. case ProtocolSMux:
  38. session, err := smux.Server(conn, nil)
  39. if err != nil {
  40. return nil, err
  41. }
  42. return &smuxSession{session}, nil
  43. case ProtocolYAMux:
  44. return yamux.Server(conn, yaMuxConfig())
  45. default:
  46. panic("unknown protocol")
  47. }
  48. }
  49. func (p Protocol) newClient(conn net.Conn) (abstractSession, error) {
  50. switch p {
  51. case ProtocolSMux:
  52. session, err := smux.Client(conn, nil)
  53. if err != nil {
  54. return nil, err
  55. }
  56. return &smuxSession{session}, nil
  57. case ProtocolYAMux:
  58. return yamux.Client(conn, yaMuxConfig())
  59. default:
  60. panic("unknown protocol")
  61. }
  62. }
  63. func yaMuxConfig() *yamux.Config {
  64. config := yamux.DefaultConfig()
  65. config.LogOutput = io.Discard
  66. config.StreamCloseTimeout = C.TCPTimeout
  67. config.StreamOpenTimeout = C.TCPTimeout
  68. return config
  69. }
  70. func (p Protocol) String() string {
  71. switch p {
  72. case ProtocolSMux:
  73. return "smux"
  74. case ProtocolYAMux:
  75. return "yamux"
  76. default:
  77. return "unknown"
  78. }
  79. }
  80. const (
  81. version0 = 0
  82. )
  83. type Request struct {
  84. Protocol Protocol
  85. }
  86. func ReadRequest(reader io.Reader) (*Request, error) {
  87. version, err := rw.ReadByte(reader)
  88. if err != nil {
  89. return nil, err
  90. }
  91. if version != version0 {
  92. return nil, E.New("unsupported version: ", version)
  93. }
  94. protocol, err := rw.ReadByte(reader)
  95. if err != nil {
  96. return nil, err
  97. }
  98. if protocol > byte(ProtocolYAMux) {
  99. return nil, E.New("unsupported protocol: ", protocol)
  100. }
  101. return &Request{Protocol: Protocol(protocol)}, nil
  102. }
  103. func EncodeRequest(buffer *buf.Buffer, request Request) {
  104. buffer.WriteByte(version0)
  105. buffer.WriteByte(byte(request.Protocol))
  106. }
  107. const (
  108. flagUDP = 1
  109. flagAddr = 2
  110. statusSuccess = 0
  111. statusError = 1
  112. )
  113. type StreamRequest struct {
  114. Network string
  115. Destination M.Socksaddr
  116. PacketAddr bool
  117. }
  118. func ReadStreamRequest(reader io.Reader) (*StreamRequest, error) {
  119. var flags uint16
  120. err := binary.Read(reader, binary.BigEndian, &flags)
  121. if err != nil {
  122. return nil, err
  123. }
  124. destination, err := M.SocksaddrSerializer.ReadAddrPort(reader)
  125. if err != nil {
  126. return nil, err
  127. }
  128. var network string
  129. var udpAddr bool
  130. if flags&flagUDP == 0 {
  131. network = N.NetworkTCP
  132. } else {
  133. network = N.NetworkUDP
  134. udpAddr = flags&flagAddr != 0
  135. }
  136. return &StreamRequest{network, destination, udpAddr}, nil
  137. }
  138. func requestLen(request StreamRequest) int {
  139. var rLen int
  140. rLen += 1 // version
  141. rLen += 2 // flags
  142. rLen += M.SocksaddrSerializer.AddrPortLen(request.Destination)
  143. return rLen
  144. }
  145. func EncodeStreamRequest(request StreamRequest, buffer *buf.Buffer) {
  146. destination := request.Destination
  147. var flags uint16
  148. if request.Network == N.NetworkUDP {
  149. flags |= flagUDP
  150. }
  151. if request.PacketAddr {
  152. flags |= flagAddr
  153. if !destination.IsValid() {
  154. destination = Destination
  155. }
  156. }
  157. common.Must(
  158. binary.Write(buffer, binary.BigEndian, flags),
  159. M.SocksaddrSerializer.WriteAddrPort(buffer, destination),
  160. )
  161. }
  162. type StreamResponse struct {
  163. Status uint8
  164. Message string
  165. }
  166. func ReadStreamResponse(reader io.Reader) (*StreamResponse, error) {
  167. var response StreamResponse
  168. status, err := rw.ReadByte(reader)
  169. if err != nil {
  170. return nil, err
  171. }
  172. response.Status = status
  173. if status == statusError {
  174. response.Message, err = rw.ReadVString(reader)
  175. if err != nil {
  176. return nil, err
  177. }
  178. }
  179. return &response, nil
  180. }
  181. type wrapStream struct {
  182. net.Conn
  183. }
  184. func (w *wrapStream) Read(p []byte) (n int, err error) {
  185. n, err = w.Conn.Read(p)
  186. err = wrapError(err)
  187. return
  188. }
  189. func (w *wrapStream) Write(p []byte) (n int, err error) {
  190. n, err = w.Conn.Write(p)
  191. err = wrapError(err)
  192. return
  193. }
  194. func (w *wrapStream) WriteIsThreadUnsafe() {
  195. }
  196. func (w *wrapStream) Upstream() any {
  197. return w.Conn
  198. }
  199. func wrapError(err error) error {
  200. switch err {
  201. case yamux.ErrStreamClosed:
  202. return io.EOF
  203. default:
  204. return err
  205. }
  206. }