Ver Fonte

ping: added util to ping seed servers for debugging availability

darkfi há 2 anos atrás
pai
commit
69fb1568ed
4 ficheiros alterados com 97 adições e 0 exclusões
  1. 10 0
      Cargo.lock
  2. 1 0
      Cargo.toml
  3. 18 0
      bin/ping/Cargo.toml
  4. 68 0
      bin/ping/src/main.rs

+ 10 - 0
Cargo.lock

@@ -5060,6 +5060,16 @@ version = "0.1.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
 
+[[package]]
+name = "ping"
+version = "0.1.0"
+dependencies = [
+ "darkfi",
+ "darkfi-serial",
+ "smol",
+ "url",
+]
+
 [[package]]
 name = "piper"
 version = "0.2.3"

+ 1 - 0
Cargo.toml

@@ -34,6 +34,7 @@ members = [
     #"bin/tau/tau-cli",
     "bin/vanityaddr",
     "bin/lilith",
+    "bin/ping",
 
     "src/sdk",
     "src/sdk/python",

+ 18 - 0
bin/ping/Cargo.toml

@@ -0,0 +1,18 @@
+[package]
+name = "ping"
+version = "0.1.0"
+homepage = "https://dark.fi"
+description = "Ping tool for servers"
+authors = ["Dyne.org foundation <foundation@dyne.org>"]
+repository = "https://codeberg.org/darkrenaissance/darkfi"
+license = "AGPL-3.0-only"
+edition = "2021"
+
+[dependencies]
+darkfi = {path = "../../", features = ["net"]}
+darkfi-serial = {path = "../../src/serial"}
+url = {version = "2.5.2", features = ["serde"]}
+smol = "2.0.0"
+
+[lints]
+workspace = true

+ 68 - 0
bin/ping/src/main.rs

@@ -0,0 +1,68 @@
+/* This file is part of DarkFi (https://dark.fi)
+ *
+ * Copyright (C) 2020-2024 Dyne.org foundation
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+use darkfi::net;
+use darkfi_serial::{
+    async_trait, AsyncDecodable, AsyncEncodable, SerialDecodable, SerialEncodable, VarInt,
+};
+use smol::{
+    io::{self, AsyncRead, AsyncReadExt, AsyncWriteExt, ReadHalf, WriteHalf},
+    lock::RwLock as AsyncRwLock,
+};
+use std::sync::Arc;
+use url::Url;
+
+const ENDPOINT: &str = "tcp+tls://lilith1.dark.fi:5262";
+
+async fn ping() {
+    println!("Pinging {ENDPOINT}");
+    let endpoint = Url::parse(ENDPOINT).unwrap();
+    let dialer = net::transport::Dialer::new(endpoint, None).await.unwrap();
+    let timeout = std::time::Duration::from_secs(5);
+
+    println!("Connecting...");
+    let Ok(mut stream) = dialer.dial(Some(timeout)).await else {
+        println!("Connection failed");
+        return
+    };
+    println!("Connected!");
+
+    let mut magic = [0u8; 4];
+    stream.read_exact(&mut magic).await.unwrap();
+    println!("read magic bytes {:?}", magic);
+
+    let command = String::decode_async(&mut stream).await.unwrap();
+    println!("read command {command}");
+
+    let payload_len = VarInt::decode_async(&mut stream).await.unwrap().0;
+    println!("payload len = {payload_len}");
+
+    let version = net::message::VersionMessage::decode_async(&mut stream).await.unwrap();
+    println!("version: {version:?}");
+}
+
+fn main() {
+    let (signal, shutdown) = smol::channel::unbounded::<()>();
+
+    let ex = Arc::new(smol::Executor::new());
+    let task = ex.spawn(async {
+        ping().await;
+        let _ = signal.send(()).await;
+    });
+
+    let _ = smol::future::block_on(ex.run(shutdown.recv()));
+}