outbound.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package vmess
  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. "github.com/sagernet/sing-box/common/tls"
  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/v2ray"
  14. "github.com/sagernet/sing-vmess"
  15. "github.com/sagernet/sing-vmess/packetaddr"
  16. "github.com/sagernet/sing/common"
  17. E "github.com/sagernet/sing/common/exceptions"
  18. "github.com/sagernet/sing/common/logger"
  19. M "github.com/sagernet/sing/common/metadata"
  20. N "github.com/sagernet/sing/common/network"
  21. "github.com/sagernet/sing/common/ntp"
  22. )
  23. func RegisterOutbound(registry *outbound.Registry) {
  24. outbound.Register[option.VMessOutboundOptions](registry, C.TypeVMess, NewOutbound)
  25. }
  26. type Outbound struct {
  27. outbound.Adapter
  28. logger logger.ContextLogger
  29. dialer N.Dialer
  30. client *vmess.Client
  31. serverAddr M.Socksaddr
  32. multiplexDialer *mux.Client
  33. tlsConfig tls.Config
  34. transport adapter.V2RayClientTransport
  35. packetAddr bool
  36. xudp bool
  37. }
  38. func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.VMessOutboundOptions) (adapter.Outbound, error) {
  39. outboundDialer, err := dialer.New(ctx, options.DialerOptions, options.ServerIsDomain())
  40. if err != nil {
  41. return nil, err
  42. }
  43. outbound := &Outbound{
  44. Adapter: outbound.NewAdapterWithDialerOptions(C.TypeVMess, tag, options.Network.Build(), options.DialerOptions),
  45. logger: logger,
  46. dialer: outboundDialer,
  47. serverAddr: options.ServerOptions.Build(),
  48. }
  49. if options.TLS != nil {
  50. outbound.tlsConfig, err = tls.NewClient(ctx, options.Server, common.PtrValueOrDefault(options.TLS))
  51. if err != nil {
  52. return nil, err
  53. }
  54. }
  55. if options.Transport != nil {
  56. outbound.transport, err = v2ray.NewClientTransport(ctx, outbound.dialer, outbound.serverAddr, common.PtrValueOrDefault(options.Transport), outbound.tlsConfig)
  57. if err != nil {
  58. return nil, E.Cause(err, "create client transport: ", options.Transport.Type)
  59. }
  60. }
  61. outbound.multiplexDialer, err = mux.NewClientWithOptions((*vmessDialer)(outbound), logger, common.PtrValueOrDefault(options.Multiplex))
  62. if err != nil {
  63. return nil, err
  64. }
  65. switch options.PacketEncoding {
  66. case "":
  67. case "packetaddr":
  68. outbound.packetAddr = true
  69. case "xudp":
  70. outbound.xudp = true
  71. default:
  72. return nil, E.New("unknown packet encoding: ", options.PacketEncoding)
  73. }
  74. var clientOptions []vmess.ClientOption
  75. if timeFunc := ntp.TimeFuncFromContext(ctx); timeFunc != nil {
  76. clientOptions = append(clientOptions, vmess.ClientWithTimeFunc(timeFunc))
  77. }
  78. if options.GlobalPadding {
  79. clientOptions = append(clientOptions, vmess.ClientWithGlobalPadding())
  80. }
  81. if options.AuthenticatedLength {
  82. clientOptions = append(clientOptions, vmess.ClientWithAuthenticatedLength())
  83. }
  84. security := options.Security
  85. if security == "" {
  86. security = "auto"
  87. }
  88. if security == "auto" && outbound.tlsConfig != nil {
  89. security = "zero"
  90. }
  91. client, err := vmess.NewClient(options.UUID, security, options.AlterId, clientOptions...)
  92. if err != nil {
  93. return nil, err
  94. }
  95. outbound.client = client
  96. return outbound, nil
  97. }
  98. func (h *Outbound) InterfaceUpdated() {
  99. if h.transport != nil {
  100. h.transport.Close()
  101. }
  102. if h.multiplexDialer != nil {
  103. h.multiplexDialer.Reset()
  104. }
  105. return
  106. }
  107. func (h *Outbound) Close() error {
  108. return common.Close(common.PtrOrNil(h.multiplexDialer), h.transport)
  109. }
  110. func (h *Outbound) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  111. if h.multiplexDialer == nil {
  112. switch N.NetworkName(network) {
  113. case N.NetworkTCP:
  114. h.logger.InfoContext(ctx, "outbound connection to ", destination)
  115. case N.NetworkUDP:
  116. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  117. }
  118. return (*vmessDialer)(h).DialContext(ctx, network, destination)
  119. } else {
  120. switch N.NetworkName(network) {
  121. case N.NetworkTCP:
  122. h.logger.InfoContext(ctx, "outbound multiplex connection to ", destination)
  123. case N.NetworkUDP:
  124. h.logger.InfoContext(ctx, "outbound multiplex packet connection to ", destination)
  125. }
  126. return h.multiplexDialer.DialContext(ctx, network, destination)
  127. }
  128. }
  129. func (h *Outbound) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  130. if h.multiplexDialer == nil {
  131. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  132. return (*vmessDialer)(h).ListenPacket(ctx, destination)
  133. } else {
  134. h.logger.InfoContext(ctx, "outbound multiplex packet connection to ", destination)
  135. return h.multiplexDialer.ListenPacket(ctx, destination)
  136. }
  137. }
  138. type vmessDialer Outbound
  139. func (h *vmessDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  140. ctx, metadata := adapter.ExtendContext(ctx)
  141. metadata.Outbound = h.Tag()
  142. metadata.Destination = destination
  143. var conn net.Conn
  144. var err error
  145. if h.transport != nil {
  146. conn, err = h.transport.DialContext(ctx)
  147. } else {
  148. conn, err = h.dialer.DialContext(ctx, N.NetworkTCP, h.serverAddr)
  149. if err == nil && h.tlsConfig != nil {
  150. conn, err = tls.ClientHandshake(ctx, conn, h.tlsConfig)
  151. }
  152. }
  153. if err != nil {
  154. common.Close(conn)
  155. return nil, err
  156. }
  157. switch N.NetworkName(network) {
  158. case N.NetworkTCP:
  159. return h.client.DialEarlyConn(conn, destination), nil
  160. case N.NetworkUDP:
  161. return h.client.DialEarlyPacketConn(conn, destination), nil
  162. default:
  163. return nil, E.Extend(N.ErrUnknownNetwork, network)
  164. }
  165. }
  166. func (h *vmessDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  167. ctx, metadata := adapter.ExtendContext(ctx)
  168. metadata.Outbound = h.Tag()
  169. metadata.Destination = destination
  170. var conn net.Conn
  171. var err error
  172. if h.transport != nil {
  173. conn, err = h.transport.DialContext(ctx)
  174. } else {
  175. conn, err = h.dialer.DialContext(ctx, N.NetworkTCP, h.serverAddr)
  176. if err == nil && h.tlsConfig != nil {
  177. conn, err = tls.ClientHandshake(ctx, conn, h.tlsConfig)
  178. }
  179. }
  180. if err != nil {
  181. return nil, err
  182. }
  183. if h.packetAddr {
  184. if destination.IsFqdn() {
  185. return nil, E.New("packetaddr: domain destination is not supported")
  186. }
  187. return packetaddr.NewConn(h.client.DialEarlyPacketConn(conn, M.Socksaddr{Fqdn: packetaddr.SeqPacketMagicAddress}), destination), nil
  188. } else if h.xudp {
  189. return h.client.DialEarlyXUDPPacketConn(conn, destination), nil
  190. } else {
  191. return h.client.DialEarlyPacketConn(conn, destination), nil
  192. }
  193. }