Compare commits
2 Commits
bf4a803dd3
...
bedc06c41b
| Author | SHA1 | Date | |
|---|---|---|---|
| bedc06c41b | |||
| db002b861a |
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@because/sneaker",
|
||||
"version": "0.0.2",
|
||||
"version": "0.0.3",
|
||||
"description": "http tunnel server and client",
|
||||
"module": "src/client.ts",
|
||||
"type": "module",
|
||||
|
|
|
|||
|
|
@ -111,9 +111,7 @@ export function connect(options: TunnelOptions): Tunnel {
|
|||
async function proxy(req: TunnelRequest): Promise<TunnelResponse> {
|
||||
try {
|
||||
const url = `${target}${req.path}`
|
||||
const hasBody = req.method !== "GET" && req.method !== "HEAD" && req.body
|
||||
|
||||
console.log(`[tunnel] >> ${req.method} ${url}`, { contentType: req.headers["content-type"], bodyLen: req.body?.length, hasBody })
|
||||
const hasBody = req.method !== "GET" && req.method !== "HEAD"
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: req.method,
|
||||
|
|
@ -123,7 +121,6 @@ export function connect(options: TunnelOptions): Tunnel {
|
|||
})
|
||||
|
||||
const contentType = response.headers.get("content-type")
|
||||
console.log(`[tunnel] << ${response.status} ${req.method} ${url}`, { contentType, location: response.headers.get("location") })
|
||||
const headers: Record<string, string> = {}
|
||||
response.headers.forEach((value, key) => {
|
||||
headers[key] = value
|
||||
|
|
|
|||
|
|
@ -64,11 +64,9 @@ app.get("/tunnel", c => {
|
|||
return upgradeWebSocket(c, {
|
||||
async onOpen(_event, ws) {
|
||||
connections[name] = { app, ws }
|
||||
console.log(`connection opened: ${name} -> ${app}`)
|
||||
send(ws, { subdomain: name })
|
||||
},
|
||||
onClose: (_event, _ws) => {
|
||||
console.log("connection closed:", name)
|
||||
delete connections[name]
|
||||
for (const [id, entry] of pending) {
|
||||
if (entry.subdomain === name) {
|
||||
|
|
@ -102,13 +100,15 @@ app.all("*", async c => {
|
|||
return c.text("Bad Gateway", 502)
|
||||
|
||||
const id = randomID()
|
||||
const headers = Object.fromEntries(c.req.raw.headers)
|
||||
const raw = c.req.raw
|
||||
const headers: Record<string, string> = {}
|
||||
raw.headers.forEach((value, key) => { headers[key] = value })
|
||||
delete headers['host']
|
||||
delete headers['connection']
|
||||
delete headers['keep-alive']
|
||||
delete headers['transfer-encoding']
|
||||
delete headers['content-length']
|
||||
const body = await c.req.text()
|
||||
const body = await raw.text()
|
||||
const app = connection.app
|
||||
|
||||
const result = await new Promise<Response>((resolve, reject) => {
|
||||
|
|
@ -146,7 +146,6 @@ app.all("*", async c => {
|
|||
})
|
||||
|
||||
function send(connection: any, msg: Request | Success) {
|
||||
console.log("sending", 'id' in msg ? `${msg.id} ${msg.method} ${msg.path}` : `connected: ${msg.subdomain}`)
|
||||
connection.send(JSON.stringify(msg))
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user