system_dialer.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. package internet
  2. import (
  3. "context"
  4. "syscall"
  5. "time"
  6. "github.com/xtls/xray-core/common"
  7. "github.com/xtls/xray-core/common/dice"
  8. "github.com/xtls/xray-core/common/net"
  9. "github.com/xtls/xray-core/common/net/cnc"
  10. "github.com/xtls/xray-core/common/session"
  11. "github.com/xtls/xray-core/features/dns"
  12. "github.com/xtls/xray-core/features/outbound"
  13. "github.com/xtls/xray-core/transport"
  14. "github.com/xtls/xray-core/transport/pipe"
  15. )
  16. var (
  17. effectiveSystemDialer SystemDialer = &DefaultSystemDialer{}
  18. )
  19. // InitSystemDialer: It's private method and you are NOT supposed to use this function.
  20. func InitSystemDialer(dc dns.Client, om outbound.Manager) {
  21. effectiveSystemDialer.Init(dc, om)
  22. }
  23. type SystemDialer interface {
  24. Dial(ctx context.Context, source net.Address, destination net.Destination, sockopt *SocketConfig) (net.Conn, error)
  25. Init(dc dns.Client, om outbound.Manager)
  26. }
  27. type DefaultSystemDialer struct {
  28. controllers []controller
  29. dns dns.Client
  30. obm outbound.Manager
  31. }
  32. func resolveSrcAddr(network net.Network, src net.Address) net.Addr {
  33. if src == nil || src == net.AnyIP {
  34. return nil
  35. }
  36. if network == net.Network_TCP {
  37. return &net.TCPAddr{
  38. IP: src.IP(),
  39. Port: 0,
  40. }
  41. }
  42. return &net.UDPAddr{
  43. IP: src.IP(),
  44. Port: 0,
  45. }
  46. }
  47. func hasBindAddr(sockopt *SocketConfig) bool {
  48. return sockopt != nil && len(sockopt.BindAddress) > 0 && sockopt.BindPort > 0
  49. }
  50. func (d *DefaultSystemDialer) lookupIP(domain string, strategy DomainStrategy, localAddr net.Address) ([]net.IP, error) {
  51. if d.dns == nil {
  52. return nil, nil
  53. }
  54. var option = dns.IPOption{
  55. IPv4Enable: true,
  56. IPv6Enable: true,
  57. FakeEnable: false,
  58. }
  59. switch {
  60. case strategy == DomainStrategy_USE_IP4 || (localAddr != nil && localAddr.Family().IsIPv4()):
  61. option = dns.IPOption{
  62. IPv4Enable: true,
  63. IPv6Enable: false,
  64. FakeEnable: false,
  65. }
  66. case strategy == DomainStrategy_USE_IP6 || (localAddr != nil && localAddr.Family().IsIPv6()):
  67. option = dns.IPOption{
  68. IPv4Enable: false,
  69. IPv6Enable: true,
  70. FakeEnable: false,
  71. }
  72. case strategy == DomainStrategy_AS_IS:
  73. return nil, nil
  74. }
  75. return d.dns.LookupIP(domain, option)
  76. }
  77. func (d *DefaultSystemDialer) canLookupIP(ctx context.Context, dst net.Destination, sockopt *SocketConfig) bool {
  78. if sockopt == nil || dst.Address.Family().IsIP() || d.dns == nil {
  79. return false
  80. }
  81. if dst.Address.Domain() == LookupDomainFromContext(ctx) {
  82. newError("infinite loop detected").AtError().WriteToLog(session.ExportIDToError(ctx))
  83. return false
  84. }
  85. return sockopt.DomainStrategy != DomainStrategy_AS_IS
  86. }
  87. func (d *DefaultSystemDialer) redirect(ctx context.Context, dst net.Destination, obt string) net.Conn {
  88. newError("redirecting request " + dst.String() + " to " + obt).WriteToLog(session.ExportIDToError(ctx))
  89. h := d.obm.GetHandler(obt)
  90. ctx = session.ContextWithOutbound(ctx, &session.Outbound{dst, nil})
  91. if h != nil {
  92. ur, uw := pipe.New(pipe.OptionsFromContext(ctx)...)
  93. dr, dw := pipe.New(pipe.OptionsFromContext(ctx)...)
  94. go h.Dispatch(ctx, &transport.Link{ur, dw})
  95. nc := cnc.NewConnection(
  96. cnc.ConnectionInputMulti(uw),
  97. cnc.ConnectionOutputMulti(dr),
  98. cnc.ConnectionOnClose(common.ChainedClosable{uw, dw}),
  99. )
  100. return nc
  101. }
  102. return nil
  103. }
  104. func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest net.Destination, sockopt *SocketConfig) (net.Conn, error) {
  105. newError("dialing to " + dest.String()).AtDebug().WriteToLog()
  106. if d.obm != nil && sockopt != nil && len(sockopt.DialerProxy) > 0 {
  107. nc := d.redirect(ctx, dest, sockopt.DialerProxy)
  108. if nc != nil {
  109. return nc, nil
  110. }
  111. }
  112. if d.canLookupIP(ctx, dest, sockopt) {
  113. ips, err := d.lookupIP(dest.Address.String(), sockopt.DomainStrategy, src)
  114. if err == nil && len(ips) > 0 {
  115. dest.Address = net.IPAddress(ips[dice.Roll(len(ips))])
  116. newError("replace destination with " + dest.String()).AtInfo().WriteToLog()
  117. } else if err != nil {
  118. newError("failed to resolve ip").Base(err).AtWarning().WriteToLog()
  119. }
  120. }
  121. if dest.Network == net.Network_UDP && !hasBindAddr(sockopt) {
  122. srcAddr := resolveSrcAddr(net.Network_UDP, src)
  123. if srcAddr == nil {
  124. srcAddr = &net.UDPAddr{
  125. IP: []byte{0, 0, 0, 0},
  126. Port: 0,
  127. }
  128. }
  129. packetConn, err := ListenSystemPacket(ctx, srcAddr, sockopt)
  130. if err != nil {
  131. return nil, err
  132. }
  133. destAddr, err := net.ResolveUDPAddr("udp", dest.NetAddr())
  134. if err != nil {
  135. return nil, err
  136. }
  137. return &PacketConnWrapper{
  138. conn: packetConn,
  139. dest: destAddr,
  140. }, nil
  141. }
  142. dialer := &net.Dialer{
  143. Timeout: time.Second * 16,
  144. DualStack: true,
  145. LocalAddr: resolveSrcAddr(dest.Network, src),
  146. }
  147. if sockopt != nil || len(d.controllers) > 0 {
  148. dialer.Control = func(network, address string, c syscall.RawConn) error {
  149. return c.Control(func(fd uintptr) {
  150. if sockopt != nil {
  151. if err := applyOutboundSocketOptions(network, address, fd, sockopt); err != nil {
  152. newError("failed to apply socket options").Base(err).WriteToLog(session.ExportIDToError(ctx))
  153. }
  154. if dest.Network == net.Network_UDP && hasBindAddr(sockopt) {
  155. if err := bindAddr(fd, sockopt.BindAddress, sockopt.BindPort); err != nil {
  156. newError("failed to bind source address to ", sockopt.BindAddress).Base(err).WriteToLog(session.ExportIDToError(ctx))
  157. }
  158. }
  159. }
  160. for _, ctl := range d.controllers {
  161. if err := ctl(network, address, fd); err != nil {
  162. newError("failed to apply external controller").Base(err).WriteToLog(session.ExportIDToError(ctx))
  163. }
  164. }
  165. })
  166. }
  167. }
  168. return dialer.DialContext(ctx, dest.Network.SystemString(), dest.NetAddr())
  169. }
  170. func (d *DefaultSystemDialer) Init(dc dns.Client, om outbound.Manager) {
  171. d.dns = dc
  172. d.obm = om
  173. }
  174. type PacketConnWrapper struct {
  175. conn net.PacketConn
  176. dest net.Addr
  177. }
  178. func (c *PacketConnWrapper) Close() error {
  179. return c.conn.Close()
  180. }
  181. func (c *PacketConnWrapper) LocalAddr() net.Addr {
  182. return c.conn.LocalAddr()
  183. }
  184. func (c *PacketConnWrapper) RemoteAddr() net.Addr {
  185. return c.dest
  186. }
  187. func (c *PacketConnWrapper) Write(p []byte) (int, error) {
  188. return c.conn.WriteTo(p, c.dest)
  189. }
  190. func (c *PacketConnWrapper) Read(p []byte) (int, error) {
  191. n, _, err := c.conn.ReadFrom(p)
  192. return n, err
  193. }
  194. func (c *PacketConnWrapper) WriteTo(p []byte, d net.Addr) (int, error) {
  195. return c.conn.WriteTo(p, d)
  196. }
  197. func (c *PacketConnWrapper) ReadFrom(p []byte) (int, net.Addr, error) {
  198. return c.conn.ReadFrom(p)
  199. }
  200. func (c *PacketConnWrapper) SetDeadline(t time.Time) error {
  201. return c.conn.SetDeadline(t)
  202. }
  203. func (c *PacketConnWrapper) SetReadDeadline(t time.Time) error {
  204. return c.conn.SetReadDeadline(t)
  205. }
  206. func (c *PacketConnWrapper) SetWriteDeadline(t time.Time) error {
  207. return c.conn.SetWriteDeadline(t)
  208. }
  209. type SystemDialerAdapter interface {
  210. Dial(network string, address string) (net.Conn, error)
  211. }
  212. type SimpleSystemDialer struct {
  213. adapter SystemDialerAdapter
  214. }
  215. func WithAdapter(dialer SystemDialerAdapter) SystemDialer {
  216. return &SimpleSystemDialer{
  217. adapter: dialer,
  218. }
  219. }
  220. func (v *SimpleSystemDialer) Init(_ dns.Client, _ outbound.Manager) {}
  221. func (v *SimpleSystemDialer) Dial(ctx context.Context, src net.Address, dest net.Destination, sockopt *SocketConfig) (net.Conn, error) {
  222. return v.adapter.Dial(dest.Network.SystemString(), dest.NetAddr())
  223. }
  224. // UseAlternativeSystemDialer replaces the current system dialer with a given one.
  225. // Caller must ensure there is no race condition.
  226. //
  227. // xray:api:stable
  228. func UseAlternativeSystemDialer(dialer SystemDialer) {
  229. if dialer == nil {
  230. effectiveSystemDialer = &DefaultSystemDialer{}
  231. }
  232. effectiveSystemDialer = dialer
  233. }
  234. // RegisterDialerController adds a controller to the effective system dialer.
  235. // The controller can be used to operate on file descriptors before they are put into use.
  236. // It only works when effective dialer is the default dialer.
  237. //
  238. // xray:api:beta
  239. func RegisterDialerController(ctl func(network, address string, fd uintptr) error) error {
  240. if ctl == nil {
  241. return newError("nil listener controller")
  242. }
  243. dialer, ok := effectiveSystemDialer.(*DefaultSystemDialer)
  244. if !ok {
  245. return newError("RegisterListenerController not supported in custom dialer")
  246. }
  247. dialer.controllers = append(dialer.controllers, ctl)
  248. return nil
  249. }