-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
wisp_network adapter #1097
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
"use strict"; | ||
|
||
const DEFAULT_DOH_SERVER = "cloudflare-dns.com"; | ||
|
||
/** | ||
|
@@ -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) => { | ||
|
@@ -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--; | ||
|
@@ -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]) { | ||
|
@@ -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]) { | ||
|
@@ -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]) | ||
|
@@ -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) { | ||
|
@@ -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); | ||
|
@@ -175,6 +168,7 @@ WispNetworkAdapter.prototype.destroy = function() | |
this.wispws.onmessage = null; | ||
this.wispws.onclose = null; | ||
this.wispws.close(); | ||
this.wispws = null; | ||
} | ||
}; | ||
|
||
|
@@ -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); | ||
|
@@ -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; | ||
|
@@ -240,7 +232,6 @@ WispNetworkAdapter.prototype.send = function(data) | |
}); | ||
|
||
tcp_conn.accept(packet); | ||
|
||
return; | ||
} | ||
|
||
|
@@ -269,6 +260,7 @@ WispNetworkAdapter.prototype.send = function(data) | |
} | ||
|
||
if(packet.dns) { | ||
// TODO: remove when this wisp client supports udp | ||
(async () => { | ||
let reply = {}; | ||
reply.eth = { ethertype: ETHERTYPE_IPV4, src: this.router_mac, dest: packet.eth.src }; | ||
|
@@ -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; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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