Skip to content

Commit

Permalink
Fetch latest version
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyMeilex committed Dec 16, 2023
1 parent d675ae1 commit 0d26f18
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 0 deletions.
81 changes: 81 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ members = [
"neothesia-core",
"midi-file",
"midi-io",
"dumb-http",
]

default-members = ["neothesia"]
Expand All @@ -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" }
8 changes: 8 additions & 0 deletions dumb-http/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "dumb-http"
version = "0.1.0"
edition = "2021"

[dependencies]
rustls = "0.22"
webpki-roots = "0.26"
56 changes: 56 additions & 0 deletions dumb-http/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::{
io::{Read, Write},
net::{TcpStream, ToSocketAddrs},
sync::{Arc, OnceLock},
};

fn config() -> Arc<rustls::ClientConfig> {
static CONFIG: OnceLock<Arc<rustls::ClientConfig>> = 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 }
}
1 change: 1 addition & 0 deletions neothesia/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
50 changes: 50 additions & 0 deletions neothesia/src/utils/get_version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
fn get_latest() -> Option<String> {

Check warning on line 1 in neothesia/src/utils/get_version.rs

View workflow job for this annotation

GitHub Actions / build_ubuntu

function `get_latest` is never used

Check warning on line 1 in neothesia/src/utils/get_version.rs

View workflow job for this annotation

GitHub Actions / build_ubuntu

function `get_latest` is never used

Check warning on line 1 in neothesia/src/utils/get_version.rs

View workflow job for this annotation

GitHub Actions / build_macos

function `get_latest` is never used

Check warning on line 1 in neothesia/src/utils/get_version.rs

View workflow job for this annotation

GitHub Actions / build_macos

function `get_latest` is never used

Check warning on line 1 in neothesia/src/utils/get_version.rs

View workflow job for this annotation

GitHub Actions / build_windows

function `get_latest` is never used

Check warning on line 1 in neothesia/src/utils/get_version.rs

View workflow job for this annotation

GitHub Actions / build_windows

function `get_latest` is never used
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<String>,

Check warning on line 29 in neothesia/src/utils/get_version.rs

View workflow job for this annotation

GitHub Actions / build_ubuntu

fields `latest` and `current` are never read

Check warning on line 29 in neothesia/src/utils/get_version.rs

View workflow job for this annotation

GitHub Actions / build_ubuntu

fields `latest` and `current` are never read

Check warning on line 29 in neothesia/src/utils/get_version.rs

View workflow job for this annotation

GitHub Actions / build_macos

fields `latest` and `current` are never read

Check warning on line 29 in neothesia/src/utils/get_version.rs

View workflow job for this annotation

GitHub Actions / build_macos

fields `latest` and `current` are never read

Check warning on line 29 in neothesia/src/utils/get_version.rs

View workflow job for this annotation

GitHub Actions / build_windows

fields `latest` and `current` are never read

Check warning on line 29 in neothesia/src/utils/get_version.rs

View workflow job for this annotation

GitHub Actions / build_windows

fields `latest` and `current` are never read
current: &'static str,
}

impl VersionCheck {
pub fn fetch() -> Self {

Check warning on line 34 in neothesia/src/utils/get_version.rs

View workflow job for this annotation

GitHub Actions / build_ubuntu

associated items `fetch`, `latest`, and `is_latest` are never used

Check warning on line 34 in neothesia/src/utils/get_version.rs

View workflow job for this annotation

GitHub Actions / build_ubuntu

associated items `fetch`, `latest`, and `is_latest` are never used

Check warning on line 34 in neothesia/src/utils/get_version.rs

View workflow job for this annotation

GitHub Actions / build_macos

associated items `fetch`, `latest`, and `is_latest` are never used

Check warning on line 34 in neothesia/src/utils/get_version.rs

View workflow job for this annotation

GitHub Actions / build_macos

associated items `fetch`, `latest`, and `is_latest` are never used

Check warning on line 34 in neothesia/src/utils/get_version.rs

View workflow job for this annotation

GitHub Actions / build_windows

associated items `fetch`, `latest`, and `is_latest` are never used

Check warning on line 34 in neothesia/src/utils/get_version.rs

View workflow job for this annotation

GitHub Actions / build_windows

associated items `fetch`, `latest`, and `is_latest` are never used
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)
}
}
1 change: 1 addition & 0 deletions neothesia/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod get_version;
pub mod window;

pub use neothesia_core::utils::*;

0 comments on commit 0d26f18

Please sign in to comment.