From 1b9aeb5b6fe50ab88606486add5596bb11594164 Mon Sep 17 00:00:00 2001 From: lza_menace Date: Mon, 24 Oct 2022 17:05:28 -0700 Subject: [PATCH] making improvements --- Cargo.lock | 1 + Cargo.toml | 2 +- src/app.rs | 136 ++++++++++++++++++++++++++------------------------ src/player.rs | 61 +++++++++++++++------- 4 files changed, 116 insertions(+), 84 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7dd0a01..4a560a6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2871,6 +2871,7 @@ dependencies = [ "regex", "reqwest", "rodio", + "serde", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 52293ef..7fe1d92 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ edition = "2021" [dependencies] libtor = { git = "https://github.com/MagicalBitcoin/libtor" } eframe = { version = "0.19.0", features = ["persistence"] } -# serde = { version = "1", features = ["derive"] } +serde = { version = "1", features = ["derive"] } reqwest = { version = "0.11.12", features = ["blocking", "json", "stream", "socks"] } egui = "0.19.0" rodio = "0.16.0" diff --git a/src/app.rs b/src/app.rs index 7615b18..b04af51 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,3 +1,4 @@ +use std::time::{Duration, SystemTime}; use eframe::egui; use egui::FontFamily::Proportional; use egui::FontId; @@ -41,73 +42,26 @@ impl eframe::App for App { ].into(); ctx.set_style(style); ctx.set_visuals(egui::Visuals::dark()); + egui::TopBottomPanel::top("top").show(ctx, |ui| { ui.heading("Wownero Operations Center"); ui.label("Made by ya boi, lza_menace"); ui.hyperlink("https://lzahq.tech"); }); - egui::CentralPanel::default().show(ctx, |ui| { - ui.horizontal(|ui| { - if ui.radio_value(&mut self.show_irc, false, "tor").clicked() { - self.show_irc = false; - } - if ui.radio_value(&mut !self.show_irc, false, "irc").clicked() { - self.show_irc = true; - } - }); - if ! self.show_irc { - ui.heading("Tor"); - if self.tor_started { - ui.label("Tor started."); - if ui.button("Clear Tor logs").clicked() { - let _r: std::io::Result<()> = crate::clear_tor_log(); - } - if ui.button("Check Connection").clicked() { - if self.player.check_radio_stream() { - self.tor_connected = true; - } - } - ui.collapsing("View Tor logs:", |ui| { - egui::ScrollArea::vertical().stick_to_bottom(true).show(ui, |ui| { - let contents = std::fs::read_to_string(crate::TOR_LOG.to_string()).unwrap(); - ui.label(contents); - }); - }); - } else { - if ui.button("Start Tor").clicked() { - let _t: std::thread::JoinHandle> = crate::start_tor(); - self.tor_started = true; - } - } - } else { - ui.heading("IRC"); - if self.tor_started { - ui.label("Chat wit da braddahs and sistas"); - ui.text_edit_singleline(&mut self.irc_message); - if ui.button("[> Send <]").clicked() { - let _r: std::io::Result<()> = crate::clear_tor_log(); - } - } else { - ui.label("You should connect to Tor network first brah"); - } - } - }); - - // if self.tor_connected { + // Show volume controls + if self.tor_connected { egui::TopBottomPanel::bottom("radio_player").show(ctx, |ui| { ui.heading(egui::RichText::new("WOW!Radio").color(egui::Color32::GREEN)); - ui.label(egui::RichText::new("Your home for the most diabolical playlist of the century, \nmade by the skeevers, scallywags, chupacabras, and whores\n of the Wownero community. Join da chat to peep da scoop.\n").color(egui::Color32::GREEN)); + ui.label(egui::RichText::new("Your home for the most diabolical playlist of the century, made by the skeevers, scallywags, chupacabras, and whores of the Wownero community. Join da chat to peep da scoop.\n").color(egui::Color32::GREEN)); ui.horizontal(|ui| { if self.player.playing { if ui.button("⏸").clicked() { let _ = &self.player.sink.pause(); - // let _ = &self.player.sink.stop(); self.player.playing = false; } ui.add(egui::Slider::new(&mut self.player.volume, 0.0..=100.0)); self.player.sink.set_volume(self.player.volume / 100.0); - // self.player.sink.stop(); } else { if ui.button("▶").clicked() { // If stream thread is done, start again @@ -115,12 +69,15 @@ impl eframe::App for App { self.player.stream_thread = self.player.start_radio_stream(); } if self.player.sink.len() != 1 { - // let rs = self.player.get_radio_source(); + let _ = self.player.wait_for_source(); let f = std::fs::File::open(&crate::RADIO_STREAM); if let Ok(fo) = f { let file = std::io::BufReader::new(fo); - let source = rodio::Decoder::new(file).unwrap(); - let _ = &self.player.sink.append(source); + let source = rodio::Decoder::new(file); + if source.is_err() { + return () + } + let _ = self.player.sink.append(source.unwrap()); } else { return () } @@ -129,19 +86,70 @@ impl eframe::App for App { self.player.playing = true; } } - if ! self.player.stream_thread.is_finished() { - ui.spinner(); - ui.label(format!("{:?}", self.player.stream_source)); - } }); + + // Show spinner when downloading, along with file size + if ! self.player.stream_thread.is_finished() { + ui.horizontal(|ui| { + ui.spinner(); + let size: u64; + let file = std::fs::File::open(crate::RADIO_STREAM); + if file.is_ok() { + size = file.unwrap().metadata().unwrap().len(); + } else { + size = 0; + } + ui.label(format!( + "{:?} -> {} ({} bytes)", + self.player.stream_source, + crate::RADIO_STREAM, + size + )); + }); + } + if self.player.playing { - ui.label(egui::RichText::new(format!("\n{:?}", self.player.get_song_meta().unwrap())).color(egui::Color32::WHITE).size(18.0)); - } else { - ui.label("\n"); + let rt = egui::RichText::new( + format!("\n{:?}", self.player.stream_exif)) + .color(egui::Color32::WHITE) + .size(18.0); + ui.label(rt); + let dur = self.player.exif_date.elapsed().unwrap(); + if dur > Duration::from_secs(10) { + self.player.exif_date = SystemTime::now(); + self.player.stream_exif = self.player.get_song_meta().unwrap(); + } } + ui.label(""); }); - // } else { - // - // } + } + + egui::CentralPanel::default().show(ctx, |ui| { + if self.tor_started { + ui.label("Tor started."); + if ui.button("Clear Tor logs").clicked() { + let _r: std::io::Result<()> = crate::clear_tor_log(); + } + if ui.button("Check Proxy").clicked() { + if self.player.check_proxy() { + self.tor_connected = true; + } + } + ui.collapsing("View Tor logs:", |ui| { + egui::ScrollArea::vertical().stick_to_bottom(true).show(ui, |ui| { + let contents = std::fs::read_to_string(crate::TOR_LOG.to_string()).unwrap(); + ui.label(contents); + }); + }); + } else { + if ui.button("Enter the WOW!Verse").clicked() { + let _t: std::thread::JoinHandle> = crate::start_tor(); + self.tor_started = true; + self.tor_connected = true; + } + } + }); + + } } \ No newline at end of file diff --git a/src/player.rs b/src/player.rs index 4f3145b..605a2b9 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,4 +1,4 @@ -use std::io::Read; +use std::{io::{Read, Seek}, time::SystemTime}; pub struct Player { pub sink: rodio::Sink, @@ -6,6 +6,7 @@ pub struct Player { pub stream_handle: rodio::OutputStreamHandle, pub stream_source: String, pub stream_exif: String, + pub exif_date: SystemTime, pub volume: f32, pub playing: bool } @@ -18,6 +19,7 @@ impl Player { stream_thread: std::thread::spawn(|| {}), stream_source: "https://radio.wownero.com/wow.ogg".to_owned(), stream_exif: "".to_owned(), + exif_date: SystemTime::now(), volume: 100.0, playing: false } @@ -29,49 +31,70 @@ impl Player { Self::new(sink, stream_handle) } - pub fn check_radio_stream(&self) -> bool { - let res: reqwest::blocking::Response = reqwest::blocking::get(self.stream_source.clone()).expect("request failed"); - if res.status().is_success() { - true - } else { - false - } + pub fn check_proxy(&self) -> bool { + let stream = std::net::TcpStream::connect("127.0.0.1:19050"); + return stream.is_ok() } pub fn start_radio_stream(&self) -> std::thread::JoinHandle<()> { let url = self.stream_source.clone(); let t: Result, std::io::Error> = std::thread::Builder::new().name("radio_stream".to_string()).spawn(move || { - let mut res: reqwest::blocking::Response = reqwest::blocking::get(url).expect("request failed"); + let proxy = reqwest::Proxy::all("socks5://127.0.0.1:19050").unwrap(); + let mut res = reqwest::blocking::Client::builder() + .proxy(proxy) + .user_agent("WOC GUI + BoomBox") + .build() + .unwrap() + .get(url) + .send() + .unwrap(); let mut out: std::fs::File = std::fs::File::create(&crate::RADIO_STREAM).expect("failed to create file"); std::io::copy(&mut res, &mut out).expect("failed to copy content"); }); return t.unwrap() } - pub fn wait_for_source() { + pub fn wait_for_source(&self) { loop { let r: Result = std::fs::File::open(&crate::RADIO_STREAM); if r.is_ok() { std::thread::sleep(std::time::Duration::from_secs(1)); return () } - std::thread::sleep(std::time::Duration::from_secs(3)); + std::thread::sleep(std::time::Duration::from_secs(2)); } } pub fn get_song_meta(&self) -> Option { let file = std::fs::File::open(&crate::RADIO_STREAM); - match file { - Ok(mut file) => { - let mut buffer = [0u8; 384]; - file.read_exact(&mut buffer).unwrap(); + if file.is_ok() { + let mut buffer = vec![]; + let file_size = file.unwrap().metadata().unwrap().len(); + let chunk_size = 200000; + let mut start_pos = if file_size < chunk_size { 0 } else { file_size - chunk_size }; + let tries = file_size / chunk_size; + for _i in 0..tries + 1 { + // println!("Scanning radio stream: {} ({} bytes)\nTry: {}", &crate::RADIO_STREAM, file_size, _i); + let mut f = std::fs::File::open(&crate::RADIO_STREAM).unwrap(); + f.seek(std::io::SeekFrom::Start(start_pos)).unwrap(); + f.take(chunk_size).read_to_end(&mut buffer).unwrap(); let s = std::string::String::from_utf8_lossy(&buffer); let re = regex::Regex::new(r"title=(.*).{4}server=").unwrap(); - let caps = re.captures(&s).unwrap(); - Some(caps.get(1).map_or("", |m| m.as_str()).to_owned()) - }, - Err(_e) => Some("".to_owned()) + let caps = re.captures(&s); + if caps.is_some() { + // eat whatever the fuck you want - food moderation and other philosophies + // im 63, i walk everyday, everyone can do that, if they want + // if you're gonna drink, you gotta balance it out + return Some(caps.unwrap().get(1).map_or("", |m| m.as_str()).to_owned()) + } + if start_pos < chunk_size { + start_pos = 0; + } else { + start_pos -= chunk_size; + } + } } + return Some("".to_owned()); } pub fn get_radio_source(&self) -> Result>, ()> {