From 0d26f18ff151ed6d27897344a2ac4b5136aa914f Mon Sep 17 00:00:00 2001 From: PolyMeilex Date: Sat, 16 Dec 2023 02:14:10 +0100 Subject: [PATCH] Fetch latest version --- Cargo.lock | 81 ++++++++++++++++++++++++++++++ Cargo.toml | 2 + dumb-http/Cargo.toml | 8 +++ dumb-http/src/lib.rs | 56 +++++++++++++++++++++ neothesia/Cargo.toml | 1 + neothesia/src/utils/get_version.rs | 50 ++++++++++++++++++ neothesia/src/utils/mod.rs | 1 + 7 files changed, 199 insertions(+) create mode 100644 dumb-http/Cargo.toml create mode 100644 dumb-http/src/lib.rs create mode 100644 neothesia/src/utils/get_version.rs diff --git a/Cargo.lock b/Cargo.lock index 71e550a6..d4b2954a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -738,6 +738,14 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" +[[package]] +name = "dumb-http" +version = "0.1.0" +dependencies = [ + "rustls", + "webpki-roots", +] + [[package]] name = "either" version = "1.9.0" @@ -2006,6 +2014,7 @@ version = "0.1.0" dependencies = [ "async-thread", "cpal", + "dumb-http", "env_logger", "fluidlite", "fps_ticker", @@ -2810,6 +2819,20 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9b1a3d5f46d53f4a3478e2be4a5a5ce5108ea58b100dcd139830eae7f79a3a1" +[[package]] +name = "ring" +version = "0.17.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "688c63d65483050968b2a8937f7995f443e27041a0f7700aa59b0822aedebb74" +dependencies = [ + "cc", + "getrandom", + "libc", + "spin", + "untrusted", + "windows-sys 0.48.0", +] + [[package]] name = "ron" version = "0.8.1" @@ -2870,6 +2893,37 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "rustls" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe6b63262c9fcac8659abfaa96cac103d28166d3ff3eaf8f412e19f3ae9e5a48" +dependencies = [ + "log", + "ring", + "rustls-pki-types", + "rustls-webpki", + "subtle", + "zeroize", +] + +[[package]] +name = "rustls-pki-types" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7673e0aa20ee4937c6aacfc12bb8341cfbf054cdd21df6bec5fd0629fe9339b" + +[[package]] +name = "rustls-webpki" +version = "0.102.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de2635c8bc2b88d367767c5de8ea1d8db9af3f6219eba28442242d9ab81d1b89" +dependencies = [ + "ring", + "rustls-pki-types", + "untrusted", +] + [[package]] name = "rustybuzz" version = "0.11.0" @@ -3095,6 +3149,12 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6637bab7722d379c8b41ba849228d680cc12d0a45ba1fa2b48f2a30577a06731" +[[package]] +name = "subtle" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" + [[package]] name = "svg_fmt" version = "0.4.1" @@ -3384,6 +3444,12 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + [[package]] name = "vcpkg" version = "0.2.15" @@ -3644,6 +3710,15 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "webpki-roots" +version = "0.26.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0de2cfda980f21be5a7ed2eadb3e6fe074d56022bea2cdeb1a62eb220fc04188" +dependencies = [ + "rustls-pki-types", +] + [[package]] name = "weezl" version = "0.1.7" @@ -4197,6 +4272,12 @@ dependencies = [ "syn 2.0.39", ] +[[package]] +name = "zeroize" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "525b4ec142c6b68a2d10f01f7bbf6755599ca3f81ea53b8431b7dd348f5fdb2d" + [[package]] name = "zune-inflate" version = "0.2.54" diff --git a/Cargo.toml b/Cargo.toml index 6c8117d9..b18bf6d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ members = [ "neothesia-core", "midi-file", "midi-io", + "dumb-http", ] default-members = ["neothesia"] @@ -24,3 +25,4 @@ neothesia = { path = "./neothesia", default-features = false } neothesia-core = { path = "./neothesia-core" } midi-file = { path = "./midi-file" } piano-math = { path = "./piano-math" } +dumb-http = { path = "./dumb-http" } diff --git a/dumb-http/Cargo.toml b/dumb-http/Cargo.toml new file mode 100644 index 00000000..16c59878 --- /dev/null +++ b/dumb-http/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "dumb-http" +version = "0.1.0" +edition = "2021" + +[dependencies] +rustls = "0.22" +webpki-roots = "0.26" diff --git a/dumb-http/src/lib.rs b/dumb-http/src/lib.rs new file mode 100644 index 00000000..108d6b3a --- /dev/null +++ b/dumb-http/src/lib.rs @@ -0,0 +1,56 @@ +use std::{ + io::{Read, Write}, + net::{TcpStream, ToSocketAddrs}, + sync::{Arc, OnceLock}, +}; + +fn config() -> Arc { + static CONFIG: OnceLock> = OnceLock::new(); + CONFIG + .get_or_init(|| { + let root_store = rustls::RootCertStore { + roots: webpki_roots::TLS_SERVER_ROOTS.to_vec(), + }; + + rustls::ClientConfig::builder() + .with_root_certificates(root_store) + .with_no_client_auth() + .into() + }) + .clone() +} + +pub struct Response { + raw: String, +} + +impl Response { + pub fn as_str(&self) -> &str { + &self.raw + } + + pub fn body(&self) -> Option<&str> { + let mut split = self.raw.split("\r\n"); + for segment in &mut split { + if segment.is_empty() { + break; + } + } + split.next() + } +} + +pub fn req(addr: impl ToSocketAddrs, host: &str, req: &str) -> Response { + let example_com = host.to_string().try_into().unwrap(); + let client = rustls::ClientConnection::new(config(), example_com).unwrap(); + let conn = TcpStream::connect(addr).unwrap(); + + let mut tls = rustls::StreamOwned::new(client, conn); + tls.write_all(req.as_bytes()).unwrap(); + + let mut plaintext = Vec::new(); + tls.read_to_end(&mut plaintext).unwrap(); + let raw = String::from_utf8(plaintext).unwrap(); + + Response { raw } +} diff --git a/neothesia/Cargo.toml b/neothesia/Cargo.toml index 26fc2327..e19e7ff5 100644 --- a/neothesia/Cargo.toml +++ b/neothesia/Cargo.toml @@ -13,6 +13,7 @@ fluid-synth = ["synth", "cpal", "fluidlite", "oxisynth"] oxi-synth = ["synth", "cpal", "oxisynth"] [dependencies] +dumb-http.workspace = true futures = { workspace = true } log = { workspace = true } env_logger = { workspace = true } diff --git a/neothesia/src/utils/get_version.rs b/neothesia/src/utils/get_version.rs new file mode 100644 index 00000000..1bb05cf2 --- /dev/null +++ b/neothesia/src/utils/get_version.rs @@ -0,0 +1,50 @@ +fn get_latest() -> Option { + let req = concat!( + "GET /repos/PolyMeilex/Neothesia/releases/latest HTTP/1.1\r\n", + "Host: api.github.com\r\n", + "Connection: close\r\n", + "Accept-Encoding: identity\r\n", + "User-Agent: PostmanRuntime\r\n", + "\r\n" + ); + + let addr = "api.github.com:443"; + let host = "api.github.com"; + + let res = dumb_http::req(addr, host, req); + + let body = res.body().unwrap_or_default(); + + let tag = "\"tag_name\":"; + + let rest = &body[body.find(tag)? + tag.len()..]; + let rest = &rest[rest.find('"')? + 1..]; + let rest = &rest[..rest.find('"')?]; + + Some(rest.to_string()) +} + +#[derive(Debug)] +pub struct VersionCheck { + latest: Option, + current: &'static str, +} + +impl VersionCheck { + pub fn fetch() -> Self { + Self { + latest: get_latest(), + current: env!("CARGO_PKG_VERSION"), + } + } + + pub fn latest(&self) -> Option<&str> { + self.latest.as_deref() + } + + pub fn is_latest(&self) -> bool { + self.latest() + .map(|latest| latest.contains(self.current)) + .unwrap_or(true) + } +} diff --git a/neothesia/src/utils/mod.rs b/neothesia/src/utils/mod.rs index 27b77fbc..8a45cb68 100644 --- a/neothesia/src/utils/mod.rs +++ b/neothesia/src/utils/mod.rs @@ -1,3 +1,4 @@ +pub mod get_version; pub mod window; pub use neothesia_core::utils::*;