vless.go 6.8 KB

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