refactored a little
This commit is contained in:
parent
5cc6259c0a
commit
4bbdeea29a
101
sunbeam.rs
101
sunbeam.rs
|
@ -23,14 +23,45 @@ static BLOCKFILE_NAME:&str = ".nosunbeam"; // file to check for as the client bl
|
|||
static ALLOWFILE_NAME:&str = ".yesunbeam"; // file to check for as the client whitelist, for Whitelist-mode connectors
|
||||
|
||||
#[derive(PartialEq)]
|
||||
#[derive(Clone)]
|
||||
#[derive(Copy)]
|
||||
enum PortPrivacy {
|
||||
Public,
|
||||
Whitelist,
|
||||
Private,
|
||||
}
|
||||
|
||||
fn load_rules(rulefilepath:&str) -> HashSet<IpAddr> {
|
||||
let mut rules:HashSet<IpAddr> = HashSet::new();
|
||||
match File::open(rulefilepath) {
|
||||
Err(why) => match why.kind() {
|
||||
io::ErrorKind::NotFound => (),
|
||||
_ => {
|
||||
eprintln!("-!- failed to open {} to read IP address ruleing rules: {}",rulefilepath,why);
|
||||
exit(1);
|
||||
},
|
||||
},
|
||||
Ok(mut file) => {
|
||||
let mut rulestring = String::new();
|
||||
match file.read_to_string(&mut rulestring) {
|
||||
Err(why) => {
|
||||
eprintln!("-!- failed to read {} for IP address ruleing rules: {}",rulefilepath,why);
|
||||
exit(1);
|
||||
},
|
||||
Ok(_) => (),
|
||||
};
|
||||
for line in rulestring.lines() {
|
||||
match line.parse::<IpAddr>() {
|
||||
Err(_why) => {
|
||||
eprintln!("-!- could not parse '{}' in {} as an IP address",line,rulefilepath);
|
||||
exit(1);
|
||||
},
|
||||
Ok(address) => rules.insert(address),
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
return rules;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let argv = args().collect::<Vec<String>>();
|
||||
if argv.len() < 2 {
|
||||
|
@ -70,68 +101,11 @@ fn main() {
|
|||
println!("-i- running in loopback mode (relaying data between all connections)");
|
||||
}
|
||||
|
||||
let mut blocklist:HashSet<IpAddr> = HashSet::new();
|
||||
match File::open(&BLOCKFILE_NAME) {
|
||||
Err(why) => match why.kind() {
|
||||
io::ErrorKind::NotFound => (),
|
||||
_ => {
|
||||
eprintln!("-!- failed to open {} to read IP address blocking rules: {}",BLOCKFILE_NAME,why);
|
||||
exit(1);
|
||||
},
|
||||
},
|
||||
Ok(mut file) => {
|
||||
let mut blockstring = String::new();
|
||||
match file.read_to_string(&mut blockstring) {
|
||||
Err(why) => {
|
||||
eprintln!("-!- failed to read {} for IP address blocking rules: {}",BLOCKFILE_NAME,why);
|
||||
exit(1);
|
||||
},
|
||||
Ok(_) => (),
|
||||
};
|
||||
for line in blockstring.lines() {
|
||||
match line.parse::<IpAddr>() {
|
||||
Err(_why) => {
|
||||
eprintln!("-!- could not parse '{}' in {} as an IP address",line,BLOCKFILE_NAME);
|
||||
exit(1);
|
||||
},
|
||||
Ok(address) => blocklist.insert(address),
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
let blocklist = load_rules(BLOCKFILE_NAME);
|
||||
let allowlist = load_rules(ALLOWFILE_NAME);
|
||||
|
||||
let mut allowlist:HashSet<IpAddr> = HashSet::new();
|
||||
match File::open(&ALLOWFILE_NAME) {
|
||||
Err(why) => match why.kind() {
|
||||
io::ErrorKind::NotFound => (),
|
||||
_ => {
|
||||
eprintln!("-!- failed to open {} to read IP address allowing rules: {}",ALLOWFILE_NAME,why);
|
||||
exit(1);
|
||||
},
|
||||
},
|
||||
Ok(mut file) => {
|
||||
let mut allowstring = String::new();
|
||||
match file.read_to_string(&mut allowstring) {
|
||||
Err(why) => {
|
||||
eprintln!("-!- failed to read {} for IP address allowing rules: {}",ALLOWFILE_NAME,why);
|
||||
exit(1);
|
||||
},
|
||||
Ok(_) => (),
|
||||
};
|
||||
for line in allowstring.lines() {
|
||||
match line.parse::<IpAddr>() {
|
||||
Err(_why) => {
|
||||
eprintln!("-!- could not parse '{}' in {} as an IP address",line,ALLOWFILE_NAME);
|
||||
exit(1);
|
||||
},
|
||||
Ok(address) => allowlist.insert(address),
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
let mut connectors:VecDeque<(PortPrivacy,u16,TcpListener,VecDeque<(TcpStream,SocketAddr)>)> = VecDeque::new();
|
||||
for (privacy,port) in ports.iter() {
|
||||
while let Some((privacy,port)) = ports.pop() {
|
||||
let bindaddress:&str = match privacy {
|
||||
PortPrivacy::Private => "127.0.0.1",
|
||||
_ => "[::]",
|
||||
|
@ -143,7 +117,7 @@ fn main() {
|
|||
},
|
||||
Ok(listener) => {
|
||||
listener.set_nonblocking(true).expect("cannot set listener to nonblocking");
|
||||
connectors.push_back((*privacy,*port,listener,VecDeque::new()));
|
||||
connectors.push_back((privacy,port,listener,VecDeque::new()));
|
||||
},
|
||||
};
|
||||
}
|
||||
|
@ -170,6 +144,7 @@ fn main() {
|
|||
}
|
||||
|
||||
loop {
|
||||
|
||||
if idlecycles < IDLE_MAX {
|
||||
idlecycles += 1;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue