camoucore 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/geolocation.d.ts +3 -2
- package/dist/geolocation.js +8 -9
- package/dist/ip.d.ts +14 -0
- package/dist/ip.js +96 -26
- package/package.json +1 -1
package/dist/geolocation.d.ts
CHANGED
|
@@ -46,8 +46,9 @@ export declare function warnIfDeprecated(config: GeoIPRepo): void;
|
|
|
46
46
|
/**
|
|
47
47
|
* Load the active GeoIP config from disk, falling back to the geoip.yml default.
|
|
48
48
|
*
|
|
49
|
-
* A saved
|
|
50
|
-
* cache written before the default changed)
|
|
49
|
+
* A saved source the user did not pick explicitly resolves to the default
|
|
50
|
+
* when it is deprecated (every cache written before the default changed) or
|
|
51
|
+
* no longer in geoip.yml. One picked explicitly and gone throws.
|
|
51
52
|
*/
|
|
52
53
|
export declare function loadGeoipConfig(): GeoIPRepo;
|
|
53
54
|
/**
|
package/dist/geolocation.js
CHANGED
|
@@ -119,19 +119,18 @@ export function warnIfDeprecated(config) {
|
|
|
119
119
|
/**
|
|
120
120
|
* Load the active GeoIP config from disk, falling back to the geoip.yml default.
|
|
121
121
|
*
|
|
122
|
-
* A saved
|
|
123
|
-
* cache written before the default changed)
|
|
122
|
+
* A saved source the user did not pick explicitly resolves to the default
|
|
123
|
+
* when it is deprecated (every cache written before the default changed) or
|
|
124
|
+
* no longer in geoip.yml. One picked explicitly and gone throws.
|
|
124
125
|
*/
|
|
125
126
|
export function loadGeoipConfig() {
|
|
126
127
|
if (fs.existsSync(GEOIP_CONFIG)) {
|
|
127
128
|
const saved = parseYaml(fs.readFileSync(GEOIP_CONFIG, "utf-8")) ?? {};
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
return saved;
|
|
134
|
-
}
|
|
129
|
+
const known = loadGeoipRepos()[0].map((r) => (r.name ?? "").toLowerCase());
|
|
130
|
+
const name = saved.explicit || known.includes(String(saved.name ?? "").toLowerCase())
|
|
131
|
+
? saved.name
|
|
132
|
+
: undefined;
|
|
133
|
+
const config = getGeoipConfigByName(name);
|
|
135
134
|
if (config.deprecated && !saved.explicit) {
|
|
136
135
|
return getGeoipConfigByName(undefined);
|
|
137
136
|
}
|
package/dist/ip.d.ts
CHANGED
|
@@ -18,6 +18,20 @@ export declare function validIPv6(ip: string | false): boolean;
|
|
|
18
18
|
export declare function validateIP(ip: string): void;
|
|
19
19
|
export declare const DIRECT_IP_TIMEOUT_MS = 5000;
|
|
20
20
|
export declare const PROXIED_IP_TIMEOUT_MS = 15000;
|
|
21
|
+
/** Where a proxy exits: its IP and that IP's timezone, country and position. */
|
|
22
|
+
export interface ExitGeo {
|
|
23
|
+
ip: string;
|
|
24
|
+
timezone: string;
|
|
25
|
+
country: string;
|
|
26
|
+
latitude: number;
|
|
27
|
+
longitude: number;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Where `proxy` exits, looked up through the proxy. Throws InvalidIP when the
|
|
31
|
+
* lookup fails: a context that silently kept the host's WebRTC IP and timezone
|
|
32
|
+
* behind a proxy would be a leak.
|
|
33
|
+
*/
|
|
34
|
+
export declare function proxyExitGeo(proxy: string): Promise<ExitGeo>;
|
|
21
35
|
/** Sends a request to a public IP api. */
|
|
22
36
|
export declare function publicIP(proxy?: string): Promise<string>;
|
|
23
37
|
/**
|
package/dist/ip.js
CHANGED
|
@@ -71,21 +71,30 @@ function once(timeoutMs, what, run) {
|
|
|
71
71
|
});
|
|
72
72
|
});
|
|
73
73
|
}
|
|
74
|
+
/** Where an HTTP(S) proxy listens. */
|
|
75
|
+
function proxyEndpoint(proxy) {
|
|
76
|
+
return {
|
|
77
|
+
host: proxy.hostname.replace(/^\[(.*)\]$/, "$1"),
|
|
78
|
+
port: proxy.port || (proxy.protocol === "https:" ? 443 : 80),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/** Basic auth for an HTTP(S) proxy, when its URL carries credentials. */
|
|
82
|
+
function proxyAuthorization(proxy) {
|
|
83
|
+
if (!proxy.username)
|
|
84
|
+
return {};
|
|
85
|
+
const auth = `${decodeURIComponent(proxy.username)}:${decodeURIComponent(proxy.password)}`;
|
|
86
|
+
return {
|
|
87
|
+
"Proxy-Authorization": `Basic ${Buffer.from(auth).toString("base64")}`,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
74
90
|
/** A TCP stream to host:port through an HTTP(S) proxy's CONNECT. */
|
|
75
91
|
function connectTunnel(proxy, host, port, timeoutMs) {
|
|
76
92
|
return once(timeoutMs, "proxy CONNECT", (resolve, reject) => {
|
|
77
|
-
const headers = { Host: `${host}:${port}` };
|
|
78
|
-
if (proxy.username) {
|
|
79
|
-
const auth = `${decodeURIComponent(proxy.username)}:${decodeURIComponent(proxy.password)}`;
|
|
80
|
-
headers["Proxy-Authorization"] =
|
|
81
|
-
`Basic ${Buffer.from(auth).toString("base64")}`;
|
|
82
|
-
}
|
|
83
93
|
const request = (proxy.protocol === "https:" ? https : http).request({
|
|
84
|
-
|
|
85
|
-
port: proxy.port || (proxy.protocol === "https:" ? 443 : 80),
|
|
94
|
+
...proxyEndpoint(proxy),
|
|
86
95
|
method: "CONNECT",
|
|
87
96
|
path: `${host}:${port}`,
|
|
88
|
-
headers,
|
|
97
|
+
headers: { Host: `${host}:${port}`, ...proxyAuthorization(proxy) },
|
|
89
98
|
});
|
|
90
99
|
request.on("connect", (response, socket) => {
|
|
91
100
|
if (response.statusCode === 200)
|
|
@@ -185,14 +194,15 @@ function connectSocks5(proxy, host, port, timeoutMs) {
|
|
|
185
194
|
socket.on("data", onData);
|
|
186
195
|
});
|
|
187
196
|
}
|
|
188
|
-
function
|
|
189
|
-
let url;
|
|
197
|
+
function parseProxy(proxy) {
|
|
190
198
|
try {
|
|
191
|
-
|
|
199
|
+
return new URL(proxy);
|
|
192
200
|
}
|
|
193
201
|
catch (error) {
|
|
194
202
|
throw new InvalidProxy(`Invalid proxy server: ${proxy}`, { cause: error });
|
|
195
203
|
}
|
|
204
|
+
}
|
|
205
|
+
function proxySocket(url, host, port, timeoutMs) {
|
|
196
206
|
switch (url.protocol) {
|
|
197
207
|
case "http:":
|
|
198
208
|
case "https:":
|
|
@@ -205,22 +215,47 @@ function proxySocket(proxy, host, port, timeoutMs) {
|
|
|
205
215
|
"Supported: http, https, socks5.");
|
|
206
216
|
}
|
|
207
217
|
}
|
|
208
|
-
/**
|
|
209
|
-
|
|
218
|
+
/**
|
|
219
|
+
* GET a URL's body, directly or through `proxy`, as requests.get() sends it:
|
|
220
|
+
* https is tunnelled and its certificate verified; plain http goes to an
|
|
221
|
+
* HTTP(S) proxy in absolute form, and through a SOCKS5 tunnel otherwise.
|
|
222
|
+
*/
|
|
223
|
+
async function getText(url, proxy) {
|
|
210
224
|
const timeoutMs = proxy ? PROXIED_IP_TIMEOUT_MS : DIRECT_IP_TIMEOUT_MS;
|
|
211
225
|
const target = new URL(url);
|
|
212
|
-
const
|
|
226
|
+
const secure = target.protocol === "https:";
|
|
227
|
+
const options = {
|
|
228
|
+
agent: false,
|
|
229
|
+
headers: { Connection: "close" },
|
|
230
|
+
};
|
|
231
|
+
let client = secure ? https : http;
|
|
213
232
|
let socket;
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
233
|
+
const proxyUrl = proxy ? parseProxy(proxy) : undefined;
|
|
234
|
+
if (proxyUrl && !secure && proxyUrl.protocol.startsWith("http")) {
|
|
235
|
+
client = proxyUrl.protocol === "https:" ? https : http;
|
|
236
|
+
Object.assign(options, proxyEndpoint(proxyUrl), {
|
|
237
|
+
path: url,
|
|
238
|
+
headers: {
|
|
239
|
+
...options.headers,
|
|
240
|
+
Host: target.host,
|
|
241
|
+
...proxyAuthorization(proxyUrl),
|
|
242
|
+
},
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
else if (proxyUrl) {
|
|
246
|
+
const port = Number(target.port || (secure ? 443 : 80));
|
|
247
|
+
const raw = await proxySocket(proxyUrl, target.hostname, port, timeoutMs);
|
|
248
|
+
const stream = secure
|
|
249
|
+
? tls.connect({ socket: raw, servername: target.hostname })
|
|
250
|
+
: raw;
|
|
251
|
+
socket = stream;
|
|
252
|
+
// Node ignores createConnection when an agent is given, `false`
|
|
253
|
+
// included, and would connect to the target itself, around the proxy.
|
|
254
|
+
options.agent = undefined;
|
|
255
|
+
options.createConnection = () => stream;
|
|
217
256
|
}
|
|
218
257
|
return once(timeoutMs, `GET ${url}`, (resolve, reject) => {
|
|
219
|
-
const
|
|
220
|
-
agent: false,
|
|
221
|
-
headers: { Connection: "close" },
|
|
222
|
-
...(socket ? { createConnection: () => socket } : {}),
|
|
223
|
-
}, (response) => {
|
|
258
|
+
const onResponse = (response) => {
|
|
224
259
|
const chunks = [];
|
|
225
260
|
response.on("data", (chunk) => chunks.push(chunk));
|
|
226
261
|
response.on("error", reject);
|
|
@@ -233,10 +268,45 @@ async function httpsGetText(url, proxy) {
|
|
|
233
268
|
else
|
|
234
269
|
resolve(Buffer.concat(chunks).toString("utf8"));
|
|
235
270
|
});
|
|
236
|
-
}
|
|
271
|
+
};
|
|
272
|
+
const request = options.path
|
|
273
|
+
? client.get(options, onResponse)
|
|
274
|
+
: client.get(url, options, onResponse);
|
|
237
275
|
request.on("error", reject);
|
|
238
276
|
}).finally(() => socket?.destroy());
|
|
239
277
|
}
|
|
278
|
+
const PROXY_LOOKUP_FAILED = "Could not look up where the proxy exits. Pass webrtc_ip, timezoneId, " +
|
|
279
|
+
"locale and geolocation explicitly to skip the lookup";
|
|
280
|
+
/**
|
|
281
|
+
* Where `proxy` exits, looked up through the proxy. Throws InvalidIP when the
|
|
282
|
+
* lookup fails: a context that silently kept the host's WebRTC IP and timezone
|
|
283
|
+
* behind a proxy would be a leak.
|
|
284
|
+
*/
|
|
285
|
+
export async function proxyExitGeo(proxy) {
|
|
286
|
+
let data;
|
|
287
|
+
try {
|
|
288
|
+
data = JSON.parse(await getText("http://ip-api.com/json?fields=status,message,query,timezone,countryCode,lat,lon", proxy));
|
|
289
|
+
}
|
|
290
|
+
catch (error) {
|
|
291
|
+
throw new InvalidIP(`${PROXY_LOOKUP_FAILED}: ${error}`, { cause: error });
|
|
292
|
+
}
|
|
293
|
+
if (data.status !== "success" ||
|
|
294
|
+
!data.query ||
|
|
295
|
+
!data.timezone ||
|
|
296
|
+
!data.countryCode ||
|
|
297
|
+
data.lat == null ||
|
|
298
|
+
data.lon == null) {
|
|
299
|
+
throw new InvalidIP(`${PROXY_LOOKUP_FAILED}: ${data.message ?? JSON.stringify(data)}`);
|
|
300
|
+
}
|
|
301
|
+
validateIP(data.query);
|
|
302
|
+
return {
|
|
303
|
+
ip: data.query,
|
|
304
|
+
timezone: data.timezone,
|
|
305
|
+
country: data.countryCode,
|
|
306
|
+
latitude: data.lat,
|
|
307
|
+
longitude: data.lon,
|
|
308
|
+
};
|
|
309
|
+
}
|
|
240
310
|
const PUBLIC_IP_URLS = [
|
|
241
311
|
// Prefers IPv4
|
|
242
312
|
"https://api.ipify.org",
|
|
@@ -273,7 +343,7 @@ export function publicIPv6() {
|
|
|
273
343
|
publicIPv6Cache ??= (async () => {
|
|
274
344
|
for (const url of ["https://api6.ipify.org", "https://v6.ident.me"]) {
|
|
275
345
|
try {
|
|
276
|
-
const ip = (await
|
|
346
|
+
const ip = (await getText(url)).trim();
|
|
277
347
|
if (validIPv6(ip))
|
|
278
348
|
return ip;
|
|
279
349
|
}
|
|
@@ -287,7 +357,7 @@ async function resolvePublicIP(proxy) {
|
|
|
287
357
|
let endException;
|
|
288
358
|
for (const url of PUBLIC_IP_URLS) {
|
|
289
359
|
try {
|
|
290
|
-
const ip = (await
|
|
360
|
+
const ip = (await getText(url, proxy)).trim();
|
|
291
361
|
validateIP(ip);
|
|
292
362
|
return ip;
|
|
293
363
|
}
|