Using Cloudflare NS Records For Better Web Proxying & DNS Service

So I decided to switch the nameserver records on my fossjon.com domain over to Cloudflare’s service for two different reasons. One is that they offer more advanced https reverse proxying tech along with a better dns management interface as well! I still have the domain registered with Google Domains as they also offer pretty good mx record email forwarding via gmail.

Cloudflare won’t let you directly rewrite the HTTP HOST header field anymore, however, they will let you setup a more advanced HTTP JavaScript worker process. This process can handle the incoming web proxy requests along with the outgoing responses and perform some modifications on them. This is an extremely powerful framework and it behaves more like a proper reverse proxy server would!

/* worker */
addEventListener("fetch", event => {
  event.respondWith(handleRequest(event.request))
});

async function handleRequest(request) {
  /* request */
  var url = new URL(request.url);
  url.hostname = "fossjon.wordpress.com";
  //url.pathname = url.pathname.replace(/\/*$/mig, "") + "/";
  var repHost = new RegExp(url.hostname, "mig");
  var reqHost = request.headers.get("host");
  const response = await fetch(url, request);

  /* response */
  const newHead = new Response(response.body, response);
  for (var keyVal of newHead.headers.entries()) {
    if (keyVal[0].toLowerCase().includes("location")) {
      var newHost = keyVal[1].replace(repHost, reqHost);
      newHead.headers.set(keyVal[0], newHost);
    }
  }
  const resText = await response.text();
  const newBody = resText.replace(repHost, reqHost);

  /* return */
  return new Response(newBody, newHead);
}

Note: It seems like CF offers a better DNS API service, however, I couldn’t yet find a DNS backup button to help save all my records locally (hacky webarchive file but at least it’s sorted nicely).

javascript:(function(){
var t = document.getElementsByTagName("table")[0];
var s = t.getElementsByTagName("tr");
var m = {}, l = [], z = 1;
for (var i=0; i<s.length; ++i) {
  var d = s[i].getElementsByTagName("td");
  if (d.length > 4) {
    var k = d[2].innerText.trim();
    if (k.endsWith(".com")) { k = "@"; }
    if (!(k in m)) { m[k] = {"r":[]}; l.push(k); }
    var r = d[1].innerText.trim();
    if (!(r in m[k])) { m[k][r] = []; m[k]["r"].push(r); }
    m[k][r].push([d[3].innerText.trim(), d[4].innerText.trim()]);
  }
}
var b = "style='border: 1px solid black;padding: 4px;white-space: nowrap;'";
var o = "<table style='padding: 8px;'><tr><th "+b+">No.</th><th "+b+">Time</th><th "+b+">Record</th><th "+b+">Type</th><th "+b+">Value</th></tr>";
l.sort();
for (var i in l) {
  var k = l[i];
  m[k]["r"].sort();
  for (var j in m[k]["r"]) {
    var r = m[k]["r"][j];
    m[k][r].sort();
    for (var d in m[k][r]) {
      o += ("<tr><td "+b+">"+z+"</td><td "+b+">"+m[k][r][d][1]+"</td><td "+b+">"+k+"</td><td "+b+">"+r+"</td><td "+b+">"+m[k][r][d][0]+"</td></tr>"); ++z;
    }
  }
}
o += "</table>";
document.head.innerHTML = document.head.innerHTML.replace(/script/ig, "xscript");
document.body.innerHTML = o;
})();
Using Cloudflare NS Records For Better Web Proxying & DNS Service

One thought on “Using Cloudflare NS Records For Better Web Proxying & DNS Service

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s