servlink 0.0.0-stage → 1.0.1

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/dist/wake.js ADDED
@@ -0,0 +1,136 @@
1
+ /**
2
+ * Wake/sleep hooks for a local service that is not always running (a stopped container,
3
+ * a dev server). Commands come only from the local operator (CLI flags / config file);
4
+ * nothing received from the relay can influence what is executed.
5
+ */
6
+ import { spawn } from 'node:child_process';
7
+ import { closeSync, openSync } from 'node:fs';
8
+ import { connect } from 'node:net';
9
+ import { ensureHome, paths } from './paths.js';
10
+ export function isPortOpen(target, timeoutMs = 1000) {
11
+ return new Promise((resolve) => {
12
+ const sock = connect({ host: target.host, port: target.port });
13
+ const done = (ok) => {
14
+ sock.removeAllListeners();
15
+ sock.destroy();
16
+ resolve(ok);
17
+ };
18
+ sock.setTimeout(timeoutMs, () => done(false));
19
+ sock.once('connect', () => done(true));
20
+ sock.once('error', () => done(false));
21
+ });
22
+ }
23
+ export async function waitForPort(target, timeoutMs, intervalMs = 200) {
24
+ const deadline = Date.now() + timeoutMs;
25
+ while (Date.now() < deadline) {
26
+ if (await isPortOpen(target, Math.min(1000, Math.max(50, deadline - Date.now()))))
27
+ return true;
28
+ await new Promise((r) => setTimeout(r, intervalMs));
29
+ }
30
+ return false;
31
+ }
32
+ function runShell(cmd) {
33
+ ensureHome();
34
+ const fd = openSync(paths.wakeLog, 'a', 0o600);
35
+ try {
36
+ return spawn(cmd, { shell: true, stdio: ['ignore', fd, fd], windowsHide: true, env: process.env });
37
+ }
38
+ finally {
39
+ closeSync(fd);
40
+ }
41
+ }
42
+ export class WakeManager {
43
+ opts;
44
+ waking = null;
45
+ idleTimer = null;
46
+ activeSinceSleep = false;
47
+ sleeping = false;
48
+ /** Number of times the wake command has actually been spawned (observable for tests/status). */
49
+ wakeRuns = 0;
50
+ constructor(opts) {
51
+ this.opts = opts;
52
+ }
53
+ get canWake() {
54
+ return Boolean(this.opts.wakeCmd);
55
+ }
56
+ /** Call on every proxied request: resets the idle-sleep countdown. */
57
+ touch() {
58
+ this.activeSinceSleep = true;
59
+ if (!this.opts.sleepCmd)
60
+ return;
61
+ if (this.idleTimer)
62
+ this.idleTimer.refresh();
63
+ else {
64
+ const ms = Math.max(1, this.opts.idleMinutes ?? 15) * 60_000;
65
+ this.idleTimer = setTimeout(() => void this.sleep(), ms);
66
+ this.idleTimer.unref();
67
+ }
68
+ }
69
+ /**
70
+ * Run the wake command once, however many requests hit a refused port at the same time
71
+ * (they all await the same promise), then poll until the port accepts connections.
72
+ */
73
+ ensureAwake() {
74
+ if (!this.opts.wakeCmd)
75
+ return Promise.reject(new Error('no wake command configured'));
76
+ if (this.waking)
77
+ return this.waking;
78
+ this.waking = (async () => {
79
+ if (await isPortOpen(this.opts.target, 500))
80
+ return;
81
+ this.opts.log?.(`waking local service: ${this.opts.wakeCmd}`);
82
+ this.wakeRuns++;
83
+ const child = runShell(this.opts.wakeCmd);
84
+ let exitedBadly = null;
85
+ child.once('exit', (code) => {
86
+ if (code !== 0 && code !== null)
87
+ exitedBadly = code;
88
+ });
89
+ child.once('error', () => {
90
+ exitedBadly = -1;
91
+ });
92
+ child.unref();
93
+ const deadline = Date.now() + (this.opts.wakeTimeoutMs ?? 10_000);
94
+ while (Date.now() < deadline) {
95
+ if (await isPortOpen(this.opts.target, 500)) {
96
+ this.sleeping = false;
97
+ this.opts.log?.('local service is up');
98
+ return;
99
+ }
100
+ if (exitedBadly !== null)
101
+ throw new Error(`wake command exited with code ${exitedBadly}`);
102
+ await new Promise((r) => setTimeout(r, 200));
103
+ }
104
+ throw new Error(`local service did not open ${this.opts.target.authority} within ${(this.opts.wakeTimeoutMs ?? 10_000) / 1000}s of waking`);
105
+ })().finally(() => {
106
+ this.waking = null;
107
+ });
108
+ return this.waking;
109
+ }
110
+ async sleep() {
111
+ this.idleTimer = null;
112
+ if (!this.opts.sleepCmd || !this.activeSinceSleep || this.sleeping || this.waking)
113
+ return;
114
+ this.activeSinceSleep = false;
115
+ this.sleeping = true;
116
+ this.opts.log?.(`idle for ${this.opts.idleMinutes ?? 15}m, running sleep command: ${this.opts.sleepCmd}`);
117
+ await new Promise((resolve) => {
118
+ const child = runShell(this.opts.sleepCmd);
119
+ const kill = setTimeout(() => child.kill(), 60_000);
120
+ child.once('exit', () => {
121
+ clearTimeout(kill);
122
+ resolve();
123
+ });
124
+ child.once('error', () => {
125
+ clearTimeout(kill);
126
+ resolve();
127
+ });
128
+ });
129
+ }
130
+ stop() {
131
+ if (this.idleTimer)
132
+ clearTimeout(this.idleTimer);
133
+ this.idleTimer = null;
134
+ }
135
+ }
136
+ //# sourceMappingURL=wake.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wake.js","sourceRoot":"","sources":["../src/wake.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,KAAK,EAAqB,MAAM,oBAAoB,CAAC;AAC9D,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAC9C,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAEnC,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAE/C,MAAM,UAAU,UAAU,CAAC,MAAc,EAAE,SAAS,GAAG,IAAI;IACzD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC7B,MAAM,IAAI,GAAG,OAAO,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC;QAC/D,MAAM,IAAI,GAAG,CAAC,EAAW,EAAE,EAAE;YAC3B,IAAI,CAAC,kBAAkB,EAAE,CAAC;YAC1B,IAAI,CAAC,OAAO,EAAE,CAAC;YACf,OAAO,CAAC,EAAE,CAAC,CAAC;QACd,CAAC,CAAC;QACF,IAAI,CAAC,UAAU,CAAC,SAAS,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,CA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E,GAAG,EAAE;gBACvB,YAAY,CAAC,IAAI,CAAC,CAAC;gBACnB,OAAO,EAAE,CAAC;YACZ,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;IAED,IAAI;QACF,IAAI,IAAI,CAAC,SAAS;YAAE,YAAY,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QACjD,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;IACxB,CAAC;CACF"}
package/package.json CHANGED
@@ -1,6 +1,54 @@
1
1
  {
2
2
  "name": "servlink",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.1",
4
+ "description": "slink: zero-trust outbound Ed25519-authenticated server tunnel to any compatible relay server, with loopback target lock, local rate/body guards and wake/sleep hooks",
5
+ "type": "module",
6
+ "bin": {
7
+ "slink": "dist/cli.js"
8
+ },
9
+ "main": "./dist/index.js",
10
+ "exports": {
11
+ ".": "./dist/index.js"
12
+ },
13
+ "files": [
14
+ "dist",
15
+ "README.md",
16
+ "LICENSE"
17
+ ],
18
+ "engines": {
19
+ "node": ">=20.12"
20
+ },
21
+ "scripts": {
22
+ "build": "tsc -p tsconfig.json",
23
+ "typecheck": "tsc -p tsconfig.json --noEmit",
24
+ "test": "npm run build && tsx --test --test-concurrency=1 test/*.test.ts",
25
+ "prepublishOnly": "npm run build"
26
+ },
27
+ "keywords": [
28
+ "tunnel",
29
+ "localhost",
30
+ "zero-trust",
31
+ "reverse-proxy",
32
+ "websocket",
33
+ "ed25519"
34
+ ],
35
+ "license": "MIT",
36
+ "author": "Sarthak Kamon <kamonsarthak@hotmail.com> (https://github.com/kamonsarthak)",
37
+ "repository": {
38
+ "type": "git",
39
+ "url": "git+https://github.com/kamonsarthak/servlink.git"
40
+ },
41
+ "homepage": "https://github.com/kamonsarthak/servlink#readme",
42
+ "bugs": {
43
+ "url": "https://github.com/kamonsarthak/servlink/issues"
44
+ },
45
+ "dependencies": {
46
+ "ws": "^8.23.0"
47
+ },
48
+ "devDependencies": {
49
+ "@types/node": "^26.6.6",
50
+ "@types/ws": "^8.18.2",
51
+ "tsx": "^4.23.15",
52
+ "typescript": "^7.0.2"
53
+ }
54
+ }