Skip to content

Commit

Permalink
Merge pull request #45 from OlofBlomqvist/use_ip_for_procs_test
Browse files Browse the repository at this point in the history
Use ip for procs test
  • Loading branch information
OlofBlomqvist authored Dec 17, 2024
2 parents e223599 + 52e0240 commit 5c19a61
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 51 deletions.
30 changes: 15 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "odd-box"
description = "a dead simple reverse proxy server and web server"
version = "0.1.10"
version = "0.1.11-alpha1"
edition = "2021"
authors = ["Olof Blomqvist <[email protected]>"]
repository = "https://github.com/OlofBlomqvist/odd-box"
Expand All @@ -23,7 +23,7 @@ axum-extra = { version = "0.9.3", features = ["typed-header"] }
# === PROXY ====================================================
dirs = "5.0.1"
schemars = { version = "0.8.21", features = ["chrono"] }
futures-util = "0.3.30"
futures-util = "0.3.31"
hyper = { version = "1.4.1" , features=["http2","client","server"] }
hyper-util = { version = "0.1.7", features = ["full"] }
regex = "1.9.5"
Expand Down
15 changes: 15 additions & 0 deletions odd-box-schema-v3.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@
"format": "uint16",
"minimum": 0.0
},
"use_loopback_ip_for_procs": {
"description": "Uses 127.0.0.1 instead of localhost when proxying to locally hosted processes.",
"type": [
"boolean",
"null"
]
},
"version": {
"description": "The schema version - you do not normally need to set this, it is set automatically when you save the configuration.",
"allOf": [
Expand Down Expand Up @@ -387,6 +394,14 @@
"null"
]
},
"keep_original_host_header": {
"description": "Defaults to true.",
"default": true,
"type": [
"boolean",
"null"
]
},
"log_format": {
"description": "The log format to use for this site. If this is not set, the default log format will be used. Currently the only supported log formats are \"standard\" and \"dotnet\".",
"anyOf": [
Expand Down
24 changes: 22 additions & 2 deletions src/configuration/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ pub struct InProcessSiteConfig {
/// If you wish to set a specific loglevel for this hosted process.
/// Defaults to "Info".
/// If this level is lower than the global log_level you will get the message elevated to the global log level instead but tagged with the actual log level.
pub log_level: Option<LogLevel>
pub log_level: Option<LogLevel>,

#[serde(default = "true_option")]
/// Defaults to true.
pub keep_original_host_header: Option<bool>
}
impl InProcessSiteConfig {
pub fn set_id(&mut self,id:ProcId){
Expand Down Expand Up @@ -292,8 +296,17 @@ impl InProcessSiteConfig {
}
};


let local_addr = {
if state.config.read().await.use_loopback_ip_for_procs.unwrap_or_default() {
"127.0.0.1"
} else {
"localhost"
}
};

let backends = vec![Backend {
address: "localhost".to_string(), // we are always connecting to localhost since we are the ones hosting this process
address: local_addr.to_string(), // we are always connecting to localhost since we are the ones hosting this process
port: port,
https: self.https,
hints: self.hints.clone(),
Expand Down Expand Up @@ -420,6 +433,9 @@ pub struct OddBoxV3Config {
/// Used for securing the admin api and web-interface. If you do not set this, anyone can access the admin api.
pub odd_box_password: Option<String>,

/// Uses 127.0.0.1 instead of localhost when proxying to locally hosted processes.
pub use_loopback_ip_for_procs: Option<bool>

}


Expand Down Expand Up @@ -661,6 +677,7 @@ impl crate::configuration::OddBoxConfiguration<OddBoxV3Config> for OddBoxV3Confi
}
fn example() -> OddBoxV3Config {
OddBoxV3Config {
use_loopback_ip_for_procs: None,
odd_box_password: None,
odd_box_url: None,
dir_server: None,
Expand All @@ -680,6 +697,7 @@ impl crate::configuration::OddBoxConfiguration<OddBoxV3Config> for OddBoxV3Confi
port_range_start: 4200,
hosted_process: Some(vec![
InProcessSiteConfig {
keep_original_host_header: None,
log_level: None,
enable_lets_encrypt: Some(false),
proc_id: ProcId::new(),
Expand Down Expand Up @@ -797,6 +815,7 @@ impl TryFrom<super::v2::OddBoxV2Config> for OddBoxV3Config{

fn try_from(old_config: super::v2::OddBoxV2Config) -> Result<Self, Self::Error> {
let new_config = Self {
use_loopback_ip_for_procs: None,
odd_box_password: None,
odd_box_url: None,
dir_server: None,
Expand Down Expand Up @@ -829,6 +848,7 @@ impl TryFrom<super::v2::OddBoxV2Config> for OddBoxV3Config{
let new_hints = if new_hints.len() == 0 { None } else { Some(new_hints) };

InProcessSiteConfig {
keep_original_host_header: None,
log_level: None,
enable_lets_encrypt: Some(false),
proc_id: ProcId::new(),
Expand Down
10 changes: 8 additions & 2 deletions src/http_proxy/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,11 +517,17 @@ async fn handle_http_request(
port,
original_path_and_query
);

let local_addr = if configuration.use_loopback_ip_for_procs.unwrap_or_default() {
"127.0.0.1"
} else {
"localhost"
};

// we add the host flag manually in proxy method, this is only to avoid dns lookup for local targets.
// todo: opt in/out via cfg (?)
let skip_dns_for_local_target_url = format!("{scheme}://{}:{}{}",
"localhost",
local_addr,
port,
original_path_and_query
);
Expand All @@ -533,7 +539,7 @@ async fn handle_http_request(
let backend = crate::configuration::Backend {
hints: hints,
// we are hosting this service so clearly it is local
address: "localhost".to_string(),
address: local_addr.to_string(),
port: port,
https: Some(enforce_https)
};
Expand Down
9 changes: 5 additions & 4 deletions src/http_proxy/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,13 @@ pub async fn proxy(
host_header_to_use = Some(req_host_name.to_string());
}
},
Target::Proc(_cfg) => {
host_header_to_use = Some(req_host_name.to_string());
Target::Proc(cfg) => {
if cfg.keep_original_host_header.unwrap_or_default() {
host_header_to_use = Some(req_host_name.to_string());
}
}
};


let hints = backend.hints.clone().unwrap_or_default();

let mut proxied_request =
Expand Down Expand Up @@ -327,7 +328,7 @@ fn create_proxied_request<B>(
let target_uri = target_url.parse::<http::Uri>()
.map_err(|e| ProxyError::InvalidUri(e))?;
*request.uri_mut() = target_uri;


// we want to pass the original host header to the backend (the one that the client requested)
// and not the one we are connecting to as that might as well just be an internal name or IP.
Expand Down
40 changes: 22 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,11 @@ pub mod global_state {

let mut result = None;
let cfg = self.config.read().await;

let local_addr = if cfg.use_loopback_ip_for_procs.unwrap_or_default() {
"127.0.0.1"
} else {
"localhost"
};
for guard in &cfg.docker_containers {
let (host_name,x) = guard.pair();
//let host_name = x.host_name_label.unwrap_or(format!("{}.odd-box.localhost",x.container_name));
Expand Down Expand Up @@ -215,7 +219,7 @@ pub mod global_state {
// use dns name to avoid issues where hyper uses ipv6 for 127.0.0.1 since tcp tunnel mode uses ipv4.
// not keeping them the same means the target backend will see different ip's for the same client
// and possibly invalidate sessions in some cases.
address: "localhost".to_string(), //y.host_name.to_owned(), // --- configurable
address: local_addr.to_string(), //y.host_name.to_owned(), // --- configurable
https: y.https,
port: y.active_port.unwrap_or_default()
}],
Expand Down Expand Up @@ -702,22 +706,22 @@ async fn main() -> anyhow::Result<()> {
tokio::task::spawn(docker_thread(global_state.clone()));


// if on a released/stable version, we notify the user when there is a later stable version
// available for them to update to. current_is_latest will not include any -rc,-pre or -dev releases
// and so we wont run this unless user is also on stable.
if !self_update::current_version().contains("-") {
match self_update::current_is_latest().await {
Err(e) => {
tracing::warn!("It was not possible to retrieve information regarding the latest available version of odd-box: {e:?}");
},
Ok(Some(v)) => {
tracing::info!("There is a newer version of odd-box available - please consider upgrading to {v:?}. For unmanaged installations you can run 'odd-box --update' otherwise see your package manager for upgrade instructions.");
},
Ok(None) => {
tracing::info!("You are running the latest version of odd-box :D");
}
}
}
// // if on a released/stable version, we notify the user when there is a later stable version
// // available for them to update to. current_is_latest will not include any -rc,-pre or -dev releases
// // and so we wont run this unless user is also on stable.
// if !self_update::current_version().contains("-") {
// match self_update::current_is_latest().await {
// Err(e) => {
// tracing::warn!("It was not possible to retrieve information regarding the latest available version of odd-box: {e:?}");
// },
// Ok(Some(v)) => {
// tracing::info!("There is a newer version of odd-box available - please consider upgrading to {v:?}. For unmanaged installations you can run 'odd-box --update' otherwise see your package manager for upgrade instructions.");
// },
// Ok(None) => {
// tracing::info!("You are running the latest version of odd-box :D");
// }
// }
// }


// if in tui mode, we can just hang around until the tui thread exits.
Expand Down
16 changes: 8 additions & 8 deletions src/self_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ pub async fn update() -> anyhow::Result<()> {
pub fn current_version() -> &'static str { cargo_crate_version!() }

/// returns Some(newer_version) or None if current is latest.
pub async fn current_is_latest() -> anyhow::Result<Option<String>> {
let current_version = current_version();
match find_latest_version(false).await {
Ok(v) if current_version != v => Ok(Some(v)),
Ok(_) => Ok(None),
Err(e) => Err(e)
}
}
// pub async fn current_is_latest() -> anyhow::Result<Option<String>> {
// let current_version = current_version();
// match find_latest_version(false).await {
// Ok(v) if current_version != v => Ok(Some(v)),
// Ok(_) => Ok(None),
// Err(e) => Err(e)
// }
// }


pub async fn find_latest_version(include_pre:bool) -> anyhow::Result<String> {
Expand Down

0 comments on commit 5c19a61

Please sign in to comment.