Implement Session version endpoint

This commit is contained in:
nielsandriesse 2021-05-28 13:55:51 +10:00
parent 66730f7dd9
commit 965b448950
5 changed files with 221 additions and 1 deletions

160
Cargo.lock generated
View File

@ -1,5 +1,20 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "addr2line"
version = "0.14.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a55f82cfe485775d02112886f4169bde0c5894d75e79ead7eafe7e40a25e45f7"
dependencies = [
"gimli",
]
[[package]]
name = "adler"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "aead"
version = "0.3.2"
@ -90,6 +105,12 @@ version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dabe5a181f83789739c194cbe5a897dde195078fac08568d09221fd6137a7ba8"
[[package]]
name = "arc-swap"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e906254e445520903e7fc9da4f709886c84ae4bc4ddaf0e093188d66df4dc820"
[[package]]
name = "arrayvec"
version = "0.5.2"
@ -117,6 +138,17 @@ dependencies = [
"syn",
]
[[package]]
name = "async-trait"
version = "0.1.50"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b98e84bbb4cbcdd97da190ba0c58a1bb0de2c1fdf67d159e192ed766aeca722"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "atty"
version = "0.2.14"
@ -134,6 +166,20 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]]
name = "backtrace"
version = "0.3.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ed203b9ba68b242c62b3fb7480f589dd49829be1edb3fe8fc8b4ffda2dcb8d"
dependencies = [
"addr2line",
"cfg-if",
"libc",
"miniz_oxide",
"object",
"rustc-demangle",
]
[[package]]
name = "base64"
version = "0.13.0"
@ -204,6 +250,7 @@ dependencies = [
"libc",
"num-integer",
"num-traits",
"serde",
"time",
"winapi",
]
@ -352,6 +399,12 @@ dependencies = [
"generic-array",
]
[[package]]
name = "doc-comment"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10"
[[package]]
name = "dtoa"
version = "0.4.8"
@ -544,6 +597,12 @@ dependencies = [
"polyval",
]
[[package]]
name = "gimli"
version = "0.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6503fe142514ca4799d4c26297c4248239fe8838d827db6bd6065c6ed29a6ce"
[[package]]
name = "h2"
version = "0.3.2"
@ -717,6 +776,23 @@ dependencies = [
"tokio-native-tls",
]
[[package]]
name = "hyperx"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82566a1ace7f56f604d83b7b2c259c78e243d99c565f23d7b4ae34466442c5a2"
dependencies = [
"base64",
"bytes",
"http",
"httparse",
"httpdate",
"language-tags",
"mime",
"percent-encoding",
"unicase",
]
[[package]]
name = "idna"
version = "0.2.2"
@ -777,6 +853,12 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "language-tags"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a91d884b6667cd606bb5a69aa0c99ba811a115fc68915e7056ec08a46e93199a"
[[package]]
name = "lazy_static"
version = "1.4.0"
@ -851,7 +933,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d1572a880d1115ff867396eee7ae2bc924554225e67a0d3c85c745b3e60ca211"
dependencies = [
"anyhow",
"arc-swap",
"arc-swap 0.4.8",
"chrono",
"derivative",
"fnv",
@ -899,6 +981,16 @@ dependencies = [
"unicase",
]
[[package]]
name = "miniz_oxide"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b"
dependencies = [
"adler",
"autocfg",
]
[[package]]
name = "mio"
version = "0.7.11"
@ -1017,6 +1109,33 @@ dependencies = [
"libc",
]
[[package]]
name = "object"
version = "0.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9a7ab5d64814df0fe4a4b5ead45ed6c5f181ee3ff04ba344313a6c80446c5d4"
[[package]]
name = "octocrab"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c83b19ec5c663a00a5e219d1c92b0f7d78a8ad1fe43a1bb40bc512efa5398337"
dependencies = [
"arc-swap 1.3.0",
"async-trait",
"base64",
"bytes",
"chrono",
"hyperx",
"once_cell",
"reqwest",
"serde",
"serde_json",
"serde_path_to_error",
"snafu",
"url",
]
[[package]]
name = "once_cell"
version = "1.7.2"
@ -1436,6 +1555,12 @@ dependencies = [
"smallvec",
]
[[package]]
name = "rustc-demangle"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "410f7acf3cb3a44527c5d9546bad4bf4e6c460915d5f9f2fc524498bfe8f70ce"
[[package]]
name = "rusticata-macros"
version = "2.1.0"
@ -1575,6 +1700,15 @@ dependencies = [
"serde",
]
[[package]]
name = "serde_path_to_error"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42f6109f0506e20f7e0f910e51a0079acf41da8e0694e6442527c4ddf5a2b158"
dependencies = [
"serde",
]
[[package]]
name = "serde_urlencoded"
version = "0.7.0"
@ -1614,6 +1748,7 @@ dependencies = [
"lazy_static",
"log",
"log4rs",
"octocrab",
"parking_lot",
"r2d2",
"r2d2_sqlite",
@ -1679,6 +1814,28 @@ version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e"
[[package]]
name = "snafu"
version = "0.6.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eab12d3c261b2308b0d80c26fffb58d17eba81a4be97890101f416b478c79ca7"
dependencies = [
"backtrace",
"doc-comment",
"snafu-derive",
]
[[package]]
name = "snafu-derive"
version = "0.6.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1508efa03c362e23817f96cde18abed596a25219a8b2c66e8db33c03543d315b"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "socket2"
version = "0.4.0"
@ -2104,6 +2261,7 @@ dependencies = [
"idna",
"matches",
"percent-encoding",
"serde",
]
[[package]]

View File

@ -24,6 +24,7 @@ http = "0.2"
lazy_static = "1.4"
log = "0.4"
log4rs = "1.0"
octocrab = "0.9"
rand = "0.8"
rand_core = "0.5"
reqwest = { version = "0.11", features = ["json"] }

View File

@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::convert::TryInto;
use std::path::Path;
use std::sync::Mutex;
use chrono;
use log::{error, info, warn};
@ -28,6 +29,13 @@ pub struct GenericStringResponse {
pub result: String,
}
pub const SESSION_VERSION_UPDATE_INTERVAL: i64 = 30 * 60;
lazy_static::lazy_static! {
pub static ref SESSION_VERSIONS: Mutex<HashMap<String, (i64, String)>> = Mutex::new(HashMap::new());
}
// Rooms
// Not publicly exposed.
@ -1001,6 +1009,28 @@ pub async fn get_url() -> Result<Response, Rejection> {
return Ok(warp::reply::json(&url).into_response());
}
pub async fn get_session_version(platform: &str) -> Result<String, Rejection> {
let mut session_versions = SESSION_VERSIONS.lock().unwrap().clone();
let now = chrono::Utc::now().timestamp();
if let Some(version_info) = session_versions.get(platform) {
let last_updated = version_info.0;
if now - last_updated < SESSION_VERSION_UPDATE_INTERVAL {
let tag = version_info.1.to_string();
println!("Returning cached value: {}", tag);
return Ok(tag);
}
}
let octocrab = octocrab::instance();
let repo = format!("session-{}", platform);
let handler = octocrab.repos("oxen-io", repo);
let release = handler.releases().get_latest().await.unwrap();
let tag = release.tag_name;
let tuple = (now, tag.clone());
session_versions.insert(platform.to_string(), tuple);
*SESSION_VERSIONS.lock().unwrap() = session_versions.clone();
return Ok(tag.clone());
}
// Utilities
fn get_pending_tokens(

View File

@ -98,6 +98,27 @@ async fn handle_get_request(
warn!("Invalid endpoint: {}.", rpc_call.endpoint);
return Err(warp::reject::custom(Error::InvalidRpcCall));
}
} else if path.starts_with("session_version") {
match MODE {
Mode::OpenGroupServer => {
warn!("Ignoring RPC call with invalid or unused endpoint: {}.", path);
return Err(warp::reject::custom(Error::InvalidRpcCall));
}
Mode::FileServer => (),
}
let components: Vec<&str> = path.split("/").collect(); // Split on subsequent slashes
if components.len() == 2 {
let platform = components[1];
let version = handlers::get_session_version(platform).await?;
let response = handlers::GenericStringResponse {
status_code: StatusCode::OK.as_u16(),
result: version,
};
return Ok(warp::reply::json(&response).into_response());
} else {
warn!("Invalid endpoint: {}.", rpc_call.endpoint);
return Err(warp::reject::custom(Error::InvalidRpcCall));
}
}
// This route requires auth in open group server mode, but not in file server mode
let pool = get_pool_for_room(&rpc_call)?;

File diff suppressed because one or more lines are too long