-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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" |
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 } | ||
} |
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 GitHub Actions / build_ubuntu
Check warning on line 1 in neothesia/src/utils/get_version.rs GitHub Actions / build_ubuntu
Check warning on line 1 in neothesia/src/utils/get_version.rs GitHub Actions / build_macos
Check warning on line 1 in neothesia/src/utils/get_version.rs GitHub Actions / build_macos
Check warning on line 1 in neothesia/src/utils/get_version.rs GitHub Actions / build_windows
|
||
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 GitHub Actions / build_ubuntu
Check warning on line 29 in neothesia/src/utils/get_version.rs GitHub Actions / build_ubuntu
Check warning on line 29 in neothesia/src/utils/get_version.rs GitHub Actions / build_macos
Check warning on line 29 in neothesia/src/utils/get_version.rs GitHub Actions / build_macos
Check warning on line 29 in neothesia/src/utils/get_version.rs GitHub Actions / build_windows
|
||
current: &'static str, | ||
} | ||
|
||
impl VersionCheck { | ||
pub fn fetch() -> Self { | ||
Check warning on line 34 in neothesia/src/utils/get_version.rs GitHub Actions / build_ubuntu
Check warning on line 34 in neothesia/src/utils/get_version.rs GitHub Actions / build_ubuntu
Check warning on line 34 in neothesia/src/utils/get_version.rs GitHub Actions / build_macos
Check warning on line 34 in neothesia/src/utils/get_version.rs GitHub Actions / build_macos
Check warning on line 34 in neothesia/src/utils/get_version.rs GitHub Actions / build_windows
|
||
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) | ||
} | ||
} |
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::*; |