From 643c658f4c75e1688cca977e2c86ff086792274b Mon Sep 17 00:00:00 2001 From: Filip Znachor Date: Fri, 21 Apr 2023 16:44:21 +0200 Subject: [PATCH] Multi-host support and listen address configuration --- Cargo.lock | 2 +- Cargo.toml | 2 +- conf.rs | 4 +++- config.toml | 6 ++++-- data.rs | 6 ++++-- main.rs | 10 +++++----- 6 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8676725..b422b20 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -302,7 +302,7 @@ dependencies = [ [[package]] name = "odproxy" -version = "0.1.0" +version = "0.1.1" dependencies = [ "hyper", "hyperlocal", diff --git a/Cargo.toml b/Cargo.toml index f897fce..134200e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "odproxy" -version = "0.1.0" +version = "0.1.1" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/conf.rs b/conf.rs index e671928..1aac9ed 100644 --- a/conf.rs +++ b/conf.rs @@ -1,3 +1,4 @@ +use std::net::SocketAddr; use std::{fs::File, process::exit}; use std::io::prelude::*; use toml::{de::from_str}; @@ -10,12 +11,13 @@ lazy_static! { #[derive(Debug, Deserialize)] pub struct RootConf { + pub listen: SocketAddr, pub proxy: Vec } #[derive(Debug, Deserialize)] pub struct ProxyConf { - pub host: String, + pub hosts: Vec, pub target: String, pub socket: Option, pub spawn: Option diff --git a/config.toml b/config.toml index 5a4992f..18b9945 100644 --- a/config.toml +++ b/config.toml @@ -1,5 +1,7 @@ +listen = "[::]:3000" + [[proxy]] -host = "website.local.gd" +hosts = [ "website.local.gd", "website-alt.local.gd" ] socket = true target = "./www.sock" [proxy.spawn] @@ -8,5 +10,5 @@ args = [ "./webserver/index.mjs" ] envs = [ ["PORT", "www.sock"] ] [[proxy]] -host = "git.local.gd" +hosts = [ "git.local.gd" ] target = "http://192.168.0.3:80" \ No newline at end of file diff --git a/data.rs b/data.rs index 37ad11b..6a743bb 100644 --- a/data.rs +++ b/data.rs @@ -4,7 +4,7 @@ use lazy_static::lazy_static; use std::collections::HashMap; use hyper::http::HeaderValue; -use crate::{conf::{CONFIG, ProxyConf}}; +use crate::conf::{CONFIG, ProxyConf}; lazy_static! { pub static ref HOST_MAP: HashMap = generate_host_map(); @@ -52,7 +52,9 @@ pub fn get_proxy_index(host: Option<&HeaderValue>) -> Option<&usize> { pub fn generate_host_map() -> HashMap { let mut hosts: Vec<(String, usize)> = vec![]; for (i, proxy) in CONFIG.proxy.iter().enumerate() { - hosts.push((proxy.host.to_string(), i)); + for host in proxy.hosts.iter() { + hosts.push((host.to_string(), i)); + }; } HashMap::from_iter(hosts) } \ No newline at end of file diff --git a/main.rs b/main.rs index 53a6d59..de4756a 100644 --- a/main.rs +++ b/main.rs @@ -1,7 +1,7 @@ mod conf; mod data; -use std::{net::SocketAddr, str::FromStr, process::Command, path::Path, time::Duration}; +use std::{str::FromStr, process::Command, path::Path, time::Duration}; use conf::{ProxyConf, SpawnConf}; use data::{HOST_MAP, SERVICES, ServiceData}; use hyperlocal::{UnixClientExt}; @@ -29,7 +29,7 @@ async fn run(req: Request) -> Result, hyper::Error> { let is_socket = p.socket.unwrap_or(false); if is_socket { - request_builder = request_builder.uri(hyperlocal::Uri::new("./www.sock", path)); + request_builder = request_builder.uri(hyperlocal::Uri::new(&p.target, path)); } else { let url = p.target.clone() + path; request_builder = request_builder.uri(hyper::Uri::from_str(url.as_str()).expect("[!] Wrong url address!")); @@ -52,6 +52,7 @@ async fn run(req: Request) -> Result, hyper::Error> { }, None => { + println!("Unknown host accessed: {:?}", host.unwrap()); return Ok(Response::new(Body::empty())); } } @@ -124,12 +125,11 @@ async fn main() { let make_service = Shared::new(service_fn(run)); - let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); - let server = Server::bind(&addr).serve(make_service); + let server = Server::bind(&CONFIG.listen).serve(make_service); let host_count = HOST_MAP.len(); let service_count = CONFIG.proxy.len(); - println!("odproxy is running with {} hosts and {} services", host_count, service_count); + println!("odproxy is listening on {} with {} hosts and {} services", CONFIG.listen, host_count, service_count); if let Err(e) = server.await { println!("error: {}", e);