Skip to content

Commit bdc78dc

Browse files
committed
Ensure we recognize all private relay IPs properly
1 parent 636a23a commit bdc78dc

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/ip-tools.ts

+19
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
export const IPTools = new class {
6+
privateRelayIPs: {minIP: number; maxIP: number}[] = [];
67
// eslint-disable-next-line max-len
78
readonly ipRegex = /^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$/;
89
getCidrRange(cidr: string): {minIP: number; maxIP: number} | null {
@@ -51,6 +52,24 @@ export const IPTools = new class {
5152
if (!range) return false;
5253
return range.minIP <= ip && ip <= range.maxIP;
5354
}
55+
56+
async loadPrivateRelayIPs() {
57+
const seen = new Set<string>();
58+
try {
59+
const res = await (await fetch("https://mask-api.icloud.com/egress-ip-ranges.csv")).text();
60+
for (const line of res.split('\n')) {
61+
const [range] = line.split(',');
62+
const [ip] = range.split('/');
63+
if (this.ipRegex.test(ip) && !seen.has(range)) {
64+
const cidr = this.getCidrRange(range);
65+
if (cidr) {
66+
this.privateRelayIPs.push(cidr);
67+
seen.add(range);
68+
}
69+
}
70+
}
71+
} catch {}
72+
}
5473
};
5574

5675
export default IPTools;

src/server.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,12 @@ export class ActionContext {
211211
}
212212
isTrustedProxy(ip: string) {
213213
// account for shit like ::ffff:127.0.0.1
214-
return ip === '::ffff:127.0.0.1' || Config.trustedproxies.some(f => IPTools.checkPattern(f, ip));
214+
const num = IPTools.ipToNumber(ip) || 0;
215+
return (
216+
ip === '::ffff:127.0.0.1' ||
217+
Config.trustedproxies.some(f => IPTools.checkPattern(f, ip)) ||
218+
IPTools.privateRelayIPs.some(f => f.minIP <= num && num <= f.maxIP)
219+
);
215220
}
216221
_ip = '';
217222
getIp() {
@@ -415,3 +420,5 @@ export class Server {
415420
);
416421
}
417422
}
423+
424+
void IPTools.loadPrivateRelayIPs();

0 commit comments

Comments
 (0)