-
Notifications
You must be signed in to change notification settings - Fork 259
/
LookupIP.go
187 lines (155 loc) · 4.94 KB
/
LookupIP.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// +build android linux darwin
/*
* Copyright (c) 2015, Psiphon Inc.
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package psiphon
import (
"context"
"errors"
"fmt"
"net"
"os"
"syscall"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
)
// LookupIP resolves a hostname. When BindToDevice is not required, it
// simply uses net.LookupIP.
// When BindToDevice is required, LookupIP explicitly creates a UDP
// socket, binds it to the device, and makes an explicit DNS request
// to the specified DNS resolver.
func LookupIP(ctx context.Context, host string, config *DialConfig) ([]net.IP, error) {
ip := net.ParseIP(host)
if ip != nil {
return []net.IP{ip}, nil
}
if config.DeviceBinder != nil {
dnsServer := config.DnsServerGetter.GetPrimaryDnsServer()
ips, err := bindLookupIP(ctx, host, dnsServer, config)
if err == nil {
if len(ips) == 0 {
err = errors.New("empty address list")
} else {
return ips, err
}
}
dnsServer = config.DnsServerGetter.GetSecondaryDnsServer()
if dnsServer == "" {
return ips, err
}
NoticeAlert("retry resolve host %s: %s", host, err)
return bindLookupIP(ctx, host, dnsServer, config)
}
addrs, err := net.DefaultResolver.LookupIPAddr(ctx, host)
if err != nil {
return nil, common.ContextError(err)
}
ips := make([]net.IP, len(addrs))
for i, addr := range addrs {
ips[i] = addr.IP
}
return ips, nil
}
// bindLookupIP implements the BindToDevice LookupIP case.
// To implement socket device binding, the lower-level syscall APIs are used.
func bindLookupIP(
ctx context.Context, host, dnsServer string, config *DialConfig) ([]net.IP, error) {
// config.DnsServerGetter.GetDnsServers() must return IP addresses
ipAddr := net.ParseIP(dnsServer)
if ipAddr == nil {
return nil, common.ContextError(errors.New("invalid IP address"))
}
// When configured, attempt to synthesize an IPv6 address from
// an IPv4 address for compatibility on DNS64/NAT64 networks.
// If synthesize fails, try the original address.
if config.IPv6Synthesizer != nil && ipAddr.To4() != nil {
synthesizedIPAddress := config.IPv6Synthesizer.IPv6Synthesize(dnsServer)
if synthesizedIPAddress != "" {
synthesizedAddr := net.ParseIP(synthesizedIPAddress)
if synthesizedAddr != nil {
ipAddr = synthesizedAddr
}
}
}
var ipv4 [4]byte
var ipv6 [16]byte
var domain int
// Get address type (IPv4 or IPv6)
if ipAddr.To4() != nil {
copy(ipv4[:], ipAddr.To4())
domain = syscall.AF_INET
} else if ipAddr.To16() != nil {
copy(ipv6[:], ipAddr.To16())
domain = syscall.AF_INET6
} else {
return nil, common.ContextError(fmt.Errorf("invalid IP address for dns server: %s", ipAddr.String()))
}
socketFd, err := syscall.Socket(domain, syscall.SOCK_DGRAM, 0)
if err != nil {
return nil, common.ContextError(err)
}
_, err = config.DeviceBinder.BindToDevice(socketFd)
if err != nil {
syscall.Close(socketFd)
return nil, common.ContextError(fmt.Errorf("BindToDevice failed: %s", err))
}
// Connect socket to the server's IP address
// Note: no timeout or interrupt for this connect, as it's a datagram socket
if domain == syscall.AF_INET {
sockAddr := syscall.SockaddrInet4{Addr: ipv4, Port: DNS_PORT}
err = syscall.Connect(socketFd, &sockAddr)
} else if domain == syscall.AF_INET6 {
sockAddr := syscall.SockaddrInet6{Addr: ipv6, Port: DNS_PORT}
err = syscall.Connect(socketFd, &sockAddr)
}
if err != nil {
syscall.Close(socketFd)
return nil, common.ContextError(err)
}
// Convert the syscall socket to a net.Conn, for use in the dns package
// This code block is from:
// https://github.com/golang/go/issues/6966
file := os.NewFile(uintptr(socketFd), "")
netConn, err := net.FileConn(file) // net.FileConn() dups socketFd
file.Close() // file.Close() closes socketFd
if err != nil {
return nil, common.ContextError(err)
}
type resolveIPResult struct {
ips []net.IP
err error
}
resultChannel := make(chan resolveIPResult)
go func() {
ips, _, err := ResolveIP(host, netConn)
netConn.Close()
resultChannel <- resolveIPResult{ips: ips, err: err}
}()
var result resolveIPResult
select {
case result = <-resultChannel:
case <-ctx.Done():
result.err = ctx.Err()
// Interrupt the goroutine
netConn.Close()
<-resultChannel
}
if result.err != nil {
return nil, common.ContextError(err)
}
return result.ips, nil
}