Projects STRLCPY jscythe Commits a8f519e5
🤬
  • ■ ■ ■ ■ ■
    .gitignore
    1 1  /target
    2 2  /*.json
    3 3  /*.js
     4 +/*.py
  • ■ ■ ■ ■ ■ ■
    Cargo.lock
    skipped 180 lines
    181 181  ]
    182 182   
    183 183  [[package]]
     184 +name = "colored"
     185 +version = "2.0.0"
     186 +source = "registry+https://github.com/rust-lang/crates.io-index"
     187 +checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd"
     188 +dependencies = [
     189 + "atty",
     190 + "lazy_static",
     191 + "winapi 0.3.9",
     192 +]
     193 + 
     194 +[[package]]
    184 195  name = "core-foundation"
    185 196  version = "0.9.3"
    186 197  source = "registry+https://github.com/rust-lang/crates.io-index"
    skipped 426 lines
    613 624  version = "1.0.0"
    614 625  dependencies = [
    615 626   "clap",
     627 + "colored",
    616 628   "lazy_static",
    617 629   "netstat2",
    618 630   "nix",
    skipped 1362 lines
  • ■ ■ ■ ■ ■
    Cargo.toml
    skipped 9 lines
    10 10   
    11 11  [dependencies]
    12 12  clap = { version = "3.2.18", features = ["derive"] }
     13 +colored = "2.0.0"
    13 14  lazy_static = "1.4.0"
    14 15  netstat2 = "0.9.1"
    15 16  nix = "0.25.0"
    skipped 6 lines
  • ■ ■ ■ ■ ■
    src/main.rs
     1 +use std::io::{self, BufRead};
     2 +use std::process::Command;
     3 + 
    1 4  use clap::Parser;
    2 5  use netstat2::*;
    3  -use std::io::{self, BufRead};
    4  -use sysinfo::{PidExt, ProcessExt, SystemExt};
    5  -use sysinfo::{ProcessRefreshKind, RefreshKind, System};
     6 +use sysinfo::{PidExt, ProcessExt, ProcessRefreshKind, RefreshKind, System, SystemExt};
    6 7  use websocket::{ClientBuilder, Message, OwnedMessage};
    7 8   
    8 9  mod protocol;
    skipped 110 lines
    119 120   /// Execute a custom request payload, use '-' to read from stdin.
    120 121   #[clap(long)]
    121 122   custom_payload: Option<String>,
     123 + /// Variable polling time in milliseconds.
     124 + #[clap(long, default_value_t = 1000)]
     125 + poll_interval: u64,
    122 126   /// Poll variable by name at regular intervals.
    123 127   #[clap(long)]
    124 128   poll_variable: Option<String>,
     129 + /// If specified along with --poll-variable, the variable value will be passed to this command
     130 + #[clap(long)]
     131 + poll_command: Option<String>,
    125 132  }
    126 133   
    127 134  fn main() {
    skipped 118 lines
    246 253   
    247 254   let resp = client.recv_message().unwrap();
    248 255   if let OwnedMessage::Text(data) = resp {
    249  - println!("{}", data);
     256 + let result: protocol::responses::ResultMessage =
     257 + serde_json::from_str(&data).unwrap();
     258 + if let Some(ref poll_command) = args.poll_command {
     259 + println!("passing variable value to {} ...", poll_command);
     260 + Command::new(poll_command)
     261 + .arg(&result.result.result.value)
     262 + .spawn()
     263 + .unwrap()
     264 + .wait()
     265 + .unwrap();
     266 + } else {
     267 + println!("{}", result.result.result.value);
     268 + }
    250 269   } else {
    251 270   println!("got non text message: {:?}", resp);
    252 271   }
    253 272   
    254  - std::thread::sleep(std::time::Duration::from_secs(1));
     273 + std::thread::sleep(std::time::Duration::from_millis(args.poll_interval));
    255 274   } else {
    256 275   println!("{:?}", client.recv_message().unwrap());
    257 276   }
    skipped 3 lines
  • ■ ■ ■ ■ ■ ■
    src/protocol.rs
    skipped 99 lines
    100 100   pub(crate) struct Protocol {
    101 101   pub domains: Vec<Domain>,
    102 102   }
     103 + 
     104 + #[derive(Deserialize)]
     105 + pub(crate) struct Result {
     106 + // #[serde(rename(deserialize = "type"))]
     107 + // pub rtype: String,
     108 + pub value: String,
     109 + }
     110 + 
     111 + #[derive(Deserialize)]
     112 + pub(crate) struct ResultWrap {
     113 + pub result: Result,
     114 + }
     115 + 
     116 + #[derive(Deserialize)]
     117 + pub(crate) struct ResultMessage {
     118 + // pub id: usize,
     119 + pub result: ResultWrap,
     120 + }
    103 121  }
    104 122   
    105 123  pub(crate) fn get_domains(port: u16) -> Result<Vec<responses::Domain>, Error> {
    skipped 41 lines
Please wait...
Page is in error, reload to recover