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
@@ -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(
|
||||
|
||||
Reference in new issue
Block a user