src: update Peer to allow multiple addresses
This commit is contained in:
parent
ef4eb20629
commit
f9fed6013d
|
@ -11,52 +11,94 @@ enum Protocol {
|
||||||
Wss,
|
Wss,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Peer {
|
enum Handshake {
|
||||||
|
Shs,
|
||||||
|
Shs2,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Address {
|
||||||
protocol: Protocol,
|
protocol: Protocol,
|
||||||
host: IpAddr,
|
host: IpAddr,
|
||||||
port: u16,
|
port: u16,
|
||||||
pubkey: PublicKey,
|
handshake: Handshake,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Peer {
|
||||||
|
addresses: Vec<Address>,
|
||||||
|
key: PublicKey,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Peer {
|
impl Peer {
|
||||||
|
// TODO: do this properly
|
||||||
pub fn to_discovery_packet(&self) -> String {
|
pub fn to_discovery_packet(&self) -> String {
|
||||||
let proto = match self.protocol {
|
let parts: Vec<String> = self
|
||||||
|
.addresses
|
||||||
|
.iter()
|
||||||
|
.map(|address| {
|
||||||
|
let proto = match address.protocol {
|
||||||
Protocol::Net => "net",
|
Protocol::Net => "net",
|
||||||
Protocol::Ws => "ws",
|
Protocol::Ws => "ws",
|
||||||
Protocol::Wss => "wss",
|
Protocol::Wss => "wss",
|
||||||
};
|
};
|
||||||
|
let hs = match address.handshake {
|
||||||
|
Handshake::Shs => "shs",
|
||||||
|
Handshake::Shs2 => "shs2",
|
||||||
|
};
|
||||||
format!(
|
format!(
|
||||||
"{}:{}:{}~shs:{}",
|
"{}:{}:{}~{}:{}",
|
||||||
proto,
|
proto,
|
||||||
self.host,
|
address.host,
|
||||||
self.port,
|
address.port,
|
||||||
self.pubkey.to_base64()
|
hs,
|
||||||
|
self.key.to_base64(),
|
||||||
)
|
)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
parts.join(";")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: do this properly
|
||||||
pub fn from_discovery_packet(packet: &str) -> Self {
|
pub fn from_discovery_packet(packet: &str) -> Self {
|
||||||
let mut packet = packet.splitn(4, ':');
|
let mut key = Option::None;
|
||||||
let protocol = match packet.next().unwrap() {
|
let addresses = packet
|
||||||
|
.split(';')
|
||||||
|
.map(|address| {
|
||||||
|
let mut address = address.splitn(2, '~');
|
||||||
|
|
||||||
|
let mut network = address.next().unwrap().splitn(3, ':');
|
||||||
|
let protocol = match network.next().unwrap() {
|
||||||
"net" => Protocol::Net,
|
"net" => Protocol::Net,
|
||||||
"ws" => Protocol::Ws,
|
"ws" => Protocol::Ws,
|
||||||
"wss" => Protocol::Wss,
|
"wss" => Protocol::Wss,
|
||||||
_ => panic!("unknown protocol"),
|
_ => panic!("unknown protocol"),
|
||||||
};
|
};
|
||||||
let host = IpAddr::V4(packet.next().unwrap().parse().unwrap());
|
let host = IpAddr::V4(network.next().unwrap().parse().unwrap());
|
||||||
let port = packet
|
let port = network.next().unwrap().parse().unwrap();
|
||||||
.next()
|
|
||||||
.unwrap()
|
let mut info = address.next().unwrap().splitn(2, ':');
|
||||||
.splitn(2, '~')
|
let handshake = match info.next().unwrap() {
|
||||||
.next()
|
"shs" => Handshake::Shs,
|
||||||
.unwrap()
|
"shs2" => Handshake::Shs2,
|
||||||
.parse()
|
_ => panic!("unknown handshake"),
|
||||||
.unwrap();
|
};
|
||||||
let pubkey = SSBPublicKey::from_base64(packet.next().unwrap());
|
let pubkey = SSBPublicKey::from_base64(info.next().unwrap());
|
||||||
Peer {
|
if key == Option::None {
|
||||||
|
key = Some(pubkey);
|
||||||
|
} else if key.unwrap() != pubkey {
|
||||||
|
panic!("unexpected pubkey");
|
||||||
|
}
|
||||||
|
|
||||||
|
Address {
|
||||||
protocol,
|
protocol,
|
||||||
host,
|
host,
|
||||||
port,
|
port,
|
||||||
pubkey,
|
handshake,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
Peer {
|
||||||
|
addresses,
|
||||||
|
key: key.unwrap(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue