Skip to content

Commit

Permalink
Merge branch 'serverless-dns:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Arisa43fps authored Oct 1, 2024
2 parents c864e1e + 3fcf47d commit 2273668
Show file tree
Hide file tree
Showing 26 changed files with 818 additions and 388 deletions.
14 changes: 11 additions & 3 deletions fly.tls.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ app = ""

kill_signal = "SIGINT"
kill_timeout = "15s"
swap_size_mb = 152
# swap must be disabled when using "suspend"
# swap_size_mb = 152

[build]
dockerfile = "node.Dockerfile"
Expand All @@ -20,11 +21,18 @@ swap_size_mb = 152
[experimental]
auto_rollback = true

# community.fly.io/t/19180
# fly.io/docs/machines/guides-examples/machine-restart-policy
[[restart]]
policy = "on-failure"
retries = 3

# DNS over HTTPS (well, h2c and http1.1)
[[services]]
internal_port = 8055
protocol = "tcp"
auto_stop_machines = true
# community.fly.io/t/20672
auto_stop_machines = "suspend"
auto_start_machines = true

[services.concurrency]
Expand Down Expand Up @@ -57,7 +65,7 @@ auto_rollback = true
[[services]]
internal_port = 10555
protocol = "tcp"
auto_stop_machines = true
auto_stop_machines = "suspend"
auto_start_machines = true

[services.concurrency]
Expand Down
14 changes: 11 additions & 3 deletions fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ app = ""

kill_signal = "SIGINT"
kill_timeout = "15s"
swap_size_mb = 152
# swap cannot be used with "suspend"
# community.fly.io/t/20672
# swap_size_mb = 152

[build]
dockerfile = "node.Dockerfile"
Expand All @@ -17,11 +19,17 @@ swap_size_mb = 152
NODE_ENV = "production"
LOG_LEVEL = "info"

# community.fly.io/t/19180
# fly.io/docs/machines/guides-examples/machine-restart-policy
[[restart]]
policy = "on-failure"
retries = 3

# DNS over HTTPS
[[services]]
protocol = "tcp"
internal_port = 8080
auto_stop_machines = true
auto_stop_machines = "suspend"
auto_start_machines = true

[[services.ports]]
Expand Down Expand Up @@ -50,7 +58,7 @@ swap_size_mb = 152
[[services]]
protocol = "tcp"
internal_port = 10000
auto_stop_machines = true
auto_stop_machines = "suspend"
auto_start_machines = true

[[services.ports]]
Expand Down
2 changes: 1 addition & 1 deletion node.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ FROM node:22-alpine AS runner
# env vals persist even at run-time: archive.is/QpXp2
# and overrides fly.toml env values
ENV NODE_ENV production
ENV NODE_OPTIONS="--max-old-space-size=320 --heapsnapshot-signal=SIGUSR2"
ENV NODE_OPTIONS="--max-old-space-size=200 --heapsnapshot-signal=SIGUSR2"
# get working dir in order
WORKDIR /app
# external deps not bundled by webpack
Expand Down
8 changes: 8 additions & 0 deletions src/commons/bufutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,19 @@ export function bufferOf(arrayBuf) {
return Buffer.from(new Uint8Array(arrayBuf));
}

/**
* @param {Buffer} b
* @returns {int}
*/
export function recycleBuffer(b) {
b.fill(0);
return 0;
}

/**
* @param {int} size
* @returns {Buffer}
*/
export function createBuffer(size) {
return Buffer.allocUnsafe(size);
}
Expand Down
16 changes: 15 additions & 1 deletion src/commons/dnsutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,19 @@ export function isAnswerQuad0(packet) {
return isAnswerBlocked(packet.answers);
}

export function ttl(packet) {
if (!hasAnswers(packet)) return 0;
return packet.answers[0].ttl || 0;
}

/**
* @param {any} dnsPacket
* @returns {string[]}
*/
export function extractDomains(dnsPacket) {
if (!hasSingleQuestion(dnsPacket)) return [];

/** @type {string} */
const names = new Set();
const answers = dnsPacket.answers;

Expand Down Expand Up @@ -416,7 +426,7 @@ export function extractDomains(dnsPacket) {

export function getInterestingAnswerData(packet, maxlen = 80, delim = "|") {
if (!hasAnswers(packet)) {
return !util.emptyObj(packet) ? packet.rcode || "WTF" : "WTF";
return !util.emptyObj(packet) ? packet.rcode || "WTF1" : "WTF2";
}

// set to true if at least one ip has been captured from ans
Expand Down Expand Up @@ -535,6 +545,10 @@ export function getQueryType(packet) {
return util.emptyString(qt) ? false : qt;
}

/**
* @param {string?} n
* @returns {string}
*/
export function normalizeName(n) {
if (util.emptyString(n)) return n;

Expand Down
13 changes: 10 additions & 3 deletions src/commons/envutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

// musn't import /depend on anything.

export function isProd() {
if (!envManager) return false;

return envManager.determineEnvStage() === "production";
}

export function onFly() {
if (!envManager) return false;

Expand Down Expand Up @@ -89,6 +95,9 @@ export function isDeno() {
return envManager.r() === "deno";
}

/**
* in milliseconds
*/
export function workersTimeout(missing = 0) {
if (!envManager) return missing;
return envManager.get("WORKER_TIMEOUT") || missing;
Expand Down Expand Up @@ -227,12 +236,10 @@ export function shutdownTimeoutMs() {
}

export function measureHeap() {
// disable; webpack can't bundle memwatch; see: server-node.js
return false;
if (!envManager) return false;
const reg = region();
if (
reg === "maa" ||
reg === "bom" ||
reg === "sin" ||
reg === "fra" ||
reg === "ams" ||
Expand Down
13 changes: 11 additions & 2 deletions src/commons/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
// musn't import any non-std modules

export function fromBrowser(ua) {
return ua && ua.startsWith("Mozilla/5.0");
if (emptyString(ua)) return false;
return ua.startsWith("Mozilla/5.0") || ua.startsWith("dohjs/");
}

export function jsonHeaders() {
Expand Down Expand Up @@ -117,7 +118,13 @@ export function objOf(map) {
return map.entries ? Object.fromEntries(map) : {};
}

export function timedOp(op, ms, cleanup = () => {}) {
/**
* @param {(function((out, err) => void))} op
* @param {int} ms
* @param {function(any)} cleanup
* @returns {Promise}
*/
export function timedOp(op, ms, cleanup = (x) => {}) {
return new Promise((resolve, reject) => {
let timedout = false;
const tid = timeout(ms, () => {
Expand All @@ -142,6 +149,7 @@ export function timedOp(op, ms, cleanup = () => {}) {
}
});
} catch (e) {
clearTimeout(tid);
if (!timedout) reject(e);
}
});
Expand Down Expand Up @@ -180,6 +188,7 @@ export function timedSafeAsyncOp(promisedOp, ms, defaultOp) {
}
})
.catch((ignored) => {
clearTimeout(tid);
if (!timedout) deferredOp();
// else: handled by timeout
});
Expand Down
Loading

0 comments on commit 2273668

Please sign in to comment.