Created
April 19, 2021 01:33
-
-
Save jonatino/3b0ec5f1c2ead721f00106589fde38dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate config; | |
#[macro_use] | |
extern crate serde_derive; | |
use std::{convert::Infallible, net::SocketAddr}; | |
use std::sync::Mutex; | |
use hyper::{Body, Request, Response, Server, StatusCode}; | |
use hyper::service::{make_service_fn, service_fn}; | |
use rand::prelude::SliceRandom; | |
use settings::Settings; | |
mod settings; | |
async fn handle(mut request: Request<Body>) -> Result<Response<Body>, Infallible> { | |
let current_path = request.uri_mut().path(); | |
match current_path { | |
"/game" => { | |
let random_proxy = unsafe { | |
let proxies = PROXIES_MUTEX.as_ref().unwrap().lock().expect("mutex lock failed!"); | |
proxies.game_server.choose(&mut rand::thread_rng()).unwrap().as_str().to_owned() | |
}; | |
Ok(Response::new(Body::from(random_proxy)).into()) | |
} | |
"/update" => { | |
let random_proxy = unsafe { | |
let proxies = PROXIES_MUTEX.as_ref().unwrap().lock().expect("mutex lock failed!"); | |
proxies.update_server.choose(&mut rand::thread_rng()).unwrap().as_str().to_owned() | |
}; | |
Ok(Response::new(Body::from(random_proxy)).into()) | |
} | |
_ => Ok(Response::builder() | |
.status(StatusCode::NOT_FOUND) | |
.body(Body::from("WHO THE FOOK IS THIS GUY?")).unwrap()) | |
} | |
} | |
static mut PROXIES_MUTEX: Option<Mutex<ProxyInfo>> = None; | |
#[tokio::main] | |
async fn main() { | |
let settings = Settings::new().unwrap(); | |
unsafe { | |
PROXIES_MUTEX = Some(Mutex::new(refresh_proxies(settings.redis.url, settings.redis.password, settings.redis.port).unwrap())); | |
} | |
unsafe { | |
let proxies = PROXIES_MUTEX.as_ref().unwrap().lock().expect("mutex lock failed!"); | |
println!("Game server proxies {:?}", proxies.game_server); | |
println!("Update server proxies {:?}", proxies.update_server); | |
} | |
let webserver_address = SocketAddr::from(([127, 0, 0, 1], settings.server.port)); | |
let make_svc = make_service_fn(move |_conn| { | |
async move { | |
Ok::<_, Infallible>(service_fn(move |req| { | |
{ | |
handle(req) | |
} | |
})) | |
} | |
}); | |
let server = Server::bind(&webserver_address).serve(make_svc); | |
println!("Listening on http://{}", webserver_address); | |
if let Err(e) = server.await { | |
eprintln!("Server error: {}", e); | |
} | |
} | |
struct ProxyInfo { | |
pub game_server: Vec<String>, | |
pub update_server: Vec<String>, | |
} | |
fn refresh_proxies(url: String, password: String, port: u16) -> redis::RedisResult<ProxyInfo> { | |
let client = redis::Client::open(format!("redis://:{}@{}:{}/", password, url, port))?; | |
let mut con = client.get_connection()?; | |
let game_server_proxies: Vec<String> = redis::cmd("SMEMBERS").arg("game_server_proxies").query(&mut con)?; | |
let update_server_proxies: Vec<String> = redis::cmd("SMEMBERS").arg("update_server_proxies").query(&mut con)?; | |
let proxy_info = ProxyInfo { | |
game_server: game_server_proxies, | |
update_server: update_server_proxies, | |
}; | |
Ok(proxy_info) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment