socket-function 0.23.0 → 0.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/SocketFunctionTypes.ts +1 -0
- package/package.json +1 -1
- package/src/callManager.ts +7 -7
- package/src/forwardPort.ts +6 -2
- package/src/networking.ts +1 -1
- package/src/webSocketServer.ts +5 -3
- package/time/trueTimeShim.ts +8 -3
package/SocketFunctionTypes.ts
CHANGED
|
@@ -38,6 +38,7 @@ export type SocketExposedShape<ExposedType extends SocketExposedInterface = Sock
|
|
|
38
38
|
hooks?: SocketFunctionHook<ExposedType>[];
|
|
39
39
|
clientHooks?: SocketFunctionClientHook<ExposedType>[];
|
|
40
40
|
noDefaultHooks?: boolean;
|
|
41
|
+
/** BUG: I think this is broken if it is on the default hooks function? */
|
|
41
42
|
noClientHooks?: boolean;
|
|
42
43
|
};
|
|
43
44
|
};
|
package/package.json
CHANGED
package/src/callManager.ts
CHANGED
|
@@ -122,13 +122,13 @@ export const runClientHooks = measureWrap(async function runClientHooks(
|
|
|
122
122
|
): Promise<ClientHookContext> {
|
|
123
123
|
let context: ClientHookContext = { call: callType, connectionId };
|
|
124
124
|
|
|
125
|
-
let clientHooks = (
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
125
|
+
let clientHooks = hooks.clientHooks?.slice() || [];
|
|
126
|
+
if (!hooks.noClientHooks) {
|
|
127
|
+
clientHooks = globalClientHooks.concat(clientHooks);
|
|
128
|
+
for (let otherClientHook of globalHooks.concat(hooks.hooks || []).map(x => x.clientHook)) {
|
|
129
|
+
if (otherClientHook) {
|
|
130
|
+
clientHooks.push(otherClientHook);
|
|
131
|
+
}
|
|
132
132
|
}
|
|
133
133
|
}
|
|
134
134
|
for (let hook of clientHooks) {
|
package/src/forwardPort.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import debugbreak from "debugbreak";
|
|
2
2
|
import * as dgram from "dgram";
|
|
3
3
|
import os from "os";
|
|
4
|
+
import { timeInHour } from "./misc";
|
|
4
5
|
|
|
5
6
|
const SSDP_DISCOVER_MX = 2;
|
|
6
7
|
const SSDP_DISCOVER_MSG = `M-SEARCH * HTTP/1.1\r\nHOST: 239.255.255.250:1900\r\nMAN: "ssdp:discover"\r\nMX: ${SSDP_DISCOVER_MX}\r\nST: urn:schemas-upnp-org:device:InternetGatewayDevice:1\r\n\r\n`;
|
|
@@ -8,8 +9,10 @@ const SSDP_DISCOVER_MSG = `M-SEARCH * HTTP/1.1\r\nHOST: 239.255.255.250:1900\r\n
|
|
|
8
9
|
export async function forwardPort(config: {
|
|
9
10
|
externalPort: number;
|
|
10
11
|
internalPort: number;
|
|
12
|
+
duration?: number;
|
|
11
13
|
}) {
|
|
12
14
|
const { externalPort, internalPort } = config;
|
|
15
|
+
let duration = config.duration ?? timeInHour;
|
|
13
16
|
|
|
14
17
|
const localObj = getLocalInterfaceAddress();
|
|
15
18
|
if (!localObj) throw new Error("Could not find the local address / gateway");
|
|
@@ -28,6 +31,7 @@ export async function forwardPort(config: {
|
|
|
28
31
|
controlPort,
|
|
29
32
|
controlPath: controlURL,
|
|
30
33
|
internalIP,
|
|
34
|
+
duration,
|
|
31
35
|
});
|
|
32
36
|
console.log(`Port mapping created on ${gatewayIP}:${externalPort} -> ${internalIP}:${internalPort}`);
|
|
33
37
|
return;
|
|
@@ -160,7 +164,7 @@ async function createPortMapping(config: {
|
|
|
160
164
|
controlPort: number;
|
|
161
165
|
controlPath: string;
|
|
162
166
|
internalIP: string;
|
|
163
|
-
|
|
167
|
+
duration: number;
|
|
164
168
|
}): Promise<void> {
|
|
165
169
|
const { externalPort, internalPort, internalIP, controlPath, controlPort, gatewayIP } = config;
|
|
166
170
|
const action = "\"urn:schemas-upnp-org:service:WANIPConnection:1#AddPortMapping\"";
|
|
@@ -177,7 +181,7 @@ async function createPortMapping(config: {
|
|
|
177
181
|
<NewInternalClient>${internalIP}</NewInternalClient>
|
|
178
182
|
<NewEnabled>1</NewEnabled>
|
|
179
183
|
<NewPortMappingDescription>My Port Mapping</NewPortMappingDescription>
|
|
180
|
-
<NewLeaseDuration
|
|
184
|
+
<NewLeaseDuration>${Math.ceil(config.duration / 1000)}</NewLeaseDuration>
|
|
181
185
|
</u:AddPortMapping>
|
|
182
186
|
</s:Body>
|
|
183
187
|
</s:Envelope>
|
package/src/networking.ts
CHANGED
|
@@ -33,7 +33,7 @@ const ipServers = [
|
|
|
33
33
|
export const getExternalIP = lazy(measureWrap(async function getExternalIP(): Promise<string> {
|
|
34
34
|
for (let server of ipServers) {
|
|
35
35
|
try {
|
|
36
|
-
return (await httpsRequest(server)).toString();
|
|
36
|
+
return (await httpsRequest(server, undefined, undefined, false)).toString();
|
|
37
37
|
} catch (e) {
|
|
38
38
|
console.warn(`Failed to get external ip from ${server}: ${e}`);
|
|
39
39
|
}
|
package/src/webSocketServer.ts
CHANGED
|
@@ -11,7 +11,7 @@ import { parseSNIExtension, parseTLSHello, SNIType } from "./tlsParsing";
|
|
|
11
11
|
import debugbreak from "debugbreak";
|
|
12
12
|
import { getNodeId } from "./nodeCache";
|
|
13
13
|
import crypto from "crypto";
|
|
14
|
-
import { Watchable, timeInHour } from "./misc";
|
|
14
|
+
import { Watchable, timeInHour, timeInMinute } from "./misc";
|
|
15
15
|
import { delay, runInfinitePoll, runInfinitePollCallAtStart } from "./batching";
|
|
16
16
|
import { magenta, red } from "./formatting/logColors";
|
|
17
17
|
import { yellow } from "./formatting/logColors";
|
|
@@ -35,7 +35,8 @@ export type SocketServerConfig = (
|
|
|
35
35
|
/** Tries forwarding ports (using UPnP), if we detect they aren't externally reachable.
|
|
36
36
|
* - This causes an extra request and delay during startup, so should only be used
|
|
37
37
|
* during development.
|
|
38
|
-
* - Ignored if public is false
|
|
38
|
+
* - Ignored if public is false (in which case we mount on 127.0.0.1, so port forwarding
|
|
39
|
+
* wouldn't matter anyways).
|
|
39
40
|
*/
|
|
40
41
|
autoForwardPort?: boolean;
|
|
41
42
|
ip?: string;
|
|
@@ -180,6 +181,7 @@ export async function startSocketServer(
|
|
|
180
181
|
});
|
|
181
182
|
|
|
182
183
|
let realServer = net.createServer(socket => {
|
|
184
|
+
//console.log("Received TCP connection from " + socket.remoteAddress);
|
|
183
185
|
const remoteAddress = socket.remoteAddress;
|
|
184
186
|
function handleTLSHello(buffer: Buffer, packetCount: number): void | "more" {
|
|
185
187
|
// All HTTPS requests start with 22, and no HTTP requests start with 22,
|
|
@@ -296,7 +298,7 @@ export async function startSocketServer(
|
|
|
296
298
|
console.log(magenta(`Forwarded port ${port} to our machine`));
|
|
297
299
|
}
|
|
298
300
|
// Every hour, in case our network configuration changes
|
|
299
|
-
runInfinitePollCallAtStart(
|
|
301
|
+
runInfinitePollCallAtStart(timeInMinute * 30, forward).catch(e => console.error(red(`Error in port forwarding ${e.stack}`)));
|
|
300
302
|
}
|
|
301
303
|
|
|
302
304
|
let nodeId = getNodeId(getCommonName(config.cert), port);
|
package/time/trueTimeShim.ts
CHANGED
|
@@ -200,10 +200,15 @@ const TimeController = SocketFunction.register(
|
|
|
200
200
|
"TimeController-ddf4753e-fc8a-413f-8cc2-b927dd449976",
|
|
201
201
|
new TimeControllerBase(),
|
|
202
202
|
() => ({
|
|
203
|
-
getTrueTime: {
|
|
204
|
-
|
|
205
|
-
|
|
203
|
+
getTrueTime: {
|
|
204
|
+
// No hooks, as this needs to run very early on. Also, it is basically just a ping,
|
|
205
|
+
// so it should be safe for anyone to use (we might even make it just a regular HTTPS endpoint,
|
|
206
|
+
// or even just set up a dedicated domain for this).
|
|
207
|
+
noDefaultHooks: true,
|
|
208
|
+
noClientHooks: true,
|
|
209
|
+
},
|
|
206
210
|
}),
|
|
211
|
+
() => ({}),
|
|
207
212
|
{
|
|
208
213
|
// NOTE: Autoexpose, because our exposed endpoints are incredibly lightweight
|
|
209
214
|
// (just a ping), and don't expose really expose any data.
|