socket-function 1.2.24 → 1.2.25

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/index.d.ts CHANGED
@@ -991,11 +991,12 @@ declare module "socket-function/src/forwardPort" {
991
991
  export declare function listPortMappings(): Promise<PortMapping[]>;
992
992
  /** Outcome of forwardPort. `owned` is true once we hold the router mapping for the port. When
993
993
  * false, `reason` says why: "declined" = noPortStealing and another host holds the port (the
994
- * caller should try a different port); "error" = UPnP unreachable / create failed (best-effort,
995
- * nothing forwarded but the caller can carry on). */
994
+ * caller should try a different port); "notBehindNat" = we have a public/directly-reachable
995
+ * address so there's nothing to forward; "error" = UPnP unreachable / create failed (best-effort,
996
+ * nothing forwarded but the caller can carry on). Only "declined" warrants trying another port. */
996
997
  export type ForwardPortResult = {
997
998
  owned: boolean;
998
- reason?: "declined" | "error";
999
+ reason?: "declined" | "notBehindNat" | "error";
999
1000
  };
1000
1001
  export declare function forwardPort(config: {
1001
1002
  externalPort: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "socket-function",
3
- "version": "1.2.24",
3
+ "version": "1.2.25",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "dependencies": {
@@ -26,11 +26,12 @@ export interface PortMapping {
26
26
  export declare function listPortMappings(): Promise<PortMapping[]>;
27
27
  /** Outcome of forwardPort. `owned` is true once we hold the router mapping for the port. When
28
28
  * false, `reason` says why: "declined" = noPortStealing and another host holds the port (the
29
- * caller should try a different port); "error" = UPnP unreachable / create failed (best-effort,
30
- * nothing forwarded but the caller can carry on). */
29
+ * caller should try a different port); "notBehindNat" = we have a public/directly-reachable
30
+ * address so there's nothing to forward; "error" = UPnP unreachable / create failed (best-effort,
31
+ * nothing forwarded but the caller can carry on). Only "declined" warrants trying another port. */
31
32
  export type ForwardPortResult = {
32
33
  owned: boolean;
33
- reason?: "declined" | "error";
34
+ reason?: "declined" | "notBehindNat" | "error";
34
35
  };
35
36
  export declare function forwardPort(config: {
36
37
  externalPort: number;
@@ -18,6 +18,13 @@ export async function resolveGateway(): Promise<{
18
18
  let internalIP = await getOutboundIP();
19
19
  if (!internalIP) throw new Error("Could not determine our local network address");
20
20
 
21
+ // A public/directly-reachable outbound address means there's no NAT and therefore no UPnP
22
+ // gateway to reach — SSDP would just multicast into the void and time out after 2s. Fail fast
23
+ // and clearly instead, so callers (and forwardPort) never spend that time on a public host.
24
+ if (!isPrivateIPv4(internalIP)) {
25
+ throw new Error(`Not behind a NAT (outbound address ${internalIP} is public) — no UPnP gateway to reach, skipping SSDP discovery.`);
26
+ }
27
+
21
28
  // The gateway that answers SSDP discovery is the router we forward through; take its IP
22
29
  // and control port straight from the discovered device URL rather than parsing routes.
23
30
  let gateway = await discoverGateway(internalIP);
@@ -89,11 +96,12 @@ const SUPERSEDE_CHECK_INTERVAL = timeInMinute * 30;
89
96
 
90
97
  /** Outcome of forwardPort. `owned` is true once we hold the router mapping for the port. When
91
98
  * false, `reason` says why: "declined" = noPortStealing and another host holds the port (the
92
- * caller should try a different port); "error" = UPnP unreachable / create failed (best-effort,
93
- * nothing forwarded but the caller can carry on). */
99
+ * caller should try a different port); "notBehindNat" = we have a public/directly-reachable
100
+ * address so there's nothing to forward; "error" = UPnP unreachable / create failed (best-effort,
101
+ * nothing forwarded but the caller can carry on). Only "declined" warrants trying another port. */
94
102
  export type ForwardPortResult = {
95
103
  owned: boolean;
96
- reason?: "declined" | "error";
104
+ reason?: "declined" | "notBehindNat" | "error";
97
105
  };
98
106
 
99
107
  export async function forwardPort(config: {
@@ -113,6 +121,12 @@ export async function forwardPort(config: {
113
121
  let duration = config.duration ?? PERMANENT_LEASE;
114
122
  let permanent = duration === PERMANENT_LEASE;
115
123
 
124
+ // Forwarding only makes sense behind a NAT. On a directly-reachable public host there's no
125
+ // gateway, so skip UPnP entirely (no SSDP) rather than discovering-and-timing-out.
126
+ if (!await isBehindNAT()) {
127
+ return { owned: false, reason: "notBehindNat" };
128
+ }
129
+
116
130
  // Take ownership of the router's mapping for this external port: delete whatever's there (a
117
131
  // stale mapping of ours, or another host's) so our AddPortMapping isn't rejected as a conflict
118
132
  // (718), then install our mapping. This makes the last writer win — whichever application