vless.go 6.5 KB

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