outbound.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. }
  106. func (h *Outbound) Close() error {
  107. return common.Close(common.PtrOrNil(h.multiplexDialer), h.transport)
  108. }
  109. func (h *Outbound) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  110. if h.multiplexDialer == nil {
  111. switch N.NetworkName(network) {
  112. case N.NetworkTCP:
  113. h.logger.InfoContext(ctx, "outbound connection to ", destination)
  114. case N.NetworkUDP:
  115. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  116. }
  117. return (*vmessDialer)(h).DialContext(ctx, network, destination)
  118. } else {
  119. switch N.NetworkName(network) {
  120. case N.NetworkTCP:
  121. h.logger.InfoContext(ctx, "outbound multiplex connection to ", destination)
  122. case N.NetworkUDP:
  123. h.logger.InfoContext(ctx, "outbound multiplex packet connection to ", destination)
  124. }
  125. return h.multiplexDialer.DialContext(ctx, network, destination)
  126. }
  127. }
  128. func (h *Outbound) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  129. if h.multiplexDialer == nil {
  130. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  131. return (*vmessDialer)(h).ListenPacket(ctx, destination)
  132. } else {
  133. h.logger.InfoContext(ctx, "outbound multiplex packet connection to ", destination)
  134. return h.multiplexDialer.ListenPacket(ctx, destination)
  135. }
  136. }
  137. type vmessDialer Outbound
  138. func (h *vmessDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  139. ctx, metadata := adapter.ExtendContext(ctx)
  140. metadata.Outbound = h.Tag()
  141. metadata.Destination = destination
  142. var conn net.Conn
  143. var err error
  144. if h.transport != nil {
  145. conn, err = h.transport.DialContext(ctx)
  146. } else {
  147. conn, err = h.dialer.DialContext(ctx, N.NetworkTCP, h.serverAddr)
  148. if err == nil && h.tlsConfig != nil {
  149. conn, err = tls.ClientHandshake(ctx, conn, h.tlsConfig)
  150. }
  151. }
  152. if err != nil {
  153. common.Close(conn)
  154. return nil, err
  155. }
  156. switch N.NetworkName(network) {
  157. case N.NetworkTCP:
  158. return h.client.DialEarlyConn(conn, destination), nil
  159. case N.NetworkUDP:
  160. return h.client.DialEarlyPacketConn(conn, destination), nil
  161. default:
  162. return nil, E.Extend(N.ErrUnknownNetwork, network)
  163. }
  164. }
  165. func (h *vmessDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  166. ctx, metadata := adapter.ExtendContext(ctx)
  167. metadata.Outbound = h.Tag()
  168. metadata.Destination = destination
  169. var conn net.Conn
  170. var err error
  171. if h.transport != nil {
  172. conn, err = h.transport.DialContext(ctx)
  173. } else {
  174. conn, err = h.dialer.DialContext(ctx, N.NetworkTCP, h.serverAddr)
  175. if err == nil && h.tlsConfig != nil {
  176. conn, err = tls.ClientHandshake(ctx, conn, h.tlsConfig)
  177. }
  178. }
  179. if err != nil {
  180. return nil, err
  181. }
  182. if h.packetAddr {
  183. if destination.IsFqdn() {
  184. return nil, E.New("packetaddr: domain destination is not supported")
  185. }
  186. return packetaddr.NewConn(h.client.DialEarlyPacketConn(conn, M.Socksaddr{Fqdn: packetaddr.SeqPacketMagicAddress}), destination), nil
  187. } else if h.xudp {
  188. return h.client.DialEarlyXUDPPacketConn(conn, destination), nil
  189. } else {
  190. return h.client.DialEarlyPacketConn(conn, destination), nil
  191. }
  192. }