diff --git a/.gitignore b/.gitignore index 4675987263..6b5b8997ef 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ scripts/run_mix.sh scripts/start_local_tmux_network.sh /.floo /.flooignore +/service-providers/simple-socks5/allowed.list diff --git a/service-providers/simple-socks5/src/allowed_hosts.rs b/service-providers/simple-socks5/src/allowed_hosts.rs new file mode 100644 index 0000000000..d8cd8262dc --- /dev/null +++ b/service-providers/simple-socks5/src/allowed_hosts.rs @@ -0,0 +1,113 @@ +struct OutboundRequestFilter { + allowed_hosts: Persistence, + unknown_hosts: Persistence, +} + +impl OutboundRequestFilter { + pub(crate) fn new( + allowed_hosts: Persistence, + unknown_hosts: Persistence, + ) -> OutboundRequestFilter { + OutboundRequestFilter { + allowed_hosts, + unknown_hosts, + } + } + + pub(crate) fn check(&self, host: &str) -> bool { + if self.allowed_hosts.contains(host.to_string()) { + true + } else { + self.unknown_hosts.maybe_add(host.to_string()); + false + } + } + + pub(crate) fn is_unknown(&self, host: &str) -> bool { + true + } +} + +#[derive(Debug)] +struct Persistence { + hosts: Vec, +} + +impl Persistence { + fn new(hosts: Vec) -> Persistence { + Persistence { hosts } + } + + fn contains(&self, host: String) -> bool { + self.hosts.contains(&host) + } + + fn maybe_add(&mut self, host: String) { + if !self.contains(host) { + self.hosts.push(host); + } + } + + /// Reloads the allowed.list and unknown.list files into memory. Used primarily for testing. + fn reload_from_disk(&self) {} + + fn append_to_file(&self, host: String) {} +} +// Appender + +#[cfg(test)] +mod requests_to_unknown_hosts { + use super::*; + + fn setup() -> OutboundRequestFilter { + let allowed = Persistence::new(vec![]); + let unknown = Persistence::new(vec![]); + OutboundRequestFilter::new(allowed, unknown) + } + + #[test] + fn are_not_allowed() { + let host = "unknown.com"; + let filter = setup(); + assert_eq!(false, filter.check(&host)); + } + + #[test] + fn get_saved_to_file() { + let host = "unknown.com"; + let filter = setup(); + filter.check(host); + assert!(true, filter.is_unknown(host)); + } + + #[test] + fn get_appended_once_to_the_unknown_hosts_list() { + let host = "unknown.com"; + let filter = setup(); + filter.check(host); + assert_eq!(1, filter.unknown_hosts.hosts.len()); + } +} + +#[cfg(test)] +mod requests_to_allowed_hosts { + use super::*; + + fn setup() -> OutboundRequestFilter { + let allowed = Persistence::new(vec!["nymtech.net".to_string()]); + let unknown = Persistence::new(vec![]); + OutboundRequestFilter::new(allowed, unknown) + } + + #[test] + fn are_allowed() { + let host = "nymtech.net"; + let filter = setup(); + assert_eq!(true, filter.check(host)); + } + + // #[test] + // fn are_not_appended_to_file() { + // todo!() + // } +} diff --git a/service-providers/simple-socks5/src/main.rs b/service-providers/simple-socks5/src/main.rs index 9095c91dcf..3082577fcb 100644 --- a/service-providers/simple-socks5/src/main.rs +++ b/service-providers/simple-socks5/src/main.rs @@ -1,3 +1,4 @@ +mod allowed_hosts; mod connection; mod core; mod websocket;