feat: add working native SAMv3 TCP client and CLI with sane defaults
This commit is contained in:
1 parent
e527b84bc3
commit
4d79d78f06
13 files changed
+1910
-11
No files matched your search
@@ -0,0 +1,253 @@
|
||||
///|
|
||||
fn print_usage() -> Unit {
|
||||
println("moon-i2p SAMv3 client (native)")
|
||||
println("")
|
||||
println("Usage:")
|
||||
println(" moon-i2p [--sam <host:port>] <command> [args...]")
|
||||
println("")
|
||||
println("Commands:")
|
||||
println(
|
||||
" keygen Generate a new Ed25519 I2P destination",
|
||||
)
|
||||
println(
|
||||
" lookup <name.i2p> Resolve a .i2p hostname via SAM NAMING LOOKUP",
|
||||
)
|
||||
println(
|
||||
" http-get <host.i2p> [path] HTTP/1.1 GET over a transient I2P STREAM session",
|
||||
)
|
||||
println(
|
||||
" dial <dest-or-host.i2p> [msg] Connect via I2P STREAM, send msg (or stdin), print reply",
|
||||
)
|
||||
println(
|
||||
" listen [reply] Accept one I2P STREAM connection, print data, optionally reply",
|
||||
)
|
||||
println("")
|
||||
println("Options:")
|
||||
println(
|
||||
" --sam <host:port> SAMv3 bridge address (default: 127.0.0.1:7656)",
|
||||
)
|
||||
println(" -h, --help Show this help message")
|
||||
println("")
|
||||
println("Environment:")
|
||||
println(
|
||||
" I2P_SAM_HOST SAMv3 bridge host (default: 127.0.0.1)",
|
||||
)
|
||||
println(" I2P_SAM_PORT SAMv3 bridge port (default: 7656)")
|
||||
}
|
||||
|
||||
///|
|
||||
fn print_error_and_exit(err : @sam_client.SamClientError) -> Unit {
|
||||
println("Error: \{err.message()}")
|
||||
match err {
|
||||
ConnectFailed(_, _, _) => {
|
||||
println("")
|
||||
println(
|
||||
"Hint: Make sure an I2P router with SAMv3 enabled is running, e.g.:",
|
||||
)
|
||||
println(" i2pd --sam.enabled=true")
|
||||
println("Or point to another SAMv3 bridge with:")
|
||||
println(" I2P_SAM_HOST=<host> I2P_SAM_PORT=<port>")
|
||||
println(" --sam <host>:<port>")
|
||||
}
|
||||
_ => ()
|
||||
}
|
||||
@env.exit(1)
|
||||
}
|
||||
|
||||
///|
|
||||
fn bytes_to_display_string(raw : Bytes) -> String {
|
||||
@encoding.utf8_bytes_to_string(raw) catch {
|
||||
_ => "<binary \{raw.length()} bytes: \{@encoding.hex_encode(raw)}>"
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn cmd_keygen(
|
||||
config : @sam_client.SamConfig,
|
||||
) -> Unit raise @sam_client.SamClientError {
|
||||
let client = @sam_client.SamClient::connect(config~)
|
||||
defer client.close()
|
||||
let dest = client.dest_generate()
|
||||
println("SAM Version: \{client.version()}")
|
||||
println("B32 Address: \{dest.b32_address}")
|
||||
println("Public: \{dest.pub_b64}")
|
||||
match dest.priv_b64 {
|
||||
Some(priv_key) => println("Private: \{priv_key}")
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn cmd_lookup(
|
||||
config : @sam_client.SamConfig,
|
||||
name : String,
|
||||
) -> Unit raise @sam_client.SamClientError {
|
||||
let client = @sam_client.SamClient::connect(config~)
|
||||
defer client.close()
|
||||
let dest = client.naming_lookup(name)
|
||||
println("Name: \{name}")
|
||||
println("B32 Address: \{dest.b32_address}")
|
||||
println("Destination: \{dest.pub_b64}")
|
||||
}
|
||||
|
||||
///|
|
||||
fn cmd_http_get(
|
||||
config : @sam_client.SamConfig,
|
||||
host : String,
|
||||
path : String,
|
||||
) -> Unit raise @sam_client.SamClientError {
|
||||
let client = @sam_client.SamClient::connect(config~)
|
||||
let session = client.session_create()
|
||||
defer session.close()
|
||||
let resp = session.http_get(host, path~)
|
||||
println(bytes_to_display_string(resp))
|
||||
}
|
||||
|
||||
///|
|
||||
fn cmd_dial(
|
||||
config : @sam_client.SamConfig,
|
||||
target : String,
|
||||
message : String?,
|
||||
) -> Unit raise @sam_client.SamClientError {
|
||||
let payload = match message {
|
||||
Some(m) => @encoding.string_to_utf8_bytes(m)
|
||||
None => @sam_client.read_all_stdin()
|
||||
}
|
||||
let client = @sam_client.SamClient::connect(config~)
|
||||
let session = client.session_create()
|
||||
defer session.close()
|
||||
let stream = session.stream_connect(target)
|
||||
defer stream.close()
|
||||
if payload.length() > 0 {
|
||||
stream.write_all(payload)
|
||||
}
|
||||
stream.shutdown_write()
|
||||
let resp = stream.read_all()
|
||||
if resp.length() > 0 {
|
||||
println(bytes_to_display_string(resp))
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn cmd_listen(
|
||||
config : @sam_client.SamConfig,
|
||||
reply : String?,
|
||||
) -> Unit raise @sam_client.SamClientError {
|
||||
let client = @sam_client.SamClient::connect(config~)
|
||||
let session = client.session_create()
|
||||
defer session.close()
|
||||
println("Session ID: \{session.id()}")
|
||||
println("B32 Address: \{session.b32_address()}")
|
||||
println("Destination: \{session.destination().pub_b64}")
|
||||
println("Waiting for incoming I2P stream...")
|
||||
@sam_client.flush_stdout()
|
||||
let (peer, stream) = session.stream_accept()
|
||||
defer stream.close()
|
||||
println("Accepted from: \{peer.b32_address}")
|
||||
@sam_client.flush_stdout()
|
||||
let incoming = stream.read_all()
|
||||
if incoming.length() > 0 {
|
||||
println("Received: \{bytes_to_display_string(incoming)}")
|
||||
}
|
||||
match reply {
|
||||
Some(r) => {
|
||||
stream.write_string(r)
|
||||
stream.shutdown_write()
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
@sam_client.flush_stdout()
|
||||
}
|
||||
|
||||
///|
|
||||
fn strip_prefix(s : String, prefix : String) -> String? {
|
||||
if !s.has_prefix(prefix) {
|
||||
return None
|
||||
}
|
||||
let chars : Array[Char] = []
|
||||
for ch in s {
|
||||
chars.push(ch)
|
||||
}
|
||||
let buf = StringBuilder()
|
||||
for i = prefix.length(); i < chars.length(); i = i + 1 {
|
||||
buf.write_char(chars[i])
|
||||
}
|
||||
Some(buf.to_string())
|
||||
}
|
||||
|
||||
///|
|
||||
fn run_cli() -> Unit raise @sam_client.SamClientError {
|
||||
let raw_args = @env.args()
|
||||
let mut config = @sam_client.SamConfig::from_env()
|
||||
let positional : Array[String] = []
|
||||
let mut i = 1
|
||||
while i < raw_args.length() {
|
||||
let arg = raw_args[i]
|
||||
if arg == "-h" || arg == "--help" {
|
||||
print_usage()
|
||||
return
|
||||
} else if arg == "--sam" {
|
||||
i += 1
|
||||
if i >= raw_args.length() {
|
||||
println("Error: missing value after --sam")
|
||||
@env.exit(2)
|
||||
}
|
||||
config = config.with_address(raw_args[i])
|
||||
} else {
|
||||
match strip_prefix(arg, "--sam=") {
|
||||
Some(addr) => config = config.with_address(addr)
|
||||
None => positional.push(arg)
|
||||
}
|
||||
}
|
||||
i += 1
|
||||
}
|
||||
|
||||
if positional.length() == 0 {
|
||||
print_usage()
|
||||
return
|
||||
}
|
||||
|
||||
let cmd = positional[0]
|
||||
if cmd == "keygen" {
|
||||
cmd_keygen(config)
|
||||
} else if cmd == "lookup" {
|
||||
if positional.length() < 2 {
|
||||
println("Usage: moon-i2p lookup <name.i2p>")
|
||||
@env.exit(2)
|
||||
}
|
||||
cmd_lookup(config, positional[1])
|
||||
} else if cmd == "http-get" {
|
||||
if positional.length() < 2 {
|
||||
println("Usage: moon-i2p http-get <host.i2p> [path]")
|
||||
@env.exit(2)
|
||||
}
|
||||
let path = if positional.length() >= 3 { positional[2] } else { "/" }
|
||||
cmd_http_get(config, positional[1], path)
|
||||
} else if cmd == "dial" {
|
||||
if positional.length() < 2 {
|
||||
println("Usage: moon-i2p dial <dest-or-host.i2p> [message]")
|
||||
@env.exit(2)
|
||||
}
|
||||
let msg = if positional.length() >= 3 { Some(positional[2]) } else { None }
|
||||
cmd_dial(config, positional[1], msg)
|
||||
} else if cmd == "listen" {
|
||||
let reply = if positional.length() >= 2 {
|
||||
Some(positional[1])
|
||||
} else {
|
||||
None
|
||||
}
|
||||
cmd_listen(config, reply)
|
||||
} else {
|
||||
println("Unknown command: \{cmd}")
|
||||
println("")
|
||||
print_usage()
|
||||
@env.exit(2)
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn main {
|
||||
run_cli() catch {
|
||||
err => print_error_and_exit(err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user