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]]
|
[[package]]
|
||||||
name = "odproxy"
|
name = "odproxy"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"hyper",
|
"hyper",
|
||||||
"hyperlocal",
|
"hyperlocal",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "odproxy"
|
name = "odproxy"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# 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::{fs::File, process::exit};
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use toml::{de::from_str};
|
use toml::{de::from_str};
|
||||||
|
@ -10,12 +11,13 @@ lazy_static! {
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct RootConf {
|
pub struct RootConf {
|
||||||
|
pub listen: SocketAddr,
|
||||||
pub proxy: Vec<ProxyConf>
|
pub proxy: Vec<ProxyConf>
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct ProxyConf {
|
pub struct ProxyConf {
|
||||||
pub host: String,
|
pub hosts: Vec<String>,
|
||||||
pub target: String,
|
pub target: String,
|
||||||
pub socket: Option<bool>,
|
pub socket: Option<bool>,
|
||||||
pub spawn: Option<SpawnConf>
|
pub spawn: Option<SpawnConf>
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
|
listen = "[::]:3000"
|
||||||
|
|
||||||
[[proxy]]
|
[[proxy]]
|
||||||
host = "website.local.gd"
|
hosts = [ "website.local.gd", "website-alt.local.gd" ]
|
||||||
socket = true
|
socket = true
|
||||||
target = "./www.sock"
|
target = "./www.sock"
|
||||||
[proxy.spawn]
|
[proxy.spawn]
|
||||||
|
@ -8,5 +10,5 @@ args = [ "./webserver/index.mjs" ]
|
||||||
envs = [ ["PORT", "www.sock"] ]
|
envs = [ ["PORT", "www.sock"] ]
|
||||||
|
|
||||||
[[proxy]]
|
[[proxy]]
|
||||||
host = "git.local.gd"
|
hosts = [ "git.local.gd" ]
|
||||||
target = "http://192.168.0.3:80"
|
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 std::collections::HashMap;
|
||||||
use hyper::http::HeaderValue;
|
use hyper::http::HeaderValue;
|
||||||
|
|
||||||
use crate::{conf::{CONFIG, ProxyConf}};
|
use crate::conf::{CONFIG, ProxyConf};
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
pub static ref HOST_MAP: HashMap<String, usize> = generate_host_map();
|
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> {
|
pub fn generate_host_map() -> HashMap<String, usize> {
|
||||||
let mut hosts: Vec<(String, usize)> = vec![];
|
let mut hosts: Vec<(String, usize)> = vec![];
|
||||||
for (i, proxy) in CONFIG.proxy.iter().enumerate() {
|
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)
|
HashMap::from_iter(hosts)
|
||||||
}
|
}
|
10
main.rs
10
main.rs
|
@ -1,7 +1,7 @@
|
||||||
mod conf;
|
mod conf;
|
||||||
mod data;
|
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 conf::{ProxyConf, SpawnConf};
|
||||||
use data::{HOST_MAP, SERVICES, ServiceData};
|
use data::{HOST_MAP, SERVICES, ServiceData};
|
||||||
use hyperlocal::{UnixClientExt};
|
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);
|
let is_socket = p.socket.unwrap_or(false);
|
||||||
|
|
||||||
if is_socket {
|
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 {
|
} else {
|
||||||
let url = p.target.clone() + path;
|
let url = p.target.clone() + path;
|
||||||
request_builder = request_builder.uri(hyper::Uri::from_str(url.as_str()).expect("[!] Wrong url address!"));
|
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 => {
|
None => {
|
||||||
|
println!("Unknown host accessed: {:?}", host.unwrap());
|
||||||
return Ok(Response::new(Body::empty()));
|
return Ok(Response::new(Body::empty()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -124,12 +125,11 @@ async fn main() {
|
||||||
|
|
||||||
let make_service = Shared::new(service_fn(run));
|
let make_service = Shared::new(service_fn(run));
|
||||||
|
|
||||||
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
let server = Server::bind(&CONFIG.listen).serve(make_service);
|
||||||
let server = Server::bind(&addr).serve(make_service);
|
|
||||||
|
|
||||||
let host_count = HOST_MAP.len();
|
let host_count = HOST_MAP.len();
|
||||||
let service_count = CONFIG.proxy.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 {
|
if let Err(e) = server.await {
|
||||||
println!("error: {}", e);
|
println!("error: {}", e);
|
||||||
|
|
Loading…
Reference in a new issue