Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wisp_network adapter #1097

Merged
merged 4 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 9 additions & 23 deletions src/browser/wisp_network.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"use strict";

const DEFAULT_DOH_SERVER = "cloudflare-dns.com";

/**
Expand Down Expand Up @@ -35,7 +36,7 @@ function WispNetworkAdapter(wisp_url, bus, config)
}, this);
}

WispNetworkAdapter.prototype.register_ws = function (wisp_url) {
WispNetworkAdapter.prototype.register_ws = function(wisp_url) {
this.wispws = new WebSocket(wisp_url.replace("wisp://", "ws://").replace("wisps://", "wss://"));
this.wispws.binaryType = "arraybuffer";
this.wispws.onmessage = (event) => {
Expand All @@ -48,7 +49,7 @@ WispNetworkAdapter.prototype.register_ws = function (wisp_url) {
};
};

WispNetworkAdapter.prototype.send_packet = function (data, type, stream_id) {
WispNetworkAdapter.prototype.send_packet = function(data, type, stream_id) {
if(this.connections[stream_id].congestion > 0) {
if(type === "DATA") {
this.connections[stream_id].congestion--;
Expand All @@ -60,7 +61,7 @@ WispNetworkAdapter.prototype.send_packet = function (data, type, stream_id) {
}
};

WispNetworkAdapter.prototype.process_incoming_wisp_frame = function (frame) {
WispNetworkAdapter.prototype.process_incoming_wisp_frame = function(frame) {
const view = new DataView(frame.buffer);
const stream_id = view.getUint32(1, true);
switch(frame[0]) {
Expand All @@ -73,8 +74,6 @@ WispNetworkAdapter.prototype.process_incoming_wisp_frame = function (frame) {
this.connections[stream_id].data_callback(frame.slice(5));
else
throw new Error("Got a DATA packet but stream not registered. ID: " + stream_id);


break;
case 3: // CONTINUE
if(this.connections[stream_id]) {
Expand All @@ -87,7 +86,6 @@ WispNetworkAdapter.prototype.process_incoming_wisp_frame = function (frame) {
}
this.connections[stream_id].congested = false;
}

break;
case 4: // CLOSE
if(this.connections[stream_id])
Expand Down Expand Up @@ -122,7 +120,7 @@ WispNetworkAdapter.prototype.process_incoming_wisp_frame = function (frame) {
//
//

WispNetworkAdapter.prototype.send_wisp_frame = function (frame_obj) {
WispNetworkAdapter.prototype.send_wisp_frame = function(frame_obj) {
let full_packet;
let view;
switch(frame_obj.type) {
Expand All @@ -142,25 +140,20 @@ WispNetworkAdapter.prototype.send_wisp_frame = function (frame_obj) {
close_callback: frame_obj.close_callback,
congestion: this.connections[0].congestion
};


break;
case "DATA":

full_packet = new Uint8Array(5 + frame_obj.data.length);
view = new DataView(full_packet.buffer);
view.setUint8(0, 0x02); // TYPE
view.setUint32(1, frame_obj.stream_id, true); // Stream ID
full_packet.set(frame_obj.data, 5); // Actual data

break;
case "CLOSE":
full_packet = new Uint8Array(5 + 1);
view = new DataView(full_packet.buffer);
view.setUint8(0, 0x04); // TYPE
view.setUint32(1, frame_obj.stream_id, true); // Stream ID
view.setUint8(5, frame_obj.reason); // Packet size

break;
default:
dbg_log("Client tried to send unknown packet: " + frame_obj.type, LOG_NET);
Expand All @@ -175,6 +168,7 @@ WispNetworkAdapter.prototype.destroy = function()
this.wispws.onmessage = null;
this.wispws.onclose = null;
this.wispws.close();
this.wispws = null;
}
};

Expand Down Expand Up @@ -202,7 +196,6 @@ WispNetworkAdapter.prototype.send = function(data)
packet.tcp.dport
].join(":");


if(packet.tcp.syn) {
if(this.tcp_conn[tuple]) {
dbg_log("SYN to already opened port", LOG_FETCH);
Expand All @@ -211,7 +204,6 @@ WispNetworkAdapter.prototype.send = function(data)

tcp_conn.state = TCP_STATE_SYN_RECEIVED;
tcp_conn.net = this;
tcp_conn.send_wisp_frame = this.send_wisp_frame;
tcp_conn.tuple = tuple;
tcp_conn.stream_id = this.last_stream++;
this.tcp_conn[tuple] = tcp_conn;
Expand Down Expand Up @@ -240,7 +232,6 @@ WispNetworkAdapter.prototype.send = function(data)
});

tcp_conn.accept(packet);

return;
}

Expand Down Expand Up @@ -269,6 +260,7 @@ WispNetworkAdapter.prototype.send = function(data)
}

if(packet.dns) {
// TODO: remove when this wisp client supports udp
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, I would like to avoid DOH (behind a switch that is disabled by default), in order to avoid leaking hostnames to cloudflare. However, since this wisp implementation doesn't have UDP yet, I'll leave it with this comment for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understandable, I'm a bit busy this week but I'll see if I can make out time in the near future to add in UDP

(async () => {
let reply = {};
reply.eth = { ethertype: ETHERTYPE_IPV4, src: this.router_mac, dest: packet.eth.src };
Expand All @@ -281,28 +273,22 @@ WispNetworkAdapter.prototype.send = function(data)
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(reply));

})();

}

if(packet.ntp) {
// TODO: remove when this wisp client supports udp
handle_fake_ntp(packet, this);
return;
}

// ICMP Ping
if(packet.icmp && packet.icmp.type === 8) {
handle_fake_ping(packet, this);
return;
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the fake ping implementation here, it's not very useful (and possibly confusing) when combined with a real networking proxy.

if(packet.dhcp) {
handle_fake_dhcp(packet, this);
return;
}

if(packet.udp && packet.udp.dport === 8) {
// TODO: remove when this wisp client supports udp
handle_udp_echo(packet, this);
}
};
Expand Down
42 changes: 14 additions & 28 deletions tests/devices/wisp_network.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,34 +70,20 @@ const tests =
assert(/192.168.86.1/.test(capture), "192.168.86.100");
},
},
{
name: "ping 1.2.3.4",
timeout: 60,
start: () =>
{
emulator.serial0_send("ping -c 2 1.2.3.4\n");
emulator.serial0_send("echo -e done\\\\tping\n");
},
end_trigger: "done\tping",
end: (capture) =>
{
assert(/2 packets transmitted, 2 packets received, 0% packet loss/.test(capture), "2 packets transmitted, 2 packets received, 0% packet loss");
},
},
{
name: "arp -a",
timeout: 60,
start: () =>
{
emulator.serial0_send("arp -a\n");
emulator.serial0_send("echo -e done\\\\tarp\n");
},
end_trigger: "done\tarp",
end: (capture) =>
{
assert(/.192.168.86.1. at 52:54:00:01:02:03 \[ether\] {2}on eth0/.test(capture), "(192.168.86.1) at 52:54:00:01:02:03 [ether] on eth0");
},
},
//{
// name: "arp -a",
// timeout: 60,
// start: () =>
// {
// emulator.serial0_send("arp -a\n");
// emulator.serial0_send("echo -e done\\\\tarp\n");
// },
// end_trigger: "done\tarp",
// end: (capture) =>
// {
// assert(/.192.168.86.1. at 52:54:00:01:02:03 \[ether\] {2}on eth0/.test(capture), "(192.168.86.1) at 52:54:00:01:02:03 [ether] on eth0");
// },
//},
{
name: "Curl example.org",
timeout: 60,
Expand Down
Loading