Skip to content

Commit 09946ab

Browse files
author
Roman Proskuryakov
authored
Merge pull request #390 from inosms/master
Remove hardcoded keys in example/echo.rs
2 parents a971a93 + 203a132 commit 09946ab

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

examples/echo.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use tox::toxcore::net_crypto::{NetCrypto, NetCryptoNewArgs};
2323
use tox::toxcore::onion::client::OnionClient;
2424
use tox::toxcore::tcp::client::Connections;
2525
use tox::toxcore::stats::Stats;
26+
use tox::toxcore::toxid::ToxId;
2627

2728
const BOOTSTRAP_NODES: [(&str, &str); 9] = [
2829
// Impyy
@@ -45,10 +46,6 @@ const BOOTSTRAP_NODES: [(&str, &str); 9] = [
4546
("2B2137E094F743AC8BD44652C55F41DFACC502F125E99E4FE24D40537489E32F", "5.189.176.217:5190"),
4647
];
4748

48-
const SELF_SK: &str = "1A5EC1D6C3F1FA720A313C01F432B6AE0D4649A5121964C9992DDF32871E8DFD";
49-
50-
const FRIEND_PK: &str = "3E6A06DA48D1AB98549AD76890770B704AE9116D8654FBCD35C9BF2DB9233E21";
51-
5249
/// Bind a UDP listener to the socket address.
5350
fn bind_socket(addr: SocketAddr) -> UdpSocket {
5451
let socket = UdpSocket::bind(&addr).expect("Failed to bind UDP socket");
@@ -64,9 +61,10 @@ fn main() {
6461

6562
let (dht_pk, dht_sk) = gen_keypair();
6663

67-
let real_sk_bytes: [u8; 32] = FromHex::from_hex(SELF_SK).unwrap();
68-
let real_sk = SecretKey::from_slice(&real_sk_bytes).unwrap();
69-
let real_pk = real_sk.public_key();
64+
// create random tox id and print it
65+
let (real_pk, real_sk) = gen_keypair();
66+
let id = ToxId::new(real_pk.clone());
67+
println!("your tox id is: {:X}",id);
7068

7169
// Create a channel for server to communicate with network
7270
let (tx, rx) = mpsc::channel(32);
@@ -91,6 +89,9 @@ fn main() {
9189
let (lossless_tx, lossless_rx) = mpsc::unbounded();
9290
let (lossy_tx, lossy_rx) = mpsc::unbounded();
9391

92+
let (friend_request_tx, friend_request_sink_rx) = mpsc::unbounded();
93+
onion_client.set_friend_request_sink(friend_request_tx);
94+
9495
let net_crypto = NetCrypto::new(NetCryptoNewArgs {
9596
udp_tx: tx,
9697
lossless_tx,
@@ -114,11 +115,6 @@ fn main() {
114115
net_crypto.clone(),
115116
);
116117

117-
let friend_pk_bytes: [u8; 32] = FromHex::from_hex(FRIEND_PK).unwrap();
118-
let friend_pk = PublicKey::from_slice(&friend_pk_bytes).unwrap();
119-
120-
friend_connections.add_friend(friend_pk);
121-
122118
// Bootstrap from nodes
123119
for &(pk, saddr) in &BOOTSTRAP_NODES {
124120
// get PK bytes of the bootstrap node
@@ -163,6 +159,15 @@ fn main() {
163159
}
164160
});
165161

162+
// handle incoming friend connections by just accepting all of them
163+
let friend_connection_c = friend_connections.clone();
164+
let friend_future = friend_request_sink_rx
165+
.map_err(|()| unreachable!("rx can't fail"))
166+
.for_each(move |(pk, _)| {
167+
friend_connection_c.add_friend(pk);
168+
future::ok(())
169+
});
170+
166171
let lossy_future = lossy_rx
167172
.map_err(|()| unreachable!("rx can't fail"))
168173
.for_each(|_| future::ok(()));
@@ -176,6 +181,7 @@ fn main() {
176181
Box::new(friend_connections.run().map_err(Error::from)),
177182
Box::new(lossless_future),
178183
Box::new(lossy_future),
184+
Box::new(friend_future),
179185
];
180186

181187
let future = future::select_all(futures)

src/toxcore/onion/client/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,11 @@ impl OnionClient {
371371
self.state.lock().dht_pk_tx.set(dht_pk_tx);
372372
}
373373

374+
/// Set sink to receive `FriendRequest`s.
375+
pub fn set_friend_request_sink(&self, friend_request_sink: FriendRequestTx) {
376+
self.state.lock().friend_request_tx.set(friend_request_sink)
377+
}
378+
374379
/// Check if a node was pinged recently.
375380
fn is_pinged_recently(&self, pk: PublicKey, search_pk: PublicKey, request_queue: &RequestQueue<AnnounceRequestData>) -> bool {
376381
let check_pks = |data: &AnnounceRequestData| -> bool {

0 commit comments

Comments
 (0)