socket-function 0.24.0 → 0.26.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/package.json +1 -1
- package/src/forwardPort.ts +61 -50
package/package.json
CHANGED
package/src/forwardPort.ts
CHANGED
|
@@ -11,35 +11,39 @@ export async function forwardPort(config: {
|
|
|
11
11
|
internalPort: number;
|
|
12
12
|
duration?: number;
|
|
13
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
|
-
|
|
14
|
+
try {
|
|
15
|
+
const { externalPort, internalPort } = config;
|
|
16
|
+
let duration = config.duration ?? timeInHour;
|
|
17
|
+
|
|
18
|
+
const localObj = getLocalInterfaceAddress();
|
|
19
|
+
if (!localObj) throw new Error("Could not find the local address / gateway");
|
|
20
|
+
|
|
21
|
+
const { internalIP, gatewayIP } = localObj;
|
|
22
|
+
console.log(`Local IP: ${internalIP}, Gateway IP: ${gatewayIP}`);
|
|
23
|
+
let gateway = await discoverGateway(internalIP);
|
|
24
|
+
let controlURLs = await getControlPaths(gateway);
|
|
25
|
+
let controlPort = Number(new URL(gateway).port);
|
|
26
|
+
|
|
27
|
+
for (let controlURL of controlURLs) {
|
|
28
|
+
try {
|
|
29
|
+
await createPortMapping({
|
|
30
|
+
externalPort, internalPort,
|
|
31
|
+
gatewayIP,
|
|
32
|
+
controlPort,
|
|
33
|
+
controlPath: controlURL,
|
|
34
|
+
internalIP,
|
|
35
|
+
duration,
|
|
36
|
+
});
|
|
37
|
+
console.log(`Port mapping created on ${gatewayIP}:${externalPort} -> ${internalIP}:${internalPort}`);
|
|
38
|
+
return;
|
|
39
|
+
} catch (e) {
|
|
40
|
+
console.error(`Failed to create port mapping using controlURL ${controlURL}`, e);
|
|
41
|
+
}
|
|
40
42
|
}
|
|
43
|
+
console.error("Failed to create port mapping, could not find controlURL");
|
|
44
|
+
} catch (e) {
|
|
45
|
+
console.error("Error in forwardPort", e);
|
|
41
46
|
}
|
|
42
|
-
console.error("Failed to create port mapping, could not find controlURL");
|
|
43
47
|
}
|
|
44
48
|
|
|
45
49
|
function getLocalInterfaceAddress(): { internalIP: string; gatewayIP: string; } | undefined {
|
|
@@ -65,34 +69,41 @@ function getLocalInterfaceAddress(): { internalIP: string; gatewayIP: string; }
|
|
|
65
69
|
}
|
|
66
70
|
}
|
|
67
71
|
} else {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
+
let gatewayMatch: RegExpMatchArray | undefined;
|
|
73
|
+
try {
|
|
74
|
+
// Attempt to get the gateway using "ip route" command (more universal)
|
|
75
|
+
const routeOutput = require("child_process").execSync("ip route show default").toString();
|
|
76
|
+
gatewayMatch = routeOutput.match(/default via (\d+\.\d+\.\d+\.\d+)/);
|
|
77
|
+
} catch (err) {
|
|
78
|
+
console.error("Failed to execute 'ip route show default', trying fallback", err);
|
|
79
|
+
}
|
|
72
80
|
|
|
73
81
|
if (!gatewayMatch) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
82
|
+
try {
|
|
83
|
+
// Fallback to "netstat -rn" for older systems
|
|
84
|
+
const netstatOutput = require("child_process").execSync("netstat -rn").toString();
|
|
85
|
+
gatewayMatch = netstatOutput.match(/default\s+(\d+\.\d+\.\d+\.\d+)/);
|
|
86
|
+
} catch (err) {
|
|
87
|
+
console.error("Failed to execute 'netstat -rn', unable to find gateway", err);
|
|
88
|
+
}
|
|
77
89
|
}
|
|
78
90
|
|
|
79
|
-
if (gatewayMatch
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
let ipMatch;
|
|
85
|
-
if (os.platform() === "darwin") {
|
|
86
|
-
ipMatch = ipOutput.match(/inet ((?!127\.0\.0\.1)\d+\.\d+\.\d+\.\d+)/);
|
|
87
|
-
} else {
|
|
88
|
-
ipMatch = ipOutput.match(/inet ((?!127\.0\.0\.1)\d+\.\d+\.\d+\.\d+)\/\d+/);
|
|
89
|
-
}
|
|
91
|
+
if (gatewayMatch) {
|
|
92
|
+
try {
|
|
93
|
+
// Use "ip addr" to get internal IP (more universal)
|
|
94
|
+
const ipOutput = require("child_process").execSync("ip addr").toString();
|
|
95
|
+
const ipMatch = ipOutput.match(/inet (?!127\.0\.0\.1)(\d+\.\d+\.\d+\.\d+)\//);
|
|
90
96
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
97
|
+
if (ipMatch) {
|
|
98
|
+
return {
|
|
99
|
+
internalIP: ipMatch[1],
|
|
100
|
+
gatewayIP: gatewayMatch[1]
|
|
101
|
+
};
|
|
102
|
+
} else {
|
|
103
|
+
console.error("Failed to match internal IP");
|
|
104
|
+
}
|
|
105
|
+
} catch (err) {
|
|
106
|
+
console.error("Failed to execute 'ip addr'", err);
|
|
96
107
|
}
|
|
97
108
|
}
|
|
98
109
|
}
|