Skip to content

Commit 88e5c02

Browse files
committed
rdns: extract b32 flags where available
1 parent 33f6260 commit 88e5c02

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

src/plugins/rdns-util.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ export function getB64Flag(uint16Arr, flagVersion) {
274274
export function msgkeyFromUrl(u) {
275275
const ans = extractStamps(u);
276276
// accesskey is at index 3
277-
return ans[3];
277+
return ans[3] || "";
278278
}
279279

280280
/**
@@ -286,8 +286,8 @@ export function msgkeyFromUrl(u) {
286286
export function blockstampFromUrl(u) {
287287
const ans = extractStamps(u);
288288
const delim = ans[0];
289-
const ver = ans[1];
290-
const blockstamp = ans[2];
289+
const ver = ans[1] || ""; // may be undefined
290+
const blockstamp = ans[2] || ""; // may be undefined
291291

292292
// delim at index 0, version at index 1, blockstamp at index 2
293293
if (util.emptyString(ver) || util.emptyString(blockstamp)) return "";
@@ -328,19 +328,26 @@ export function extractStamps(u) {
328328
const recStamp = recBlockstampFrom(url);
329329
const useRecStamp = !util.emptyString(recStamp);
330330

331-
const paths = url.pathname.split("/");
332-
333-
if (!useRecStamp && paths.length <= 1) {
334-
return emptystamp;
335-
}
336-
337331
let s = emptystr;
338332
// note: the legacy free.bravedns endpoint need not support
339333
// gateway queries or auth
340334
if (useRecStamp) {
341335
s = recStamp;
342336
}
343337

338+
const paths = url.pathname.split("/");
339+
const domains = url.hostname.split(".");
340+
// could be a b32 flag in the hostname,
341+
// even if its a http-req (possible for a cc request)
342+
for (const d of domains) {
343+
if (d.length === 0) continue;
344+
// capture the first occurence of a b32 delimiter "-"
345+
if (isStampQuery(d)) {
346+
s = d;
347+
break;
348+
}
349+
}
350+
// overwrite if there exists a b64 flag in path
344351
for (const p of paths) {
345352
if (p.length === 0) continue;
346353
// skip to next if path has `/dns-query` or `/gateway` or '/l:'
@@ -350,8 +357,9 @@ export function extractStamps(u) {
350357
}
351358
}
352359

353-
// get blockstamp with access-key from paths[1|2]
360+
// get blockstamp with access-key from paths[1|2] or from hostname[0|1]
354361
try {
362+
// FIXME: the array returned here may not always be of length 4
355363
return splitBlockstamp(s);
356364
} catch (e) {
357365
log.d("Rdns:blockstampFromUrl", e);
@@ -379,17 +387,16 @@ export function base32ToUintV1(flag) {
379387
return bufutil.decodeFromBinaryArray(rbase32(b32));
380388
}
381389

382-
export function splitBlockstamp(s) {
390+
function splitBlockstamp(s) {
383391
// delim, version, blockstamp, accesskey
384-
let out = ["", "", "", ""];
385392

386-
if (util.emptyString(s)) return out;
387-
if (!isStampQuery(s)) return out;
393+
if (util.emptyString(s)) return emptystamp;
394+
if (!isStampQuery(s)) return emptystamp;
388395

389396
if (isB32Stamp(s)) {
390-
out = [_b32delim, ...s.split(_b32delim)];
397+
return [_b32delim, ...s.split(_b32delim)];
391398
} else {
392-
out = [_b64delim, ...s.split(_b64delim)];
399+
return [_b64delim, ...s.split(_b64delim)];
393400
}
394401

395402
return out;

src/server-node.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ async function handleHTTPRequest(b, req, res) {
836836

837837
// nb: req.url is a url-path, for ex: /a/b/c
838838
const fReq = new Request(new URL(req.url, `https://${host}`), {
839-
// Note: In VM container, Object spread may not be working for all
839+
// Note: In a VM container, Object spread may not be working for all
840840
// properties, especially of "hidden" Symbol values!? like "headers"?
841841
...req,
842842
// TODO: populate req ip in x-nile-client-ip header

0 commit comments

Comments
 (0)