fix proxy
This commit is contained in:
parent
bf4a803dd3
commit
db002b861a
|
|
@ -111,9 +111,7 @@ export function connect(options: TunnelOptions): Tunnel {
|
||||||
async function proxy(req: TunnelRequest): Promise<TunnelResponse> {
|
async function proxy(req: TunnelRequest): Promise<TunnelResponse> {
|
||||||
try {
|
try {
|
||||||
const url = `${target}${req.path}`
|
const url = `${target}${req.path}`
|
||||||
const hasBody = req.method !== "GET" && req.method !== "HEAD" && req.body
|
const hasBody = req.method !== "GET" && req.method !== "HEAD"
|
||||||
|
|
||||||
console.log(`[tunnel] >> ${req.method} ${url}`, { contentType: req.headers["content-type"], bodyLen: req.body?.length, hasBody })
|
|
||||||
|
|
||||||
const response = await fetch(url, {
|
const response = await fetch(url, {
|
||||||
method: req.method,
|
method: req.method,
|
||||||
|
|
@ -123,7 +121,6 @@ export function connect(options: TunnelOptions): Tunnel {
|
||||||
})
|
})
|
||||||
|
|
||||||
const contentType = response.headers.get("content-type")
|
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> = {}
|
const headers: Record<string, string> = {}
|
||||||
response.headers.forEach((value, key) => {
|
response.headers.forEach((value, key) => {
|
||||||
headers[key] = value
|
headers[key] = value
|
||||||
|
|
|
||||||
|
|
@ -64,11 +64,9 @@ app.get("/tunnel", c => {
|
||||||
return upgradeWebSocket(c, {
|
return upgradeWebSocket(c, {
|
||||||
async onOpen(_event, ws) {
|
async onOpen(_event, ws) {
|
||||||
connections[name] = { app, ws }
|
connections[name] = { app, ws }
|
||||||
console.log(`connection opened: ${name} -> ${app}`)
|
|
||||||
send(ws, { subdomain: name })
|
send(ws, { subdomain: name })
|
||||||
},
|
},
|
||||||
onClose: (_event, _ws) => {
|
onClose: (_event, _ws) => {
|
||||||
console.log("connection closed:", name)
|
|
||||||
delete connections[name]
|
delete connections[name]
|
||||||
for (const [id, entry] of pending) {
|
for (const [id, entry] of pending) {
|
||||||
if (entry.subdomain === name) {
|
if (entry.subdomain === name) {
|
||||||
|
|
@ -102,13 +100,15 @@ app.all("*", async c => {
|
||||||
return c.text("Bad Gateway", 502)
|
return c.text("Bad Gateway", 502)
|
||||||
|
|
||||||
const id = randomID()
|
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['host']
|
||||||
delete headers['connection']
|
delete headers['connection']
|
||||||
delete headers['keep-alive']
|
delete headers['keep-alive']
|
||||||
delete headers['transfer-encoding']
|
delete headers['transfer-encoding']
|
||||||
delete headers['content-length']
|
delete headers['content-length']
|
||||||
const body = await c.req.text()
|
const body = await raw.text()
|
||||||
const app = connection.app
|
const app = connection.app
|
||||||
|
|
||||||
const result = await new Promise<Response>((resolve, reject) => {
|
const result = await new Promise<Response>((resolve, reject) => {
|
||||||
|
|
@ -146,7 +146,6 @@ app.all("*", async c => {
|
||||||
})
|
})
|
||||||
|
|
||||||
function send(connection: any, msg: Request | Success) {
|
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))
|
connection.send(JSON.stringify(msg))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user