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/LICENSE +21 -0
- package/README.md +184 -2
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +311 -0
- package/dist/cli.js.map +1 -0
- package/dist/guard.d.ts +44 -0
- package/dist/guard.js +157 -0
- package/dist/guard.js.map +1 -0
- package/dist/identity.d.ts +20 -0
- package/dist/identity.js +56 -0
- package/dist/identity.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/logs.d.ts +32 -0
- package/dist/logs.js +110 -0
- package/dist/logs.js.map +1 -0
- package/dist/paths.d.ts +14 -0
- package/dist/paths.js +48 -0
- package/dist/paths.js.map +1 -0
- package/dist/protocol.d.ts +130 -0
- package/dist/protocol.js +85 -0
- package/dist/protocol.js.map +1 -0
- package/dist/state.d.ts +34 -0
- package/dist/state.js +50 -0
- package/dist/state.js.map +1 -0
- package/dist/tunnel.d.ts +61 -0
- package/dist/tunnel.js +490 -0
- package/dist/tunnel.js.map +1 -0
- package/dist/wake.d.ts +32 -0
- package/dist/wake.js +136 -0
- package/dist/wake.js.map +1 -0
- package/package.json +52 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sarthak Kamon
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,185 @@
|
|
|
1
|
-
#
|
|
1
|
+
# servlink (`slink`)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A standalone, zero-trust tunnel that exposes a local server through one **outbound**, Ed25519-authenticated WebSocket to a relay server you choose. You don't open inbound ports, configure DNS or set up TLS on your machine.
|
|
4
|
+
|
|
5
|
+
- **Ed25519 hardware-style identity.** A keypair is generated once on the machine and the private key never leaves it. The relay only ever holds the public key, and every connection proves possession by signing a fresh challenge.
|
|
6
|
+
- **Strict loopback target locking.** The local host and port are fixed at startup. Nothing the relay sends can make `slink` connect anywhere else.
|
|
7
|
+
- **Local token-bucket rate limiting and payload caps.** Enforced on your machine, before a request reaches your app.
|
|
8
|
+
- **Wake/sleep process hooks.** Start your app on the first request and stop it after a quiet period.
|
|
9
|
+
|
|
10
|
+
`slink` is the client half. It works with any relay that speaks the [wire protocol](#relay-wire-protocol) below.
|
|
11
|
+
|
|
12
|
+
## Quick start
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm i -g servlink # or run any command as: npx servlink <command>
|
|
16
|
+
|
|
17
|
+
slink init # create this machine's identity
|
|
18
|
+
# register the printed node_id and public key with your relay, then:
|
|
19
|
+
slink connect --port 3000 --relay wss://relay.example.com/tunnel
|
|
20
|
+
|
|
21
|
+
slink status # identity, saved target, daemon state
|
|
22
|
+
slink logs # tail the sanitized request log
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
The relay is remembered after the first connect, so later runs only need `slink connect --port 3000`.
|
|
26
|
+
|
|
27
|
+
Requires Node.js 20.12 or newer.
|
|
28
|
+
|
|
29
|
+
## Commands
|
|
30
|
+
|
|
31
|
+
| Command | What it does |
|
|
32
|
+
|---|---|
|
|
33
|
+
| `slink init [--node-id <id>] [--force]` | Generates an Ed25519 keypair and a `node_id` (`<hostname>-<8 hex>`), saved to `~/.servlink/identity.json` with mode `0600`. Prints the `node_id` and the public key as raw hex, base64 and PEM. Register the `node_id` and public key with your relay. |
|
|
34
|
+
| `slink connect` | Opens and maintains the tunnel. Settings are saved to `~/.servlink/config.json` and reused on the next run. |
|
|
35
|
+
| `slink status` | Shows the `node_id`, public key, saved target, relay and limits, and whether a `slink connect` daemon is running and its tunnel state. |
|
|
36
|
+
| `slink logs [-n 50] [--no-follow]` | Tails `~/.servlink/logs/requests.log`: `[timestamp] [lease_id] METHOD /path -> STATUS (latency_ms)`. |
|
|
37
|
+
|
|
38
|
+
### `slink connect` flags
|
|
39
|
+
|
|
40
|
+
| Flag | Default | |
|
|
41
|
+
|---|---|---|
|
|
42
|
+
| `--port <n>` | saved value, else prompted | local port to expose |
|
|
43
|
+
| `--host <ip>` | `127.0.0.1` | `127.0.0.1`, `::1`, `localhost`, or an explicit private IP |
|
|
44
|
+
| `--relay <wss_url>` | none, see below | the relay's tunnel endpoint (`wss://` or `ws://`) |
|
|
45
|
+
| `--max-rps <n>` | `30` | local token bucket (burst 3×) |
|
|
46
|
+
| `--max-body-mb <n>` | `5` | local request-body cap |
|
|
47
|
+
| `--wake-cmd "<cmd>"` | | run when the port refuses connections |
|
|
48
|
+
| `--sleep-cmd "<cmd>"` | | run after `--idle-minutes` without traffic |
|
|
49
|
+
| `--idle-minutes <n>` | `15` | |
|
|
50
|
+
| `--force` | | start even if another daemon is running |
|
|
51
|
+
|
|
52
|
+
### Choosing the relay
|
|
53
|
+
|
|
54
|
+
There is no built-in relay. `slink connect` uses the first of these that is set:
|
|
55
|
+
|
|
56
|
+
1. the `--relay <wss_url>` flag
|
|
57
|
+
2. the `SERVLINK_RELAY_URL` environment variable
|
|
58
|
+
3. the `relay` saved in `~/.servlink/config.json` by an earlier connect
|
|
59
|
+
4. an interactive prompt, when run in a terminal
|
|
60
|
+
|
|
61
|
+
The relay in use is written to `~/.servlink/config.json` each time `slink connect` starts. Without a terminal and with none of the first three set, `slink connect` exits with an error.
|
|
62
|
+
|
|
63
|
+
## Host-protection guardrails
|
|
64
|
+
|
|
65
|
+
These run on your machine and do not depend on the relay behaving well.
|
|
66
|
+
|
|
67
|
+
- **Target lock (no SSRF or lateral movement).** Host and port are fixed when `slink connect` starts. Only loopback (`127.0.0.0/8`, `::1`, `localhost` pinned to `127.0.0.1`) or private IPs you pass explicitly (`10/8`, `172.16/12`, `192.168/16`, `fc00::/7`) are accepted. Hostnames are refused, which rules out DNS rebinding, and so is link-local `169.254/16`, which hosts cloud metadata. Relay frames carry only a path. The path must be origin-form (`/…`): `//host`, `scheme://`, backslashes and control characters get a `400`. WebSocket URLs are built from the locked authority and checked again after parsing.
|
|
68
|
+
- **Rate limit.** Over `--max-rps`, slink answers `429` with `Retry-After: 1` without touching your server. The same limit applies to WebSocket opens and visitor messages.
|
|
69
|
+
- **Body cap.** An oversized body is rejected with `413` before it is decoded, based on its base64 size. A streaming byte counter also aborts the local request if the cap is crossed mid-write. WebSocket messages are capped too.
|
|
70
|
+
- **Timeouts.** Connecting to your server and receiving response headers has a hard 15 s limit, after which slink reports an error to the relay. An established response stream may idle for up to 5 minutes. At most 128 concurrent streams are allowed.
|
|
71
|
+
- **Crash safety.** Every frame, socket, HTTP and WebSocket handler is wrapped in an error boundary. Malformed frames are dropped, connection refusals are reported as errors, and the process logs any stray exception and keeps running.
|
|
72
|
+
- **Header sanitising.** slink drops `X-Forwarded-*`, `X-Real-IP`, `Forwarded`, hop-by-hop headers and malformed headers. It sets `Host: <host>:<port>` and adds `X-Servlink-Verified: 1`, so your app can tell a request came through the tunnel.
|
|
73
|
+
- **Wake/sleep.** On `ECONNREFUSED` with `--wake-cmd` set, slink runs the command once (a mutex covers concurrent requests) and polls the port for up to 10 s before forwarding. Command output goes to `~/.servlink/logs/wake.log`. Commands come only from your local flags and config, never from the relay.
|
|
74
|
+
- **Logs.** Logs never contain request or response bodies, cookies or query strings. The query is replaced by `?…`, and control characters are escaped to prevent log injection. The file rotates at 5 MB and keeps 3 old copies.
|
|
75
|
+
|
|
76
|
+
## Relay wire protocol
|
|
77
|
+
|
|
78
|
+
Everything travels as **JSON text frames** over the single WebSocket that `slink` opens to the relay URL. Binary frames are ignored. A relay that implements this section works with `slink` unchanged. The TypeScript types are in `src/protocol.ts`.
|
|
79
|
+
|
|
80
|
+
In the tables, **R→N** is relay to node (`slink`) and **N→R** is node to relay.
|
|
81
|
+
|
|
82
|
+
### Registration (out of band)
|
|
83
|
+
|
|
84
|
+
Before a node can connect, the relay must know its `node_id` and its raw 32-byte Ed25519 public key (`slink init` prints it as 64 hex characters). How you register them is up to the relay. A `node_id` matches `^[A-Za-z0-9][A-Za-z0-9._:-]{2,127}$`.
|
|
85
|
+
|
|
86
|
+
### Handshake
|
|
87
|
+
|
|
88
|
+
| Dir | Frame |
|
|
89
|
+
|---|---|
|
|
90
|
+
| R→N | `{ "type": "challenge", "nonce": "<64 hex chars>", "server_time": <ms> }` |
|
|
91
|
+
| N→R | `{ "type": "auth", "node_id": "<id>", "signature": "<base64>" }` |
|
|
92
|
+
| R→N | `{ "type": "auth_ok", "node_id": "<id>" }` |
|
|
93
|
+
| R→N | `{ "type": "auth_error", "reason": "<text>" }`, then close with `4401` |
|
|
94
|
+
|
|
95
|
+
The relay sends `challenge` as soon as the socket opens. The nonce must be exactly 64 hex characters (32 random bytes). The signature is Ed25519 over the **UTF-8 bytes of the nonce string** (the 64 characters, not the decoded 32 bytes), base64-encoded. The relay verifies it against the public key registered for `node_id`.
|
|
96
|
+
|
|
97
|
+
### HTTP
|
|
98
|
+
|
|
99
|
+
| Dir | Frame |
|
|
100
|
+
|---|---|
|
|
101
|
+
| R→N | `{ "type": "http_request", "id", "method", "path", "headers", "body"?, "lease_id"? }` |
|
|
102
|
+
| R→N | `{ "type": "http_cancel", "id" }` |
|
|
103
|
+
| N→R | `{ "type": "http_response", "id", "status", "headers", "body"? }` |
|
|
104
|
+
| N→R | `{ "type": "http_response_start", "id", "status", "headers" }` |
|
|
105
|
+
| N→R | `{ "type": "http_response_chunk", "id", "data" }` |
|
|
106
|
+
| N→R | `{ "type": "http_response_end", "id" }` |
|
|
107
|
+
| N→R | `{ "type": "http_error", "id", "message" }` |
|
|
108
|
+
|
|
109
|
+
- `id` is chosen by the relay: 1 to 64 characters of `A-Z a-z 0-9 -`, unique among open streams.
|
|
110
|
+
- `path` is origin-form and includes the query string, for example `/api/items?page=2`.
|
|
111
|
+
- `headers` is an object whose values are strings or arrays of strings.
|
|
112
|
+
- `body` and `data` are base64.
|
|
113
|
+
- `lease_id` is optional and opaque to `slink`. It is only written to the request log, so a relay can use it to tag which grant or link a request arrived through.
|
|
114
|
+
- Responses from the local server are streamed: `http_response_start`, zero or more `http_response_chunk`, then `http_response_end`. The single-shot `http_response` is used for replies `slink` generates itself (`400`, `413`, `429`, `503`); these carry the header `x-servlink-guard: 1`.
|
|
115
|
+
- `http_error` means the local server could not be reached, timed out or failed. If it arrives before `http_response_start`, the relay should answer the visitor with `502` or `504`. If it arrives after, the relay should abort the visitor's response.
|
|
116
|
+
- `http_cancel` tells `slink` the visitor went away. It aborts the local request and sends nothing further for that `id`.
|
|
117
|
+
|
|
118
|
+
### WebSocket
|
|
119
|
+
|
|
120
|
+
Visitor WebSockets are multiplexed over the tunnel by stream `id`.
|
|
121
|
+
|
|
122
|
+
| Dir | Frame |
|
|
123
|
+
|---|---|
|
|
124
|
+
| R→N | `{ "type": "ws_open", "id", "path", "headers", "lease_id"? }` |
|
|
125
|
+
| N→R | `{ "type": "ws_opened", "id" }` |
|
|
126
|
+
| N→R | `{ "type": "ws_error", "id", "message" }` |
|
|
127
|
+
| both | `{ "type": "ws_message", "id", "data", "binary" }` |
|
|
128
|
+
| both | `{ "type": "ws_close", "id", "code"?, "reason"? }` |
|
|
129
|
+
|
|
130
|
+
`slink` answers `ws_open` with `ws_opened` once the local socket is open, or `ws_error` if it cannot be opened. In `ws_message`, `data` is base64 when `binary` is `true` and plain text otherwise. Subprotocols are taken from the `sec-websocket-protocol` header of `ws_open`.
|
|
131
|
+
|
|
132
|
+
### Liveness
|
|
133
|
+
|
|
134
|
+
`slink` sends a WebSocket-level ping every 20 s and drops the connection if no pong arrives before the next one. Standard WebSocket servers answer these automatically. The relay may ping too. An application-level `{ "type": "pong" }` frame from the relay is accepted and ignored.
|
|
135
|
+
|
|
136
|
+
### Close codes
|
|
137
|
+
|
|
138
|
+
| Code | Meaning | What `slink` does |
|
|
139
|
+
|---|---|---|
|
|
140
|
+
| `4400` | protocol error | reconnects |
|
|
141
|
+
| `4401` | authentication failed: unknown `node_id` or bad signature | reconnects, in case the key is registered later |
|
|
142
|
+
| `4408` | authentication timed out | reconnects |
|
|
143
|
+
| `4409` | superseded: another `slink` connected with the same `node_id` | exits |
|
|
144
|
+
| `4410` | the node's registration changed or was removed | reconnects |
|
|
145
|
+
|
|
146
|
+
Any other close is treated as a dropped connection. Reconnects use exponential backoff with jitter: 1 s doubling up to 30 s, reset after each successful auth.
|
|
147
|
+
|
|
148
|
+
### Requirements for a relay
|
|
149
|
+
|
|
150
|
+
A relay decides who may reach a node and how visitors are routed to it. Whatever it does there, it should:
|
|
151
|
+
|
|
152
|
+
- send requests only to the node whose signature it verified on that socket
|
|
153
|
+
- allow one live socket per `node_id`, closing the older one with `4409`
|
|
154
|
+
|
|
155
|
+
It does not need to scrub `X-Servlink-Verified` or forwarding headers from visitor requests: `slink` drops whatever arrives and sets `X-Servlink-Verified: 1` itself on every request it forwards.
|
|
156
|
+
|
|
157
|
+
## Files
|
|
158
|
+
|
|
159
|
+
```
|
|
160
|
+
~/.servlink/identity.json Ed25519 identity (0600) ← keep secret
|
|
161
|
+
~/.servlink/config.json last connect settings, including the relay
|
|
162
|
+
~/.servlink/daemon.json pid + live tunnel state (for `slink status`)
|
|
163
|
+
~/.servlink/logs/requests.log sanitized request log (+ .1 .2 .3)
|
|
164
|
+
~/.servlink/logs/wake.log wake/sleep command output
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Environment
|
|
168
|
+
|
|
169
|
+
| Variable | |
|
|
170
|
+
|---|---|
|
|
171
|
+
| `SERVLINK_RELAY_URL` | relay used when `--relay` is not passed |
|
|
172
|
+
| `SERVLINK_HOME` | state directory, default `~/.servlink` |
|
|
173
|
+
| `NO_COLOR` | disable coloured output |
|
|
174
|
+
|
|
175
|
+
## Development
|
|
176
|
+
|
|
177
|
+
```bash
|
|
178
|
+
npm install
|
|
179
|
+
npm run build
|
|
180
|
+
npm test # build + unit tests + mock-relay tunnel tests + compiled CLI tests
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## License
|
|
184
|
+
|
|
185
|
+
[MIT](LICENSE) © Sarthak Kamon ([@kamonsarthak](https://github.com/kamonsarthak))
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* slink — expose a local port through an outbound, Ed25519-authenticated tunnel to a relay server.
|
|
4
|
+
*
|
|
5
|
+
* slink init [--node-id <id>] [--force]
|
|
6
|
+
* slink connect --port <n> [--host 127.0.0.1] [--relay wss://relay.example.com/tunnel]
|
|
7
|
+
* [--max-rps 30] [--max-body-mb 5] [--wake-cmd "<cmd>"] [--sleep-cmd "<cmd>"] [--idle-minutes 15]
|
|
8
|
+
* slink status
|
|
9
|
+
* slink logs [-n 50] [--no-follow]
|
|
10
|
+
*/
|
|
11
|
+
import { createInterface } from 'node:readline/promises';
|
|
12
|
+
import { parseArgs } from 'node:util';
|
|
13
|
+
import { GuardError, lockTarget } from './guard.js';
|
|
14
|
+
import { createIdentity, identityPermissionWarning, loadIdentity, privateKeyOf, saveIdentity } from './identity.js';
|
|
15
|
+
import { followFile, RequestLog, tailLines } from './logs.js';
|
|
16
|
+
import { paths } from './paths.js';
|
|
17
|
+
import { activeDaemon, clearDaemonState, DEFAULTS, loadConfig, saveConfig, writeDaemonState } from './state.js';
|
|
18
|
+
import { SlinkTunnel } from './tunnel.js';
|
|
19
|
+
import { WakeManager } from './wake.js';
|
|
20
|
+
const VERSION = '1.0.1';
|
|
21
|
+
const color = process.stdout.isTTY === true && !process.env.NO_COLOR;
|
|
22
|
+
const paint = (code) => (s) => (color ? `\x1b[${code}m${s}\x1b[0m` : String(s));
|
|
23
|
+
const c = { green: paint('38;2;16;185;129'), orange: paint('38;2;255;77;0'), dim: paint('2'), bold: paint('1') };
|
|
24
|
+
function out(msg = '') {
|
|
25
|
+
process.stdout.write(`${msg}\n`);
|
|
26
|
+
}
|
|
27
|
+
function fail(msg) {
|
|
28
|
+
process.stderr.write(`${c.orange('✗')} ${msg}\n`);
|
|
29
|
+
process.exit(1);
|
|
30
|
+
}
|
|
31
|
+
function stamp() {
|
|
32
|
+
return c.dim(new Date().toISOString().slice(11, 19));
|
|
33
|
+
}
|
|
34
|
+
// ─── init ─────────────────────────────────────────────────────────────────
|
|
35
|
+
function cmdInit(argv) {
|
|
36
|
+
const { values } = parseArgs({ args: argv, options: { 'node-id': { type: 'string' }, force: { type: 'boolean', default: false } } });
|
|
37
|
+
const existing = loadIdentity();
|
|
38
|
+
if (existing && !values.force) {
|
|
39
|
+
fail(`An identity already exists for node ${c.bold(existing.node_id)} (${paths.identity}). Use --force to replace it.`);
|
|
40
|
+
}
|
|
41
|
+
let id;
|
|
42
|
+
try {
|
|
43
|
+
id = createIdentity(values['node-id']);
|
|
44
|
+
}
|
|
45
|
+
catch (err) {
|
|
46
|
+
fail(err.message);
|
|
47
|
+
}
|
|
48
|
+
saveIdentity(id);
|
|
49
|
+
out(`${c.green('~>')} ${c.bold('slink identity created')} ${c.dim(`(${paths.identity}, mode 0600)`)}`);
|
|
50
|
+
out();
|
|
51
|
+
out(` ${c.dim('node_id ')} ${c.bold(id.node_id)}`);
|
|
52
|
+
out(` ${c.dim('public_key')} ${c.bold(id.public_key_hex)}`);
|
|
53
|
+
out(` ${c.dim(' base64 ')} ${id.public_key_base64}`);
|
|
54
|
+
out();
|
|
55
|
+
out(`${c.orange('Next:')} register your node_id and Ed25519 public key with your relay:`);
|
|
56
|
+
out(c.dim(` {"node_id":"${id.node_id}","public_key":"${id.public_key_hex}"}`));
|
|
57
|
+
out(`then run ${c.green('slink connect --port <your-app-port> --relay wss://<your-relay>/tunnel')}.`);
|
|
58
|
+
out(c.dim('PEM form of the public key, if you need it:'));
|
|
59
|
+
out(c.dim(id.public_key_pem.trim()));
|
|
60
|
+
}
|
|
61
|
+
// ─── connect ──────────────────────────────────────────────────────────────
|
|
62
|
+
function num(name, v, min, max) {
|
|
63
|
+
if (v === undefined)
|
|
64
|
+
return undefined;
|
|
65
|
+
const n = Number(v);
|
|
66
|
+
if (!Number.isFinite(n) || n < min || n > max)
|
|
67
|
+
fail(`--${name} must be a number between ${min} and ${max}`);
|
|
68
|
+
return n;
|
|
69
|
+
}
|
|
70
|
+
async function ask(rl, q, def) {
|
|
71
|
+
const answer = (await rl.question(`${c.green('?')} ${q}${def !== undefined ? c.dim(` (${def})`) : ''}: `)).trim();
|
|
72
|
+
return answer || (def !== undefined ? String(def) : '');
|
|
73
|
+
}
|
|
74
|
+
async function resolveConnectConfig(argv) {
|
|
75
|
+
const { values } = parseArgs({
|
|
76
|
+
args: argv,
|
|
77
|
+
options: {
|
|
78
|
+
port: { type: 'string', short: 'p' },
|
|
79
|
+
host: { type: 'string' },
|
|
80
|
+
relay: { type: 'string' },
|
|
81
|
+
'max-rps': { type: 'string' },
|
|
82
|
+
'max-body-mb': { type: 'string' },
|
|
83
|
+
'wake-cmd': { type: 'string' },
|
|
84
|
+
'sleep-cmd': { type: 'string' },
|
|
85
|
+
'idle-minutes': { type: 'string' },
|
|
86
|
+
force: { type: 'boolean', default: false },
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
const saved = loadConfig();
|
|
90
|
+
let port = num('port', values.port, 1, 65535);
|
|
91
|
+
let host = values.host;
|
|
92
|
+
// Relay precedence: --relay, then SERVLINK_RELAY_URL, then the saved config, then a prompt (TTY only).
|
|
93
|
+
let relay = values.relay?.trim() || process.env.SERVLINK_RELAY_URL?.trim() || saved?.relay?.trim() || undefined;
|
|
94
|
+
let target;
|
|
95
|
+
const rl = process.stdin.isTTY && (port === undefined || !relay) ? createInterface({ input: process.stdin, output: process.stdout }) : undefined;
|
|
96
|
+
try {
|
|
97
|
+
if (port === undefined) {
|
|
98
|
+
if (rl) {
|
|
99
|
+
port = num('port', await ask(rl, 'Local port to expose', saved?.port), 1, 65535);
|
|
100
|
+
host = host ?? (await ask(rl, 'Local host (loopback or private IP)', saved?.host ?? DEFAULTS.host));
|
|
101
|
+
}
|
|
102
|
+
else if (saved) {
|
|
103
|
+
port = saved.port;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (port === undefined)
|
|
107
|
+
fail('--port <number> is required (no saved config and no interactive terminal)');
|
|
108
|
+
host = host ?? saved?.host ?? DEFAULTS.host;
|
|
109
|
+
// Lock the target before asking about the relay: a refused target should fail first.
|
|
110
|
+
try {
|
|
111
|
+
target = lockTarget(host, port);
|
|
112
|
+
}
|
|
113
|
+
catch (err) {
|
|
114
|
+
fail(err instanceof GuardError ? err.message : String(err));
|
|
115
|
+
}
|
|
116
|
+
if (!relay && rl)
|
|
117
|
+
relay = await ask(rl, 'Relay WebSocket URL (wss://…)');
|
|
118
|
+
}
|
|
119
|
+
finally {
|
|
120
|
+
rl?.close();
|
|
121
|
+
}
|
|
122
|
+
if (!relay) {
|
|
123
|
+
fail('No relay configured. Pass --relay <wss_url> or set SERVLINK_RELAY_URL (the relay is remembered in config.json after the first connect).');
|
|
124
|
+
}
|
|
125
|
+
const cfg = {
|
|
126
|
+
port,
|
|
127
|
+
host,
|
|
128
|
+
relay,
|
|
129
|
+
maxRps: num('max-rps', values['max-rps'], 1, 10_000) ?? saved?.maxRps ?? DEFAULTS.maxRps,
|
|
130
|
+
maxBodyMb: num('max-body-mb', values['max-body-mb'], 0.001, 1024) ?? saved?.maxBodyMb ?? DEFAULTS.maxBodyMb,
|
|
131
|
+
wakeCmd: values['wake-cmd'] ?? saved?.wakeCmd,
|
|
132
|
+
sleepCmd: values['sleep-cmd'] ?? saved?.sleepCmd,
|
|
133
|
+
idleMinutes: num('idle-minutes', values['idle-minutes'], 1, 10_080) ?? saved?.idleMinutes ?? DEFAULTS.idleMinutes,
|
|
134
|
+
};
|
|
135
|
+
if (!/^wss?:\/\//i.test(cfg.relay))
|
|
136
|
+
fail(`The relay must be a ws:// or wss:// URL (got "${cfg.relay}")`);
|
|
137
|
+
return { cfg, target, force: values.force ?? false };
|
|
138
|
+
}
|
|
139
|
+
async function cmdConnect(argv) {
|
|
140
|
+
const identity = loadIdentity();
|
|
141
|
+
if (!identity)
|
|
142
|
+
fail(`No identity yet. Run ${c.green('slink init')} first.`);
|
|
143
|
+
const permWarning = identityPermissionWarning();
|
|
144
|
+
if (permWarning)
|
|
145
|
+
out(`${c.orange('!')} ${permWarning}`);
|
|
146
|
+
const { cfg, target, force } = await resolveConnectConfig(argv);
|
|
147
|
+
const running = activeDaemon();
|
|
148
|
+
if (running && running.pid !== process.pid && !force) {
|
|
149
|
+
fail(`slink is already running (pid ${running.pid}, ${running.target}). Stop it first or pass --force.`);
|
|
150
|
+
}
|
|
151
|
+
saveConfig(cfg);
|
|
152
|
+
// Error boundary: a misbehaving local service or malformed frame must never kill the tunnel.
|
|
153
|
+
process.on('uncaughtException', (err) => out(`${stamp()} ${c.orange('error')} ${err.message}`));
|
|
154
|
+
process.on('unhandledRejection', (err) => out(`${stamp()} ${c.orange('error')} ${err?.message ?? String(err)}`));
|
|
155
|
+
const startedAt = new Date().toISOString();
|
|
156
|
+
const persist = (state, detail) => {
|
|
157
|
+
try {
|
|
158
|
+
writeDaemonState({ pid: process.pid, started_at: startedAt, node_id: identity.node_id, relay: cfg.relay, target: target.authority, state, detail });
|
|
159
|
+
}
|
|
160
|
+
catch {
|
|
161
|
+
/* status file is best-effort */
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
persist('connecting');
|
|
165
|
+
const log = (msg) => out(`${stamp()} ${c.dim(msg)}`);
|
|
166
|
+
const wake = cfg.wakeCmd || cfg.sleepCmd ? new WakeManager({ target, wakeCmd: cfg.wakeCmd, sleepCmd: cfg.sleepCmd, idleMinutes: cfg.idleMinutes, log }) : undefined;
|
|
167
|
+
out(`${c.green('~>')} ${c.bold('slink')} ${c.dim(`v${VERSION}`)}`);
|
|
168
|
+
out(` ${c.dim('node ')} ${identity.node_id}`);
|
|
169
|
+
out(` ${c.dim('relay ')} ${cfg.relay}`);
|
|
170
|
+
out(` ${c.dim('target')} http://${target.authority} ${c.dim('(locked)')}`);
|
|
171
|
+
out(` ${c.dim('limits')} ${cfg.maxRps} req/s · ${cfg.maxBodyMb} MB body · 15s local timeout`);
|
|
172
|
+
if (cfg.wakeCmd)
|
|
173
|
+
out(` ${c.dim('wake ')} ${cfg.wakeCmd}`);
|
|
174
|
+
if (cfg.sleepCmd)
|
|
175
|
+
out(` ${c.dim('sleep ')} ${cfg.sleepCmd} ${c.dim(`after ${cfg.idleMinutes}m idle`)}`);
|
|
176
|
+
out(` ${c.dim('logs ')} ${paths.requestLog} ${c.dim('(slink logs)')}`);
|
|
177
|
+
out();
|
|
178
|
+
const tunnel = new SlinkTunnel({
|
|
179
|
+
relay: cfg.relay,
|
|
180
|
+
nodeId: identity.node_id,
|
|
181
|
+
privateKey: privateKeyOf(identity),
|
|
182
|
+
target,
|
|
183
|
+
maxRps: cfg.maxRps,
|
|
184
|
+
maxBodyMb: cfg.maxBodyMb,
|
|
185
|
+
wake,
|
|
186
|
+
requestLog: new RequestLog(),
|
|
187
|
+
log,
|
|
188
|
+
onState: (state, detail) => {
|
|
189
|
+
persist(state, detail);
|
|
190
|
+
const label = state === 'online' ? c.green('● ONLINE') : state === 'stopped' ? c.orange('■ stopped') : c.dim(`○ ${state}`);
|
|
191
|
+
out(`${stamp()} ${label}${detail ? c.dim(` ${detail}`) : ''}`);
|
|
192
|
+
if (state === 'stopped' && detail)
|
|
193
|
+
shutdown(1);
|
|
194
|
+
},
|
|
195
|
+
});
|
|
196
|
+
let exiting = false;
|
|
197
|
+
function shutdown(code = 0) {
|
|
198
|
+
if (exiting)
|
|
199
|
+
return;
|
|
200
|
+
exiting = true;
|
|
201
|
+
tunnel.stop();
|
|
202
|
+
wake?.stop();
|
|
203
|
+
clearDaemonState();
|
|
204
|
+
setTimeout(() => process.exit(code), 100).unref();
|
|
205
|
+
}
|
|
206
|
+
process.on('SIGINT', () => shutdown(0));
|
|
207
|
+
process.on('SIGTERM', () => shutdown(0));
|
|
208
|
+
process.on('exit', () => clearDaemonState());
|
|
209
|
+
tunnel.start();
|
|
210
|
+
}
|
|
211
|
+
// ─── status / logs ────────────────────────────────────────────────────────
|
|
212
|
+
function cmdStatus() {
|
|
213
|
+
const id = loadIdentity();
|
|
214
|
+
const cfg = loadConfig();
|
|
215
|
+
const d = activeDaemon();
|
|
216
|
+
out(`${c.green('~>')} ${c.bold('slink status')}`);
|
|
217
|
+
out(` ${c.dim('home ')} ${paths.home}`);
|
|
218
|
+
if (!id) {
|
|
219
|
+
out(` ${c.dim('identity ')} ${c.orange('none')} ${c.dim('(run slink init)')}`);
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
out(` ${c.dim('node_id ')} ${c.bold(id.node_id)}`);
|
|
223
|
+
out(` ${c.dim('public_key')} ${id.public_key_hex}`);
|
|
224
|
+
}
|
|
225
|
+
if (cfg) {
|
|
226
|
+
out(` ${c.dim('target ')} ${cfg.host}:${cfg.port}`);
|
|
227
|
+
out(` ${c.dim('relay ')} ${cfg.relay}`);
|
|
228
|
+
out(` ${c.dim('limits ')} ${cfg.maxRps} req/s · ${cfg.maxBodyMb} MB body`);
|
|
229
|
+
if (cfg.wakeCmd)
|
|
230
|
+
out(` ${c.dim('wake ')} ${cfg.wakeCmd}`);
|
|
231
|
+
if (cfg.sleepCmd)
|
|
232
|
+
out(` ${c.dim('sleep ')} ${cfg.sleepCmd} (after ${cfg.idleMinutes}m idle)`);
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
out(` ${c.dim('target ')} ${c.dim('not configured (slink connect --port <n>)')}`);
|
|
236
|
+
}
|
|
237
|
+
if (d) {
|
|
238
|
+
const label = d.state === 'online' ? c.green('● running · ONLINE') : c.orange(`● running · ${d.state}`);
|
|
239
|
+
out(` ${c.dim('daemon ')} ${label} ${c.dim(`pid ${d.pid} since ${d.started_at}`)}`);
|
|
240
|
+
if (d.detail && d.state !== 'online')
|
|
241
|
+
out(` ${c.dim(' ')}${c.dim(d.detail)}`);
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
out(` ${c.dim('daemon ')} ${c.dim('○ not running')}`);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
function cmdLogs(argv) {
|
|
248
|
+
const { values } = parseArgs({
|
|
249
|
+
args: argv,
|
|
250
|
+
options: { lines: { type: 'string', short: 'n', default: '50' }, 'no-follow': { type: 'boolean', default: false } },
|
|
251
|
+
});
|
|
252
|
+
const n = Math.max(0, Math.min(10_000, Number(values.lines) || 50));
|
|
253
|
+
const { lines, size } = tailLines(paths.requestLog, n);
|
|
254
|
+
for (const l of lines)
|
|
255
|
+
out(l);
|
|
256
|
+
if (values['no-follow']) {
|
|
257
|
+
if (lines.length === 0)
|
|
258
|
+
out(c.dim(`no requests logged yet (${paths.requestLog})`));
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
out(c.dim(`— following ${paths.requestLog} (Ctrl+C to stop)`));
|
|
262
|
+
const stop = followFile(paths.requestLog, size, (line) => out(line));
|
|
263
|
+
process.on('SIGINT', () => {
|
|
264
|
+
stop();
|
|
265
|
+
process.exit(0);
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
function help() {
|
|
269
|
+
out(`${c.green('~>')} ${c.bold('slink')} ${c.dim(`v${VERSION}`)} ${c.dim('servlink: outbound Ed25519-authenticated server tunnel')}
|
|
270
|
+
|
|
271
|
+
${c.green('slink init')} [--node-id <id>] [--force] create ~/.servlink/identity.json (Ed25519)
|
|
272
|
+
${c.green('slink connect')} --port <n> [options] open the tunnel (prompts when flags are omitted)
|
|
273
|
+
--host <ip> 127.0.0.1 | ::1 | localhost | explicit private IP (default 127.0.0.1)
|
|
274
|
+
--relay <wss_url> relay server tunnel endpoint; remembered after the first connect
|
|
275
|
+
--max-rps <n> local rate limit, default 30 (burst 3×)
|
|
276
|
+
--max-body-mb <n> local request body cap, default 5
|
|
277
|
+
--wake-cmd "<cmd>" run when the port refuses connections, then wait up to 10s
|
|
278
|
+
--sleep-cmd "<cmd>" run after --idle-minutes <n> (default 15) without traffic
|
|
279
|
+
--force start even if another slink daemon is running
|
|
280
|
+
${c.green('slink status')} identity, saved target, daemon state
|
|
281
|
+
${c.green('slink logs')} [-n 50] [--no-follow] tail sanitized request logs
|
|
282
|
+
|
|
283
|
+
${c.dim('Env: SERVLINK_RELAY_URL (relay when --relay is omitted), SERVLINK_HOME (default ~/.servlink), NO_COLOR')}`);
|
|
284
|
+
}
|
|
285
|
+
async function main() {
|
|
286
|
+
const [cmd, ...rest] = process.argv.slice(2);
|
|
287
|
+
switch (cmd) {
|
|
288
|
+
case 'init':
|
|
289
|
+
return cmdInit(rest);
|
|
290
|
+
case 'connect':
|
|
291
|
+
case 'up':
|
|
292
|
+
return cmdConnect(rest);
|
|
293
|
+
case 'status':
|
|
294
|
+
return cmdStatus();
|
|
295
|
+
case 'logs':
|
|
296
|
+
return cmdLogs(rest);
|
|
297
|
+
case '--version':
|
|
298
|
+
case '-v':
|
|
299
|
+
case 'version':
|
|
300
|
+
return out(VERSION);
|
|
301
|
+
case undefined:
|
|
302
|
+
case 'help':
|
|
303
|
+
case '--help':
|
|
304
|
+
case '-h':
|
|
305
|
+
return help();
|
|
306
|
+
default:
|
|
307
|
+
fail(`Unknown command "${cmd}". Run slink help.`);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
main().catch((err) => fail(err instanceof Error ? err.message : String(err)));
|
|
311
|
+
//# sourceMappingURL=cli.js.map
|
package/dist/cli.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AACA;;;;;;;;GAQG;AACH,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,UAAU,EAAE,UAAU,EAAe,MAAM,YAAY,CAAC;AACjE,OAAO,EAAE,cAAc,EAAE,yBAAyB,EAAE,YAAY,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AACpH,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAC9D,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,QAAQ,EAAE,UAAU,EAAE,UAAU,EAAE,gBAAgB,EAAoB,MAAM,YAAY,CAAC;AAClI,OAAO,EAAE,WAAW,EAAoB,MAAM,aAAa,CAAC;AAC5D,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAExC,MAAM,OAAO,GAAG,OAAO,CAAC;AAExB,MAAM,KAAK,GAAG,OAAO,CAAC,MAAM,CAAC,KAAK,KAAK,IAAI,IAAI,CAAC,OAAO,CAA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CAAC,CAAC;AACrH,CAAC;AAED,KAAK,UAAU,IAAI;IACjB,MAAM,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAC7C,QAAQ,GAAG,EAAE,CAAC;QACZ,KAAK,MAAM;YACT,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC;QACvB,KAAK,SAAS,CAAC;QACf,KAAK,IAAI;YACP,OAAO,UAAU,CAAC,IAAI,CAAC,CAAC;QAC1B,KAAK,QAAQ;YACX,OAAO,SAAS,EAAE,CAAC;QACrB,KAAK,MAAM;YACT,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC;QACvB,KAAK,WAAW,CAAC;QACjB,KAAK,IAAI,CAAC;QACV,KAAK,SAAS;YACZ,OAAO,GAAG,CAAC,OAAO,CAAC,CAAC;QACtB,KAAK,SAAS,CAAC;QACf,KAAK,MAAM,CAAC;QACZ,KAAK,QAAQ,CAAC;QACd,KAAK,IAAI;YACP,OAAO,IAAI,EAAE,CAAC;QAChB;YACE,IAAI,CAAC,oBAAoB,GAAG,oBAAoB,CAAC,CAAC;IACtD,CAAC;AACH,CAAC;AAED,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,GAAY,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC"}
|
package/dist/guard.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { HeaderMap } from './protocol.js';
|
|
2
|
+
export declare class GuardError extends Error {
|
|
3
|
+
readonly status: number;
|
|
4
|
+
constructor(message: string, status?: number);
|
|
5
|
+
}
|
|
6
|
+
export interface Target {
|
|
7
|
+
/** Address actually dialled (never a hostname needing DNS). */
|
|
8
|
+
host: string;
|
|
9
|
+
port: number;
|
|
10
|
+
/** `host:port` as sent in the Host header, IPv6 bracketed. */
|
|
11
|
+
authority: string;
|
|
12
|
+
}
|
|
13
|
+
export declare function isLoopback(ip: string): boolean;
|
|
14
|
+
/** RFC 1918 IPv4 and RFC 4193 IPv6 (fc00::/7). Link-local (169.254/16, fe80::/10) is excluded: it hosts cloud metadata endpoints. */
|
|
15
|
+
export declare function isPrivate(ip: string): boolean;
|
|
16
|
+
/** Validate and lock the forwarding target. Throws GuardError for anything not loopback/private. */
|
|
17
|
+
export declare function lockTarget(hostInput: string, portInput: number | string): Target;
|
|
18
|
+
/**
|
|
19
|
+
* Validate a relay-supplied request path. It must be origin-form (`/…`), so it can never
|
|
20
|
+
* name another host: no scheme, no `//authority`, no backslashes, no control characters.
|
|
21
|
+
*/
|
|
22
|
+
export declare function safePath(path: unknown): string;
|
|
23
|
+
export declare function safeMethod(method: unknown): string;
|
|
24
|
+
/** Build the local WebSocket URL from the locked target, and prove the authority did not change. */
|
|
25
|
+
export declare function targetWsUrl(target: Target, path: string): string;
|
|
26
|
+
/**
|
|
27
|
+
* Sanitise relay-supplied request headers before they reach the local server: drop
|
|
28
|
+
* hop-by-hop, `X-Forwarded-*`, `X-Real-IP`, `Forwarded`; drop malformed names/values;
|
|
29
|
+
* force `Host: <host>:<port>` and add `X-Servlink-Verified: 1`.
|
|
30
|
+
*/
|
|
31
|
+
export declare function sanitizeHeaders(input: HeaderMap | undefined, target: Target, opts?: {
|
|
32
|
+
websocket?: boolean;
|
|
33
|
+
}): Record<string, string | string[]>;
|
|
34
|
+
/** Token bucket: refills `ratePerSec` tokens/s up to `burst`. */
|
|
35
|
+
export declare class TokenBucket {
|
|
36
|
+
readonly ratePerSec: number;
|
|
37
|
+
readonly burst: number;
|
|
38
|
+
private tokens;
|
|
39
|
+
private last;
|
|
40
|
+
constructor(ratePerSec: number, burst?: number);
|
|
41
|
+
take(now?: number): boolean;
|
|
42
|
+
}
|
|
43
|
+
/** Upper bound of decoded bytes for a base64 string, computed without decoding it. */
|
|
44
|
+
export declare function base64DecodedSize(b64: string): number;
|