Skip to content

Instantly share code, notes, and snippets.

@dustyfresh
Created December 28, 2020 23:05
Show Gist options
  • Save dustyfresh/cf6842b96dde4f956053fe33fb8ffab0 to your computer and use it in GitHub Desktop.
Save dustyfresh/cf6842b96dde4f956053fe33fb8ffab0 to your computer and use it in GitHub Desktop.
certstream logger written in Rust
[package]
name = "certstream-logger"
version = "0.1.0"
authors = [""]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
websocket = "0.26.2"
ctrlc = "3.1.7"
uuid = { version = "0.8", features = ["v4"] }
use uuid::Uuid;
use std::process;
use std::time::{Instant};
use std::fs::OpenOptions;
use std::io::{Write, BufWriter};
use websocket::{ClientBuilder, OwnedMessage};
// Websocket certstream feed
const CERTSTREAM: &'static str = "wss://certstream.calidog.io";
fn main() {
ctrlc::set_handler(move || {
println!("received Ctrl+C! Quitting...");
process::exit(0x0100);
}).expect("Error setting Ctrl-C handler");
loop {
// establish log file for this connection
let log_name = format!("certstream-{}.json", Uuid::new_v4());
let mut outfile = BufWriter::new(OpenOptions::new()
.write(true)
.append(true)
.create(true)
.open(log_name)
.unwrap());
// websocket client for retrieving messages
let mut client = ClientBuilder::new(CERTSTREAM).unwrap().connect_secure(None).unwrap();
println!("Successfully connected to certstream!");
// timer var for keeping track of how long the connection to certstream was open
let timer = Instant::now();
// loop through each message coming in from the websocket client
for message in client.incoming_messages() {
match message {
Ok(message) => {
if let OwnedMessage::Text(s) = message {
// Write the json message to the certstream log file
writeln!(outfile, "{}", &s).expect("Error!");
}
},
Err(err) => {
// log error message and break so that we reconnect again
println!("{:?} -- connected to stream for {:?}, reconnecting...", err, timer.elapsed());
break
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment