fix some sloppy unwraps, teensy refactor
This commit is contained in:
parent
75bd0000bb
commit
c765db8148
2 changed files with 65 additions and 40 deletions
101
src/main.rs
101
src/main.rs
|
@ -49,17 +49,17 @@ env!("CARGO_PKG_VERSION"));
|
||||||
if args.len() > 2 { address = args[2].to_string() }
|
if args.len() > 2 { address = args[2].to_string() }
|
||||||
let trusted_proxies: Vec<IpAddr> =
|
let trusted_proxies: Vec<IpAddr> =
|
||||||
match env::var("MCSTATUSFACE_TRUSTED_PROXIES") {
|
match env::var("MCSTATUSFACE_TRUSTED_PROXIES") {
|
||||||
|
Err(_) => { vec![] }
|
||||||
Ok(envar) => {
|
Ok(envar) => {
|
||||||
let mut trusted_proxies: Vec<IpAddr> = Vec::new();
|
let mut trusted_proxies: Vec<IpAddr> = Vec::new();
|
||||||
for addr in envar.split(",") {
|
for addr in envar.split(",") {
|
||||||
match IpAddr::from_str(addr) {
|
match IpAddr::from_str(addr) {
|
||||||
Ok(addr) => { trusted_proxies.push(addr); }
|
|
||||||
Err(_) => {}
|
Err(_) => {}
|
||||||
|
Ok(addr) => { trusted_proxies.push(addr); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
trusted_proxies
|
trusted_proxies
|
||||||
}
|
}
|
||||||
Err(_) => { vec![] }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
HttpServer::new(address, 64, trusted_proxies).start(|request, mut response| {
|
HttpServer::new(address, 64, trusted_proxies).start(|request, mut response| {
|
||||||
|
@ -90,6 +90,9 @@ env!("CARGO_PKG_VERSION"));
|
||||||
response.status(StatusCode::OK);
|
response.status(StatusCode::OK);
|
||||||
let query_response: String;
|
let query_response: String;
|
||||||
match request.query().get("s") {
|
match request.query().get("s") {
|
||||||
|
None => {
|
||||||
|
query_response = String::from("");
|
||||||
|
}
|
||||||
Some(query_address) => {
|
Some(query_address) => {
|
||||||
let mut address = query_address.to_string();
|
let mut address = query_address.to_string();
|
||||||
if !address.contains(":") { address.push_str(":25565"); }
|
if !address.contains(":") { address.push_str(":25565"); }
|
||||||
|
@ -103,17 +106,25 @@ env!("CARGO_PKG_VERSION"));
|
||||||
Ok(mut addrs_iter) => {
|
Ok(mut addrs_iter) => {
|
||||||
let address = addrs_iter.next().unwrap();
|
let address = addrs_iter.next().unwrap();
|
||||||
|
|
||||||
let status = MinecraftStatus::fetch(address).unwrap();
|
match MinecraftStatus::fetch(address) {
|
||||||
|
Err(_) => {
|
||||||
|
query_response = format!(
|
||||||
|
"<hr/>
|
||||||
|
<h2>Server Details</h2>
|
||||||
|
<pre><code>Failed to connect to {}.</pre></code>",
|
||||||
|
sanitize_html(&address.to_string()),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Ok(status) => {
|
||||||
|
let minecraft_status = MinecraftStatusResponse{
|
||||||
|
version: &status.version.name,
|
||||||
|
players: status.players.online,
|
||||||
|
max_players: status.players.max,
|
||||||
|
motd: status.parse_description(),
|
||||||
|
};
|
||||||
|
|
||||||
let minecraft_status = MinecraftStatusResponse{
|
query_response = format!(
|
||||||
version: &status.version.name,
|
"<hr/>
|
||||||
players: status.players.online,
|
|
||||||
max_players: status.players.max,
|
|
||||||
motd: status.parse_description(),
|
|
||||||
};
|
|
||||||
|
|
||||||
query_response = format!(
|
|
||||||
"<hr/>
|
|
||||||
<h2>Server Details</h2>
|
<h2>Server Details</h2>
|
||||||
<p>
|
<p>
|
||||||
<strong>Version:</strong> <code>{}</code><br/>
|
<strong>Version:</strong> <code>{}</code><br/>
|
||||||
|
@ -125,20 +136,19 @@ env!("CARGO_PKG_VERSION"));
|
||||||
minecraft_status.players,
|
minecraft_status.players,
|
||||||
minecraft_status.max_players,
|
minecraft_status.max_players,
|
||||||
sanitize_html(&minecraft_status.motd).to_string(),
|
sanitize_html(&minecraft_status.motd).to_string(),
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => {
|
|
||||||
query_response = String::from("");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let response_content = content
|
let response_content = content
|
||||||
.replace("{{response}}", &query_response)
|
.replace("{{response}}", &query_response)
|
||||||
.replace("{{host}}", match request.headers().get("host") {
|
.replace("{{host}}", match request.headers().get("host") {
|
||||||
Some(host) => { host }
|
|
||||||
None => { "mcq.bliss.town" }
|
None => { "mcq.bliss.town" }
|
||||||
|
Some(host) => { host }
|
||||||
});
|
});
|
||||||
response.body(response_content.to_string());
|
response.body(response_content.to_string());
|
||||||
return response.send();
|
return response.send();
|
||||||
|
@ -154,31 +164,44 @@ env!("CARGO_PKG_VERSION"));
|
||||||
Some(query_address) => {
|
Some(query_address) => {
|
||||||
let mut address = query_address.to_string();
|
let mut address = query_address.to_string();
|
||||||
if !address.contains(":") { address.push_str(":25565"); }
|
if !address.contains(":") { address.push_str(":25565"); }
|
||||||
let mut addrs_iter = address.to_socket_addrs().unwrap();
|
match address.to_socket_addrs() {
|
||||||
let address = addrs_iter.next().unwrap();
|
Err(_) => {
|
||||||
|
|
||||||
let status = MinecraftStatus::fetch(address).unwrap();
|
|
||||||
|
|
||||||
let minecraft_status = MinecraftStatusResponse{
|
|
||||||
version: &status.version.name,
|
|
||||||
players: status.players.online,
|
|
||||||
max_players: status.players.max,
|
|
||||||
motd: status.parse_description(),
|
|
||||||
};
|
|
||||||
|
|
||||||
match serde_json::to_string(&minecraft_status) {
|
|
||||||
Ok(json) => {
|
|
||||||
response.status(StatusCode::OK);
|
|
||||||
response.set_header("Content-Type", "application/json".to_string());
|
|
||||||
response.body(json + "\n");
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
eprintln!("Request to {address} failed: {e}");
|
|
||||||
response.status(StatusCode::InternalServerError);
|
response.status(StatusCode::InternalServerError);
|
||||||
response.body("Unable to reach the requested server.\n".to_string());
|
response.body("Invalid server address.\n".to_string());
|
||||||
|
}
|
||||||
|
Ok(mut addrs_iter) => {
|
||||||
|
let address = addrs_iter.next().unwrap();
|
||||||
|
|
||||||
|
match MinecraftStatus::fetch(address) {
|
||||||
|
Err(_) => {
|
||||||
|
response.status(StatusCode::InternalServerError);
|
||||||
|
response.body(format!("Failed to connect to {address}.\n"));
|
||||||
|
}
|
||||||
|
Ok(status) => {
|
||||||
|
let minecraft_status = MinecraftStatusResponse{
|
||||||
|
version: &status.version.name,
|
||||||
|
players: status.players.online,
|
||||||
|
max_players: status.players.max,
|
||||||
|
motd: status.parse_description(),
|
||||||
|
};
|
||||||
|
|
||||||
|
match serde_json::to_string(&minecraft_status) {
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Failed to parse status for {address}: {e}");
|
||||||
|
response.status(StatusCode::InternalServerError);
|
||||||
|
response.body(format!("Failed to parse response from {address}.\n"));
|
||||||
|
}
|
||||||
|
Ok(json) => {
|
||||||
|
response.status(StatusCode::OK);
|
||||||
|
response.set_header("Content-Type", "application/json".to_string());
|
||||||
|
response.body(json + "\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.send()
|
return response.send()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,8 +51,10 @@ impl MinecraftStatus {
|
||||||
pub fn fetch(address: SocketAddr) -> Result<MinecraftStatus> {
|
pub fn fetch(address: SocketAddr) -> Result<MinecraftStatus> {
|
||||||
// println!("Connecting to {address}...");
|
// println!("Connecting to {address}...");
|
||||||
|
|
||||||
let mut stream = TcpStream::connect(address.to_string()).unwrap();
|
let stream = TcpStream::connect(address.to_string());
|
||||||
|
if stream.is_err() { return Err(stream.unwrap_err()); }
|
||||||
// println!("Connected!");
|
// println!("Connected!");
|
||||||
|
let mut stream = stream.unwrap();
|
||||||
|
|
||||||
let mut send_buffer: Vec<u8> = Vec::new();
|
let mut send_buffer: Vec<u8> = Vec::new();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue