@rubriclab/bunl 0.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/.dockerignore +2 -0
- package/Dockerfile +15 -0
- package/LICENSE +21 -0
- package/README.md +27 -0
- package/build/client.js +53 -0
- package/bun.lockb +0 -0
- package/client.ts +69 -0
- package/demo.ts +18 -0
- package/fly.toml +19 -0
- package/index.ts +67 -0
- package/package.json +39 -0
- package/server.ts +82 -0
- package/tsconfig.json +29 -0
- package/utils.ts +6 -0
package/.dockerignore
ADDED
package/Dockerfile
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015 Roman Shtylman
|
|
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
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# bunl
|
|
2
|
+
|
|
3
|
+
## A Bun WebSocket re-write of LocalTunnel
|
|
4
|
+
|
|
5
|
+
To install dependencies:
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun i
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
To run the client:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
bun lt
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
To run the server:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
bun server
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
To run a dummy server on localhost:3000:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
bun demo
|
|
27
|
+
```
|
package/build/client.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// client.ts
|
|
3
|
+
import {parseArgs} from "util";
|
|
4
|
+
async function main({
|
|
5
|
+
url,
|
|
6
|
+
domain,
|
|
7
|
+
subdomain
|
|
8
|
+
}) {
|
|
9
|
+
const serverUrl = `ws://${domain || "localhost:1234"}?new${subdomain ? `&subdomain=${subdomain}` : ""}`;
|
|
10
|
+
const socket = new WebSocket(serverUrl);
|
|
11
|
+
socket.addEventListener("message", (event) => {
|
|
12
|
+
const data = JSON.parse(event.data);
|
|
13
|
+
console.log("message:", data);
|
|
14
|
+
if (data.method) {
|
|
15
|
+
fetch(`${domain}${data.path}`, {
|
|
16
|
+
method: data.method,
|
|
17
|
+
headers: data.headers
|
|
18
|
+
}).then((res) => res.text()).then((res) => {
|
|
19
|
+
socket.send(res);
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
socket.addEventListener("open", (event) => {
|
|
24
|
+
console.log("socket ready:", !!event.target.readyState);
|
|
25
|
+
socket.ping();
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
var { values } = parseArgs({
|
|
29
|
+
args: Bun.argv,
|
|
30
|
+
options: {
|
|
31
|
+
port: {
|
|
32
|
+
type: "string",
|
|
33
|
+
required: true,
|
|
34
|
+
short: "p"
|
|
35
|
+
},
|
|
36
|
+
domain: {
|
|
37
|
+
type: "string",
|
|
38
|
+
short: "d"
|
|
39
|
+
},
|
|
40
|
+
subdomain: {
|
|
41
|
+
type: "string",
|
|
42
|
+
short: "s"
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
allowPositionals: true
|
|
46
|
+
});
|
|
47
|
+
if (!values.port)
|
|
48
|
+
throw "pass --port 3000";
|
|
49
|
+
main({
|
|
50
|
+
url: `localhost:${values.port}`,
|
|
51
|
+
domain: values.domain,
|
|
52
|
+
subdomain: values.subdomain
|
|
53
|
+
});
|
package/bun.lockb
ADDED
|
Binary file
|
package/client.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { parseArgs } from "util";
|
|
2
|
+
|
|
3
|
+
async function main({
|
|
4
|
+
url,
|
|
5
|
+
domain,
|
|
6
|
+
subdomain,
|
|
7
|
+
}: {
|
|
8
|
+
url: string;
|
|
9
|
+
domain?: string;
|
|
10
|
+
subdomain?: string;
|
|
11
|
+
}) {
|
|
12
|
+
const serverUrl = `ws://${domain || "localhost:1234"}?new${
|
|
13
|
+
subdomain ? `&subdomain=${subdomain}` : ""
|
|
14
|
+
}`;
|
|
15
|
+
const socket = new WebSocket(serverUrl);
|
|
16
|
+
|
|
17
|
+
socket.addEventListener("message", (event) => {
|
|
18
|
+
const data = JSON.parse(event.data as string);
|
|
19
|
+
console.log("message:", data);
|
|
20
|
+
|
|
21
|
+
if (data.method) {
|
|
22
|
+
fetch(`${domain}${data.path}`, {
|
|
23
|
+
method: data.method,
|
|
24
|
+
headers: data.headers,
|
|
25
|
+
})
|
|
26
|
+
.then((res) => res.text())
|
|
27
|
+
.then((res) => {
|
|
28
|
+
socket.send(res);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
socket.addEventListener("open", (event) => {
|
|
34
|
+
console.log("socket ready:", !!(event.target as any).readyState);
|
|
35
|
+
socket.ping();
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Eg. `bun client.ts -p 3000 -d tunnel.example.so -s my-subdomain`
|
|
41
|
+
* > my-subdomain.tunnel.example.so will be proxied to localhost:3000
|
|
42
|
+
*/
|
|
43
|
+
const { values } = parseArgs({
|
|
44
|
+
args: Bun.argv,
|
|
45
|
+
options: {
|
|
46
|
+
port: {
|
|
47
|
+
type: "string",
|
|
48
|
+
required: true,
|
|
49
|
+
short: "p",
|
|
50
|
+
},
|
|
51
|
+
domain: {
|
|
52
|
+
type: "string",
|
|
53
|
+
short: "d",
|
|
54
|
+
},
|
|
55
|
+
subdomain: {
|
|
56
|
+
type: "string",
|
|
57
|
+
short: "s",
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
allowPositionals: true,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
if (!values.port) throw "pass --port 3000";
|
|
64
|
+
|
|
65
|
+
main({
|
|
66
|
+
url: `localhost:${values.port}`,
|
|
67
|
+
domain: values.domain,
|
|
68
|
+
subdomain: values.subdomain,
|
|
69
|
+
});
|
package/demo.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { serve } from "bun";
|
|
2
|
+
|
|
3
|
+
const port = 3000;
|
|
4
|
+
|
|
5
|
+
serve({
|
|
6
|
+
port,
|
|
7
|
+
fetch: async (req) => {
|
|
8
|
+
const url = new URL(req.url);
|
|
9
|
+
console.log(req.method, url.pathname, url.search);
|
|
10
|
+
|
|
11
|
+
const bod = await req.text();
|
|
12
|
+
bod && console.log("Body: ", bod);
|
|
13
|
+
|
|
14
|
+
return new Response(`hello from localhost:${port}`);
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
console.log(`Serving app at http://localhost:${port}`);
|
package/fly.toml
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
|
|
2
|
+
|
|
3
|
+
app = 'tunnel'
|
|
4
|
+
primary_region = 'yul'
|
|
5
|
+
|
|
6
|
+
[build]
|
|
7
|
+
|
|
8
|
+
[http_service]
|
|
9
|
+
internal_port = 1234
|
|
10
|
+
force_https = false
|
|
11
|
+
auto_stop_machines = true
|
|
12
|
+
auto_start_machines = true
|
|
13
|
+
min_machines_running = 0
|
|
14
|
+
processes = ['app']
|
|
15
|
+
|
|
16
|
+
[[vm]]
|
|
17
|
+
memory = '1gb'
|
|
18
|
+
cpu_kind = 'shared'
|
|
19
|
+
cpus = 1
|
package/index.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import "localenv";
|
|
2
|
+
import optimist from "optimist";
|
|
3
|
+
import Debug from "debug";
|
|
4
|
+
import { CreateServer } from "./server";
|
|
5
|
+
import { type AddressInfo } from "ws";
|
|
6
|
+
|
|
7
|
+
const debug = Debug("localtunnel");
|
|
8
|
+
|
|
9
|
+
const argv = optimist
|
|
10
|
+
.usage("Usage: $0 --port [num]")
|
|
11
|
+
.options("secure", {
|
|
12
|
+
default: false,
|
|
13
|
+
describe: "use this flag to indicate proxy over https",
|
|
14
|
+
})
|
|
15
|
+
.options("port", {
|
|
16
|
+
default: "80",
|
|
17
|
+
describe: "listen on this port for outside requests",
|
|
18
|
+
})
|
|
19
|
+
.options("address", {
|
|
20
|
+
default: "0.0.0.0",
|
|
21
|
+
describe: "IP address to bind to",
|
|
22
|
+
})
|
|
23
|
+
.options("domain", {
|
|
24
|
+
describe:
|
|
25
|
+
"Specify the base domain name. This is optional if hosting localtunnel from a regular example.com domain. This is required if hosting a localtunnel server from a subdomain (i.e. lt.example.dom where clients will be client-app.lt.example.come)",
|
|
26
|
+
})
|
|
27
|
+
.options("max-sockets", {
|
|
28
|
+
default: 10,
|
|
29
|
+
describe:
|
|
30
|
+
"maximum number of tcp sockets each client is allowed to establish at one time (the tunnels)",
|
|
31
|
+
}).argv;
|
|
32
|
+
|
|
33
|
+
if (argv.help) {
|
|
34
|
+
console.log("Showing help");
|
|
35
|
+
optimist.showHelp();
|
|
36
|
+
process.exit();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const server = CreateServer({
|
|
40
|
+
max_tcp_sockets: argv["max-sockets"],
|
|
41
|
+
secure: argv.secure,
|
|
42
|
+
domain: argv.domain,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
server.listen(argv.port, argv.address, () => {
|
|
46
|
+
debug("server listening on port: %d", (server.address() as AddressInfo).port);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
process.on("SIGINT", () => {
|
|
50
|
+
console.error("SIGINT");
|
|
51
|
+
process.exit();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
process.on("SIGTERM", () => {
|
|
55
|
+
console.warn("SIGTERM");
|
|
56
|
+
// process.exit();
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
process.on("uncaughtException", (err) => {
|
|
60
|
+
console.error("err:");
|
|
61
|
+
console.error(err);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
process.on("unhandledRejection", (reason, promise) => {
|
|
65
|
+
console.error("reason:");
|
|
66
|
+
console.error(reason);
|
|
67
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"bin": {
|
|
3
|
+
"bunl": "./client.js"
|
|
4
|
+
},
|
|
5
|
+
"name": "@rubriclab/bunl",
|
|
6
|
+
"description": "expose localhost to the world",
|
|
7
|
+
"version": "0.0.0",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/RubricLab/bunl.git"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"human-id": "^4.1.1"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@types/bun": "latest"
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"server": "bun --hot server.ts",
|
|
21
|
+
"lt": "bun --hot client.ts",
|
|
22
|
+
"demo": "bun --hot demo.ts"
|
|
23
|
+
},
|
|
24
|
+
"type": "module",
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"typescript": "^5.0.0"
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"@RubricLab:registry": "https://registry.npmjs.org",
|
|
30
|
+
"access": "public",
|
|
31
|
+
"registry": "https://registry.npmjs.org"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://github.com/RubricLab/bunl#readme",
|
|
34
|
+
"keywords": [
|
|
35
|
+
"local",
|
|
36
|
+
"tunnel",
|
|
37
|
+
"rubric"
|
|
38
|
+
]
|
|
39
|
+
}
|
package/server.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { serve, sleep, type ServerWebSocket } from "bun";
|
|
2
|
+
import { uid } from "./utils";
|
|
3
|
+
|
|
4
|
+
type Client = { id: string };
|
|
5
|
+
|
|
6
|
+
const port = Bun.env.PORT || 1234;
|
|
7
|
+
const protocol = "http";
|
|
8
|
+
const domain = Bun.env.DOMAIN || `localhost:${port}`;
|
|
9
|
+
|
|
10
|
+
const clients = new Map<string, ServerWebSocket<Client>>();
|
|
11
|
+
const clientData = new Map<string, any>();
|
|
12
|
+
|
|
13
|
+
serve<Client>({
|
|
14
|
+
port,
|
|
15
|
+
fetch: async (req, server) => {
|
|
16
|
+
const reqUrl = new URL(req.url);
|
|
17
|
+
|
|
18
|
+
if (reqUrl.searchParams.has("new")) {
|
|
19
|
+
const id = reqUrl.searchParams.get("subdomain") || uid();
|
|
20
|
+
const upgraded = server.upgrade(req, { data: { id } });
|
|
21
|
+
if (upgraded) return;
|
|
22
|
+
else return new Response("Upgrade failed :(", { status: 500 });
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const subdomain = reqUrl.hostname.split(".")[0];
|
|
26
|
+
|
|
27
|
+
if (!clients.has(subdomain)) {
|
|
28
|
+
console.log(`\x1b[31m${subdomain} not found \x1b[0m`);
|
|
29
|
+
return new Response("client not found :(", { status: 404 });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// The magic: forward the req to the client
|
|
33
|
+
const client = clients.get(subdomain)!;
|
|
34
|
+
const { method, url, headers } = req;
|
|
35
|
+
const path = new URL(url).pathname;
|
|
36
|
+
client.send(JSON.stringify({ method, path, headers }));
|
|
37
|
+
|
|
38
|
+
// Wait for the client to cache its response above
|
|
39
|
+
await sleep(1);
|
|
40
|
+
|
|
41
|
+
let retries = 5;
|
|
42
|
+
let res = clientData.get(subdomain);
|
|
43
|
+
|
|
44
|
+
// Poll every second for the client to respond
|
|
45
|
+
// TODO: replace this with a way for the client to trigger this
|
|
46
|
+
while (!res) {
|
|
47
|
+
await sleep(1000);
|
|
48
|
+
retries--;
|
|
49
|
+
|
|
50
|
+
res = clientData.get(subdomain);
|
|
51
|
+
|
|
52
|
+
if (retries < 1) {
|
|
53
|
+
console.log(`\x1b[31m${subdomain} not responding \x1b[0m`);
|
|
54
|
+
return new Response("client not responding :(", { status: 500 });
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return new Response(res);
|
|
59
|
+
},
|
|
60
|
+
websocket: {
|
|
61
|
+
open(ws) {
|
|
62
|
+
console.log("connecting to", ws.data.id);
|
|
63
|
+
clients.set(ws.data.id, ws);
|
|
64
|
+
ws.send(
|
|
65
|
+
JSON.stringify({
|
|
66
|
+
id: ws.data.id,
|
|
67
|
+
url: `${protocol}://${ws.data.id}.${domain}`,
|
|
68
|
+
})
|
|
69
|
+
);
|
|
70
|
+
},
|
|
71
|
+
message(ws, message) {
|
|
72
|
+
console.log("message from", ws.data.id);
|
|
73
|
+
clientData.set(ws.data.id, message);
|
|
74
|
+
},
|
|
75
|
+
close(ws) {
|
|
76
|
+
console.log(`closing ${ws.data.id}`);
|
|
77
|
+
clients.delete(ws.data.id);
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
console.log(`Websocket server up at ws://${domain}`);
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
// Enable latest features
|
|
4
|
+
"lib": ["esnext"],
|
|
5
|
+
"target": "esnext",
|
|
6
|
+
"module": "esnext",
|
|
7
|
+
"moduleDetection": "force",
|
|
8
|
+
"jsx": "react-jsx",
|
|
9
|
+
"allowJs": true,
|
|
10
|
+
"types": ["bun-types"],
|
|
11
|
+
|
|
12
|
+
// Bundler mode
|
|
13
|
+
"moduleResolution": "bundler",
|
|
14
|
+
"allowImportingTsExtensions": true,
|
|
15
|
+
"verbatimModuleSyntax": true,
|
|
16
|
+
"noEmit": true,
|
|
17
|
+
"declaration": true,
|
|
18
|
+
|
|
19
|
+
// Best practices
|
|
20
|
+
"strict": true,
|
|
21
|
+
"skipLibCheck": true,
|
|
22
|
+
"noFallthroughCasesInSwitch": true,
|
|
23
|
+
|
|
24
|
+
// Some stricter flags (disabled by default)
|
|
25
|
+
"noUnusedLocals": false,
|
|
26
|
+
"noUnusedParameters": false,
|
|
27
|
+
"noPropertyAccessFromIndexSignature": false
|
|
28
|
+
}
|
|
29
|
+
}
|