forked from sindresorhus/public-ip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
243 lines (204 loc) · 4.52 KB
/
index.js
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
'use strict';
const {promisify} = require('util');
const dgram = require('dgram');
const dns = require('dns-socket');
const {get: got, CancelError} = require('got');
const isIp = require('is-ip');
const defaults = {
timeout: 5000,
onlyHttps: false
};
const dnsServers = [
{
v4: {
servers: [
'208.67.222.222',
'208.67.220.220',
'208.67.222.220',
'208.67.220.222'
],
name: 'myip.opendns.com',
type: 'A'
},
v6: {
servers: [
'2620:0:ccc::2',
'2620:0:ccd::2'
],
name: 'myip.opendns.com',
type: 'AAAA'
}
},
{
v4: {
servers: [
'216.239.32.10',
'216.239.34.10',
'216.239.36.10',
'216.239.38.10'
],
name: 'o-o.myaddr.l.google.com',
type: 'TXT',
transform: ip => ip.replace(/"/g, '')
},
v6: {
servers: [
'2001:4860:4802:32::a',
'2001:4860:4802:34::a',
'2001:4860:4802:36::a',
'2001:4860:4802:38::a'
],
name: 'o-o.myaddr.l.google.com',
type: 'TXT',
transform: ip => ip.replace(/"/g, '')
}
}
];
const type = {
v4: {
dnsServers: dnsServers.map(({v4: {servers, ...question}}) => ({
servers, question
})),
httpsUrls: [
'https://icanhazip.com/',
'https://api.ipify.org/'
]
},
v6: {
dnsServers: dnsServers.map(({v6: {servers, ...question}}) => ({
servers, question
})),
httpsUrls: [
'https://icanhazip.com/',
'https://api6.ipify.org/'
]
}
};
const queryDns = (version, options) => {
const data = type[version];
const socket = dns({
retries: 0,
maxQueries: 1,
socket: dgram.createSocket(version === 'v6' ? 'udp6' : 'udp4'),
timeout: options.timeout
});
socket.retries = 0; // Temp fix, pull request: https://github.com/mafintosh/dns-socket/pull/22
const socketQuery = promisify(socket.query.bind(socket));
const promise = (async () => {
for (const dnsServerInfo of data.dnsServers) {
const {servers, question} = dnsServerInfo;
for (const server of servers) {
if (socket.destroyed) {
return;
}
try {
const {name, type, transform} = question;
// eslint-disable-next-line no-await-in-loop
const dnsResponse = await socketQuery({questions: [{name, type}]}, 53, server);
const {
answers: {
0: {
data
}
}
} = dnsResponse;
const response = (typeof data === 'string' ? data : data.toString()).trim();
const ip = transform ? transform(response) : response;
if (ip && isIp[version](ip)) {
socket.destroy();
return ip;
}
} catch (_) {}
}
}
socket.destroy();
throw new Error('Couldn\'t find your IP');
})();
promise.cancel = () => {
socket.destroy();
};
return promise;
};
const queryHttps = (version, options) => {
let cancel;
const promise = (async () => {
try {
const requestOptions = {
family: version === 'v6' ? 6 : 4,
retries: 0,
timeout: options.timeout
};
const urls = [].concat.apply(type[version].httpsUrls, options.fallbackUrls || []);
for (const url of urls) {
try {
const gotPromise = got(url, requestOptions);
cancel = gotPromise.cancel;
// eslint-disable-next-line no-await-in-loop
const response = await gotPromise;
const ip = (response.body || '').trim();
if (ip && isIp[version](ip)) {
return ip;
}
} catch (error) {
if (error instanceof CancelError) {
throw error;
}
}
}
throw new Error('Couldn\'t find your IP');
} catch (error) {
// Don't throw a cancellation error for consistency with DNS
if (!(error instanceof CancelError)) {
throw error;
}
}
})();
promise.cancel = function () {
return cancel.apply(this);
};
return promise;
};
const queryAll = (version, options) => {
let cancel;
const promise = (async () => {
let response;
const dnsPromise = queryDns(version, options);
cancel = dnsPromise.cancel;
try {
response = await dnsPromise;
} catch (_) {
const httpsPromise = queryHttps(version, options);
cancel = httpsPromise.cancel;
response = await httpsPromise;
}
return response;
})();
promise.cancel = cancel;
return promise;
};
module.exports.v4 = options => {
options = {
...defaults,
...options
};
if (!options.onlyHttps) {
return queryAll('v4', options);
}
if (options.onlyHttps) {
return queryHttps('v4', options);
}
return queryDns('v4', options);
};
module.exports.v6 = options => {
options = {
...defaults,
...options
};
if (!options.onlyHttps) {
return queryAll('v6', options);
}
if (options.onlyHttps) {
return queryHttps('v6', options);
}
return queryDns('v6', options);
};