chore: clean up, rename types.rs to server.rs
This commit is contained in:
parent
a8bc61f40c
commit
0c68399210
9 changed files with 110 additions and 41 deletions
|
@ -1,16 +1,20 @@
|
|||
use std::{hash, net::{IpAddr, SocketAddr}, pin::Pin, sync::Arc};
|
||||
use std::{net::IpAddr, pin::Pin, sync::Arc};
|
||||
|
||||
use base64::{Engine, prelude::BASE64_STANDARD};
|
||||
use bcrypt::DEFAULT_COST;
|
||||
use bcrypt::bcrypt;
|
||||
use http_body_util::{BodyExt, Full};
|
||||
use hyper::{
|
||||
body::{Bytes, Incoming}, service::Service, Method, Request, Response, StatusCode, Uri
|
||||
Method, Request, Response, StatusCode,
|
||||
body::{Bytes, Incoming},
|
||||
service::Service,
|
||||
};
|
||||
use log::{info, trace};
|
||||
use log::{debug, info, warn};
|
||||
use tokio::{net::TcpStream, sync::Mutex};
|
||||
|
||||
use crate::{
|
||||
config::{Client, Config}, db::{BoxyDatabase, Endpoint}, types::{GeneralBody, GeneralResponse, TcpIntercept}
|
||||
config::{Client, Config},
|
||||
db::{BoxyDatabase, Endpoint},
|
||||
server::{GeneralBody, GeneralResponse, TcpIntercept},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
@ -20,7 +24,7 @@ pub struct ApiService {
|
|||
pub _address: Option<IpAddr>,
|
||||
}
|
||||
|
||||
async fn default_response() -> Response<http_body_util::Either<Incoming, Full<Bytes>>> {
|
||||
async fn default_response() -> GeneralResponse {
|
||||
Response::builder()
|
||||
.status(404)
|
||||
.body(GeneralBody::Right(Full::from(Bytes::from(
|
||||
|
@ -29,7 +33,7 @@ async fn default_response() -> Response<http_body_util::Either<Incoming, Full<By
|
|||
.unwrap()
|
||||
}
|
||||
|
||||
async fn custom_resp(e: StatusCode, m: String) -> Response<http_body_util::Either<Incoming, Full<Bytes>>> {
|
||||
async fn custom_resp(e: StatusCode, m: &'static str) -> GeneralResponse {
|
||||
Response::builder()
|
||||
.status(e)
|
||||
.body(GeneralBody::Right(Full::from(Bytes::from(m))))
|
||||
|
@ -37,7 +41,7 @@ async fn custom_resp(e: StatusCode, m: String) -> Response<http_body_util::Eithe
|
|||
}
|
||||
|
||||
impl TcpIntercept for ApiService {
|
||||
fn handle(&mut self, stream: &TcpStream) {
|
||||
fn stream(&mut self, stream: &TcpStream) {
|
||||
self._address = Some(stream.peer_addr().unwrap().ip());
|
||||
}
|
||||
}
|
||||
|
@ -51,37 +55,73 @@ impl Service<Request<Incoming>> for ApiService {
|
|||
let database = self.database.clone();
|
||||
let config = self.config.clone();
|
||||
let address = self._address.clone().unwrap();
|
||||
|
||||
Box::pin(async move {
|
||||
match *req.method() {
|
||||
Method::POST => match req.uri().path() {
|
||||
"/register" => {
|
||||
let encoded_header = req.headers().get(hyper::header::AUTHORIZATION).unwrap().to_str().unwrap();
|
||||
|
||||
let auth_string = String::from_utf8(BASE64_STANDARD.decode(&encoded_header[6..]).unwrap()).unwrap();
|
||||
|
||||
let auth_string_split: Vec<&str> = auth_string.split(':').collect();
|
||||
debug!("new api register request from {}", address);
|
||||
|
||||
let name = auth_string_split.first().unwrap();
|
||||
let secret = auth_string_split.get(1).unwrap();
|
||||
let encoded_header = req
|
||||
.headers()
|
||||
.get(hyper::header::AUTHORIZATION)
|
||||
.unwrap()
|
||||
.to_str()
|
||||
.unwrap();
|
||||
|
||||
let matched_clients: Vec<&Client> = config.clients.iter().filter(|x| x.name.eq(name)).collect();
|
||||
debug!("authorization header: {}", encoded_header);
|
||||
|
||||
let client = matched_clients.first().unwrap();
|
||||
let auth_string = String::from_utf8(
|
||||
BASE64_STANDARD.decode(&encoded_header[6..]).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
if !bcrypt::verify(secret, client.secret.as_str()).unwrap() {
|
||||
return Ok(custom_resp(StatusCode::UNAUTHORIZED, "Invalid credentials.".to_string()).await);
|
||||
debug!("decoded auth string: {}", auth_string);
|
||||
|
||||
if !Client::verify(auth_string.clone(), config).await {
|
||||
warn!(
|
||||
"Authentication for string {} from {} failed.",
|
||||
auth_string, address
|
||||
);
|
||||
|
||||
return Ok(custom_resp(
|
||||
StatusCode::UNAUTHORIZED,
|
||||
"Invalid credentials.",
|
||||
)
|
||||
.await);
|
||||
}
|
||||
|
||||
let body = String::from_utf8(req.collect().await.unwrap().to_bytes().iter().cloned().collect::<Vec<u8>>()).unwrap();
|
||||
let body = String::from_utf8(
|
||||
req.collect()
|
||||
.await
|
||||
.unwrap()
|
||||
.to_bytes()
|
||||
.iter()
|
||||
.cloned()
|
||||
.collect::<Vec<u8>>(),
|
||||
)
|
||||
.unwrap();
|
||||
let json = json::parse(body.as_str()).unwrap();
|
||||
|
||||
info!("body: {}", body);
|
||||
debug!("body: {}", body);
|
||||
|
||||
let mut endpoint = Endpoint::new(None, address, json["port"].as_u16().unwrap(), json["callback"].as_str().unwrap_or("/").to_string()).await;
|
||||
let mut endpoint = Endpoint::new(
|
||||
None,
|
||||
address,
|
||||
json["port"].as_u16().unwrap(),
|
||||
json["callback"].as_str().unwrap_or("/").to_string(),
|
||||
)
|
||||
.await;
|
||||
|
||||
endpoint.register(*database.lock().await, json["hostname"].as_str().unwrap().to_string()).await.unwrap();
|
||||
endpoint
|
||||
.register(
|
||||
*database.lock().await,
|
||||
json["hostname"].as_str().unwrap().to_string(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
Ok(custom_resp(StatusCode::OK, "yay".to_string()).await)
|
||||
Ok(custom_resp(StatusCode::OK, "").await)
|
||||
}
|
||||
_ => Ok(default_response().await),
|
||||
},
|
||||
|
|
|
@ -12,7 +12,7 @@ use tokio::sync::Mutex;
|
|||
use crate::{
|
||||
config::{self, Client, Config, Host},
|
||||
db::{BoxyDatabase, Endpoint},
|
||||
types::{GeneralBody, GeneralResponse, TcpIntercept},
|
||||
server::{GeneralBody, GeneralResponse, TcpIntercept},
|
||||
};
|
||||
|
||||
use super::proxy::ProxyService;
|
||||
|
@ -23,8 +23,7 @@ pub struct ControllerService {
|
|||
}
|
||||
|
||||
impl TcpIntercept for ControllerService {
|
||||
fn handle(&mut self, stream: &tokio::net::TcpStream) {
|
||||
}
|
||||
fn stream(&mut self, _: &tokio::net::TcpStream) {}
|
||||
}
|
||||
|
||||
impl Service<Request<Incoming>> for ControllerService {
|
||||
|
|
|
@ -5,7 +5,7 @@ use hyper_util::rt::TokioIo;
|
|||
use log::error;
|
||||
use tokio::net::TcpStream;
|
||||
|
||||
use crate::types::{GeneralResponse, to_general_response};
|
||||
use crate::server::{GeneralResponse, to_general_response};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ProxyService {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue