main.rs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use darkfi::net;
  19. use darkfi_serial::{AsyncDecodable, VarInt};
  20. use smol::io::AsyncReadExt;
  21. use std::sync::Arc;
  22. use url::Url;
  23. const ENDPOINT: &str = "tcp+tls://lilith1.dark.fi:5262";
  24. async fn ping(endpoint: &str) {
  25. let Ok(endpoint) = Url::parse(endpoint) else {
  26. println!("Invalid endpoint {endpoint}");
  27. return
  28. };
  29. println!("Pinging {endpoint}");
  30. let dialer = net::transport::Dialer::new(endpoint, None, None).await.unwrap();
  31. let timeout = std::time::Duration::from_secs(60);
  32. println!("Connecting...");
  33. let Ok(mut stream) = dialer.dial(Some(timeout)).await else {
  34. println!("Connection failed");
  35. return
  36. };
  37. println!("Connected!");
  38. let mut magic = [0u8; 4];
  39. stream.read_exact(&mut magic).await.unwrap();
  40. println!("read magic bytes {:?}", magic);
  41. let command = String::decode_async(&mut stream).await.unwrap();
  42. println!("read command {command}");
  43. let payload_len = VarInt::decode_async(&mut stream).await.unwrap().0;
  44. println!("payload len = {payload_len}");
  45. let version = net::message::VersionMessage::decode_async(&mut stream).await.unwrap();
  46. println!("version: {version:?}");
  47. }
  48. fn main() {
  49. let args: Vec<String> = std::env::args().collect();
  50. let endpoint = if args.len() == 1 { ENDPOINT } else { &args[1] };
  51. let (signal, shutdown) = smol::channel::unbounded::<()>();
  52. let ex = Arc::new(smol::Executor::new());
  53. let _task = ex.spawn(async {
  54. ping(endpoint).await;
  55. let _ = signal.send(()).await;
  56. });
  57. let _ = smol::future::block_on(ex.run(shutdown.recv()));
  58. }