mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-05 17:58:55 +00:00
load ip sets from file
This commit is contained in:
1 parent
0d99c2fc08
commit
f3dd785331
7 files changed
+72
-7
No files matched your search
+3
-3
@@ -1,4 +1,4 @@
|
||||
use crate::middleware::ip_filter::ip_set::IpSet;
|
||||
use crate::middleware::ip_filter::ip_ban_list::ip_set::IpSet;
|
||||
use errors::{anyhow, AnyhowResult};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, RwLock};
|
||||
@@ -50,8 +50,8 @@ impl IpBanList {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::middleware::ip_filter::ip_ban_list::IpBanList;
|
||||
use crate::middleware::ip_filter::ip_set::IpSet;
|
||||
use crate::middleware::ip_filter::ip_ban_list::ip_ban_list::IpBanList;
|
||||
use crate::middleware::ip_filter::ip_ban_list::ip_set::IpSet;
|
||||
|
||||
#[test]
|
||||
fn test_contains_ip() {
|
||||
+6
@@ -13,6 +13,12 @@ impl IpSet {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_set(ip_set: HashSet<String>) -> Self {
|
||||
Self {
|
||||
ip_set,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn replace_set(&mut self, ip_set: HashSet<String>) {
|
||||
self.ip_set = ip_set;
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
use crate::middleware::ip_filter::ip_ban_list::ip_set::IpSet;
|
||||
use errors::AnyhowResult;
|
||||
use std::collections::HashSet;
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::path::Path;
|
||||
|
||||
pub fn load_ip_set_from_file<P: AsRef<Path>>(path: P) -> AnyhowResult<IpSet> {
|
||||
let file = File::open(path)?;
|
||||
let reader = BufReader::new(file);
|
||||
|
||||
let lines = reader.lines()
|
||||
.filter_map(|line| line.ok())
|
||||
.map(|line| line.trim().to_string())
|
||||
.filter(|line| !(line.starts_with("#") || line.is_empty()))
|
||||
.collect::<HashSet<String>>();
|
||||
|
||||
Ok(IpSet::from_set(lines))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::path::PathBuf;
|
||||
use crate::middleware::ip_filter::ip_ban_list::load_ip_set_from_file::load_ip_set_from_file;
|
||||
|
||||
fn test_file(path_from_repo_root: &str) -> PathBuf {
|
||||
// https://doc.rust-lang.org/cargo/reference/environment-variables.html
|
||||
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
path.push(format!("../../../{}", path_from_repo_root));
|
||||
path
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_load_ip_set_from_file() {
|
||||
let filename = test_file("test_data/text_files/ip_addresses_and_comments.txt");
|
||||
let ip_set = load_ip_set_from_file(filename).unwrap();
|
||||
|
||||
// Comments are not included
|
||||
assert_eq!(ip_set.contains_ip_address("# this is test data"), false);
|
||||
|
||||
// IP addresses in the file are.
|
||||
assert_eq!(ip_set.contains_ip_address("127.0.0.1"), true);
|
||||
|
||||
// Length is expected
|
||||
assert_eq!(ip_set.len(), 4);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
pub mod ip_ban_list;
|
||||
pub mod ip_set;
|
||||
pub mod load_ip_set_from_file;
|
||||
@@ -1,11 +1,11 @@
|
||||
use actix_web::Error;
|
||||
use actix_web::dev::{Service, Transform};
|
||||
use actix_web::dev::{ServiceRequest, ServiceResponse};
|
||||
use actix_web::Error;
|
||||
use crate::extractors::get_service_request_ip_address::get_service_request_ip_address;
|
||||
use crate::middleware::ip_filter::banned_error::BannedError;
|
||||
use crate::middleware::ip_filter::ip_ban_list::IpBanList;
|
||||
use crate::middleware::ip_filter::ip_ban_list::ip_ban_list::IpBanList;
|
||||
use futures_util::future::{err, ok, Either, Ready};
|
||||
use std::task::{Context, Poll};
|
||||
use crate::extractors::get_service_request_ip_address::get_service_request_ip_address;
|
||||
|
||||
// There are two steps in middleware processing.
|
||||
// 1. Middleware initialization, middleware factory gets called with
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
pub mod banned_error;
|
||||
pub mod ip_ban_list;
|
||||
pub mod ip_filter_middleware;
|
||||
pub mod ip_set;
|
||||
@@ -0,0 +1,10 @@
|
||||
# this is test data
|
||||
|
||||
# local addresses
|
||||
127.0.0.1
|
||||
192.168.0.1
|
||||
|
||||
# remote addresses
|
||||
4.4.4.4
|
||||
8.8.8.8
|
||||
|
||||
Reference in new issue
Block a user