Skip to content

Commit

Permalink
rm redundant terminate_tls env var
Browse files Browse the repository at this point in the history
  • Loading branch information
ignoramous committed Jan 24, 2022
1 parent 84f8f8e commit c53b2a3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 48 deletions.
4 changes: 0 additions & 4 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ CF_ON_INVALID_FLAG_STOPPROCESSING="true", "false"
CF_PROCESS_DNS_ON_DNSPARSER_EXCEPTION="true", "false"

# Specifies path to fullchain-CA-certificate & CA-certificate-private-key
# Deno: Optional, Required only if `TERMINATE_TLS` is true.
# Node: Optional, can be used only in development and if `TLS_` doesn't exist.
TLS_CRT_PATH=""
TLS_KEY_PATH=""
Expand All @@ -49,9 +48,6 @@ TLS_KEY_PATH=""
# Required
DENO_ENV="production", "development"

# Optional
TERMINATE_TLS="true"

# ---------- Node runtime specific ---------- #

# Required
Expand Down
4 changes: 0 additions & 4 deletions src/commons/envutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ export function secondaryDohResolver() {
return env.secondaryDohResolver;
}

export function terminateTls() {
return envManager && envManager.get("TERMINATE_TLS");
}

export function tlsCrtPath() {
if (!envManager) return "";
return envManager.get("TLS_CRT_PATH") || "";
Expand Down
4 changes: 2 additions & 2 deletions src/core/deno/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ function setup() {
dotEnvConfig({ export: true });
} catch (e) {
// throws without --allow-read flag
console.warn(".env file may not be loaded => ", e.name, ":", e.message);
console.warn(".env missing => ", e.name, e.message);
}

try {
// override: if we are running this file, then we're on Deno
Deno.env.set("RUNTIME", "deno");
} catch (e) {
// Warning: `set()` method is not available in Deno deploy.
console.warn("Deno.env.set() is not available => ", e.name, ":", e.message);
console.warn("Deno.env.set() => ", e.name, e.message);
}

window.envManager = new EnvManager();
Expand Down
66 changes: 28 additions & 38 deletions src/server-deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ let log: any = null;
})();

function systemUp() {
const DOH_PORT = 8080;
const DOT_PORT = 10000;

const onDenoDeploy = envutil.onDenoDeploy() as boolean;
const dohConnOpts = { port: 8080 };
const dotConnOpts = { port: 10000 };
const tlsOpts = {
certFile: envutil.tlsCrtPath() as string,
keyFile: envutil.tlsKeyPath() as string,
Expand All @@ -29,30 +29,23 @@ function systemUp() {
alpnProtocols: ["h2", "http/1.1"],
};

const onDenoDeploy = envutil.onDenoDeploy() as boolean;
const terminateTls = envutil.terminateTls() as boolean;

log = util.logger("Deno");
if (!log) throw new Error("logger unavailable on system up");

log.i(envutil.tlsKeyPath(), envutil.tlsCrtPath());
startDoh();

startDotIfPossible();

async function startDoh() {
const doh = terminateTls
? // doc.deno.land/deno/stable/~/Deno.listenTls
Deno.listenTls({
port: DOH_PORT,
// obj spread (es2018) works only within objs
// doc.deno.land/deno/stable/~/Deno.listenTls
// doc.deno.land/deno/stable/~/Deno.listen
const doh = terminateTls()
? Deno.listenTls({
...dohConnOpts,
...tlsOpts,
...httpOpts,
})
: // doc.deno.land/deno/stable/~/Deno.listen
Deno.listen({
port: DOH_PORT,
});
: Deno.listen({ ...dohConnOpts });

up("DoH", doh.addr as Deno.NetAddr);

Expand All @@ -69,14 +62,9 @@ function systemUp() {
// No DoT on Deno Deploy which supports only http workloads
if (onDenoDeploy) return;

const dot = terminateTls
? Deno.listenTls({
port: DOT_PORT,
...tlsOpts,
})
: Deno.listen({
port: DOT_PORT,
});
const dot = terminateTls()
? Deno.listenTls({ ...dotConnOpts, ...tlsOpts })
: Deno.listen({ ...dotConnOpts });

up("DoT (no blocklists)", dot.addr as Deno.NetAddr);

Expand All @@ -89,28 +77,30 @@ function systemUp() {
}
}

function up(server: string, addr: Deno.NetAddr) {
log.i(server, `listening on: [${addr.hostname}]:${addr.port}`);
function up(p: string, addr: Deno.NetAddr) {
log.i(p, `on [${addr.hostname}]:${addr.port}`, "tls?", terminateTls());
}

function terminateTls() {
if (onDenoDeploy) return false;
if (util.emptyString(tlsOpts.keyFile)) return false;
if (util.emptyString(tlsOpts.certFile)) return false;
return true;
}
}

async function serveHttp(conn: Deno.Conn) {
const httpConn = Deno.serveHttp(conn);

while (true) {
let requestEvent = null;
try {
requestEvent = await httpConn.nextRequest();
} catch (e) {
log.w("err http read", e);
break;
}
if (!requestEvent) {
log.d("no more reqs, bail");
break;
}
const requestEvent = await httpConn.nextRequest();

if (!requestEvent) {
log.d("no more reqs, bail");
break;
}

try {
// doc.deno.land/deno/stable/~/Deno.RequestEvent
// deno.land/manual/runtime/http_server_apis#http-requests-and-responses
const req = requestEvent.request;
Expand All @@ -121,7 +111,7 @@ async function serveHttp(conn: Deno.Conn) {
await requestEvent.respondWith(res as Response | Promise<Response>);
} catch (e) {
// Client may close conn abruptly before a response could be sent
log.w("send fail doh response", e);
log.w("doh fail", e);
break;
}
}
Expand Down

0 comments on commit c53b2a3

Please sign in to comment.