-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
706 additions
and
53 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use std::time::Duration; | ||
|
||
use chia_client::{create_tls_connector, Client, ClientOptions, Event}; | ||
use chia_ssl::ChiaCertificate; | ||
use tokio::time::sleep; | ||
|
||
#[tokio::main] | ||
async fn main() -> anyhow::Result<()> { | ||
env_logger::init(); | ||
|
||
log::info!("Generating certificate"); | ||
let cert = ChiaCertificate::generate()?; | ||
let tls_connector = create_tls_connector(cert.cert_pem.as_bytes(), cert.key_pem.as_bytes())?; | ||
|
||
log::info!("Creating client"); | ||
let (client, mut receiver) = Client::with_options( | ||
tls_connector, | ||
ClientOptions { | ||
target_peers: 20, | ||
..Default::default() | ||
}, | ||
); | ||
|
||
log::info!("Connecting to DNS introducers"); | ||
client.find_peers().await; | ||
|
||
let client_clone = client.clone(); | ||
|
||
tokio::spawn(async move { | ||
loop { | ||
sleep(Duration::from_secs(10)).await; | ||
let count = client_clone.peer_count().await; | ||
log::info!("Currently connected to {} peers", count); | ||
client.find_peers().await; | ||
} | ||
}); | ||
|
||
while let Some(event) = receiver.recv().await { | ||
match event { | ||
Event::Message(peer_id, message) => { | ||
log::info!( | ||
"Received message from peer {}: {:?}", | ||
peer_id, | ||
message.msg_type | ||
); | ||
} | ||
Event::ConnectionClosed(peer_id) => { | ||
log::info!("Peer {} disconnected", peer_id); | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use std::time::Duration; | ||
|
||
use chia_client::{create_tls_connector, Peer}; | ||
use chia_protocol::{Handshake, NodeType, ProtocolMessageTypes}; | ||
use chia_ssl::ChiaCertificate; | ||
use chia_traits::Streamable; | ||
use dns_lookup::lookup_host; | ||
use tokio::time::timeout; | ||
|
||
#[tokio::main] | ||
async fn main() -> anyhow::Result<()> { | ||
env_logger::init(); | ||
|
||
let cert = ChiaCertificate::generate()?; | ||
let tls = create_tls_connector(cert.cert_pem.as_bytes(), cert.key_pem.as_bytes())?; | ||
|
||
for ip in lookup_host("dns-introducer.chia.net")? { | ||
let Ok(response) = | ||
timeout(Duration::from_secs(3), Peer::connect(ip, 8444, tls.clone())).await | ||
else { | ||
log::info!("{ip} exceeded connection timeout of 3 seconds"); | ||
continue; | ||
}; | ||
|
||
let (peer, mut receiver) = response?; | ||
|
||
peer.send(Handshake { | ||
network_id: "mainnet".to_string(), | ||
protocol_version: "0.0.37".to_string(), | ||
software_version: "0.0.0".to_string(), | ||
server_port: 0, | ||
node_type: NodeType::Wallet, | ||
capabilities: vec![ | ||
(1, "1".to_string()), | ||
(2, "1".to_string()), | ||
(3, "1".to_string()), | ||
], | ||
}) | ||
.await?; | ||
|
||
let Ok(message) = timeout(Duration::from_secs(1), receiver.recv()).await else { | ||
log::info!("{ip} exceeded timeout of 1 second"); | ||
continue; | ||
}; | ||
|
||
let Some(message) = message else { | ||
log::info!("{ip} did not send any messages"); | ||
continue; | ||
}; | ||
|
||
if message.msg_type != ProtocolMessageTypes::Handshake { | ||
log::info!("{ip} sent an unexpected message {:?}", message.msg_type); | ||
continue; | ||
} | ||
|
||
let Ok(handshake) = Handshake::from_bytes(&message.data) else { | ||
log::info!("{ip} sent an invalid handshake"); | ||
continue; | ||
}; | ||
|
||
log::info!( | ||
"{ip} handshake sent with protocol version {}", | ||
handshake.protocol_version | ||
); | ||
} | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.