feat: add native MoonBit I2P router core and embedded SAMv3 bridge
This commit is contained in:
1 parent
4866f3c46d
commit
cadb8cd898
6 files changed
+913
-42
No files matched your search
@@ -0,0 +1,120 @@
|
||||
///|
|
||||
fn parse_port(s : String) -> Int32? {
|
||||
if s.length() == 0 {
|
||||
return None
|
||||
}
|
||||
let mut v = 0
|
||||
for i = 0; i < s.length(); i = i + 1 {
|
||||
let c = s[i].to_int()
|
||||
if c < 48 || c > 57 {
|
||||
return None
|
||||
}
|
||||
v = v * 10 + (c - 48)
|
||||
}
|
||||
if v >= 0 && v <= 65535 {
|
||||
Some(v)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn split_host_port(spec : String) -> (String, Int32)? {
|
||||
let mut colon = -1
|
||||
for i = 0; i < spec.length(); i = i + 1 {
|
||||
if spec[i] == ':' {
|
||||
colon = i
|
||||
}
|
||||
}
|
||||
if colon <= 0 {
|
||||
return None
|
||||
}
|
||||
let host = spec[:colon].to_owned()
|
||||
let port_s = spec[colon + 1:].to_owned()
|
||||
match parse_port(port_s) {
|
||||
Some(p) => Some((host, p))
|
||||
None => None
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn main {
|
||||
let args = @env.args()
|
||||
let mut sam_host = "127.0.0.1"
|
||||
let mut sam_port = 7656
|
||||
let mut ntcp2_host = "127.0.0.1"
|
||||
let mut ntcp2_port = 0
|
||||
let peer_files : Array[String] = []
|
||||
|
||||
let mut i = 1
|
||||
while i < args.length() {
|
||||
match args[i] {
|
||||
"--sam" =>
|
||||
if i + 1 < args.length() {
|
||||
match split_host_port(args[i + 1]) {
|
||||
Some((h, p)) => {
|
||||
sam_host = h
|
||||
sam_port = p
|
||||
}
|
||||
None => println("warning: ignoring invalid --sam \{args[i + 1]}")
|
||||
}
|
||||
i += 2
|
||||
} else {
|
||||
i += 1
|
||||
}
|
||||
"--ntcp2-port" =>
|
||||
if i + 1 < args.length() {
|
||||
match parse_port(args[i + 1]) {
|
||||
Some(p) => ntcp2_port = p
|
||||
None => println("warning: ignoring invalid --ntcp2-port")
|
||||
}
|
||||
i += 2
|
||||
} else {
|
||||
i += 1
|
||||
}
|
||||
"--ntcp2-host" =>
|
||||
if i + 1 < args.length() {
|
||||
ntcp2_host = args[i + 1]
|
||||
i += 2
|
||||
} else {
|
||||
i += 1
|
||||
}
|
||||
"--peer" =>
|
||||
if i + 1 < args.length() {
|
||||
peer_files.push(args[i + 1])
|
||||
i += 2
|
||||
} else {
|
||||
i += 1
|
||||
}
|
||||
_ => i += 1
|
||||
}
|
||||
}
|
||||
|
||||
let r = @i2p_router.Router::start({
|
||||
sam_host,
|
||||
sam_port,
|
||||
ntcp2_host,
|
||||
ntcp2_port,
|
||||
}) catch {
|
||||
_ => {
|
||||
println("error: failed to start moon-i2p router")
|
||||
return
|
||||
}
|
||||
}
|
||||
for pf in peer_files {
|
||||
let _ = r.load_peer_file(pf) catch {
|
||||
_ => {
|
||||
println("warning: failed to load peer RouterInfo from \{pf}")
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
println("moon-i2p router started (transient identity)")
|
||||
println(" Router Hash: \{r.router_hash().to_base64()}")
|
||||
println(" Router B32: \{r.router_hash().to_b32_address()}")
|
||||
println(" NTCP2: \{ntcp2_host}:\{r.ntcp2_port()}")
|
||||
println(" SAMv3: \{sam_host}:\{r.sam_port()}")
|
||||
println(" NetDB peers: \{r.netdb.router_count()}")
|
||||
@ntcp2_net.flush_stdout()
|
||||
r.run()
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import {
|
||||
"moonbitlang/core/env",
|
||||
"experiments/moon_i2p/ntcp2_net",
|
||||
"experiments/moon_i2p/router" @i2p_router,
|
||||
}
|
||||
|
||||
pkgtype(kind: "executable")
|
||||
|
||||
supported_targets = "-all+native"
|
||||
Reference in new issue
Block a user