This commit is contained in:
Pascal Engélibert 2025-04-06 15:28:05 +02:00
commit 5dc6ca05ce
6 changed files with 187 additions and 24 deletions

View file

@ -8,7 +8,12 @@ use policy::{CompiledPolicies, Policy};
use rand::Rng;
use realm_syscall::socket2::TcpKeepalive;
use regex::bytes::Regex;
use std::{io::Write, net::SocketAddr, time::Duration};
use std::{
io::{Read, Write},
net::SocketAddr,
str::FromStr,
time::Duration,
};
use tokio::{
io::{AsyncWriteExt, ReadBuf},
net::{TcpSocket, TcpStream},
@ -34,28 +39,38 @@ macro_rules! mk_static {
#[tokio::main]
async fn main() {
let mut config_file =
std::fs::File::open("example-config.yaml").expect("Cannot open config file");
let mut config_str = String::new();
config_file
.read_to_string(&mut config_str)
.expect("Cannot read config file");
let config_yaml = saphyr::Yaml::load_from_str(&config_str).expect("Error parsing config");
let config_doc = &config_yaml[0];
let listen_addr: SocketAddr = config_doc["listen"]
.as_str()
.expect("Missing listen address in config")
.parse()
.expect("Invalid listen address");
let pass_addr: SocketAddr = config_doc["pass"]
.as_str()
.expect("Missing pass address in config")
.parse()
.expect("Invalid pass address");
let default_action = policy::Action::from_str(
config_doc["default-action"]
.as_str()
.expect("Missing default action in config"),
)
.expect("Invalid default action");
let policy_groups: Vec<Vec<Policy>> = config_doc["policy-groups"].as_vec().expect("Missing policies in config").into_iter().map(|policy_group| policy_group.as_vec().expect("Missing policies in config").into_iter().map(|policy| Policy {
name: policy["name"].as_str().expect("Expected policy name string").to_string(),
first_line_regex: policy["first-line"].as_str().expect("Expected policy first line regex string").to_string(),
action: policy::Action::from_str(policy["action"].as_str().expect("Expected policy action string")).expect("Invalid policy action"),
}).collect()).collect();
let mut rng = rand::thread_rng();
let listen_addr: SocketAddr = "127.0.0.1:8504".parse().unwrap();
let pass_addr: SocketAddr = "127.0.0.1:80".parse().unwrap();
let policy_groups = vec![vec![
Policy {
name: String::from("Favicon"),
first_line_regex: String::from(r"^GET /favicon.ico"),
action: policy::Action::Allow,
},
Policy {
name: String::from("robots.txt"),
first_line_regex: String::from(r"^GET /robots.txt"),
action: policy::Action::Allow,
},
Policy {
name: String::from("Block"),
first_line_regex: String::from(r"^GET /block"),
action: policy::Action::Drop,
},
]];
let default_action = policy::Action::Challenge;
let secret: [u8; SECRET_LEN] = rng.r#gen();
let policy_groups = &*mk_static!(

View file

@ -7,6 +7,18 @@ pub enum Action {
Drop,
}
impl std::str::FromStr for Action {
type Err = ();
fn from_str(s: &str) -> Result<Self, ()> {
match s {
"allow" | "Allow" => Ok(Self::Allow),
"challenge" | "Challenge" => Ok(Self::Challenge),
"drop" | "Drop" => Ok(Self::Drop),
_ => Err(()),
}
}
}
#[derive(Clone, Debug)]
pub struct Policy {
pub name: String,