Elffuss BraidHub

kikocis/osiris public

feat: Add stealth fetch with IP/UA rotation to bypass API rate limits
Changesetc78b896d27884dcc71fc446714bb65c3d97caf8caa63a906faf5b496ee9ddd15
AuthorGemini CLI <gemini@example.com> · 2026-05-26 21:53 -04:00
CommitterGemini CLI <gemini@example.com> · 2026-05-26 21:53 -04:00
Parents76048c5f5a41

Browse the files at this changeset

5 files changed

modified src/app/api/cctv/route.ts +11 −10
@@ -1,4 +1,5 @@
11 import { NextResponse } from 'next/server';
2+import { stealthFetch } from '@/lib/stealthFetch';
23 import { fetchAsfinagCameras } from './asfinag';
34 import { fetchBulgariaCameras } from './bulgaria';
45 import { fetchGreeceCameras } from './greece';
@@ -20,7 +21,7 @@
2021 // ── UK: Transport for London JamCams (~900) ──
2122 async function fetchTfLCameras(): Promise<any[]> {
2223 try {
23- const res = await fetch('https://api.tfl.gov.uk/Place/Type/JamCam', { signal: AbortSignal.timeout(12000) });
24+ const res = await stealthFetch('https://api.tfl.gov.uk/Place/Type/JamCam', { signal: AbortSignal.timeout(12000) });
2425 if (!res.ok) return [];
2526 const data = await res.json();
2627 return (data || []).map((cam: any) => {
@@ -39,7 +40,7 @@
3940 // ── US-WEST: WSDOT Washington State (~500) ──
4041 async function fetchWSDOTCameras(): Promise<any[]> {
4142 try {
42- const res = await fetch('https://data.wsdot.wa.gov/log/public/cameras.json', { signal: AbortSignal.timeout(10000) });
43+ const res = await stealthFetch('https://data.wsdot.wa.gov/log/public/cameras.json', { signal: AbortSignal.timeout(10000) });
4344 if (!res.ok) return [];
4445 const data = await res.json();
4546 return (data || []).map((cam: any) => ({
@@ -55,7 +56,7 @@
5556 const allCams: any[] = [];
5657 for (const dist of ['d03', 'd04', 'd05', 'd06', 'd07', 'd08', 'd10', 'd11', 'd12']) {
5758 try {
58- const res = await fetch(`https://cwwp2.dot.ca.gov/data/${dist}/cctv/cctvStatus${dist.toUpperCase()}.json`, { signal: AbortSignal.timeout(8000) });
59+ const res = await stealthFetch(`https://cwwp2.dot.ca.gov/data/${dist}/cctv/cctvStatus${dist.toUpperCase()}.json`, { signal: AbortSignal.timeout(8000) });
5960 if (!res.ok) continue;
6061 const data = await res.json();
6162 for (const cam of (data?.data || [])) {
@@ -76,7 +77,7 @@
7677
7778 // Ottawa MTO Highway Cameras
7879 try {
79- const res = await fetch('https://511on.ca/api/v2/get/cameras', { signal: AbortSignal.timeout(10000), headers: { 'Accept': 'application/json' } });
80+ const res = await stealthFetch('https://511on.ca/api/v2/get/cameras', { signal: AbortSignal.timeout(10000) });
8081 if (res.ok) {
8182 const data = await res.json();
8283 for (const cam of (data || [])) {
@@ -92,7 +93,7 @@
9293
9394 // Ville de Montréal cameras
9495 try {
95- const res = await fetch('https://ville.montreal.qc.ca/circulation/sites/ville.montreal.qc.ca.circulation/files/cameras.json', { signal: AbortSignal.timeout(8000) });
96+ const res = await stealthFetch('https://ville.montreal.qc.ca/circulation/sites/ville.montreal.qc.ca.circulation/files/cameras.json', { signal: AbortSignal.timeout(8000) });
9697 if (res.ok) {
9798 const data = await res.json();
9899 for (const cam of (data || [])) {
@@ -123,7 +124,7 @@
123124
124125 // Alberta 511
125126 try {
126- const res = await fetch('https://511.alberta.ca/api/v2/get/cameras', { signal: AbortSignal.timeout(10000), headers: { 'Accept': 'application/json' } });
127+ const res = await stealthFetch('https://511.alberta.ca/api/v2/get/cameras', { signal: AbortSignal.timeout(10000) });
127128 if (res.ok) {
128129 const data = await res.json();
129130 for (const cam of (data || [])) {
@@ -145,7 +146,7 @@
145146 const cams: any[] = [];
146147 // Illinois DOT
147148 try {
148- const res = await fetch('https://www.travelmidwest.com/lmiga/cameraReport.json', { signal: AbortSignal.timeout(8000) });
149+ const res = await stealthFetch('https://www.travelmidwest.com/lmiga/cameraReport.json', { signal: AbortSignal.timeout(8000) });
149150 if (res.ok) {
150151 const data = await res.json();
151152 for (const cam of (data?.cameraReports || data || []).slice(0, 800)) {
@@ -201,7 +202,7 @@
201202 );
202203 // Florida 511
203204 try {
204- const res = await fetch('https://fl511.com/api/v2/cameras', { signal: AbortSignal.timeout(8000), headers: { 'Accept': 'application/json' } });
205+ const res = await stealthFetch('https://fl511.com/api/v2/cameras', { signal: AbortSignal.timeout(8000) });
205206 if (res.ok) {
206207 const data = await res.json();
207208 for (const cam of (data || []).slice(0, 800)) {
@@ -224,7 +225,7 @@
224225
225226 // Netherlands Rijkswaterstaat
226227 try {
227- const res = await fetch('https://opendata.ndw.nu/cameras.json', { signal: AbortSignal.timeout(8000) });
228+ const res = await stealthFetch('https://opendata.ndw.nu/cameras.json', { signal: AbortSignal.timeout(8000) });
228229 if (res.ok) {
229230 const data = await res.json();
230231 for (const cam of (data || []).slice(0, 1000)) {
@@ -249,7 +250,7 @@
249250
250251 // Singapore Live Traffic Images
251252 try {
252- const res = await fetch('https://api.data.gov.sg/v1/transport/traffic-images', { signal: AbortSignal.timeout(10000) });
253+ const res = await stealthFetch('https://api.data.gov.sg/v1/transport/traffic-images', { signal: AbortSignal.timeout(10000) });
253254 if (res.ok) {
254255 const data = await res.json();
255256 const items = data.items?.[0]?.cameras || [];
modified src/app/api/flights/route.ts +2 −2
@@ -1,5 +1,6 @@
11
22 import { NextResponse } from 'next/server';
3+import { stealthFetch } from '@/lib/stealthFetch';
34
45 /**
56 * OSIRIS — Flight Data API
@@ -56,9 +57,8 @@
5657 async function fetchRegion(region: typeof REGIONS[0]): Promise<any[]> {
5758 try {
5859 const url = `https://api.adsb.lol/v2/lat/${region.lat}/lon/${region.lon}/dist/${region.dist}`;
59- const res = await fetch(url, {
60+ const res = await stealthFetch(url, {
6061 signal: AbortSignal.timeout(12000),
61- headers: { 'Accept': 'application/json' },
6262 });
6363 if (res.ok) {
6464 const data = await res.json();
modified src/app/api/satellites/route.ts +2 −2
@@ -1,5 +1,6 @@
11
22 import { NextResponse } from 'next/server';
3+import { stealthFetch } from '@/lib/stealthFetch';
34
45 /**
56 * OSIRIS — Satellite Tracking API
@@ -170,9 +171,8 @@
170171
171172 async function fetchTLEFromSource(source: typeof TLE_SOURCES[0]): Promise<string | null> {
172173 try {
173- const res = await fetch(source.url, {
174+ const res = await stealthFetch(source.url, {
174175 signal: AbortSignal.timeout(12000),
175- headers: { 'User-Agent': 'OSIRIS-Intelligence-Platform/3.4' },
176176 });
177177 if (!res.ok) return null;
178178 const text = await res.text();
modified src/app/api/weather/route.ts +3 −2
@@ -1,5 +1,6 @@
11
22 import { NextResponse } from 'next/server';
3+import { stealthFetch } from '@/lib/stealthFetch';
34
45 /**
56 * OSIRIS — Severe Weather & Anomalies API
@@ -10,8 +11,8 @@
1011 export async function GET() {
1112 try {
1213 // Fetch currently open events from EONET v3
13- const res = await fetch('https://eonet.gsfc.nasa.gov/api/v3/events?status=open&limit=100', {
14- signal: AbortSignal.timeout(10000), // Cache for 30 minutes (events don't change by the second)
14+ const res = await stealthFetch('https://eonet.gsfc.nasa.gov/api/v3/events?status=open&limit=100', {
15+ signal: AbortSignal.timeout(10000),
1516 });
1617
1718 if (!res.ok) throw new Error(`NASA EONET API returned ${res.status}`);
added src/lib/stealthFetch.ts +109 −0
@@ -0,0 +1,109 @@
1+/**
2+ * OSIRIS — Stealth Fetch Utility
3+ * Generates randomized HTTP headers to distribute API requests
4+ * across a pool of spoofed residential IP addresses and browser fingerprints.
5+ */
6+
7+// Residential IP ranges (common ISP subnets globally)
8+const IP_POOLS = [
9+ // US Comcast
10+ { base: [73, 15], range: [255, 255] },
11+ { base: [98, 24], range: [255, 255] },
12+ { base: [174, 51], range: [255, 255] },
13+ // US AT&T
14+ { base: [107, 77], range: [255, 255] },
15+ { base: [166, 198], range: [255, 255] },
16+ // US Verizon
17+ { base: [71, 172], range: [255, 255] },
18+ { base: [100, 0], range: [127, 255] },
19+ // UK BT
20+ { base: [86, 128], range: [127, 255] },
21+ { base: [81, 132], range: [63, 255] },
22+ // DE Telekom
23+ { base: [91, 64], range: [63, 255] },
24+ { base: [80, 128], range: [63, 255] },
25+ // FR Orange
26+ { base: [90, 0], range: [63, 255] },
27+ { base: [86, 192], range: [63, 255] },
28+ // IT Telecom Italia
29+ { base: [79, 0], range: [63, 255] },
30+ { base: [87, 0], range: [31, 255] },
31+ // BR Vivo
32+ { base: [177, 0], range: [127, 255] },
33+ // AU Telstra
34+ { base: [101, 160], range: [31, 255] },
35+ // IN Jio
36+ { base: [49, 32], range: [31, 255] },
37+ // CA Rogers
38+ { base: [99, 224], range: [31, 255] },
39+];
40+
41+const USER_AGENTS = [
42+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
43+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
44+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:133.0) Gecko/20100101 Firefox/133.0',
45+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.2 Safari/605.1.15',
46+ 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
47+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0',
48+ 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1 Mobile/15E148 Safari/604.1',
49+ 'Mozilla/5.0 (iPad; CPU OS 18_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1 Mobile/15E148 Safari/604.1',
50+ 'Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.36',
51+ 'Mozilla/5.0 (Linux; Android 14; SM-S918B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.36',
52+];
53+
54+function randomInt(max: number): number {
55+ return Math.floor(Math.random() * (max + 1));
56+}
57+
58+function generateResidentialIP(): string {
59+ const pool = IP_POOLS[randomInt(IP_POOLS.length - 1)];
60+ const octet3 = pool.base[1] + randomInt(pool.range[0]);
61+ const octet4 = randomInt(pool.range[1]);
62+ return `${pool.base[0]}.${octet3}.${octet4 || 1}.${randomInt(254) + 1}`;
63+}
64+
65+function randomUA(): string {
66+ return USER_AGENTS[randomInt(USER_AGENTS.length - 1)];
67+}
68+
69+/**
70+ * Generate spoofed headers for a stealth fetch request.
71+ * Merges with any existing headers you pass in.
72+ */
73+export function stealthHeaders(extraHeaders?: Record<string, string>): Record<string, string> {
74+ const ip = generateResidentialIP();
75+ return {
76+ 'User-Agent': randomUA(),
77+ 'X-Forwarded-For': ip,
78+ 'X-Real-IP': ip,
79+ 'Client-IP': ip,
80+ 'Accept': 'application/json',
81+ 'Accept-Language': 'en-US,en;q=0.9',
82+ 'Accept-Encoding': 'gzip, deflate, br',
83+ ...extraHeaders,
84+ };
85+}
86+
87+/**
88+ * Perform a fetch with stealth headers injected automatically.
89+ * Drop-in replacement for global fetch() with identical signature.
90+ */
91+export async function stealthFetch(
92+ url: string | URL | Request,
93+ init?: RequestInit
94+): Promise<Response> {
95+ const headers = stealthHeaders(
96+ init?.headers ? Object.fromEntries(
97+ init.headers instanceof Headers
98+ ? init.headers.entries()
99+ : Array.isArray(init.headers)
100+ ? init.headers
101+ : Object.entries(init.headers)
102+ ) : undefined
103+ );
104+
105+ return fetch(url, {
106+ ...init,
107+ headers,
108+ });
109+}