LookupIP.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //go:build android || linux || darwin
  2. // +build android linux darwin
  3. /*
  4. * Copyright (c) 2015, Psiphon Inc.
  5. * All rights reserved.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. package psiphon
  22. import (
  23. "context"
  24. std_errors "errors"
  25. "net"
  26. "strconv"
  27. "syscall"
  28. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  29. )
  30. // LookupIP resolves a hostname. When BindToDevice is not required, it
  31. // simply uses net.LookupIP.
  32. // When BindToDevice is required, LookupIP explicitly creates a UDP
  33. // socket, binds it to the device, and makes an explicit DNS request
  34. // to the specified DNS resolver.
  35. func LookupIP(ctx context.Context, host string, config *DialConfig) ([]net.IP, error) {
  36. ip := net.ParseIP(host)
  37. if ip != nil {
  38. return []net.IP{ip}, nil
  39. }
  40. if config.DeviceBinder != nil {
  41. dnsServer := config.DnsServerGetter.GetPrimaryDnsServer()
  42. ips, err := bindLookupIP(ctx, host, dnsServer, config)
  43. if err == nil {
  44. if len(ips) == 0 {
  45. err = std_errors.New("empty address list")
  46. } else {
  47. return ips, err
  48. }
  49. }
  50. dnsServer = config.DnsServerGetter.GetSecondaryDnsServer()
  51. if dnsServer == "" {
  52. return ips, err
  53. }
  54. if GetEmitNetworkParameters() {
  55. NoticeWarning("retry resolve host %s: %s", host, err)
  56. }
  57. return bindLookupIP(ctx, host, dnsServer, config)
  58. }
  59. addrs, err := net.DefaultResolver.LookupIPAddr(ctx, host)
  60. // Remove domain names from "net" error messages.
  61. if err != nil && !GetEmitNetworkParameters() {
  62. err = RedactNetError(err)
  63. }
  64. if err != nil {
  65. return nil, errors.Trace(err)
  66. }
  67. ips := make([]net.IP, len(addrs))
  68. for i, addr := range addrs {
  69. ips[i] = addr.IP
  70. }
  71. return ips, nil
  72. }
  73. // bindLookupIP implements the BindToDevice LookupIP case.
  74. // To implement socket device binding, the lower-level syscall APIs are used.
  75. func bindLookupIP(
  76. ctx context.Context, host, dnsServer string, config *DialConfig) ([]net.IP, error) {
  77. // config.DnsServerGetter.GetDnsServers() must return IP addresses
  78. ipAddr := net.ParseIP(dnsServer)
  79. if ipAddr == nil {
  80. return nil, errors.TraceNew("invalid IP address")
  81. }
  82. // When configured, attempt to synthesize an IPv6 address from
  83. // an IPv4 address for compatibility on DNS64/NAT64 networks.
  84. // If synthesize fails, try the original address.
  85. if config.IPv6Synthesizer != nil && ipAddr.To4() != nil {
  86. synthesizedIPAddress := config.IPv6Synthesizer.IPv6Synthesize(dnsServer)
  87. if synthesizedIPAddress != "" {
  88. synthesizedAddr := net.ParseIP(synthesizedIPAddress)
  89. if synthesizedAddr != nil {
  90. ipAddr = synthesizedAddr
  91. }
  92. }
  93. }
  94. dialer := &net.Dialer{
  95. Control: func(_, _ string, c syscall.RawConn) error {
  96. var controlErr error
  97. err := c.Control(func(fd uintptr) {
  98. socketFD := int(fd)
  99. _, err := config.DeviceBinder.BindToDevice(socketFD)
  100. if err != nil {
  101. controlErr = errors.Tracef("BindToDevice failed: %s", err)
  102. return
  103. }
  104. })
  105. if controlErr != nil {
  106. return errors.Trace(controlErr)
  107. }
  108. return errors.Trace(err)
  109. },
  110. }
  111. netConn, err := dialer.DialContext(
  112. ctx, "udp", net.JoinHostPort(ipAddr.String(), strconv.Itoa(DNS_PORT)))
  113. if err != nil {
  114. return nil, errors.Trace(err)
  115. }
  116. type resolveIPResult struct {
  117. ips []net.IP
  118. err error
  119. }
  120. resultChannel := make(chan resolveIPResult)
  121. go func() {
  122. ips, _, err := ResolveIP(host, netConn)
  123. netConn.Close()
  124. resultChannel <- resolveIPResult{ips: ips, err: err}
  125. }()
  126. var result resolveIPResult
  127. select {
  128. case result = <-resultChannel:
  129. case <-ctx.Done():
  130. result.err = ctx.Err()
  131. // Interrupt the goroutine
  132. netConn.Close()
  133. <-resultChannel
  134. }
  135. if result.err != nil {
  136. return nil, errors.Trace(err)
  137. }
  138. return result.ips, nil
  139. }