- NTCP2: queue outbound data frames and flush them without blocking (send_blocks_nb, send_i2np_nb, flush_nonblocking) - NTCP2: bound write_all with a deadline and set TCP keepalive and user timeout on peer sockets - Firefox extension 0.1.3: save settings as a patch, keep unsaved form fields from being overwritten on refresh, add timeouts to router API calls, and honor the interceptTorrents toggle in the content script
471 lines
13 KiB
MoonBit
471 lines
13 KiB
MoonBit
///|
|
|
/// Autonomous public I2P network bootstrap: live `.su3` reseed, NAT-friendly
|
|
/// Proposal 157 Inbound/Outbound tunnel pool maintenance, `hosts.txt` aliases,
|
|
/// and periodic NetDB floodfill exploration.
|
|
|
|
///|
|
|
pub let default_reseed_urls : Array[String] = [
|
|
"https://reseed.stormycloud.org/i2pseeds.su3", "https://reseed.diva.exchange/i2pseeds.su3",
|
|
"https://i2p.novg.net/i2pseeds.su3", "https://reseed2.i2p.net/i2pseeds.su3",
|
|
]
|
|
|
|
///|
|
|
/// Reseeds `NetDb` from verified public I2P HTTPS reseed servers until >= 20
|
|
/// routers are imported or all built-in URLs have been tried.
|
|
pub fn Router::auto_reseed(self : Router) -> Int32 {
|
|
let mut total = 0
|
|
for url in default_reseed_urls {
|
|
let n = self.reseed_from_url(url) catch {
|
|
_ => {
|
|
println(" Reseed skipped (\{url})")
|
|
continue
|
|
}
|
|
}
|
|
println(" Reseeded \{n} peer(s) from \{url}")
|
|
total += n
|
|
if total >= 20 {
|
|
break
|
|
}
|
|
}
|
|
total
|
|
}
|
|
|
|
///|
|
|
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,
|
|
) -> Int32 raise RouterError {
|
|
let raw = @ntcp2_net.read_file_bytes(path) catch { e => raise Net(e) }
|
|
let text = @encoding.utf8_bytes_to_string(raw) catch {
|
|
_ => raise Protocol("hosts file is not valid UTF-8")
|
|
}
|
|
let mut count = 0
|
|
for line_view in text.split("\n") {
|
|
let line = line_view.to_owned().trim().to_owned()
|
|
if line.length() == 0 || line.has_prefix("#") {
|
|
continue
|
|
}
|
|
let mut eq = -1
|
|
for i = 0; i < line.length(); i = i + 1 {
|
|
if line[i] == '=' {
|
|
eq = i
|
|
break
|
|
}
|
|
}
|
|
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 || parse_b32_hash(dest_b64) is Some(_)) {
|
|
self.register_alias(host, dest_b64)
|
|
count += 1
|
|
}
|
|
}
|
|
}
|
|
count
|
|
}
|
|
|
|
///|
|
|
fn Router::is_usable_public_peer(
|
|
self : Router,
|
|
h : @common.Hash,
|
|
ri : @common.RouterInfo,
|
|
now_ms : UInt64,
|
|
) -> Bool {
|
|
if h == self.router_hash() ||
|
|
self.is_peer_backed_off(h, now_ms) ||
|
|
ri.identity.inner.crypto_type() != X25519 {
|
|
return false
|
|
}
|
|
let info = @ntcp2_net.extract_ntcp2_peer(ri) catch { _ => return false }
|
|
if info.host.contains(":") ||
|
|
info.host == "127.0.0.1" ||
|
|
info.host == "0.0.0.0" {
|
|
return false
|
|
}
|
|
true
|
|
}
|
|
|
|
///|
|
|
fn Router::is_inbound_build_pending(self : Router) -> Int32 {
|
|
let mut n = 0
|
|
for pb in self.pending_builds {
|
|
if pb.direction == Inbound {
|
|
n += 1
|
|
}
|
|
}
|
|
n
|
|
}
|
|
|
|
///|
|
|
fn Router::is_outbound_build_pending(self : Router) -> Int32 {
|
|
let mut n = 0
|
|
for pb in self.pending_builds {
|
|
if pb.direction == Outbound {
|
|
n += 1
|
|
}
|
|
}
|
|
n
|
|
}
|
|
|
|
///|
|
|
fn is_peer_in_list(h : @common.Hash, list : Array[@common.Hash]) -> Bool {
|
|
for x in list {
|
|
if x == h {
|
|
return true
|
|
}
|
|
}
|
|
false
|
|
}
|
|
|
|
///|
|
|
fn Router::pick_usable_peers(
|
|
self : Router,
|
|
count : Int32,
|
|
excluded : Array[@common.Hash],
|
|
now_ms : UInt64,
|
|
) -> Array[@common.Hash] {
|
|
let out : Array[@common.Hash] = []
|
|
let total = self.netdb.routers.length()
|
|
if total == 0 || count <= 0 {
|
|
return out
|
|
}
|
|
let rbytes = @ntcp2_net.random_bytes(4) catch { _ => Bytes::make(4, b'\x01') }
|
|
let start = u32_from_be_prefix(rbytes).reinterpret_as_int() % total
|
|
for step = 0; step < total && out.length() < count; step = step + 1 {
|
|
let idx = (start + step) % total
|
|
let (h, ri) = self.netdb.routers[idx]
|
|
if is_peer_in_list(h, excluded) || is_peer_in_list(h, out) {
|
|
continue
|
|
}
|
|
if self.is_usable_public_peer(h, ri, now_ms) {
|
|
out.push(h)
|
|
}
|
|
}
|
|
out
|
|
}
|
|
|
|
///|
|
|
fn Router::pick_connected_hop(
|
|
self : Router,
|
|
excluded : Array[@common.Hash],
|
|
now_ms : UInt64,
|
|
) -> @common.Hash? {
|
|
if self.peers.length() == 0 {
|
|
return None
|
|
}
|
|
let rbytes = @ntcp2_net.random_bytes(4) catch { _ => Bytes::make(4, b'\x01') }
|
|
let start = u32_from_be_prefix(rbytes).reinterpret_as_int() %
|
|
self.peers.length()
|
|
for step = 0; step < self.peers.length(); step = step + 1 {
|
|
let idx = (start + step) % self.peers.length()
|
|
let ph = self.peers[idx].hash
|
|
if is_peer_in_list(ph, excluded) || self.is_peer_backed_off(ph, now_ms) {
|
|
continue
|
|
}
|
|
match self.netdb.get_router_info(ph) {
|
|
Some(ri) =>
|
|
if ri.identity.inner.crypto_type() == X25519 {
|
|
return Some(ph)
|
|
}
|
|
None => ()
|
|
}
|
|
}
|
|
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()
|
|
if now_ms < self.last_bootstrap_ms + 1500UL {
|
|
return
|
|
}
|
|
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_nb([@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)
|
|
if self.public_mode {
|
|
self.mark_peer_failed(p.hash, 60000UL)
|
|
}
|
|
}
|
|
} 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 {
|
|
let excl : Array[@common.Hash] = []
|
|
for p in self.peers {
|
|
excl.push(p.hash)
|
|
}
|
|
for pd in self.pending_dials {
|
|
excl.push(pd.peer_hash)
|
|
}
|
|
let need = target_peers -
|
|
(self.peers.length() + self.pending_dials.length())
|
|
let to_dial = self.pick_usable_peers(need, excl, now_ms)
|
|
for ph in to_dial {
|
|
self.ensure_peer_dial(ph)
|
|
}
|
|
}
|
|
|
|
// 2. Maintain >= 2 Inbound tunnels (behind NAT: 1-hop on connected peers first,
|
|
// then upgrade to `tunnel_hops`-hop `[IBGW -> connected_last_hop -> self]` once Outbound exists)
|
|
let pending_in = self.is_inbound_build_pending()
|
|
let mut in_full = 0
|
|
for t in self.tunnels.inbound {
|
|
if t.hops.length() >= self.tunnel_hops {
|
|
in_full += 1
|
|
}
|
|
}
|
|
let need_in_bootstrap = self.tunnels.inbound.length() + pending_in < 2
|
|
let need_in_upgrade = self.tunnel_hops >= 2 &&
|
|
self.tunnels.inbound.length() >= 1 &&
|
|
self.tunnels.outbound.length() >= 1 &&
|
|
in_full < 2 &&
|
|
self.tunnels.inbound.length() + pending_in < 4
|
|
if (need_in_bootstrap || need_in_upgrade) && pending_in < 2 {
|
|
let excl_in : Array[@common.Hash] = []
|
|
for t in self.tunnels.inbound {
|
|
if t.hops.length() > 0 {
|
|
excl_in.push(t.hops[t.hops.length() - 1].router_hash)
|
|
}
|
|
}
|
|
for pb in self.pending_builds {
|
|
if pb.direction == Inbound && pb.hops.length() > 0 {
|
|
excl_in.push(pb.hops[pb.hops.length() - 1].router_hash)
|
|
}
|
|
}
|
|
match self.pick_connected_hop(excl_in, now_ms) {
|
|
Some(h0) =>
|
|
if self.tunnel_hops >= 2 &&
|
|
self.tunnels.inbound.length() >= 1 &&
|
|
self.tunnels.outbound.length() >= 1 {
|
|
let out_t = self.tunnels.outbound[0]
|
|
let excl_ibgw : Array[@common.Hash] = [h0]
|
|
if out_t.hops.length() > 0 {
|
|
excl_ibgw.push(out_t.hops[out_t.hops.length() - 1].router_hash)
|
|
}
|
|
let ibgw_opt = match self.pick_connected_hop(excl_ibgw, now_ms) {
|
|
Some(c) => Some(c)
|
|
None => {
|
|
let usables = self.pick_usable_peers(1, excl_ibgw, now_ms)
|
|
if usables.length() == 1 {
|
|
Some(usables[0])
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
}
|
|
match ibgw_opt {
|
|
Some(ibgw) => {
|
|
let _ = self.build_tunnel(
|
|
[ibgw, h0],
|
|
Inbound,
|
|
outbound_via=Some(out_t),
|
|
) catch {
|
|
_ => 0U
|
|
}
|
|
}
|
|
None => ()
|
|
}
|
|
} else {
|
|
let _ = self.build_tunnel([h0], Inbound) catch { _ => 0U }
|
|
}
|
|
None => ()
|
|
}
|
|
}
|
|
|
|
// 3. Maintain >= 2 Outbound tunnels (replying via an active Inbound tunnel)
|
|
let pending_out = self.is_outbound_build_pending()
|
|
if self.tunnels.inbound.length() >= 1 &&
|
|
self.tunnels.outbound.length() + pending_out < 2 &&
|
|
pending_out < 3 {
|
|
let reply_in = self.tunnels.inbound[0]
|
|
if reply_in.hops.length() > 0 {
|
|
let reply_ibgw = reply_in.hops[0].router_hash
|
|
let excl_out : Array[@common.Hash] = [reply_ibgw]
|
|
for pb in self.pending_builds {
|
|
if pb.direction == Outbound {
|
|
for h in pb.hops {
|
|
excl_out.push(h.router_hash)
|
|
}
|
|
}
|
|
}
|
|
if self.tunnel_hops >= 2 {
|
|
match self.pick_connected_hop(excl_out, now_ms) {
|
|
Some(h0) => {
|
|
excl_out.push(h0)
|
|
let obep_opt = match self.pick_connected_hop(excl_out, now_ms) {
|
|
Some(c) => Some(c)
|
|
None => {
|
|
let obeps = self.pick_usable_peers(1, excl_out, now_ms)
|
|
if obeps.length() == 1 {
|
|
Some(obeps[0])
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
}
|
|
match obep_opt {
|
|
Some(obep) => {
|
|
let _ = self.build_tunnel(
|
|
[h0, obep],
|
|
Outbound,
|
|
reply_tunnel=Some(reply_in),
|
|
) catch {
|
|
_ => 0U
|
|
}
|
|
}
|
|
None => ()
|
|
}
|
|
}
|
|
None => ()
|
|
}
|
|
} else {
|
|
// Prefer an OBEP peer != reply_ibgw; if connected peer available use it, else any usable peer
|
|
match self.pick_connected_hop(excl_out, now_ms) {
|
|
Some(h0) => {
|
|
let _ = self.build_tunnel(
|
|
[h0],
|
|
Outbound,
|
|
reply_tunnel=Some(reply_in),
|
|
) catch {
|
|
_ => 0U
|
|
}
|
|
}
|
|
None => {
|
|
let obeps = self.pick_usable_peers(1, excl_out, now_ms)
|
|
if obeps.length() == 1 {
|
|
let _ = self.build_tunnel(
|
|
[obeps[0]],
|
|
Outbound,
|
|
reply_tunnel=Some(reply_in),
|
|
) catch {
|
|
_ => 0U
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 4. Periodic NetDB exploration once tunnels are up to learn additional live floodfills
|
|
if self.tunnels.inbound.length() >= 1 &&
|
|
self.tunnels.outbound.length() >= 1 &&
|
|
(self.last_explore_ms == 0UL || now_ms > self.last_explore_ms + 30000UL) {
|
|
self.last_explore_ms = now_ms
|
|
let rkey = @common.Hash::from_bytes(next_rand_32()) catch {
|
|
_ => self.router_hash()
|
|
}
|
|
self.start_background_lookup(rkey, 3)
|
|
}
|
|
}
|