vless.go 6.8 KB

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