Creating HTTPS / HTTP SRV record support via Google Chrome Extension

Asphyxia

Owner
Administrator
Apr 25, 2015
1,772
2,151
287
This is a WIP (Work-In-Progress) as I have never built Google Chrome extensions before, ever.


So keep in mind this is a glorified Hello World app trying to make rocketships launch, might be a lot of smoke!

Anyway, I created the typical manifest file:
Code:
{

  "name": "SRV HTTPS",

  "version": "1.1",

  "description": "Adding support for SRV records to web browsing.",

  "manifest_version": 2,

  "background": {

    "scripts":["background.js"]

  },

  "content_scripts": [

    {

      "matches": ["http://*/*", "https://*/*"],

      "js": ["content.js"]

    }

  ],

  "permissions": [

    "tabs",

    "notifications",

    "webNavigation",

    "background"

  ]

}


Then I have the background.js file:
Code:
chrome.webRequest.onBeforeRequest.addlistener(function(details)) {
chrome.tabs.executeScript(details.tabId, {"file": "content.js"};
};

Finally, the content.js file:

Code:
var start = async function(a, b) {
var response = await fetch('https://dns.google/resolve?name=_https._tcp.'+window.location.hostname+'&type=srv');
var json = await response.json();
console.log(json);
console.log(json.Answer[0].data);
var srvData = json.Answer[0].data;
var parseSrv = srvData.split(" ");
console.log(parseSrv);//Number 2 is the port
console.log(parseSrv[2]);
alert(window.location.host);
alert(window.location.hostname);
}
start();

Keep in mind none of this is fully working and the logic is extremely broken at-present, I must fine-tune all of this to have appropriate port/protocol checking so for example if the requested site already has a specified :123 (port) or whatever, there is no sense in checking SRV record as that would over-ride any DNS (I think).

Anyways.. this is full of .log entries, alerts, and stuff and serves as an extremely barebones look at how an extension version of this would function.

At line console.log(parseSrv[2]); --- this is the SRV port resolved.

The next step is to add in error handling in the event SRV records do not exist, add support for checking for both _http and _https then choosing _https if that exists and sending to a redirect via https:// or if _http only exists then send to that via http://

Pretty straight-forward stuff here, just simply a redirect script that works on SRV records, which browsers should already have in-built support for.

Kinda surprised they do not, really
 

Asphyxia

Owner
Administrator
Apr 25, 2015
1,772
2,151
287
Revision to content.js - moving this to GitHub:
Code:
var start = async function(a, b) {

//Request the srv type of record from Google's DNS DoH
var response = await fetch('https://dns.google/resolve?name=_https._tcp.'+window.location.hostname+'&type=srv');

//Get the JSON response, select the data, split the data by space characters
var json = await response.json();
var srvData = json.Answer[0].data;
var parseSrv = srvData.split(" ");

//Get the current host, check if host contains ':' as this represents a custom port.
var hosty = (window.location.host);
var n = hosty.includes(":");
if(!n){
//If there is no custom port, we are going to reroute to the SRV record - this if could probably go in front of the JSON request to avoid wasting requests, oh well.. will optimize later.
window.location.replace("http://"+window.location.hostname+":"+parseSrv[2]); //May want to consider adding the prior path and such to avoid losing directory/file
}

}
start();

v1.2

Code:
//Maybe there should be a local cookie or cache to indicate last SRV record check per domain, honor the TTL. I like the idea of local cookies for this with a timestamp to to expire upon TTL. Because having too many extra DoH requests could be extra noisy.
var hosty = (window.location.host);
var n = hosty.includes(":");
if(!n){

var start = async function(a, b) {

var response = await fetch('https://dns.google/resolve?name=_https._tcp.'+window.location.hostname+'&type=srv');

var json = await response.json();

if(json.Answer)
{

var srvData = json.Answer[0].data;
var parseSrv = srvData.split(" ");

window.location.replace("http://"+window.location.hostname+":"+parseSrv[2]);

}
}
start();
}

If upon the first query for the SRV, there is no entry found that domain could be cached to not check for an hour or some pre-defined # to avoid excessive noise on SRV checks. Just spitting random ideas

Example of working extension, notice the :7331 added since that SRV record exists:

Untitlek.gif
 
Last edited:
Top