Skip to content

Commit

Permalink
moved DoH-code to fake_network.js
Browse files Browse the repository at this point in the history
- added member "dns_method" to both FetchNetworkAdapter (value "static") and WispNetworkAdapter (value "doh")
- moved the DoH-code from WispNetworkAdapter.send() into new function handle_fake_dns_doh() in fake_network.js
- renamed function handle_fake_dns() into handle_fake_dns_static()
- recreated function handle_fake_dns() that now calls either of the two depending on the value of adapter.dns_method
  • Loading branch information
chschnell authored and copy committed Nov 26, 2024
1 parent 31b5f66 commit 7138331
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 51 deletions.
49 changes: 46 additions & 3 deletions src/browser/fake_network.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ const TCP_PAYLOAD_OFFSET = IPV4_PAYLOAD_OFFSET + TCP_HEADER_SIZE;
const TCP_PAYLOAD_SIZE = IPV4_PAYLOAD_SIZE - TCP_HEADER_SIZE;
const ICMP_HEADER_SIZE = 4;

const DEFAULT_DOH_SERVER = "cloudflare-dns.com";

function a2ethaddr(bytes) {
return [0,1,2,3,4,5].map((i) => bytes[i].toString(16)).map(x => x.length === 1 ? "0" + x : x).join(":");
}
Expand Down Expand Up @@ -273,7 +275,7 @@ function handle_fake_tcp(packet, adapter)
adapter.tcp_conn[tuple].process(packet);
}

function handle_fake_dns(packet, adapter)
function handle_fake_dns_static(packet, adapter)
{
let reply = {};
reply.eth = { ethertype: ETHERTYPE_IPV4, src: adapter.router_mac, dest: packet.eth.src };
Expand Down Expand Up @@ -316,6 +318,47 @@ function handle_fake_dns(packet, adapter)
return true;
}

function handle_fake_dns_doh(packet, adapter)
{
const fetch_url = `https://${adapter.doh_server || DEFAULT_DOH_SERVER}/dns-query`;
const fetch_opts = {
method: "POST",
headers: [["content-type", "application/dns-message"]],
body: packet.udp.data
};
fetch(fetch_url, fetch_opts).then(async (resp) => {
const reply = {
eth: {
ethertype: ETHERTYPE_IPV4,
src: adapter.router_mac,
dest: packet.eth.src
},
ipv4: {
proto: IPV4_PROTO_UDP,
src: adapter.router_ip,
dest: packet.ipv4.src
},
udp: {
sport: 53,
dport: packet.udp.sport,
data: new Uint8Array(await resp.arrayBuffer())
}
};
adapter.receive(make_packet(adapter.eth_encoder_buf, reply));
});
return true;
}

function handle_fake_dns(packet, adapter)
{
if(adapter.dns_method === 'static') {
return handle_fake_dns_static(packet, adapter);
}
else {
return handle_fake_dns_doh(packet, adapter);
}
}

function handle_fake_ntp(packet, adapter) {
let now = Date.now(); // - 1000 * 60 * 60 * 24 * 7;
let now_n = now + NTP_EPOC_DIFF;
Expand Down Expand Up @@ -1235,11 +1278,11 @@ TCPConnection.prototype.close = function() {
};

TCPConnection.prototype.on_shutdown = function() {
// forward FIN event from guest to network provider
// forward FIN event from guest device to network adapter
};

TCPConnection.prototype.on_close = function() {
// forward RST event from guest to network provider
// forward RST event from guest device to network adapter
};

TCPConnection.prototype.release = function() {
Expand Down
2 changes: 1 addition & 1 deletion src/browser/fetch_network.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ function FetchNetworkAdapter(bus, config)
this.vm_ip = new Uint8Array((config.vm_ip || "192.168.86.100").split(".").map(function(x) { return parseInt(x, 10); }));
this.masquerade = config.masquerade === undefined || !!config.masquerade;
this.vm_mac = new Uint8Array(6);

this.tcp_conn = {};
this.eth_encoder_buf = create_eth_encoder_buf();
this.dns_method = "static";

// Ex: 'https://corsproxy.io/?'
this.cors_proxy = config.cors_proxy;
Expand Down
52 changes: 5 additions & 47 deletions src/browser/wisp_network.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"use strict";

const DEFAULT_DOH_SERVER = "cloudflare-dns.com";

/**
* @constructor
*
Expand All @@ -10,7 +8,6 @@ const DEFAULT_DOH_SERVER = "cloudflare-dns.com";
*/
function WispNetworkAdapter(wisp_url, bus, config)
{

this.register_ws(wisp_url);
this.last_stream = 1;
this.connections = {0: {congestion: 0}};
Expand All @@ -24,15 +21,15 @@ function WispNetworkAdapter(wisp_url, bus, config)
this.vm_ip = new Uint8Array((config.vm_ip || "192.168.86.100").split(".").map(function(x) { return parseInt(x, 10); }));
this.masquerade = config.masquerade === undefined || !!config.masquerade;
this.vm_mac = new Uint8Array(6);
this.doh_server = config.doh_server || DEFAULT_DOH_SERVER;
this.tcp_conn = {};
this.eth_encoder_buf = create_eth_encoder_buf();
this.dns_method = "doh";
this.doh_server = config.doh_server;

this.bus.register("net" + this.id + "-mac", function(mac) {
this.vm_mac = new Uint8Array(mac.split(":").map(function(x) { return parseInt(x, 16); }));
}, this);
this.bus.register("net" + this.id + "-send", function(data)
{
this.bus.register("net" + this.id + "-send", function(data) {
this.send(data);
}, this);
}
Expand Down Expand Up @@ -231,47 +228,8 @@ WispNetworkAdapter.prototype.on_tcp_connection = function(packet, tuple)
*/
WispNetworkAdapter.prototype.send = function(data)
{
let packet = {};
parse_eth(data, packet);

if(packet.ipv4) {
if(packet.tcp) {
handle_fake_tcp(packet, this);
}
else if(packet.udp) {
// TODO: remove when this wisp client supports udp
if(packet.dns) {
(async () => {
const reply = {
eth: { ethertype: ETHERTYPE_IPV4, src: this.router_mac, dest: packet.eth.src },
ipv4: { proto: IPV4_PROTO_UDP, src: this.router_ip, dest: packet.ipv4.src },
udp: { sport: 53, dport: packet.udp.sport }
};
const result = await (await fetch(`https://${this.doh_server}/dns-query`, {
method: "POST",
headers: [["content-type", "application/dns-message"]],
body: packet.udp.data})).arrayBuffer();
reply.udp.data = new Uint8Array(result);
this.receive(make_packet(this.eth_encoder_buf, reply));
})();
}
else if(packet.dhcp) {
handle_fake_dhcp(packet, this);
}
else if(packet.ntp) {
handle_fake_ntp(packet, this);
}
else if(packet.udp.dport === 8) {
handle_udp_echo(packet, this);
}
}
else if(packet.icmp && packet.icmp.type === 8) {
handle_fake_ping(packet, this);
}
}
else if(packet.arp && packet.arp.oper === 1 && packet.arp.ptype === ETHERTYPE_IPV4) {
arp_whohas(packet, this);
}
// TODO: forward UDP traffic to WISP server once this WISP client supports UDP
handle_fake_networking(data, this);
};

/**
Expand Down

0 comments on commit 7138331

Please sign in to comment.