added two new fonctionalities: The possibility to blacklist ip address with port, and the possibility to forward a message from an ip to another.
This commit is contained in:
24
src/main.rs
24
src/main.rs
@@ -1,9 +1,13 @@
|
||||
mod config;
|
||||
|
||||
use std::io::stdin;
|
||||
use std::net::UdpSocket;
|
||||
use crate::config::Config;
|
||||
|
||||
fn main() {
|
||||
let config = Config::from_json("config.json".to_string());
|
||||
let server_address: String = get_server_address();
|
||||
listen(&server_address);
|
||||
listen(&server_address, &config);
|
||||
}
|
||||
|
||||
fn get_server_address() -> String {
|
||||
@@ -13,7 +17,8 @@ fn get_server_address() -> String {
|
||||
return server_address.trim().to_string();
|
||||
}
|
||||
|
||||
fn listen(server_address: &str) {
|
||||
fn listen(server_address: &str, config: &Config) {
|
||||
let serv_config = config;
|
||||
println!("listening on {}", server_address);
|
||||
let socket: UdpSocket = UdpSocket::bind(server_address).expect("Error while binding the socket.");
|
||||
|
||||
@@ -21,8 +26,17 @@ fn listen(server_address: &str) {
|
||||
let mut message: String = String::new();
|
||||
let mut buf: [u8; 1024] = [0; 1024];
|
||||
let (num_bytes, src_addr) = socket.recv_from(&mut buf).expect("Error while receiving message");
|
||||
message.push_str(std::str::from_utf8(&buf[..num_bytes]).expect("Invalid UTF-8 data"));
|
||||
|
||||
println!("{}:{}", src_addr.to_string(), message);
|
||||
if !serv_config.addr_is_blacklisted(src_addr.to_string()) {
|
||||
message.push_str(std::str::from_utf8(&buf[..num_bytes]).expect("Invalid UTF-8 data"));
|
||||
match serv_config.is_forwarded(src_addr.to_string()) {
|
||||
Some(v) => {
|
||||
socket.send_to(message.as_bytes(), v).expect("Error forwarding message to another ip.");
|
||||
},
|
||||
None => {
|
||||
//Nothing to do :)
|
||||
}
|
||||
}
|
||||
println!("{} say: {}", src_addr.to_string(), message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user