feat: add HTTP-over-I2P proxy to router daemon
- Add --http-proxy/--proxy [host:port] (default 127.0.0.1:4444) to route .i2p and .b32.i2p HTTP requests over I2P streams - Block clearnet hosts with 403 and reject CONNECT and https:// with 501 - Support ?i2paddresshelper= jump links that register host aliases - Add `just proxy` target and -h/--help usage output - Expire idle ratchet sessions after 300s and add RatchetSessionManager::drop_session for fresh NewSession handshakes - Add NetDb::remove_lease_set2 and bound the outbound pending queue to 64 entries
This commit is contained in:
1 parent
54cfb2faa9
commit
14eae00f5c
15 files changed
+1941
-41
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,102 @@
|
||||
///|
|
||||
test "parse_http_proxy_request rewrites GET request and strips leak/proxy headers" {
|
||||
let raw = b"GET http://stats.i2p/path?x=1 HTTP/1.1\r\nHost: stats.i2p\r\nProxy-Connection: keep-alive\r\nProxy-Authorization: Basic dXNlcjpwYXNz\r\nConnection: keep-alive\r\nKeep-Alive: timeout=5\r\nX-Forwarded-For: 1.2.3.4\r\nForwarded: for=1.2.3.4\r\nVia: 1.1 proxy\r\nReferer: https://google.com/search\r\nCookie: sid=xyz\r\nUser-Agent: TestBrowser/1.0\r\n\r\n"
|
||||
match parse_http_proxy_request(raw) {
|
||||
Ok(req) => {
|
||||
assert_eq(req.http_method, "GET")
|
||||
assert_eq(req.host, "stats.i2p")
|
||||
assert_eq(req.port, 80U)
|
||||
assert_eq(req.origin_path, "/path?x=1")
|
||||
assert_true(req.wire_headers.has_prefix("GET /path?x=1 HTTP/1.1\r\n"))
|
||||
assert_true(req.wire_headers.contains("Host: stats.i2p\r\n"))
|
||||
assert_true(req.wire_headers.contains("Cookie: sid=xyz\r\n"))
|
||||
assert_true(
|
||||
req.wire_headers.contains("User-Agent: MYOB/6.66 (AN/ON)\r\n"),
|
||||
)
|
||||
assert_true(!req.wire_headers.contains("TestBrowser/1.0"))
|
||||
assert_true(req.wire_headers.has_suffix("Connection: close\r\n\r\n"))
|
||||
assert_true(!req.wire_headers.contains("Proxy-Connection"))
|
||||
assert_true(!req.wire_headers.contains("Proxy-Authorization"))
|
||||
assert_true(!req.wire_headers.contains("Keep-Alive"))
|
||||
assert_true(!req.wire_headers.contains("X-Forwarded-For"))
|
||||
assert_true(!req.wire_headers.contains("Forwarded:"))
|
||||
assert_true(!req.wire_headers.contains("Via:"))
|
||||
assert_true(!req.wire_headers.contains("google.com"))
|
||||
}
|
||||
Error(code, _, detail) => fail("unexpected error \{code}: \{detail}")
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
test "parse_http_proxy_request preserves POST headers, port, and .i2p Referer" {
|
||||
let raw = b"POST http://tracker2.postman.i2p:8080/?action=Enter HTTP/1.1\r\nHost: tracker2.postman.i2p:8080\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 42\r\nReferer: http://tracker2.postman.i2p/index.php\r\n\r\n"
|
||||
match parse_http_proxy_request(raw) {
|
||||
Ok(req) => {
|
||||
assert_eq(req.http_method, "POST")
|
||||
assert_eq(req.host, "tracker2.postman.i2p")
|
||||
assert_eq(req.port, 8080U)
|
||||
assert_eq(req.origin_path, "/?action=Enter")
|
||||
assert_true(
|
||||
req.wire_headers.contains(
|
||||
"Content-Type: application/x-www-form-urlencoded\r\n",
|
||||
),
|
||||
)
|
||||
assert_true(req.wire_headers.contains("Content-Length: 42\r\n"))
|
||||
assert_true(
|
||||
req.wire_headers.contains(
|
||||
"Referer: http://tracker2.postman.i2p/index.php\r\n",
|
||||
),
|
||||
)
|
||||
}
|
||||
Error(code, _, detail) => fail("unexpected error \{code}: \{detail}")
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
test "parse_http_proxy_request blocks clearnet hosts with 403 and CONNECT/https with 501" {
|
||||
match
|
||||
parse_http_proxy_request(
|
||||
b"GET http://example.com/secret HTTP/1.1\r\nHost: example.com\r\n\r\n",
|
||||
) {
|
||||
Ok(_) => fail("expected 403 for clearnet host")
|
||||
Error(code, _, _) => assert_eq(code, 403)
|
||||
}
|
||||
match
|
||||
parse_http_proxy_request(
|
||||
b"CONNECT stats.i2p:443 HTTP/1.1\r\nHost: stats.i2p:443\r\n\r\n",
|
||||
) {
|
||||
Ok(_) => fail("expected 501 for CONNECT")
|
||||
Error(code, _, _) => assert_eq(code, 501)
|
||||
}
|
||||
match
|
||||
parse_http_proxy_request(
|
||||
b"GET https://stats.i2p/ HTTP/1.1\r\nHost: stats.i2p\r\n\r\n",
|
||||
) {
|
||||
Ok(_) => fail("expected 501 for https://")
|
||||
Error(code, _, _) => assert_eq(code, 501)
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
test "parse_http_proxy_request extracts i2paddresshelper and builds clean redirect URL" {
|
||||
match
|
||||
parse_http_proxy_request(
|
||||
b"GET http://newsite.i2p/?i2paddresshelper=TESTB64DATA~- HTTP/1.1\r\nHost: newsite.i2p\r\n\r\n",
|
||||
) {
|
||||
Ok(req) => {
|
||||
assert_eq(req.helper_dest_b64, Some("TESTB64DATA~-"))
|
||||
assert_eq(req.clean_redirect_url, Some("http://newsite.i2p/"))
|
||||
}
|
||||
Error(code, _, detail) => fail("unexpected error \{code}: \{detail}")
|
||||
}
|
||||
match
|
||||
parse_http_proxy_request(
|
||||
b"GET http://newsite.i2p/dir?a=1&i2paddresshelper=ABC%7E1&b=2 HTTP/1.1\r\nHost: newsite.i2p\r\n\r\n",
|
||||
) {
|
||||
Ok(req) => {
|
||||
assert_eq(req.helper_dest_b64, Some("ABC~1"))
|
||||
assert_eq(req.clean_redirect_url, Some("http://newsite.i2p/dir?a=1&b=2"))
|
||||
}
|
||||
Error(code, _, detail) => fail("unexpected error \{code}: \{detail}")
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ priv struct PendingNetDbLookup {
|
||||
candidate_peers : Array[@common.Hash]
|
||||
started_ms : UInt64
|
||||
mut last_query_ms : UInt64
|
||||
kind : Int32 // 0 = NAMING LOOKUP (.b32), 1 = STREAM CONNECT awaiting LeaseSet2, 2 = background RI/exploration
|
||||
kind : Int32 // 0 = NAMING LOOKUP (.b32), 1 = STREAM CONNECT awaiting LeaseSet2, 2 = background RI/exploration, 3 = HTTP proxy LeaseSet2 lookup
|
||||
client_idx : Int32
|
||||
req_name : String
|
||||
session_id : String
|
||||
@@ -427,6 +427,17 @@ fn Router::start_netdb_lookup(
|
||||
true
|
||||
}
|
||||
|
||||
///|
|
||||
fn Router::start_proxy_netdb_lookup(
|
||||
self : Router,
|
||||
target_hash : @common.Hash,
|
||||
) -> Bool {
|
||||
if self.has_pending_lookup(target_hash, 1) {
|
||||
return true
|
||||
}
|
||||
self.start_netdb_lookup(target_hash, 3, -1, "", "", true)
|
||||
}
|
||||
|
||||
///|
|
||||
fn Router::handle_database_store_msg(
|
||||
self : Router,
|
||||
@@ -690,7 +701,7 @@ fn Router::complete_lookups_for_hash(
|
||||
let lk = self.pending_lookups[i]
|
||||
if lk.target_hash == key {
|
||||
let _ = self.pending_lookups.remove(i)
|
||||
if lk.client_idx < self.sam_clients.length() {
|
||||
if lk.client_idx >= 0 && lk.client_idx < self.sam_clients.length() {
|
||||
let c = self.sam_clients[lk.client_idx]
|
||||
if !c.closed {
|
||||
if lk.kind == 0 {
|
||||
@@ -699,7 +710,7 @@ fn Router::complete_lookups_for_hash(
|
||||
c.sock,
|
||||
"NAMING REPLY RESULT=OK NAME=\{lk.req_name} VALUE=\{val}",
|
||||
)
|
||||
} else {
|
||||
} else if lk.kind == 1 {
|
||||
self.finish_remote_stream_connect(lk, ls2)
|
||||
}
|
||||
}
|
||||
@@ -739,14 +750,15 @@ fn Router::poll_netdb_lookups(self : Router) -> Unit {
|
||||
match self.netdb.get_lease_set2(lk.target_hash, now_sec) {
|
||||
Some(ls2) => {
|
||||
let _ = self.pending_lookups.remove(i)
|
||||
if lk.client_idx < self.sam_clients.length() &&
|
||||
if lk.client_idx >= 0 &&
|
||||
lk.client_idx < self.sam_clients.length() &&
|
||||
!self.sam_clients[lk.client_idx].closed {
|
||||
if lk.kind == 0 {
|
||||
let _ = send_sam_line(
|
||||
self.sam_clients[lk.client_idx].sock,
|
||||
"NAMING REPLY RESULT=OK NAME=\{lk.req_name} VALUE=\{ls2.destination.to_base64()}",
|
||||
)
|
||||
} else {
|
||||
} else if lk.kind == 1 {
|
||||
self.finish_remote_stream_connect(lk, ls2)
|
||||
}
|
||||
}
|
||||
@@ -778,7 +790,7 @@ fn Router::poll_netdb_lookups(self : Router) -> Unit {
|
||||
}
|
||||
if now_ms > lk.started_ms + timeout_ms {
|
||||
let _ = self.pending_lookups.remove(i)
|
||||
if lk.client_idx < self.sam_clients.length() {
|
||||
if lk.client_idx >= 0 && lk.client_idx < self.sam_clients.length() {
|
||||
let c = self.sam_clients[lk.client_idx]
|
||||
if !c.closed {
|
||||
if lk.kind == 0 {
|
||||
@@ -786,7 +798,7 @@ fn Router::poll_netdb_lookups(self : Router) -> Unit {
|
||||
c.sock,
|
||||
"NAMING REPLY RESULT=KEY_NOT_FOUND NAME=\{lk.req_name}",
|
||||
)
|
||||
} else {
|
||||
} else if lk.kind == 1 {
|
||||
if !lk.silent {
|
||||
let _ = send_sam_line(
|
||||
c.sock,
|
||||
|
||||
+114
-2
@@ -31,7 +31,37 @@ pub fn Router::auto_reseed(self : Router) -> Int32 {
|
||||
}
|
||||
|
||||
///|
|
||||
/// Loads `hostname=base64dest` addressbook aliases from a local `hosts.txt` file.
|
||||
pub let default_public_b32_aliases : Array[(String, String)] = [
|
||||
("stats.i2p", "kqypgjpjwrphnzebod5ev3ts2vtii6e5tntrg4rnfijqc7rypldq.b32.i2p"),
|
||||
("zzz.i2p", "lhbd7ojcaiofbfku7ixh47qj537g572zmhdc4oilvugzxdpdghua.b32.i2p"),
|
||||
(
|
||||
"tracker2.postman.i2p", "6a4kxkg5wp33p25qqhgwl6sj4yh4xuf5b3p3qldwgclebchm3eea.b32.i2p",
|
||||
),
|
||||
("notbob.i2p", "nytzrhrjjfsutowojvxi7hphesskpqqr65wpistz6wa7cpajhp7a.b32.i2p"),
|
||||
(
|
||||
"insulaocculta.i2p", "ls5mvws4ytegkfa2s56lo7wl5h5elocyrfhmufpzq4eyozc3vosa.b32.i2p",
|
||||
),
|
||||
(
|
||||
"tracker.insulaocculta.i2p", "2rv2kch37xsn2fqdz6huyevq76njop4inumscnspklh3ufo7mkwa.b32.i2p",
|
||||
),
|
||||
(
|
||||
"tracker.queke.i2p", "j4nrlimeg2loq5sfslb3c6nmnvbucet4gyrmog66igxrk5l5fxla.b32.i2p",
|
||||
),
|
||||
(
|
||||
"opentracker.dg2.i2p", "w7tpbzncbcocrqtwwm3nezhnnsw4ozadvi2hmvzdhrqzfxfum7wa.b32.i2p",
|
||||
),
|
||||
]
|
||||
|
||||
///|
|
||||
/// Registers built-in `.i2p` -> `.b32.i2p` aliases for well-known public eepsites.
|
||||
pub fn Router::register_default_public_aliases(self : Router) -> Unit {
|
||||
for entry in default_public_b32_aliases {
|
||||
self.register_alias(entry.0, entry.1)
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Loads `hostname=base64dest` or `hostname=<52char>.b32.i2p` aliases from a local `hosts.txt` file.
|
||||
pub fn Router::load_hosts_file(
|
||||
self : Router,
|
||||
path : String,
|
||||
@@ -56,7 +86,8 @@ pub fn Router::load_hosts_file(
|
||||
if eq > 0 && eq + 1 < line.length() {
|
||||
let host = line[:eq].to_owned().trim().to_owned()
|
||||
let dest_b64 = line[eq + 1:].to_owned().trim().to_owned()
|
||||
if host.length() > 0 && dest_b64.length() > 100 {
|
||||
if host.length() > 0 &&
|
||||
(dest_b64.length() > 100 || parse_b32_hash(dest_b64) is Some(_)) {
|
||||
self.register_alias(host, dest_b64)
|
||||
count += 1
|
||||
}
|
||||
@@ -174,6 +205,16 @@ fn Router::pick_connected_hop(
|
||||
None
|
||||
}
|
||||
|
||||
///|
|
||||
fn Router::has_connected_peer(self : Router, hash : @common.Hash) -> Bool {
|
||||
for p in self.peers {
|
||||
if p.hash == hash {
|
||||
return true
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
///|
|
||||
fn Router::step_public_bootstrap(self : Router) -> Unit {
|
||||
let now_ms = @ntcp2_net.unix_time_ms()
|
||||
@@ -182,6 +223,77 @@ fn Router::step_public_bootstrap(self : Router) -> Unit {
|
||||
}
|
||||
self.last_bootstrap_ms = now_ms
|
||||
|
||||
// Keepalive NTCP2 peers carrying active Inbound/Outbound tunnels so long-lived sessions stay reachable
|
||||
let tunnel_peers : Array[@common.Hash] = []
|
||||
for t in self.tunnels.inbound {
|
||||
if t.hops.length() > 0 {
|
||||
let h = t.hops[t.hops.length() - 1].router_hash
|
||||
if !is_peer_in_list(h, tunnel_peers) {
|
||||
tunnel_peers.push(h)
|
||||
}
|
||||
}
|
||||
}
|
||||
for t in self.tunnels.outbound {
|
||||
if t.hops.length() > 0 {
|
||||
let h = t.hops[0].router_hash
|
||||
if !is_peer_in_list(h, tunnel_peers) {
|
||||
tunnel_peers.push(h)
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut pi = 0
|
||||
while pi < self.peers.length() {
|
||||
let p = self.peers[pi]
|
||||
if is_peer_in_list(p.hash, tunnel_peers) &&
|
||||
now_ms > p.last_active_ms + 90000UL {
|
||||
let ok = try {
|
||||
p.conn.send_blocks([@ntcp2.Ntcp2Block::Padding(b"\x00\x00\x00\x00")])
|
||||
true
|
||||
} catch {
|
||||
_ => false
|
||||
}
|
||||
if ok {
|
||||
p.last_active_ms = now_ms
|
||||
pi += 1
|
||||
} else {
|
||||
p.conn.close()
|
||||
let _ = self.peers.remove(pi)
|
||||
}
|
||||
} else {
|
||||
pi += 1
|
||||
}
|
||||
}
|
||||
|
||||
// Prune expired, nearly-expired, or disconnected-hop client tunnels so fresh replacements build automatically
|
||||
let now_sec = (now_ms / 1000UL).to_uint()
|
||||
let mut inbound_pruned = false
|
||||
let mut ii = 0
|
||||
while ii < self.tunnels.inbound.length() {
|
||||
let t = self.tunnels.inbound[ii]
|
||||
let dead_last_hop = t.hops.length() > 0 &&
|
||||
!self.has_connected_peer(t.hops[t.hops.length() - 1].router_hash)
|
||||
if t.expires_sec <= now_sec + 30U || dead_last_hop {
|
||||
let _ = self.tunnels.inbound.remove(ii)
|
||||
inbound_pruned = true
|
||||
} else {
|
||||
ii += 1
|
||||
}
|
||||
}
|
||||
let mut oi = 0
|
||||
while oi < self.tunnels.outbound.length() {
|
||||
let t = self.tunnels.outbound[oi]
|
||||
let dead_first_hop = t.hops.length() > 0 &&
|
||||
!self.has_connected_peer(t.hops[0].router_hash)
|
||||
if t.expires_sec <= now_sec + 30U || dead_first_hop {
|
||||
let _ = self.tunnels.outbound.remove(oi)
|
||||
} else {
|
||||
oi += 1
|
||||
}
|
||||
}
|
||||
if inbound_pruned && self.tunnels.inbound.length() > 0 {
|
||||
self.republish_all_sessions()
|
||||
}
|
||||
|
||||
// 1. Keep up to 6 live outbound NTCP2 TCP peer sessions active
|
||||
let target_peers = 6
|
||||
if self.peers.length() + self.pending_dials.length() < target_peers {
|
||||
|
||||
+62
-7
@@ -66,7 +66,11 @@ pub struct Router {
|
||||
priv bridges : Array[StreamBridge]
|
||||
priv remote_bridges : Array[RemoteStreamBridge]
|
||||
priv aliases : Array[(String, String)] // hostname -> pub_b64
|
||||
priv b32_aliases : Array[(String, String)] // hostname -> lowercase <52>.b32.i2p
|
||||
priv failed_peers : Array[(@common.Hash, UInt64)] // (router_hash, retry_after_ms)
|
||||
priv mut http_proxy_listener : @ntcp2_net.TcpListener?
|
||||
priv http_proxy_conns : Array[HttpProxyConn]
|
||||
priv mut http_proxy_session_id : String?
|
||||
priv mut lease_rr : Int32
|
||||
priv mut outbound_rr : Int32
|
||||
priv mut last_bootstrap_ms : UInt64
|
||||
@@ -225,7 +229,7 @@ pub fn Router::start(
|
||||
} else {
|
||||
tunnel_hops
|
||||
}
|
||||
{
|
||||
let r : Router = {
|
||||
ed_seed,
|
||||
static_key,
|
||||
ntcp2_iv,
|
||||
@@ -254,12 +258,20 @@ pub fn Router::start(
|
||||
bridges: [],
|
||||
remote_bridges: [],
|
||||
aliases: [],
|
||||
b32_aliases: [],
|
||||
failed_peers: [],
|
||||
http_proxy_listener: None,
|
||||
http_proxy_conns: [],
|
||||
http_proxy_session_id: None,
|
||||
lease_rr: 0,
|
||||
outbound_rr: 0,
|
||||
last_bootstrap_ms: 0UL,
|
||||
last_explore_ms: 0UL,
|
||||
}
|
||||
if public_mode {
|
||||
r.register_default_public_aliases()
|
||||
}
|
||||
r
|
||||
}
|
||||
|
||||
///|
|
||||
@@ -297,6 +309,33 @@ pub fn Router::router_hash(self : Router) -> @common.Hash {
|
||||
self.router_info.identity.hash()
|
||||
}
|
||||
|
||||
///|
|
||||
fn remove_alias_entry(list : Array[(String, String)], key : String) -> Unit {
|
||||
let mut i = 0
|
||||
while i < list.length() {
|
||||
if list[i].0 == key {
|
||||
let _ = list.remove(i)
|
||||
} else {
|
||||
i += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn upsert_alias_entry(
|
||||
list : Array[(String, String)],
|
||||
key : String,
|
||||
val : String,
|
||||
) -> Unit {
|
||||
for i = 0; i < list.length(); i = i + 1 {
|
||||
if list[i].0 == key {
|
||||
list[i] = (key, val)
|
||||
return
|
||||
}
|
||||
}
|
||||
list.push((key, val))
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn Router::register_alias(
|
||||
self : Router,
|
||||
@@ -304,13 +343,13 @@ pub fn Router::register_alias(
|
||||
dest_pub_b64 : String,
|
||||
) -> Unit {
|
||||
let key = hostname.to_lower()
|
||||
for i = 0; i < self.aliases.length(); i = i + 1 {
|
||||
if self.aliases[i].0 == key {
|
||||
self.aliases[i] = (key, dest_pub_b64)
|
||||
return
|
||||
}
|
||||
if parse_b32_hash(dest_pub_b64) is Some(_) {
|
||||
remove_alias_entry(self.aliases, key)
|
||||
upsert_alias_entry(self.b32_aliases, key, dest_pub_b64.to_lower())
|
||||
} else {
|
||||
remove_alias_entry(self.b32_aliases, key)
|
||||
upsert_alias_entry(self.aliases, key, dest_pub_b64)
|
||||
}
|
||||
self.aliases.push((key, dest_pub_b64))
|
||||
}
|
||||
|
||||
///|
|
||||
@@ -393,6 +432,19 @@ pub fn Router::reseed_from_url(
|
||||
|
||||
///|
|
||||
pub fn Router::close(self : Router) -> Unit {
|
||||
for pc in self.http_proxy_conns {
|
||||
if !pc.closed {
|
||||
pc.sock.close()
|
||||
pc.closed = true
|
||||
}
|
||||
}
|
||||
match self.http_proxy_listener {
|
||||
Some(l) => {
|
||||
l.close()
|
||||
self.http_proxy_listener = None
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
for c in self.sam_clients {
|
||||
if !c.closed {
|
||||
c.sock.close()
|
||||
@@ -509,6 +561,9 @@ pub fn Router::step(self : Router, poll_ms~ : Int32 = 10) -> Unit {
|
||||
if self.step_sam_clients() {
|
||||
did_work = true
|
||||
}
|
||||
if self.step_http_proxy() {
|
||||
did_work = true
|
||||
}
|
||||
self.poll_netdb_lookups()
|
||||
self.poll_tunnel_builds()
|
||||
if self.public_mode {
|
||||
|
||||
@@ -193,6 +193,154 @@ test "Two live routers end-to-end: NTCP2 + ECIES NetDB + Garlic Ratchet + Ed2551
|
||||
assert_eq(got_on_alice, b"ack-from-bob")
|
||||
}
|
||||
|
||||
///|
|
||||
fn pump_read_until_bytes_contains(
|
||||
sock : @ntcp2_net.TcpSocket,
|
||||
rs : Array[@router.Router],
|
||||
marker : String,
|
||||
max_ticks~ : Int32 = 1500,
|
||||
) -> String raise {
|
||||
let out : Array[Byte] = []
|
||||
for _i = 0; _i < max_ticks; _i = _i + 1 {
|
||||
step_routers(rs, 1)
|
||||
while sock.poll_readable(0) {
|
||||
let chunk = sock.read_some(4096)
|
||||
if chunk.length() == 0 {
|
||||
return @encoding.utf8_bytes_to_string(Bytes::from_array(out))
|
||||
}
|
||||
for j = 0; j < chunk.length(); j = j + 1 {
|
||||
out.push(chunk[j])
|
||||
}
|
||||
}
|
||||
let s = @encoding.utf8_bytes_to_string(Bytes::from_array(out)) catch {
|
||||
_ => ""
|
||||
}
|
||||
if s.contains(marker) {
|
||||
return s
|
||||
}
|
||||
}
|
||||
fail("timed out waiting for marker \{marker}")
|
||||
}
|
||||
|
||||
///|
|
||||
test "Two-router HTTP-over-I2P proxy GET, POST, clearnet block, CONNECT block, and addresshelper" {
|
||||
let router_b = @router.Router::start(
|
||||
{
|
||||
sam_host: "127.0.0.1",
|
||||
sam_port: 0,
|
||||
ntcp2_host: "127.0.0.1",
|
||||
ntcp2_port: 0,
|
||||
},
|
||||
floodfill=true,
|
||||
)
|
||||
defer router_b.close()
|
||||
let router_a = @router.Router::start({
|
||||
sam_host: "127.0.0.1",
|
||||
sam_port: 0,
|
||||
ntcp2_host: "127.0.0.1",
|
||||
ntcp2_port: 0,
|
||||
})
|
||||
defer router_a.close()
|
||||
let _ = router_a.netdb.store_router_info(router_b.router_info)
|
||||
let _ = router_b.netdb.store_router_info(router_a.router_info)
|
||||
let proxy_port = router_a.start_http_proxy(host="127.0.0.1", port=0)
|
||||
assert_true(proxy_port > 0)
|
||||
assert_eq(router_a.http_proxy_port(), proxy_port)
|
||||
let rs = [router_a, router_b]
|
||||
|
||||
// Bob creates a hidden eepsite destination on Router B and opens SAM STREAM ACCEPT
|
||||
let bob_ctrl = sam_connect_and_hello(router_b.sam_port(), rs)
|
||||
defer bob_ctrl.close()
|
||||
bob_ctrl.write_all(
|
||||
b"SESSION CREATE STYLE=STREAM ID=bobhttp DESTINATION=TRANSIENT\n",
|
||||
)
|
||||
assert_true(
|
||||
pump_read_line(bob_ctrl, rs).has_prefix("SESSION STATUS RESULT=OK"),
|
||||
)
|
||||
bob_ctrl.write_all(b"NAMING LOOKUP NAME=ME\n")
|
||||
let bob_pub_b64 = extract_kv(pump_read_line(bob_ctrl, rs), "VALUE")
|
||||
let bob_b32 = @common.Destination::from_base64(bob_pub_b64).b32_address()
|
||||
|
||||
// 1. Proxied HTTP/1.1 GET over .b32.i2p -> NetDB -> ECIES Garlic -> Streaming
|
||||
let bob_acc1 = sam_connect_and_hello(router_b.sam_port(), rs)
|
||||
defer bob_acc1.close()
|
||||
bob_acc1.write_all(b"STREAM ACCEPT ID=bobhttp SILENT=false\n")
|
||||
assert_eq(pump_read_line(bob_acc1, rs), "STREAM STATUS RESULT=OK")
|
||||
|
||||
let p1 = @ntcp2_net.TcpSocket::connect("127.0.0.1", proxy_port)
|
||||
defer p1.close()
|
||||
let req1 = "GET http://\{bob_b32}/hello?x=1 HTTP/1.1\r\nHost: \{bob_b32}\r\nCookie: sid=abc\r\nProxy-Connection: keep-alive\r\nReferer: http://clearnet.example.com/\r\nConnection: keep-alive\r\n\r\n"
|
||||
p1.write_all(@encoding.string_to_utf8_bytes(req1))
|
||||
|
||||
let peer_b64_1 = pump_read_line(bob_acc1, rs)
|
||||
assert_true(peer_b64_1.length() > 100)
|
||||
let got_req1 = pump_read_until_bytes_contains(bob_acc1, rs, "\r\n\r\n")
|
||||
assert_true(got_req1.has_prefix("GET /hello?x=1 HTTP/1.1\r\n"))
|
||||
assert_true(got_req1.contains("Host: \{bob_b32}\r\n"))
|
||||
assert_true(got_req1.contains("Cookie: sid=abc\r\n"))
|
||||
assert_true(got_req1.contains("Connection: close\r\n"))
|
||||
assert_true(!got_req1.contains("Proxy-Connection"))
|
||||
assert_true(!got_req1.contains("clearnet.example.com"))
|
||||
|
||||
bob_acc1.write_all(
|
||||
b"HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 9\r\n\r\npong-http",
|
||||
)
|
||||
bob_acc1.shutdown_write()
|
||||
let resp1_str = @encoding.utf8_bytes_to_string(pump_read_until_eof(p1, rs))
|
||||
assert_true(resp1_str.has_prefix("HTTP/1.1 200 OK\r\n"))
|
||||
assert_true(resp1_str.has_suffix("pong-http"))
|
||||
|
||||
// 2. Proxied HTTP/1.1 POST over .b32.i2p alias with body forwarding & .i2p Referer
|
||||
router_a.register_alias("bobsite.i2p", bob_b32)
|
||||
let bob_acc2 = sam_connect_and_hello(router_b.sam_port(), rs)
|
||||
defer bob_acc2.close()
|
||||
bob_acc2.write_all(b"STREAM ACCEPT ID=bobhttp SILENT=false\n")
|
||||
assert_eq(pump_read_line(bob_acc2, rs), "STREAM STATUS RESULT=OK")
|
||||
|
||||
let p2 = @ntcp2_net.TcpSocket::connect("127.0.0.1", proxy_port)
|
||||
defer p2.close()
|
||||
p2.write_all(
|
||||
b"POST http://bobsite.i2p/form HTTP/1.1\r\nHost: bobsite.i2p\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 7\r\nReferer: http://bobsite.i2p/\r\n\r\na=1&b=2",
|
||||
)
|
||||
let _ = pump_read_line(bob_acc2, rs)
|
||||
let got_req2 = pump_read_until_bytes_contains(bob_acc2, rs, "a=1&b=2")
|
||||
assert_true(got_req2.has_prefix("POST /form HTTP/1.1\r\n"))
|
||||
assert_true(got_req2.contains("Referer: http://bobsite.i2p/\r\n"))
|
||||
assert_true(got_req2.has_suffix("a=1&b=2"))
|
||||
|
||||
bob_acc2.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 7\r\n\r\npost-ok")
|
||||
bob_acc2.shutdown_write()
|
||||
let resp2_str = @encoding.utf8_bytes_to_string(pump_read_until_eof(p2, rs))
|
||||
assert_true(resp2_str.has_suffix("post-ok"))
|
||||
|
||||
// 3. Clearnet host rejection (403 Forbidden)
|
||||
let p3 = @ntcp2_net.TcpSocket::connect("127.0.0.1", proxy_port)
|
||||
defer p3.close()
|
||||
p3.write_all(b"GET http://example.com/ HTTP/1.1\r\nHost: example.com\r\n\r\n")
|
||||
let resp3_str = @encoding.utf8_bytes_to_string(pump_read_until_eof(p3, rs))
|
||||
assert_true(resp3_str.has_prefix("HTTP/1.1 403 Forbidden\r\n"))
|
||||
|
||||
// 4. CONNECT rejection (501 Not Implemented)
|
||||
let p4 = @ntcp2_net.TcpSocket::connect("127.0.0.1", proxy_port)
|
||||
defer p4.close()
|
||||
p4.write_all(
|
||||
b"CONNECT example.com:443 HTTP/1.1\r\nHost: example.com:443\r\n\r\n",
|
||||
)
|
||||
let resp4_str = @encoding.utf8_bytes_to_string(pump_read_until_eof(p4, rs))
|
||||
assert_true(resp4_str.has_prefix("HTTP/1.1 501 Not Implemented\r\n"))
|
||||
|
||||
// 5. ?i2paddresshelper= jump link registration + 302 redirect
|
||||
let p5 = @ntcp2_net.TcpSocket::connect("127.0.0.1", proxy_port)
|
||||
defer p5.close()
|
||||
let req5 = "GET http://jumpnew.i2p/page?foo=1&i2paddresshelper=\{bob_pub_b64}&bar=2 HTTP/1.1\r\nHost: jumpnew.i2p\r\n\r\n"
|
||||
p5.write_all(@encoding.string_to_utf8_bytes(req5))
|
||||
let resp5_str = @encoding.utf8_bytes_to_string(pump_read_until_eof(p5, rs))
|
||||
assert_true(resp5_str.has_prefix("HTTP/1.1 302 Found\r\n"))
|
||||
assert_true(
|
||||
resp5_str.contains("Location: http://jumpnew.i2p/page?foo=1&bar=2\r\n"),
|
||||
)
|
||||
}
|
||||
|
||||
///|
|
||||
test "Three-router live Proposal 157 multi-hop tunnel build and fragmented 2500B TunnelData stream" {
|
||||
let router_b = @router.Router::start(
|
||||
|
||||
+116
-16
@@ -51,6 +51,10 @@ priv struct RemoteStreamBridge {
|
||||
mut closed : Bool
|
||||
started_ms : UInt64
|
||||
pending_buf : Array[Byte]
|
||||
is_http_proxy : Bool
|
||||
http_host : String
|
||||
mut closing_since_ms : UInt64
|
||||
mut ratchet_reset : Bool
|
||||
}
|
||||
|
||||
///|
|
||||
@@ -133,6 +137,17 @@ fn extract_x25519_key(ls2 : @common.LeaseSet2) -> Bytes? {
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn Router::resolve_b32_alias(self : Router, name : String) -> @common.Hash? {
|
||||
let lower = name.to_lower()
|
||||
for entry in self.b32_aliases {
|
||||
if entry.0 == lower {
|
||||
return parse_b32_hash(entry.1)
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
///|
|
||||
fn Router::resolve_name_local(
|
||||
self : Router,
|
||||
@@ -156,12 +171,20 @@ fn Router::resolve_name_local(
|
||||
return Some(entry.1)
|
||||
}
|
||||
}
|
||||
let now_sec = @ntcp2_net.unix_time_sec()
|
||||
for entry in self.b32_aliases {
|
||||
if entry.0 == lower {
|
||||
match self.netdb.get_lease_set2_by_b32(entry.1, now_sec) {
|
||||
Some(ls2) => return Some(ls2.destination.to_base64())
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
}
|
||||
for s in self.sessions {
|
||||
if s.b32 == lower {
|
||||
return Some(s.pub_b64)
|
||||
}
|
||||
}
|
||||
let now_sec = @ntcp2_net.unix_time_sec()
|
||||
match self.netdb.get_lease_set2_by_b32(lower, now_sec) {
|
||||
Some(ls2) => return Some(ls2.destination.to_base64())
|
||||
None => ()
|
||||
@@ -356,6 +379,10 @@ fn Router::initiate_remote_stream(
|
||||
closed: false,
|
||||
started_ms: now_ms,
|
||||
pending_buf,
|
||||
is_http_proxy: false,
|
||||
http_host: "",
|
||||
closing_since_ms: 0UL,
|
||||
ratchet_reset: false,
|
||||
})
|
||||
self.flush_stream_outbound(
|
||||
caller_sess,
|
||||
@@ -597,8 +624,12 @@ fn Router::handle_sam_command(
|
||||
"NAMING REPLY RESULT=OK NAME=\{name} VALUE=\{val}",
|
||||
)
|
||||
}
|
||||
None =>
|
||||
match parse_b32_hash(name) {
|
||||
None => {
|
||||
let target_opt = match parse_b32_hash(name) {
|
||||
Some(h) => Some(h)
|
||||
None => self.resolve_b32_alias(name)
|
||||
}
|
||||
match target_opt {
|
||||
Some(target_hash) => {
|
||||
let sid = match client.session_id {
|
||||
Some(s) => s
|
||||
@@ -620,6 +651,7 @@ fn Router::handle_sam_command(
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
("SESSION", Some("CREATE")) => {
|
||||
@@ -1105,6 +1137,10 @@ fn Router::step_remote_bridges(self : Router) -> Bool {
|
||||
closed: false,
|
||||
started_ms: now_ms,
|
||||
pending_buf,
|
||||
is_http_proxy: false,
|
||||
http_host: "",
|
||||
closing_since_ms: 0UL,
|
||||
ratchet_reset: false,
|
||||
})
|
||||
did_work = true
|
||||
self.flush_stream_outbound(
|
||||
@@ -1122,6 +1158,29 @@ fn Router::step_remote_bridges(self : Router) -> Bool {
|
||||
}
|
||||
}
|
||||
|
||||
// 1b. Self-heal stalled SynSent streams by dropping potentially expired ECIES ratchet state
|
||||
// before the next SYN retransmit so a fresh Bound NewSession + LeaseSet2 is sent
|
||||
for br in self.remote_bridges {
|
||||
if br.state == 0 && !br.ratchet_reset && now_ms > br.started_ms + 7000UL {
|
||||
br.ratchet_reset = true
|
||||
match self.find_session_by_id(br.session_id) {
|
||||
Some(sess) =>
|
||||
match sess.ratchet {
|
||||
Some(r) => {
|
||||
r.drop_session(br.peer_static_pub)
|
||||
if self.debug {
|
||||
println(
|
||||
"[debug] resetting ECIES ratchet for stalled SYN to \{br.peer_hash.to_b32_address()}",
|
||||
)
|
||||
}
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Pump retransmits & flush outbound packets (ACKs, SYN-ACKs, retransmissions)
|
||||
let rto_ms = if self.public_mode { 3500UL } else { 1500UL }
|
||||
for sess in self.sessions {
|
||||
@@ -1205,7 +1264,19 @@ fn Router::step_remote_bridges(self : Router) -> Bool {
|
||||
"[debug] Streaming failed/timed out to \{br.peer_hash.to_b32_address()}",
|
||||
)
|
||||
}
|
||||
if !br.silent {
|
||||
match sess.ratchet {
|
||||
Some(r) => r.drop_session(br.peer_static_pub)
|
||||
None => ()
|
||||
}
|
||||
let _ = self.netdb.remove_lease_set2(br.peer_hash)
|
||||
if br.is_http_proxy {
|
||||
send_http_proxy_error(
|
||||
br.sock,
|
||||
504,
|
||||
"Gateway Timeout",
|
||||
"Timed out establishing an I2P Streaming connection to \{br.http_host} (\{br.peer_hash.to_b32_address()}).",
|
||||
)
|
||||
} else if !br.silent {
|
||||
let _ = send_sam_line(br.sock, "STREAM STATUS RESULT=CANT_REACH_PEER")
|
||||
}
|
||||
remove_stream_by_id(mgr, br.local_stream_id)
|
||||
@@ -1227,6 +1298,9 @@ fn Router::step_remote_bridges(self : Router) -> Bool {
|
||||
br.sock.write_all(avail) catch {
|
||||
_ => {
|
||||
br.local_eof = true
|
||||
if br.closing_since_ms == 0UL {
|
||||
br.closing_since_ms = now_ms
|
||||
}
|
||||
let _ = conn.close(now_ms) catch { _ => () }
|
||||
}
|
||||
}
|
||||
@@ -1238,13 +1312,31 @@ fn Router::step_remote_bridges(self : Router) -> Bool {
|
||||
}
|
||||
br.sock.shutdown_write()
|
||||
br.remote_eof_sent = true
|
||||
if br.is_http_proxy && !br.local_eof {
|
||||
br.local_eof = true
|
||||
br.closing_since_ms = now_ms
|
||||
let _ = conn.close(now_ms) catch { _ => () }
|
||||
}
|
||||
}
|
||||
if br.pending_buf.length() > 0 {
|
||||
did_work = true
|
||||
let pbytes = Bytes::from_array(br.pending_buf)
|
||||
let wrote = conn.write(pbytes, now_ms) catch { _ => 0 }
|
||||
for _w = 0; _w < wrote; _w = _w + 1 {
|
||||
let _ = br.pending_buf.remove(0)
|
||||
if conn.state != Established && conn.state != SynSent {
|
||||
br.pending_buf.clear()
|
||||
} else {
|
||||
did_work = true
|
||||
let pbytes = Bytes::from_array(br.pending_buf)
|
||||
let wrote = conn.write(pbytes, now_ms) catch { _ => 0 }
|
||||
if wrote >= br.pending_buf.length() {
|
||||
br.pending_buf.clear()
|
||||
} else if wrote > 0 {
|
||||
let rem : Array[Byte] = []
|
||||
for k = wrote; k < br.pending_buf.length(); k = k + 1 {
|
||||
rem.push(br.pending_buf[k])
|
||||
}
|
||||
br.pending_buf.clear()
|
||||
for b in rem {
|
||||
br.pending_buf.push(b)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if !br.local_eof {
|
||||
@@ -1254,6 +1346,9 @@ fn Router::step_remote_bridges(self : Router) -> Bool {
|
||||
let chunk = br.sock.read_some(4096) catch { _ => b"" }
|
||||
if chunk.length() == 0 {
|
||||
br.local_eof = true
|
||||
if br.closing_since_ms == 0UL {
|
||||
br.closing_since_ms = now_ms
|
||||
}
|
||||
let _ = conn.close(now_ms) catch { _ => () }
|
||||
} else {
|
||||
let wrote = conn.write(chunk, now_ms) catch { _ => 0 }
|
||||
@@ -1272,13 +1367,18 @@ fn Router::step_remote_bridges(self : Router) -> Bool {
|
||||
br.lease_tid,
|
||||
send_ls2=false,
|
||||
)
|
||||
if br.local_eof && conn.is_eof() && !conn.has_unacked() {
|
||||
did_work = true
|
||||
remove_stream_by_id(mgr, br.local_stream_id)
|
||||
br.sock.close()
|
||||
br.closed = true
|
||||
let _ = self.remote_bridges.remove(ri)
|
||||
continue
|
||||
if br.local_eof && conn.is_eof() {
|
||||
if br.closing_since_ms == 0UL {
|
||||
br.closing_since_ms = now_ms
|
||||
}
|
||||
if !conn.has_unacked() || now_ms > br.closing_since_ms + 3000UL {
|
||||
did_work = true
|
||||
remove_stream_by_id(mgr, br.local_stream_id)
|
||||
br.sock.close()
|
||||
br.closed = true
|
||||
let _ = self.remote_bridges.remove(ri)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
ri += 1
|
||||
|
||||
Reference in new issue
Block a user