Compare commits

..

No commits in common. "5f0de72b88196a13fb619fe926dad10b26f69dfe" and "fd86162450d57d1932b9f2d1de69b747a8b94f55" have entirely different histories.

6 changed files with 19 additions and 58 deletions

View File

@ -1,5 +1,5 @@
local executableName = 'fourth';
local build_image = 'img.kie.rs/jjkiers/rust-cross:rust1.71.1-zig';
local build_image = 'img.kie.rs/jjkiers/rust-cross:rust1.70-zig';
local archs = [
{ target: 'aarch64-unknown-linux-musl', short: 'arm64-musl' },

View File

@ -173,10 +173,6 @@ fn load_config(path: &str) -> Result<ParsedConfig, ConfigError> {
name: name.to_string(),
addr: format!("{}:{}", upstream_host, upsteam_port),
protocol: upstream_url.scheme().to_string(),
addresses: Addr(Mutex::new(UpstreamAddress::new(format!(
"{}:{}",
upstream_host, upsteam_port
)))),
..Default::default()
}),
);

View File

@ -7,10 +7,10 @@ use crate::servers::Server;
use log::{debug, error};
use std::env;
use std::path::Path;
fn main() {
let config_path = find_config();
let config_path =
env::var("FOURTH_CONFIG").unwrap_or_else(|_| "/etc/fourth/config.yaml".to_string());
let config = match Config::new(&config_path) {
Ok(config) => config,
@ -27,18 +27,3 @@ fn main() {
let _ = server.run();
error!("Server ended with errors");
}
fn find_config() -> String {
let config_path =
env::var("FOURTH_CONFIG").unwrap_or_else(|_| "/etc/fourth/config.yaml".to_string());
if Path::new(&config_path).exists() {
return config_path;
}
if Path::new("config.yaml").exists() {
return String::from("config.yaml");
}
String::from("")
}

View File

@ -13,6 +13,7 @@ use protocol::tcp;
#[derive(Debug)]
pub(crate) struct Server {
pub proxies: Vec<Arc<Proxy>>,
pub config: ParsedConfig,
}
#[derive(Debug, Clone)]
@ -30,6 +31,7 @@ impl Server {
pub fn new(config: ParsedConfig) -> Self {
let mut new_server = Server {
proxies: Vec::new(),
config: config.clone(),
};
for (name, proxy) in config.servers.iter() {

View File

@ -72,17 +72,21 @@ async fn accept(inbound: TcpStream, proxy: Arc<Proxy>) -> Result<(), Box<dyn std
"No upstream named {:?} on server {:?}",
proxy.default_action, proxy.name
);
return process(inbound, proxy.upstream.get(&proxy.default_action).unwrap()).await;
return process(
inbound,
proxy.upstream.get(&proxy.default_action).unwrap().clone(),
)
.await;
// ToDo: Remove unwrap and check default option
}
};
return process(inbound, &upstream).await;
return process(inbound, upstream.clone()).await;
}
async fn process(
mut inbound: TcpStream,
upstream: &Upstream,
upstream: Upstream,
) -> Result<(), Box<dyn std::error::Error>> {
match upstream {
Upstream::Ban => {

View File

@ -19,13 +19,6 @@ impl Display for UpstreamAddress {
}
impl UpstreamAddress {
pub fn new(address: String) -> Self {
UpstreamAddress {
address,
..Default::default()
}
}
pub fn is_valid(&self) -> bool {
if let Some(resolved) = self.resolved_time {
if let Some(ttl) = self.ttl {
@ -51,24 +44,20 @@ impl UpstreamAddress {
pub async fn resolve(&mut self, mode: ResolutionMode) -> Result<Vec<SocketAddr>> {
if self.is_resolved() && self.is_valid() {
debug!(
"Already got address {:?}, still valid for {:.3}s",
"Already got address {:?}, still valid for {}",
&self.resolved_addresses,
self.time_remaining().as_seconds_f64()
self.time_remaining()
);
return Ok(self.resolved_addresses.clone());
}
debug!(
"Resolving addresses for {} with mode {:?}",
&self.address, &mode
);
debug!("Resolving addresses for {}", &self.address);
let lookup_result = tokio::net::lookup_host(&self.address).await;
let resolved_addresses: Vec<SocketAddr> = match lookup_result {
Ok(resolved_addresses) => resolved_addresses.into_iter().collect(),
let resolved_addresses = match lookup_result {
Ok(resolved_addresses) => resolved_addresses,
Err(e) => {
debug!("Failed looking up {}: {}", &self.address, &e);
// Protect against DNS flooding. Cache the result for 1 second.
self.resolved_time = Some(Instant::now());
self.ttl = Some(Duration::seconds(3));
@ -76,8 +65,6 @@ impl UpstreamAddress {
}
};
debug!("Resolved addresses: {:?}", &resolved_addresses);
let addresses: Vec<SocketAddr> = match mode {
ResolutionMode::Ipv4 => resolved_addresses
.into_iter()
@ -89,13 +76,10 @@ impl UpstreamAddress {
.filter(|a| a.is_ipv6())
.collect(),
_ => resolved_addresses,
_ => resolved_addresses.collect(),
};
debug!(
"Got {} addresses for {}: {:?}",
&mode, &self.address, &addresses
);
debug!("Got addresses for {}: {:?}", &self.address, &addresses);
debug!(
"Resolved at {}",
OffsetDateTime::now_utc()
@ -129,13 +113,3 @@ impl From<&str> for ResolutionMode {
}
}
}
impl Display for ResolutionMode {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ResolutionMode::Ipv4 => write!(f, "IPv4Only"),
ResolutionMode::Ipv6 => write!(f, "IPv6Only"),
ResolutionMode::Ipv4AndIpv6 => write!(f, "IPv4 and IPv6"),
}
}
}