Multi-host support and listen address configuration
This commit is contained in:
parent
c3bbc6e541
commit
643c658f4c
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -302,7 +302,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "odproxy"
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
dependencies = [
|
||||
"hyper",
|
||||
"hyperlocal",
|
||||
|
|
|
@ -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
|
||||
|
|
4
conf.rs
4
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<ProxyConf>
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct ProxyConf {
|
||||
pub host: String,
|
||||
pub hosts: Vec<String>,
|
||||
pub target: String,
|
||||
pub socket: Option<bool>,
|
||||
pub spawn: Option<SpawnConf>
|
||||
|
|
|
@ -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"
|
6
data.rs
6
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<String, usize> = generate_host_map();
|
||||
|
@ -52,7 +52,9 @@ pub fn get_proxy_index(host: Option<&HeaderValue>) -> Option<&usize> {
|
|||
pub fn generate_host_map() -> HashMap<String, usize> {
|
||||
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)
|
||||
}
|
10
main.rs
10
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<Body>) -> Result<Response<Body>, 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<Body>) -> Result<Response<Body>, 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);
|
||||
|
|
Loading…
Reference in a new issue