Add /absproxy to remove --proxy-path-passthrough
See https://github.com/cdr/code-server/issues/2222#issuecomment-765235938 Makes way more sense.
This commit is contained in:
@ -103,8 +103,25 @@ export const register = async (
|
||||
app.use("/", domainProxy.router)
|
||||
wsApp.use("/", domainProxy.wsRouter.router)
|
||||
|
||||
app.use("/proxy", proxy.router)
|
||||
wsApp.use("/proxy", proxy.wsRouter.router)
|
||||
app.all("/proxy/(:port)(/*)?", (req, res) => {
|
||||
proxy.proxy(req, res)
|
||||
})
|
||||
wsApp.get("/proxy/(:port)(/*)?", (req, res) => {
|
||||
proxy.wsProxy(req as WebsocketRequest)
|
||||
})
|
||||
// These two routes pass through the path directly.
|
||||
// So the proxied app must be aware it is running
|
||||
// under /absproxy/<someport>/
|
||||
app.all("/absproxy/(:port)(/*)?", (req, res) => {
|
||||
proxy.proxy(req, res, {
|
||||
passthroughPath: true,
|
||||
})
|
||||
})
|
||||
wsApp.get("/absproxy/(:port)(/*)?", (req, res) => {
|
||||
proxy.wsProxy(req as WebsocketRequest, {
|
||||
passthroughPath: true,
|
||||
})
|
||||
})
|
||||
|
||||
app.use(bodyParser.json())
|
||||
app.use(bodyParser.urlencoded({ extended: true }))
|
||||
|
@ -1,14 +1,13 @@
|
||||
import { Request, Router } from "express"
|
||||
import { Request, Response } from "express"
|
||||
import * as path from "path"
|
||||
import qs from "qs"
|
||||
import { HttpCode, HttpError } from "../../common/http"
|
||||
import { normalize } from "../../common/util"
|
||||
import { authenticated, ensureAuthenticated, redirect } from "../http"
|
||||
import { proxy } from "../proxy"
|
||||
import { Router as WsRouter } from "../wsRouter"
|
||||
import { proxy as _proxy } from "../proxy"
|
||||
import { WebsocketRequest } from "../wsRouter"
|
||||
|
||||
export const router = Router()
|
||||
|
||||
const getProxyTarget = (req: Request, passthroughPath: boolean): string => {
|
||||
const getProxyTarget = (req: Request, passthroughPath?: boolean): string => {
|
||||
if (passthroughPath) {
|
||||
return `http://0.0.0.0:${req.params.port}/${req.originalUrl}`
|
||||
}
|
||||
@ -16,7 +15,13 @@ const getProxyTarget = (req: Request, passthroughPath: boolean): string => {
|
||||
return `http://0.0.0.0:${req.params.port}/${req.params[0] || ""}${query ? `?${query}` : ""}`
|
||||
}
|
||||
|
||||
router.all("/(:port)(/*)?", (req, res) => {
|
||||
export function proxy(
|
||||
req: Request,
|
||||
res: Response,
|
||||
opts?: {
|
||||
passthroughPath?: boolean
|
||||
},
|
||||
): void {
|
||||
if (!authenticated(req)) {
|
||||
// If visiting the root (/:port only) redirect to the login page.
|
||||
if (!req.params[0] || req.params[0] === "/") {
|
||||
@ -28,22 +33,27 @@ router.all("/(:port)(/*)?", (req, res) => {
|
||||
throw new HttpError("Unauthorized", HttpCode.Unauthorized)
|
||||
}
|
||||
|
||||
if (!req.args["proxy-path-passthrough"]) {
|
||||
if (!opts?.passthroughPath) {
|
||||
// Absolute redirects need to be based on the subpath when rewriting.
|
||||
;(req as any).base = `${req.baseUrl}/${req.params.port}`
|
||||
// See proxy.ts.
|
||||
;(req as any).base = req.path.split(path.sep).slice(0, 3).join(path.sep)
|
||||
}
|
||||
|
||||
proxy.web(req, res, {
|
||||
_proxy.web(req, res, {
|
||||
ignorePath: true,
|
||||
target: getProxyTarget(req, req.args["proxy-path-passthrough"] || false),
|
||||
target: getProxyTarget(req, opts?.passthroughPath),
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export const wsRouter = WsRouter()
|
||||
|
||||
wsRouter.ws("/(:port)(/*)?", ensureAuthenticated, (req) => {
|
||||
proxy.ws(req, req.ws, req.head, {
|
||||
export function wsProxy(
|
||||
req: WebsocketRequest,
|
||||
opts?: {
|
||||
passthroughPath?: boolean
|
||||
},
|
||||
): void {
|
||||
ensureAuthenticated(req)
|
||||
_proxy.ws(req, req.ws, req.head, {
|
||||
ignorePath: true,
|
||||
target: getProxyTarget(req, req.args["proxy-path-passthrough"] || false),
|
||||
target: getProxyTarget(req, opts?.passthroughPath),
|
||||
})
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user