-
Notifications
You must be signed in to change notification settings - Fork 0
/
upnp.ts
160 lines (144 loc) · 4.27 KB
/
upnp.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
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
// Copyright (C) 2020-2022 Russell Clarey. All rights reserved. MIT license.
import { withTimeout } from "./utils.ts";
const TIMEOUT = 2000;
const te = new TextEncoder();
const td = new TextDecoder();
const clientAddr = {
hostname: "0.0.0.0",
port: 0,
transport: "udp",
} as const;
const serverAddr = {
hostname: "239.255.255.250",
port: 1900,
transport: "udp",
} as const;
const serviceName = "urn:schemas-upnp-org:service:WANIPConnection:1";
const ctrlUrlPattern = new RegExp(
`<serviceType>${serviceName}</serviceType>.*?<controlURL>(?<url>.*?)<\/controlURL>`,
"s",
);
const search = te.encode(
"M-SEARCH * HTTP/1.1\r\n" +
"HOST:239.255.255.250:1900\r\n" +
"ST:urn:schemas-upnp-org:device:InternetGatewayDevice:1\r\n" +
"MX:2\r\n" +
'MAN:"ssdp:discover"\r\n' +
"\r\n",
);
function getGatewayControlUrl(): Promise<URL> {
return withTimeout(async () => {
const conn = Deno.listenDatagram(clientAddr);
await conn.send(search, serverAddr);
const [searchRes, addr] = await conn.receive();
conn.close();
const resStr = td.decode(searchRes);
const locMatch = resStr.match(/location: (?<url>.*)/i);
if (!locMatch?.groups?.url) {
throw new Error(
"UPnP: Failed to extract description URL from gateway response",
);
}
const baseUrl = new URL(locMatch.groups.url);
baseUrl.hostname = (addr as Deno.NetAddr).hostname;
const desc = await (await fetch(baseUrl.toString())).text();
const ctrlMatch = desc.match(ctrlUrlPattern);
if (!ctrlMatch?.groups?.url) {
throw new Error(
"UPnp: Failed to extract control URL from gateway response",
);
}
return new URL(ctrlMatch.groups.url, baseUrl);
}, TIMEOUT);
}
function action(
ctrlUrl: URL,
name: string,
args: Record<string, string | number>,
): Promise<Response> {
return fetch(ctrlUrl, {
method: "POST",
headers: {
"Content-Type": "text/xml",
SOAPAction: `"${serviceName}#${name}"`,
},
body: `<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:${name} xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1">
${
Object.entries(args)
.map(([k, v]) => `<${k}>${v}</${k}>`)
.join("\n")
}
</u:${name}>
</s:Body>
</s:Envelope>`,
});
}
function getInternalIp(ctrlUrl: URL): Promise<string> {
return withTimeout(async () => {
console.log("connect:", ctrlUrl.hostname, ctrlUrl.port);
const conn = await Deno.connect({
hostname: ctrlUrl.hostname,
port: Number(ctrlUrl.port),
});
const internalIp = (conn.localAddr as Deno.NetAddr).hostname;
conn.close();
return internalIp;
}, TIMEOUT);
}
function getExternalIp(ctrlUrl: URL): Promise<string> {
return withTimeout(async () => {
const res = await action(ctrlUrl, "GetExternalIPAddress", {
NewExternalIPAddress: "",
});
if (!res.ok) {
// TODO: parse SOAP error message
throw new Error(await res.text());
}
const match = (await res.text()).match(
/<NewExternalIPAddress>(?<ip>.*?)<\/NewExternalIPAddress>/,
);
if (!match?.groups?.ip) {
throw new Error(
"UPnP: Failed to extract external IP address from gateway response",
);
}
return match.groups.ip;
}, TIMEOUT);
}
function addPortMapping(
ctrlUrl: URL,
internalIp: string,
port: number,
): Promise<void> {
return withTimeout(async () => {
const res = await action(ctrlUrl, "AddPortMapping", {
NewRemoteHost: "",
NewExternalPort: port,
NewProtocol: "TCP",
NewInternalPort: port,
NewInternalClient: internalIp,
NewEnabled: "True",
NewPortMappingDescription: "via upnp.ts",
// 30min
NewLeaseDuration: 60,
});
if (!res.ok) {
// TODO: parse SOAP error message
throw new Error(await res.text());
}
}, TIMEOUT);
}
export async function getIpAddrsAndMapPort(
port: number,
): Promise<[string, string]> {
const ctrlUrl = await getGatewayControlUrl();
const internalIp = await getInternalIp(ctrlUrl);
const [externalIp] = await Promise.all([
getExternalIp(ctrlUrl),
addPortMapping(ctrlUrl, internalIp, port),
]);
return [internalIp, externalIp];
}