Add SAM facility to get a new destination
This commit is contained in:

committed by
bonedaddy

parent
1605fa03e1
commit
e92f36e54d
20
examples/gen_dest.rs
Normal file
20
examples/gen_dest.rs
Normal file
@ -0,0 +1,20 @@
|
||||
extern crate env_logger;
|
||||
extern crate i2p;
|
||||
extern crate log;
|
||||
|
||||
use log::*;
|
||||
use std::{thread, time};
|
||||
use std::io::{Read, Write};
|
||||
use std::str::from_utf8;
|
||||
|
||||
use i2p::sam::{SamConnection, DEFAULT_API};
|
||||
|
||||
// Run with RUST_LOG=debug to see the action
|
||||
fn main() {
|
||||
env_logger::init();
|
||||
|
||||
let mut sam_conn = SamConnection::connect(DEFAULT_API).unwrap();
|
||||
let (pubkey, seckey) = sam_conn.generate_destination().unwrap();
|
||||
println!("New public key: {}", pubkey);
|
||||
println!("New secret key: {}", seckey);
|
||||
}
|
21
src/sam.rs
21
src/sam.rs
@ -13,7 +13,7 @@ use rand::{self, Rng};
|
||||
|
||||
use crate::error::{Error, ErrorKind};
|
||||
use crate::net::{I2pAddr, I2pSocketAddr};
|
||||
use crate::parsers::{sam_hello, sam_naming_reply, sam_session_status, sam_stream_status};
|
||||
use crate::parsers::{sam_hello, sam_naming_reply, sam_session_status, sam_stream_status, sam_dest_reply};
|
||||
|
||||
pub static DEFAULT_API: &'static str = "127.0.0.1:7656";
|
||||
|
||||
@ -107,7 +107,6 @@ impl SamConnection {
|
||||
let tcp_stream = TcpStream::connect(addr)?;
|
||||
|
||||
let mut socket = SamConnection { conn: tcp_stream };
|
||||
|
||||
socket.handshake()?;
|
||||
|
||||
Ok(socket)
|
||||
@ -115,11 +114,17 @@ impl SamConnection {
|
||||
|
||||
// TODO: Implement a lookup table
|
||||
pub fn naming_lookup(&mut self, name: &str) -> Result<String, Error> {
|
||||
let create_naming_lookup_msg = format!("NAMING LOOKUP NAME={name} \n", name = name);
|
||||
let ret = self.send(create_naming_lookup_msg, sam_naming_reply)?;
|
||||
let naming_lookup_msg = format!("NAMING LOOKUP NAME={name} \n", name = name);
|
||||
let ret = self.send(naming_lookup_msg, sam_naming_reply)?;
|
||||
Ok(ret["VALUE"].clone())
|
||||
}
|
||||
|
||||
pub fn generate_destination(&mut self) -> Result<(String, String), Error> {
|
||||
let dest_gen_msg = format!("DEST GENERATE \n");
|
||||
let ret = self.send(dest_gen_msg, sam_dest_reply)?;
|
||||
Ok((ret["PUB"].clone(), ret["PRIV"].clone()))
|
||||
}
|
||||
|
||||
pub fn duplicate(&self) -> Result<SamConnection, Error> {
|
||||
self.conn.try_clone().map(|s| SamConnection { conn: s }).map_err(|e| e.into())
|
||||
}
|
||||
@ -200,18 +205,18 @@ impl StreamConnect {
|
||||
let mut sam = SamConnection::connect(session.sam_api()?).unwrap();
|
||||
let dest = sam.naming_lookup(dest)?;
|
||||
|
||||
let mut create_stream_msg = format!(
|
||||
let mut stream_msg = format!(
|
||||
"STREAM CONNECT ID={nickname} DESTINATION={destination} SILENT=false\n",
|
||||
nickname = session.nickname,
|
||||
destination = dest,
|
||||
);
|
||||
if port > 0 {
|
||||
create_stream_msg.push_str(&format!(" TO_PORT={port}\n", port = port));
|
||||
stream_msg.push_str(&format!(" TO_PORT={port}\n", port = port));
|
||||
} else {
|
||||
create_stream_msg.push_str("\n");
|
||||
stream_msg.push_str("\n");
|
||||
}
|
||||
|
||||
sam.send(create_stream_msg, sam_stream_status)?;
|
||||
sam.send(stream_msg, sam_stream_status)?;
|
||||
|
||||
Ok(StreamConnect {
|
||||
sam: sam,
|
||||
|
Reference in New Issue
Block a user