Skip to content

Add robots.txt #2080

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ci/build/build-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ bundle_code_server() {
rsync src/browser/media/ "$RELEASE_PATH/src/browser/media"
mkdir -p "$RELEASE_PATH/src/browser/pages"
rsync src/browser/pages/*.html "$RELEASE_PATH/src/browser/pages"
rsync src/browser/robots.txt "$RELEASE_PATH/src/browser"

# Adds the commit to package.json
jq --slurp '.[0] * .[1]' package.json <(
Expand Down
2 changes: 2 additions & 0 deletions src/browser/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
User-agent: *
Disallow: /
30 changes: 20 additions & 10 deletions src/node/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ export abstract class HttpProvider {
/**
* Helper to error if not authorized.
*/
protected ensureAuthenticated(request: http.IncomingMessage): void {
public ensureAuthenticated(request: http.IncomingMessage): void {
if (!this.authenticated(request)) {
throw new HttpError("Unauthorized", HttpCode.Unauthorized)
}
Expand Down Expand Up @@ -647,10 +647,7 @@ export class HttpServer {
}

try {
const payload =
this.maybeRedirect(request, route) ||
(route.provider.authenticated(request) && this.maybeProxy(request)) ||
(await route.provider.handleRequest(route, request))
const payload = (await this.handleRequest(route, request)) || (await route.provider.handleRequest(route, request))
if (payload.proxy) {
this.doProxy(route, request, response, payload.proxy)
} else {
Expand Down Expand Up @@ -685,15 +682,23 @@ export class HttpServer {
}

/**
* Return any necessary redirection before delegating to a provider.
* Handle requests that are always in effect no matter what provider is
* registered at the route.
*/
private maybeRedirect(request: http.IncomingMessage, route: ProviderRoute): RedirectResponse | undefined {
private async handleRequest(route: ProviderRoute, request: http.IncomingMessage): Promise<HttpResponse | undefined> {
// If we're handling TLS ensure all requests are redirected to HTTPS.
if (this.options.cert && !(request.connection as tls.TLSSocket).encrypted) {
return { redirect: route.fullPath }
}

return undefined
// Return robots.txt.
if (route.fullPath === "/robots.txt") {
const filePath = path.resolve(__dirname, "../../src/browser/robots.txt")
return { content: await fs.readFile(filePath), filePath }
}

// Handle proxy domains.
return this.maybeProxy(route, request)
}

/**
Expand Down Expand Up @@ -744,7 +749,7 @@ export class HttpServer {
// can't be transferred so we need an in-between).
const socketProxy = await this.socketProvider.createProxy(socket)
const payload =
this.maybeProxy(request) || (await route.provider.handleWebSocket(route, request, socketProxy, head))
this.maybeProxy(route, request) || (await route.provider.handleWebSocket(route, request, socketProxy, head))
if (payload && payload.proxy) {
this.doProxy(route, request, { socket: socketProxy, head }, payload.proxy)
}
Expand Down Expand Up @@ -894,8 +899,10 @@ export class HttpServer {
*
* For example if `coder.com` is specified `8080.coder.com` will be proxied
* but `8080.test.coder.com` and `test.8080.coder.com` will not.
*
* Throw an error if proxying but the user isn't authenticated.
*/
public maybeProxy(request: http.IncomingMessage): HttpResponse | undefined {
public maybeProxy(route: ProviderRoute, request: http.IncomingMessage): HttpResponse | undefined {
// Split into parts.
const host = request.headers.host || ""
const idx = host.indexOf(":")
Expand All @@ -909,6 +916,9 @@ export class HttpServer {
return undefined
}

// Must be authenticated to use the proxy.
route.provider.ensureAuthenticated(request)

return {
proxy: {
port,
Expand Down