@easytunnel/et-sdk 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +46 -2
- package/etunnel.js +137 -0
- package/package.json +38 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 etunnel authors
|
|
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,47 @@
|
|
|
1
|
-
#
|
|
1
|
+
# etunnel for Node.js
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Expose a local service through an etunnel reverse-tunnel server. Zero dependencies (Node >= 22 built-in WebSocket).
|
|
4
|
+
|
|
5
|
+
Zero registration: every SDK client speaks the same tiny wire protocol
|
|
6
|
+
([PROTOCOL.md](../../PROTOCOL.md)) against any etunnel server
|
|
7
|
+
(default `wss://t.ggcode.uk/tunnel`).
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
npm install @easytunnel/et-sdk
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Requires Node >= 22.
|
|
16
|
+
|
|
17
|
+
## CLI
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
npx @easytunnel/et-sdk 3000
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Prints `TUNNEL_URL: https://<subdomain>.t.ggcode.uk` — that URL now routes to
|
|
24
|
+
your local service. Ctrl-C closes the tunnel.
|
|
25
|
+
|
|
26
|
+
## Embed
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
const { connect } = require('@easytunnel/et-sdk');
|
|
30
|
+
const t = await connect('wss://t.ggcode.uk/tunnel', 3000);
|
|
31
|
+
console.log(t.url);
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Environment variables
|
|
35
|
+
|
|
36
|
+
| Variable | Meaning | Default |
|
|
37
|
+
|---|---|---|
|
|
38
|
+
| `ETUNNEL_SERVER` | tunnel server WebSocket URL | `wss://t.ggcode.uk/tunnel` |
|
|
39
|
+
| `ETUNNEL_LOCAL` | local port or `host:port` (or 1st CLI arg) | `3000` |
|
|
40
|
+
| `ETUNNEL_TOKEN` | paid-plan API key (optional) | - |
|
|
41
|
+
| `ETUNNEL_SUBDOMAIN` | custom subdomain (paid plans) | random |
|
|
42
|
+
|
|
43
|
+
## License
|
|
44
|
+
|
|
45
|
+
MIT — see [LICENSE](../../LICENSE). Buffer-based protocol client: request and
|
|
46
|
+
response bodies are buffered per stream (fine for webhooks/dashboards/demos;
|
|
47
|
+
use the [Go SDK](../../sdk) for streaming and WebSocket pass-through).
|
package/etunnel.js
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// etunnel SDK for Node.js (>= 22, built-in WebSocket, zero dependencies).
|
|
3
|
+
// Buffer-based protocol client: request/response bodies are buffered per
|
|
4
|
+
// stream (fine for typical webhooks and demos; WebSocket upgrades are not
|
|
5
|
+
// forwarded — use the Go SDK for full streaming/upgrades).
|
|
6
|
+
//
|
|
7
|
+
// const { connect } = require('./etunnel');
|
|
8
|
+
// const t = await connect('wss://t.ggcode.uk/tunnel', 3000);
|
|
9
|
+
// console.log(t.url);
|
|
10
|
+
|
|
11
|
+
const MAXP = 32768;
|
|
12
|
+
|
|
13
|
+
function frame(kind, id, payload) {
|
|
14
|
+
const p = payload || Buffer.alloc(0);
|
|
15
|
+
const b = Buffer.alloc(9 + p.length);
|
|
16
|
+
b.writeUInt8(kind, 0);
|
|
17
|
+
b.writeUInt32BE(id, 1);
|
|
18
|
+
b.writeUInt32BE(p.length, 5);
|
|
19
|
+
p.copy(b, 9);
|
|
20
|
+
return b;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const HOP = new Set(['connection', 'proxy-connection', 'keep-alive', 'proxy-authenticate', 'proxy-authorization', 'te', 'trailer', 'transfer-encoding', 'upgrade']);
|
|
24
|
+
|
|
25
|
+
function pickHostPort(local) {
|
|
26
|
+
if (/^\d+$/.test(String(local))) return { host: '127.0.0.1', port: Number(local) };
|
|
27
|
+
const m = /^(\[[^\]]+\]|[^:]+):(\d+)$/.exec(String(local));
|
|
28
|
+
if (m) return { host: m[1].replace(/^[\[\]]|$/g, ''), port: Number(m[2]) };
|
|
29
|
+
return { host: String(local), port: 80 };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function connect(server, local, opts = {}) {
|
|
33
|
+
const { host, port } = pickHostPort(local);
|
|
34
|
+
const ws = new WebSocket(server, 'etunnel-v1');
|
|
35
|
+
ws.binaryType = 'arraybuffer';
|
|
36
|
+
|
|
37
|
+
await new Promise((res, rej) => {
|
|
38
|
+
ws.onopen = res;
|
|
39
|
+
ws.onerror = (e) => rej(new Error('ws: ' + (e.message || 'connect failed')));
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
ws.send(JSON.stringify({ type: 'hello', proto: 1, subdomain: opts.subdomain || undefined, token: opts.token || undefined, client: opts.client || 'etunnel-node/1.0' }));
|
|
43
|
+
|
|
44
|
+
const info = await new Promise((res, rej) => {
|
|
45
|
+
const to = setTimeout(() => rej(new Error('welcome timeout')), 15000);
|
|
46
|
+
ws.onmessage = (ev) => {
|
|
47
|
+
const m = JSON.parse(typeof ev.data === 'string' ? ev.data : Buffer.from(ev.data).toString());
|
|
48
|
+
if (m.type === 'welcome') { clearTimeout(to); res(m); }
|
|
49
|
+
else if (m.type === 'error') { clearTimeout(to); rej(Object.assign(new Error(m.code + ': ' + (m.message || '')), { code: m.code })); }
|
|
50
|
+
};
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const state = { url: info.url, closed: false, streams: new Map() };
|
|
54
|
+
const send = (o) => ws.send(typeof o === 'string' ? o : o);
|
|
55
|
+
const sendBin = (buf) => ws.send(buf);
|
|
56
|
+
|
|
57
|
+
ws.onmessage = (ev) => {
|
|
58
|
+
if (typeof ev.data === 'string') {
|
|
59
|
+
const m = JSON.parse(ev.data);
|
|
60
|
+
if (m.type === 'req') {
|
|
61
|
+
state.streams.set(m.streamId, { req: m, body: [] });
|
|
62
|
+
} else if (m.type === 'ping') {
|
|
63
|
+
send(JSON.stringify({ type: 'pong', ts: m.ts }));
|
|
64
|
+
} else if (m.type === 'bye') {
|
|
65
|
+
state.closed = true;
|
|
66
|
+
if (opts.onClose) opts.onClose(m.reason);
|
|
67
|
+
try { ws.close(); } catch {}
|
|
68
|
+
}
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const b = Buffer.from(ev.data);
|
|
72
|
+
const kind = b.readUInt8(0);
|
|
73
|
+
const id = b.readUInt32BE(1);
|
|
74
|
+
const payload = b.subarray(9, 9 + b.readUInt32BE(5));
|
|
75
|
+
const st = state.streams.get(id);
|
|
76
|
+
if (!st) return;
|
|
77
|
+
if (kind === 0x01) st.body.push(payload);
|
|
78
|
+
else if (kind === 0x02) { state.streams.delete(id); handle(st, id).catch(() => trySend(frame(0x03, id))); }
|
|
79
|
+
else if (kind === 0x03) state.streams.delete(id);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
async function handle(st, id) {
|
|
83
|
+
const body = Buffer.concat(st.body);
|
|
84
|
+
const headers = {};
|
|
85
|
+
let isHop = false;
|
|
86
|
+
for (const [k, vs] of Object.entries(st.req.headers || {})) {
|
|
87
|
+
const lk = k.toLowerCase();
|
|
88
|
+
if (HOP.has(lk)) { if (lk === 'connection' && /upgrade/i.test(String(vs))) isHop = true; continue; }
|
|
89
|
+
headers[k] = vs.join(', ');
|
|
90
|
+
}
|
|
91
|
+
let resp;
|
|
92
|
+
try {
|
|
93
|
+
resp = await fetch(`http://${host}:${port}${st.req.path}`, {
|
|
94
|
+
method: st.req.method,
|
|
95
|
+
headers,
|
|
96
|
+
body: ['GET', 'HEAD'].includes(st.req.method) ? undefined : body,
|
|
97
|
+
});
|
|
98
|
+
} catch (e) {
|
|
99
|
+
send(JSON.stringify({ type: 'res', streamId: id, status: 502, headers: { 'Content-Type': ['text/plain'] } }));
|
|
100
|
+
sendBin(frame(0x01, id, Buffer.from('etunnel: local service error: ' + e.message + '\n')));
|
|
101
|
+
sendBin(frame(0x02, id));
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
const rh = {};
|
|
105
|
+
resp.headers.forEach((v, k) => { const lk = k.toLowerCase(); if (!HOP.has(lk)) (rh[k] = rh[k] || []).push(v); });
|
|
106
|
+
send(JSON.stringify({ type: 'res', streamId: id, status: resp.status, headers: rh }));
|
|
107
|
+
const buf = Buffer.from(await resp.arrayBuffer());
|
|
108
|
+
for (let i = 0; i < buf.length; i += MAXP) sendBin(frame(0x01, id, buf.subarray(i, Math.min(i + MAXP, buf.length))));
|
|
109
|
+
sendBin(frame(0x02, id));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function trySend(buf) { try { ws.send(buf); } catch {} }
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
url: info.url,
|
|
116
|
+
hostname: info.hostname,
|
|
117
|
+
subdomain: info.subdomain,
|
|
118
|
+
plan: info.plan || '',
|
|
119
|
+
close(reason = 'client closed') {
|
|
120
|
+
if (state.closed) return;
|
|
121
|
+
state.closed = true;
|
|
122
|
+
try { send(JSON.stringify({ type: 'bye', reason })); } catch {}
|
|
123
|
+
try { ws.close(); } catch {}
|
|
124
|
+
if (opts.onClose) opts.onClose(reason);
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
module.exports = { connect };
|
|
130
|
+
|
|
131
|
+
if (require.main === module) {
|
|
132
|
+
const server = process.env.ETUNNEL_SERVER || 'wss://t.ggcode.uk/tunnel';
|
|
133
|
+
const local = process.env.ETUNNEL_LOCAL || process.argv[2] || '3000';
|
|
134
|
+
connect(server, local, { token: process.env.ETUNNEL_TOKEN, subdomain: process.env.ETUNNEL_SUBDOMAIN })
|
|
135
|
+
.then((t) => { console.log('TUNNEL_URL: ' + t.url); })
|
|
136
|
+
.catch((e) => { console.error('etunnel:', e.message); process.exit(1); });
|
|
137
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@easytunnel/et-sdk",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Expose a local service through an etunnel reverse-tunnel server with one line of code. Zero dependencies, Node >= 22.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/topcheer/etunnel.git",
|
|
9
|
+
"directory": "sdks/node"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/topcheer/etunnel#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/topcheer/etunnel/issues"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"tunnel",
|
|
17
|
+
"reverse-proxy",
|
|
18
|
+
"webhook",
|
|
19
|
+
"localhost",
|
|
20
|
+
"expose",
|
|
21
|
+
"ngrok",
|
|
22
|
+
"easytunnel"
|
|
23
|
+
],
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=22"
|
|
26
|
+
},
|
|
27
|
+
"main": "etunnel.js",
|
|
28
|
+
"bin": {
|
|
29
|
+
"etunnel": "etunnel.js"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"etunnel.js",
|
|
33
|
+
"README.md",
|
|
34
|
+
"LICENSE"
|
|
35
|
+
],
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public",
|
|
38
|
+
"provenance": true
|
|
39
|
+
}
|
|
40
|
+
}
|