feat: add ratchet, streaming, SU3 reseed, and router floodfill
- Add ratchet/ with ECIES-X25519-AEAD-Ratchet (Proposal 144), Garlic cloves, and ECIES DatabaseLookup replies - Add streaming/ with Ed25519-signed I2P Streaming (SYN/ACK/NACK/CLOSE/RESET, retransmit, window) and Repliable/Raw Datagrams - Add common/su3.mbt to parse and verify reseed .su3 bundles - Add Elligator2 encoding in crypto/ and OfflineSignature support in LeaseSet2 (flags bit 0) - Router: NTCP2 peer pool, floodfill NetDB crawl, transit tunnel relay, and Garlic/Streaming bridge - cmd/router: add --peer-dir, --su3, --reseed, --floodfill, --no-clock-skew - cmd/client: add --dest for persistent destination keys - Mark Phase 6 complete in README and document the transport scope (NTCP2 only, no SSU2)
This commit is contained in:
1 parent
8eafa11b22
commit
ae72b00323
54 files changed
+9607
-704
No files matched your search
+35
-6
@@ -124,6 +124,15 @@ pub fn SamConfig::with_address(
|
||||
{ ..self, host, port, }
|
||||
}
|
||||
|
||||
///|
|
||||
/// Sets the SAM `DESTINATION` parameter (`"TRANSIENT"` or Base64 private keys).
|
||||
pub fn SamConfig::with_destination(
|
||||
self : SamConfig,
|
||||
destination : String,
|
||||
) -> SamConfig {
|
||||
{ ..self, destination, }
|
||||
}
|
||||
|
||||
///|
|
||||
fn parse_port(s : String) -> Int32 raise SamClientError {
|
||||
if s.length() == 0 {
|
||||
@@ -194,6 +203,26 @@ fn ensure_ok(reply : @sam.SamMessage) -> Unit raise SamClientError {
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn read_sam_reply_handling_ping(
|
||||
conn : TcpConn,
|
||||
) -> @sam.SamMessage raise SamClientError {
|
||||
while true {
|
||||
let reply = parse_sam_reply(conn.read_line())
|
||||
if reply.verb == "PING" {
|
||||
let pong = @sam.SamMessage::new(
|
||||
"PONG",
|
||||
subcommand=reply.subcommand,
|
||||
params=reply.params,
|
||||
)
|
||||
conn.write_string(pong.serialize())
|
||||
continue
|
||||
}
|
||||
return reply
|
||||
}
|
||||
raise Closed
|
||||
}
|
||||
|
||||
///|
|
||||
fn perform_hello(
|
||||
conn : TcpConn,
|
||||
@@ -201,7 +230,7 @@ fn perform_hello(
|
||||
) -> String raise SamClientError {
|
||||
let cmd = @sam.hello_version(min=config.min_version, max=config.max_version)
|
||||
conn.write_string(cmd.serialize())
|
||||
let reply = parse_sam_reply(conn.read_line())
|
||||
let reply = read_sam_reply_handling_ping(conn)
|
||||
if reply.verb != "HELLO" || reply.subcommand != Some("REPLY") {
|
||||
raise ProtocolError("expected HELLO REPLY, got '\{reply.verb}'")
|
||||
}
|
||||
@@ -264,7 +293,7 @@ pub fn SamClient::dest_generate(
|
||||
}
|
||||
let cmd = @sam.dest_generate(signature_type=sig.to_string())
|
||||
self.conn.write_string(cmd.serialize())
|
||||
let reply = parse_sam_reply(self.conn.read_line())
|
||||
let reply = read_sam_reply_handling_ping(self.conn)
|
||||
if reply.verb != "DEST" || reply.subcommand != Some("REPLY") {
|
||||
raise ProtocolError("expected DEST REPLY, got '\{reply.verb}'")
|
||||
}
|
||||
@@ -300,7 +329,7 @@ fn naming_lookup_on_conn(
|
||||
) -> SamDestination raise SamClientError {
|
||||
let cmd = @sam.naming_lookup(name)
|
||||
conn.write_string(cmd.serialize())
|
||||
let reply = parse_sam_reply(conn.read_line())
|
||||
let reply = read_sam_reply_handling_ping(conn)
|
||||
if reply.verb != "NAMING" || reply.subcommand != Some("REPLY") {
|
||||
raise ProtocolError("expected NAMING REPLY, got '\{reply.verb}'")
|
||||
}
|
||||
@@ -344,7 +373,7 @@ pub fn SamClient::session_create(
|
||||
options=opts,
|
||||
)
|
||||
self.conn.write_string(cmd.serialize())
|
||||
let reply = parse_sam_reply(self.conn.read_line())
|
||||
let reply = read_sam_reply_handling_ping(self.conn)
|
||||
if reply.verb != "SESSION" || reply.subcommand != Some("STATUS") {
|
||||
raise ProtocolError("expected SESSION STATUS, got '\{reply.verb}'")
|
||||
}
|
||||
@@ -431,7 +460,7 @@ pub fn SamSession::stream_connect(
|
||||
let _ = perform_hello(data_conn, self.config)
|
||||
let cmd = @sam.stream_connect(self.id, dest_b64, silent=false)
|
||||
data_conn.write_string(cmd.serialize())
|
||||
let reply = parse_sam_reply(data_conn.read_line())
|
||||
let reply = read_sam_reply_handling_ping(data_conn)
|
||||
if reply.verb != "STREAM" || reply.subcommand != Some("STATUS") {
|
||||
raise ProtocolError("expected STREAM STATUS, got '\{reply.verb}'")
|
||||
}
|
||||
@@ -454,7 +483,7 @@ pub fn SamSession::stream_accept(
|
||||
let _ = perform_hello(data_conn, self.config)
|
||||
let cmd = @sam.stream_accept(self.id, silent=false)
|
||||
data_conn.write_string(cmd.serialize())
|
||||
let reply = parse_sam_reply(data_conn.read_line())
|
||||
let reply = read_sam_reply_handling_ping(data_conn)
|
||||
if reply.verb != "STREAM" || reply.subcommand != Some("STATUS") {
|
||||
raise ProtocolError("expected STREAM STATUS, got '\{reply.verb}'")
|
||||
}
|
||||
|
||||
@@ -47,12 +47,74 @@ extern "C" fn c_read_stdin(buf : FixedArray[Byte], max_len : Int32) -> Int32 = "
|
||||
///|
|
||||
extern "C" fn c_flush_stdout() -> Unit = "moon_i2p_flush_stdout"
|
||||
|
||||
///|
|
||||
#borrow(path)
|
||||
extern "C" fn c_file_size(path : Bytes, path_len : Int32) -> Int32 = "moon_i2p_file_size"
|
||||
|
||||
///|
|
||||
#borrow(path, dst)
|
||||
extern "C" fn c_read_file(
|
||||
path : Bytes,
|
||||
path_len : Int32,
|
||||
dst : FixedArray[Byte],
|
||||
max_len : Int32,
|
||||
) -> Int32 = "moon_i2p_read_file"
|
||||
|
||||
///|
|
||||
/// Flushes standard output (useful before blocking on `stream_accept`).
|
||||
pub fn flush_stdout() -> Unit {
|
||||
c_flush_stdout()
|
||||
}
|
||||
|
||||
///|
|
||||
/// Returns `true` if `path` exists as a regular file.
|
||||
pub fn file_exists(path : String) -> Bool {
|
||||
let pb = @encoding.string_to_utf8_bytes(path)
|
||||
c_file_size(pb, pb.length()) >= 0
|
||||
}
|
||||
|
||||
///|
|
||||
/// Reads a UTF-8 text file and strips leading/trailing ASCII whitespace.
|
||||
pub fn read_trimmed_text_file(path : String) -> String raise SamClientError {
|
||||
let pb = @encoding.string_to_utf8_bytes(path)
|
||||
let sz = c_file_size(pb, pb.length())
|
||||
if sz < 0 {
|
||||
raise IoError("failed to stat '\{path}': \{last_os_error()}")
|
||||
}
|
||||
let bytes = if sz == 0 {
|
||||
b""
|
||||
} else {
|
||||
let buf = FixedArray::make(sz, b'\x00')
|
||||
let n = c_read_file(pb, pb.length(), buf, sz)
|
||||
if n < 0 {
|
||||
raise IoError("failed to read '\{path}': \{last_os_error()}")
|
||||
}
|
||||
Bytes::from_array(buf)[:n].to_owned()
|
||||
}
|
||||
let text = @encoding.utf8_bytes_to_string(bytes) catch {
|
||||
_ => raise IoError("invalid UTF-8 in '\{path}'")
|
||||
}
|
||||
let mut start = 0
|
||||
let mut end = text.length()
|
||||
while start < end {
|
||||
let c = text[start].to_int()
|
||||
if c == 32 || c == 9 || c == 13 || c == 10 {
|
||||
start += 1
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
while end > start {
|
||||
let c = text[end - 1].to_int()
|
||||
if c == 32 || c == 9 || c == 13 || c == 10 {
|
||||
end -= 1
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
text[start:end].to_owned()
|
||||
}
|
||||
|
||||
///|
|
||||
fn last_os_error() -> String {
|
||||
let n = c_last_error_len()
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
@@ -192,3 +193,52 @@ MOONBIT_FFI_EXPORT
|
||||
void moon_i2p_flush_stdout(void) {
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t moon_i2p_file_size(moonbit_bytes_t path_bytes, int32_t path_len) {
|
||||
if (path_len <= 0 || path_len >= 1024) {
|
||||
set_last_error("invalid file path");
|
||||
return -1;
|
||||
}
|
||||
char path[1024];
|
||||
memcpy(path, path_bytes, (size_t)path_len);
|
||||
path[path_len] = '\0';
|
||||
struct stat st;
|
||||
if (stat(path, &st) < 0) {
|
||||
set_last_error(strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
if (!S_ISREG(st.st_mode)) {
|
||||
set_last_error("not a regular file");
|
||||
return -1;
|
||||
}
|
||||
return (int32_t)st.st_size;
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t moon_i2p_read_file(
|
||||
moonbit_bytes_t path_bytes,
|
||||
int32_t path_len,
|
||||
moonbit_bytes_t dst,
|
||||
int32_t max_len) {
|
||||
if (path_len <= 0 || path_len >= 1024) {
|
||||
set_last_error("invalid file path");
|
||||
return -1;
|
||||
}
|
||||
char path[1024];
|
||||
memcpy(path, path_bytes, (size_t)path_len);
|
||||
path[path_len] = '\0';
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) {
|
||||
set_last_error(strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
size_t n = fread(dst, 1, (size_t)max_len, f);
|
||||
if (ferror(f)) {
|
||||
set_last_error(strerror(errno));
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
fclose(f);
|
||||
return (int32_t)n;
|
||||
}
|
||||
Reference in new issue
Block a user