forked from localtunnel/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.ts
More file actions
143 lines (123 loc) · 3.63 KB
/
client.ts
File metadata and controls
143 lines (123 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { parseArgs } from 'node:util'
import type { TunnelInit, TunnelRequest, TunnelResponse } from './types'
import { fromBase64, openBrowser, page, toBase64 } from './utils'
function badGatewayHtml(port: string) {
return page(
'Bad Gateway',
`<h1>Bad Gateway</h1>
<p>Could not reach <code>localhost:${port}</code>.</p>
<p>Make sure your local server is running.</p>`
)
}
async function main({
port,
domain,
subdomain,
open
}: {
port?: string
domain?: string
subdomain?: string
open?: boolean
}) {
const params = new URLSearchParams({
new: '',
...(subdomain ? { subdomain } : {})
}).toString()
const isLocal = /^(localhost|127\.|0\.0\.0\.0|\[::1\])/.test(domain || '')
const wsScheme = isLocal ? 'ws' : 'wss'
const serverUrl = `${wsScheme}://${domain}?${params}`
const socket = new WebSocket(serverUrl)
const localHost = Bun.env.HOST || 'localhost'
const localOrigin = `http://${localHost}:${port}`
socket.addEventListener('message', async event => {
const data = JSON.parse(event.data as string)
if (data.type === 'init') {
const init = data as TunnelInit
console.log(`\n↪ Your URL: \x1b[32m${init.url}\x1b[0m\n`)
if (open) openBrowser(init.url)
return
}
if (data.type === 'request') {
const req = data as TunnelRequest
const now = performance.now()
try {
const reqBody =
req.body && req.method !== 'GET' && req.method !== 'HEAD' ? fromBase64(req.body) : null
const fwdHeaders = { ...req.headers }
delete fwdHeaders.host
delete fwdHeaders.connection
delete fwdHeaders['transfer-encoding']
const res = await fetch(`${localOrigin}${req.pathname}`, {
body: reqBody,
headers: fwdHeaders,
method: req.method
})
const elapsed = (performance.now() - now).toFixed(1)
console.log(`\x1b[32m${req.method}\x1b[0m ${req.pathname} → ${res.status} (${elapsed}ms)`)
const resBody = await res.arrayBuffer()
const headers: Record<string, string> = {}
res.headers.forEach((v, k) => {
headers[k] = v
})
const response: TunnelResponse = {
body: toBase64(resBody),
headers,
id: req.id,
status: res.status,
statusText: res.statusText,
type: 'response'
}
socket.send(JSON.stringify(response))
} catch (err) {
console.error(`\x1b[31mERR\x1b[0m ${req.method} ${req.pathname}: ${err}`)
const html = badGatewayHtml(port || '3000')
const response: TunnelResponse = {
body: toBase64(new TextEncoder().encode(html).buffer),
headers: { 'content-type': 'text/html; charset=utf-8' },
id: req.id,
status: 502,
statusText: 'Bad Gateway',
type: 'response'
}
socket.send(JSON.stringify(response))
}
}
})
socket.addEventListener('open', () => {
console.log(`Connected to ${serverUrl}`)
})
socket.addEventListener('close', () => {
console.warn('Server closed connection')
process.exit(1)
})
socket.addEventListener('error', err => {
console.error('WebSocket error:', err)
process.exit(1)
})
}
const { values } = parseArgs({
allowPositionals: true,
args: process.argv,
options: {
domain: { default: 'bunl.sh', short: 'd', type: 'string' },
open: { short: 'o', type: 'boolean' },
port: { default: '3000', short: 'p', type: 'string' },
subdomain: { short: 's', type: 'string' },
version: { short: 'v', type: 'boolean' }
}
})
if (values.version) {
const pkg = await Bun.file(new URL('./package.json', import.meta.url)).json()
console.log(pkg.version)
process.exit()
}
main({
domain: values.domain || 'bunl.sh',
open: values.open || false,
port: values.port || '3000',
subdomain: values.subdomain || ''
}).catch(err => {
console.error(err)
process.exit(1)
})