-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
executable file
·103 lines (101 loc) · 2.87 KB
/
main.ts
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
#!/usr/bin/env -S deno serve --allow-import=jsr.io:443 --allow-net
import { serverListPing } from "./mod.ts";
const defaultTimeout = 10000;
const maxTimeout = 120000;
const handler = async (req: Request) => {
if (req.method === "OPTIONS") {
return new Response(null, {
headers: [
["access-control-allow-methods", "*"],
["access-control-allow-headers", "*"],
["access-control-max-age", "86400"],
],
});
}
if (!(req.method === "GET" || req.method === "HEAD")) {
return new Response(null, {
status: 405,
headers: [
["allow", "GET, HEAD"],
],
});
}
const url = new URL(req.url);
if (url.pathname === "/") {
return new Response(`Usage: ${url.origin}/:address`);
}
if (url.pathname === "/favicon.ico") {
return new Response(null, { status: 404 });
}
const parseErrors: string[] = [];
let protocol: number | undefined;
parseProtocol: {
const protocolParam = url.searchParams.get("protocol");
if (protocolParam === null) {
break parseProtocol;
}
if (!/^-?\d+$/.test(protocolParam)) {
parseErrors.push("Protocol version must be an integer");
break parseProtocol;
}
protocol = Number(protocolParam);
if ((protocol | 0) !== protocol) {
parseErrors.push("Protocol version must fit in 32 bits");
}
}
let timeout = defaultTimeout;
parseTimeout: {
const timeoutParam = url.searchParams.get("timeout");
if (timeoutParam === null) {
break parseTimeout;
}
if (!/^\d+$/.test(timeoutParam)) {
parseErrors.push("Timeout must be a non-negative integer");
break parseTimeout;
}
timeout = Number(timeoutParam);
if (timeout > maxTimeout) {
parseErrors.push(`Timeout must be at most ${maxTimeout} ms`);
}
}
const parser = new URL("dummy://");
try {
const addr = decodeURIComponent(url.pathname.substring(1));
if (!/[/\\]/.test(addr)) {
parser.host = addr;
}
} catch {
// handled below
}
if (!parser.hostname) {
parseErrors.push("Invalid address");
}
if (parseErrors.length !== 0) {
return new Response(parseErrors.join("\n"), { status: 400 });
}
try {
const json = await serverListPing({
hostname: parser.hostname,
port: parser.port ? parseInt(parser.port, 10) : undefined,
protocol,
signal: AbortSignal.timeout(timeout),
});
return new Response(json, {
headers: [
["content-type", "application/json"],
],
});
} catch (e) {
if (e instanceof DOMException && e.name === "TimeoutError") {
return new Response("Request timed out", { status: 504 });
}
return new Response(`Request failed: ${e}`, { status: 502 });
}
};
export default {
async fetch(req) {
const res = await handler(req);
res.headers.append("access-control-allow-origin", "*");
return res;
},
} satisfies Deno.ServeDefaultExport;