From fd339a74333e63f6ea9aa9ec5c0945e35ec1d9dd Mon Sep 17 00:00:00 2001 From: Asher Date: Tue, 31 Mar 2020 13:11:35 -0500 Subject: [PATCH] Include query parameters when proxying --- doc/FAQ.md | 3 +++ src/node/app/proxy.ts | 16 +++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/doc/FAQ.md b/doc/FAQ.md index 73a43007..29e5c505 100644 --- a/doc/FAQ.md +++ b/doc/FAQ.md @@ -66,10 +66,12 @@ You can use [Let's Encrypt](https://letsencrypt.org/) to get an SSL certificate for free. ## How do I access web services? + code-server is capable of proxying to any port using either a subdomain or a subpath. ### Sub-domains + Set up a wildcard certificate for your domain and a wildcard DNS entry (or you can configure each subdomain individually for the ports you expect to use). @@ -83,6 +85,7 @@ Now you can browse to `.coder.com`. Note that this uses the host header so ensure your reverse proxy forwards that information if you are using one. ### Sub-paths + Just browse to `/proxy//`. ## x86 releases? diff --git a/src/node/app/proxy.ts b/src/node/app/proxy.ts index 80aa7a7b..1afdd1ce 100644 --- a/src/node/app/proxy.ts +++ b/src/node/app/proxy.ts @@ -2,6 +2,7 @@ import { logger } from "@coder/logger" import * as http from "http" import proxy from "http-proxy" import * as net from "net" +import * as querystring from "querystring" import { HttpCode, HttpError } from "../../common/http" import { HttpProvider, HttpProviderOptions, HttpProxyProvider, HttpResponse, Route } from "../http" @@ -47,7 +48,7 @@ export class ProxyHttpProvider extends HttpProvider implements HttpProxyProvider } } - const payload = this.doProxy(route.requestPath, request, response, base) + const payload = this.doProxy(route.requestPath, route.query, request, response, base) if (payload) { return payload } @@ -62,7 +63,7 @@ export class ProxyHttpProvider extends HttpProvider implements HttpProxyProvider head: Buffer, ): Promise { this.ensureAuthenticated(request) - this.doProxy(route.requestPath, request, socket, head, route.base.replace(/^\//, "")) + this.doProxy(route.requestPath, route.query, request, socket, head, route.base.replace(/^\//, "")) } public getCookieDomain(host: string): string { @@ -83,7 +84,7 @@ export class ProxyHttpProvider extends HttpProvider implements HttpProxyProvider response: http.ServerResponse, ): HttpResponse | undefined { const port = this.getPort(request) - return port ? this.doProxy(route.fullPath, request, response, port) : undefined + return port ? this.doProxy(route.fullPath, route.query, request, response, port) : undefined } public maybeProxyWebSocket( @@ -93,7 +94,7 @@ export class ProxyHttpProvider extends HttpProvider implements HttpProxyProvider head: Buffer, ): HttpResponse | undefined { const port = this.getPort(request) - return port ? this.doProxy(route.fullPath, request, socket, head, port) : undefined + return port ? this.doProxy(route.fullPath, route.query, request, socket, head, port) : undefined } private getPort(request: http.IncomingMessage): string | undefined { @@ -121,12 +122,14 @@ export class ProxyHttpProvider extends HttpProvider implements HttpProxyProvider private doProxy( path: string, + query: querystring.ParsedUrlQuery, request: http.IncomingMessage, response: http.ServerResponse, portStr: string, ): HttpResponse private doProxy( path: string, + query: querystring.ParsedUrlQuery, request: http.IncomingMessage, socket: net.Socket, head: Buffer, @@ -134,6 +137,7 @@ export class ProxyHttpProvider extends HttpProvider implements HttpProxyProvider ): HttpResponse private doProxy( path: string, + query: querystring.ParsedUrlQuery, request: http.IncomingMessage, responseOrSocket: http.ServerResponse | net.Socket, headOrPortStr: Buffer | string, @@ -159,7 +163,9 @@ export class ProxyHttpProvider extends HttpProvider implements HttpProxyProvider autoRewrite: true, changeOrigin: true, ignorePath: true, - target: `http://127.0.0.1:${port}${path}`, + target: `http://127.0.0.1:${port}${path}${ + Object.keys(query).length > 0 ? `?${querystring.stringify(query)}` : "" + }`, } if (responseOrSocket instanceof net.Socket) {