generated from google-gemini/aistudio-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
220 lines (188 loc) · 5.85 KB
/
server.ts
File metadata and controls
220 lines (188 loc) · 5.85 KB
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
import express from "express";
import { createServer as createViteServer } from "vite";
import path from "path";
import net from "net";
import dgram from "dgram";
import tls from "tls";
import https from "https";
async function startServer() {
const app = express();
const PORT = 3000;
app.use(express.json());
// API Routes
app.get("/api/health", (req, res) => {
res.json({ status: "ok", timestamp: new Date().toISOString() });
});
// DoH Proxy Endpoint
app.use("/dns-query", (req, res) => {
const options = {
hostname: 'cloudflare-dns.com',
port: 443,
path: req.originalUrl,
method: req.method,
headers: {
...req.headers,
host: 'cloudflare-dns.com',
}
};
// Remove headers that might cause issues
delete options.headers['connection'];
delete options.headers['content-length'];
const proxyReq = https.request(options, (proxyRes) => {
res.writeHead(proxyRes.statusCode || 500, proxyRes.headers);
proxyRes.pipe(res);
});
proxyReq.on('error', (err) => {
res.status(500).send('DNS Proxy Error');
});
if (req.method === 'POST') {
req.pipe(proxyReq);
} else {
proxyReq.end();
}
});
// Reality Scanner Logic
app.post("/api/scan-reality", async (req, res) => {
const { host, port, sni } = req.body;
const parsedPort = parseInt(port, 10);
if (isNaN(parsedPort) || parsedPort <= 0 || parsedPort >= 65536) {
return res.status(400).json({ status: 'error', message: 'Invalid port number' });
}
const options = {
host,
port: parsedPort,
servername: sni,
rejectUnauthorized: false,
timeout: 5000,
};
const socket = tls.connect(options, () => {
const cert = socket.getPeerCertificate();
socket.end();
res.json({
status: 'success',
host,
sni,
subject: cert.subject,
issuer: cert.issuer,
valid_to: cert.valid_to
});
});
socket.on('timeout', () => {
socket.destroy();
res.json({ status: 'timeout', host, sni });
});
socket.on('error', (err) => {
res.json({ status: 'error', host, sni, message: err.message });
});
});
function checkIp(ip: string, port: number, timeoutMs: number): Promise<number | null> {
return new Promise((resolve) => {
const socket = new net.Socket();
const start = Date.now();
let isResolved = false;
const done = (latency: number | null) => {
if (!isResolved) {
isResolved = true;
socket.destroy();
resolve(latency);
}
};
socket.setTimeout(timeoutMs);
socket.on('connect', () => done(Date.now() - start));
socket.on('timeout', () => done(null));
socket.on('error', () => done(null));
socket.connect(port, ip);
});
}
// CDN IP Scanner Logic
app.post("/api/scan-cdn", async (req, res) => {
const { subnets } = req.body;
if (!Array.isArray(subnets)) {
return res.status(400).json({ error: 'subnets must be an array' });
}
const ipsToTest: string[] = [];
subnets.forEach(subnet => {
const match = subnet.match(/^(\d+\.\d+\.\d+)/);
if (match) {
const base = match[1];
// Pick 5 random IPs from each subnet to keep the scan fast
for(let i = 0; i < 5; i++) {
ipsToTest.push(`${base}.${Math.floor(Math.random() * 253) + 1}`);
}
}
});
// Test all in parallel
const results = await Promise.all(ipsToTest.map(async (ip) => {
const latency = await checkIp(ip, 443, 2000);
return { ip, latency };
}));
const cleanIps = results
.filter(r => r.latency !== null)
.sort((a, b) => (a.latency as number) - (b.latency as number));
res.json({ status: 'success', cleanIps });
});
app.post("/api/test-connectivity", async (req, res) => {
const { host, port, protocol, payload } = req.body;
if (protocol === 'tcp') {
const socket = new net.Socket();
socket.setTimeout(3000);
socket.connect(port, host, () => {
if (payload) socket.write(payload);
socket.end();
res.json({ status: 'connected' });
});
socket.on('timeout', () => {
socket.destroy();
res.json({ status: 'timeout' });
});
socket.on('error', (err) => {
res.json({ status: 'error', message: err.message });
});
} else if (protocol === 'udp') {
const client = dgram.createSocket('udp4');
const message = Buffer.from(payload || '');
client.send(message, port, host, (err) => {
if (err) {
client.close();
res.json({ status: 'error', message: err.message });
} else {
client.close();
res.json({ status: 'sent' });
}
});
} else {
res.status(400).json({ error: 'Invalid protocol' });
}
});
app.post("/api/test-throughput", express.raw({ type: '*/*', limit: '1mb' }), (req, res) => {
const data = req.body;
// Echo back the data
res.send(data);
});
// Mock endpoint for network intelligence
app.get("/api/intel", (req, res) => {
res.json({
dpi_status: "active",
blocked_ports: [443, 80, 2083],
recommended_sni: ["microsoft.com", "apple.com", "cdn.jsdelivr.net"],
spilnet_detected: true
});
});
// Vite middleware for development
if (process.env.NODE_ENV !== "production") {
const vite = await createViteServer({
server: { middlewareMode: true },
appType: "spa",
});
app.use(vite.middlewares);
} else {
app.use(express.static(path.join(process.cwd(), "dist")));
app.get("*", (req, res) => {
res.sendFile(path.join(process.cwd(), "dist", "index.html"));
});
}
app.listen(PORT, "0.0.0.0", () => {
console.log(`[SERVER] Project OpenGate running on http://localhost:${PORT}`);
});
}
startServer();