Compare commits

..

No commits in common. "d2fb78108ee13ec13153d898a40d8c6471203df1" and "f5ef25145828cc13179349ae595f057ce0396ad9" have entirely different histories.

3 changed files with 14 additions and 66 deletions

View file

@ -7,14 +7,9 @@ For more information, see https://minecraft.wiki/w/Query
## Usage
McStatusFace can simply be run with `mcstatusface <address:[port]>`.
McStatusFace can be run as a web server with `mcstatusface serve`. This will
provide server information in JSON format to requests on `GET /?s=<address[:port]>`.
(e.g. `curl -sS "127.0.0.1:8080?s=127.0.0.1:25565" | jq .`)
Alternatively, you can start a web interface with
`mcstatusface serve [address:[port]]`.
This provides a web interface to query server information via a frontend, or
in JSON format if the request is cURLed.
The server queries Minecraft servers when a `GET /?s=<address[:port]>` request
is received. (e.g. `curl -sS "127.0.0.1:8080?s=127.0.0.1:25565" | jq .`)
Alternatively, you can simply run `mcstatusface <address[:port]>`, and the
tool will provide server details in plain-text format.

View file

@ -1,10 +1,4 @@
use std::{
collections::HashMap,
io::{BufRead, BufReader, Error, ErrorKind, Result, Write},
net::{IpAddr, SocketAddr, TcpListener, TcpStream},
str::FromStr,
sync::Arc,
};
use std::{collections::HashMap, io::{BufRead, BufReader, Error, ErrorKind, Result, Write}, net::{SocketAddr, TcpListener, TcpStream}};
use chrono::Local;
@ -51,11 +45,10 @@ pub struct Request<'a> {
headers: HashMap<&'a str, &'a str>,
query: HashMap<&'a str, &'a str>,
body: Option<String>,
real_address: IpAddr,
}
impl<'a> Request<'a> {
pub fn new(stream: &'a TcpStream, lines: &'a Vec<String>, trusted_proxies: Vec<IpAddr>) -> Result<Request<'a>> {
pub fn new(stream: &'a TcpStream, lines: &'a Vec<String>) -> Result<Request<'a>> {
let request_line = lines[0].as_str();
let request_line_split: Vec<&str> = request_line.split(" ").collect();
if request_line_split.len() < 3 {
@ -95,22 +88,6 @@ impl<'a> Request<'a> {
}
}
let mut real_address = IpAddr::from(stream.peer_addr().unwrap().ip());
headers.get("X-Forwarded-For").inspect(|address| {
match IpAddr::from_str(address) {
Ok(address) => {
for proxy in trusted_proxies {
if real_address == proxy {
real_address = address;
break;
}
}
}
Err(_) => {}
}
});
let mut body: Option<String> = None;
if lines.len() > headers.len() + 2 &&
(method == "POST" || method == "PUT") &&
@ -127,16 +104,12 @@ impl<'a> Request<'a> {
headers,
query,
body,
real_address,
})
}
pub fn address(&self) -> Result<SocketAddr> {
self.stream.peer_addr()
}
pub fn real_address(&self) -> &IpAddr {
&self.real_address
}
pub fn path(&self) -> &'a str {
self.path
}
@ -214,12 +187,11 @@ impl<'a> Response<'a> {
pub struct HttpServer {
address: String,
port: u16,
trusted_proxies: Arc<Vec<IpAddr>>,
max_connections: usize,
}
impl HttpServer {
pub fn new(address: String, max_connections: usize, trusted_proxies: Vec<IpAddr>) -> HttpServer {
pub fn new(address: String, max_connections: usize) -> HttpServer {
let mut _address = address.clone();
let mut _port: u16 = 8080;
match address.split_once(":") {
@ -232,7 +204,6 @@ impl HttpServer {
HttpServer {
address: _address,
port: _port,
trusted_proxies: Arc::new(trusted_proxies),
max_connections,
}
}
@ -246,9 +217,8 @@ impl HttpServer {
for stream in listener.incoming() {
match stream {
Ok(stream) => {
let trusted_proxies = self.trusted_proxies.clone();
pool.execute(move || {
HttpServer::handle_client(&stream, handler, trusted_proxies);
HttpServer::handle_client(&stream, handler);
});
}
Err(e) => {
@ -260,7 +230,7 @@ impl HttpServer {
Ok(())
}
fn handle_client(stream: &TcpStream, handler: HttpHandlerFunc, trusted_proxies: Arc<Vec<IpAddr>>) {
fn handle_client(stream: &TcpStream, handler: HttpHandlerFunc) {
let buf_reader = BufReader::new(stream);
let http_request: Vec<String> = buf_reader
.lines()
@ -268,7 +238,7 @@ impl HttpServer {
.take_while(|line| !line.is_empty())
.collect();
let request = Request::new(stream, &http_request, trusted_proxies.to_vec());
let request = Request::new(stream, &http_request);
if request.is_err() {
eprintln!("Failed to process request: {}", request.err().unwrap());
return;
@ -281,8 +251,6 @@ impl HttpServer {
match handler(&request, response) {
Ok(status) => {
let end_date = Local::now();
println!(
"[{}] {} {} {} - {} {} - {}ms - {} ({})",
start_date.format("%Y-%m-%d %H:%M:%S"),
@ -297,7 +265,7 @@ impl HttpServer {
(end_date - start_date).num_milliseconds(),
request.headers().get("User-Agent").map_or("[]", |v| v),
request.real_address().to_string(),
request.address().unwrap().ip(),
);
}
Err(e) => {

View file

@ -1,6 +1,5 @@
use std::io::{Result};
use std::net::{IpAddr, ToSocketAddrs};
use std::str::FromStr;
use std::net::{ToSocketAddrs};
use std::{env};
use mcstatusface::http::{HttpServer, StatusCode};
@ -47,22 +46,8 @@ env!("CARGO_PKG_VERSION"));
let mut address = "0.0.0.0:8080".to_string();
if args.len() > 2 { address = args[2].to_string() }
let trusted_proxies: Vec<IpAddr> =
match env::var("MCSTATUSFACE_TRUSTED_PROXIES") {
Ok(envar) => {
let mut trusted_proxies: Vec<IpAddr> = Vec::new();
for addr in envar.split(",") {
match IpAddr::from_str(addr) {
Ok(addr) => { trusted_proxies.push(addr); }
Err(_) => {}
}
}
trusted_proxies
}
Err(_) => { vec![] }
};
HttpServer::new(address, 64, trusted_proxies).start(|request, mut response| {
HttpServer::new(address, 64).start(|request, mut response| {
response.status(StatusCode::OK);
response.set_header("Content-Type", "text/plain".to_string());
response.set_header("x-powered-by", "GIRL FUEL".to_string());
@ -147,7 +132,7 @@ env!("CARGO_PKG_VERSION"));
// JSON response
match request.query().get("s") {
None => {
response.status(StatusCode::OK);
response.status(StatusCode::BadRequest);
response.body("?s=<server address>\n".to_string());
return response.send();
}