Add option for list of IP to bind in command-line argument

This commit is contained in:
_Frky 2022-09-15 17:46:56 +02:00
parent 851a418add
commit 726b5d2e87
2 changed files with 56 additions and 4 deletions

View file

@ -116,18 +116,23 @@ fn main() {
)
.arg(
Arg::new("mac")
.short('a')
.short('m')
.long("mac-addr")
.help("MAC address to use in the response packets")
.takes_value(true),
)
.arg(
Arg::new("ip")
.short('f')
Arg::new("ipfile")
.long("ip-addr-file")
.help("File with the list of IP addresses to impersonate")
.takes_value(true),
)
.arg(
Arg::new("iplist")
.long("ip-addr")
.help("Inline list of IP addresses to impersonate, comma-separated")
.takes_value(true),
)
.arg(
Arg::new("verbosity")
.short('v')
@ -173,7 +178,7 @@ fn main() {
};
/* Parse ip address file specified */
/* FIXME: .and_then(|path| File::open(path).map(|file| )).unwrap_or_default() ? */
let ip_list = if let Some(ref path) = args.value_of("ip") {
let mut ip_list = if let Some(ref path) = args.value_of("ipfile") {
if let Ok(file) = File::open(path) {
info!("parsing ip address file: {}", &path);
file.extract_ip_addresses_only(None)
@ -183,9 +188,17 @@ fn main() {
} else {
HashSet::new()
};
if let Some(ip_inline_list) = args.value_of("iplist") {
ip_list.extend(ip_inline_list.extract_ip_addresses_only(None));
}
let ip_addresses = if !ip_list.is_empty() {
for ip in &ip_list {
info!("binding........{}", ip);
}
Some(&ip_list)
} else {
info!("binding........0.0.0.0");
info!("binding........::");
None
};
let mut masscanned = Masscanned {

View file

@ -134,6 +134,45 @@ impl IpAddrParser for File {
}
}
/* Parse IP addresses from a comma-separated list in a string */
impl IpAddrParser for &str {
fn extract_ip_addresses_with_count(
self,
_blacklist: Option<HashSet<IpAddr>>,
) -> HashMap<IpAddr, u32> {
panic!("not implemented");
}
fn extract_ip_addresses_only(self, blacklist: Option<HashSet<IpAddr>>) -> HashSet<IpAddr> {
let mut ip_addresses = HashSet::new();
for line in self.split(",") {
/* Should never occur */
if line.is_empty() {
warn!("cannot parse line: {}", line);
continue;
}
let ip: IpAddr;
if let Ok(val) = line.parse::<Ipv4Addr>() {
ip = IpAddr::V4(val);
} else if let Ok(val) = line.parse::<Ipv6Addr>() {
ip = IpAddr::V6(val);
} else {
warn!(
"cannot parse IP address from line: {}", line
);
continue;
}
if let Some(ref b) = blacklist {
if b.contains(&ip) {
info!("[blacklist] ignoring {}", &ip);
continue;
}
}
ip_addresses.insert(ip);
}
ip_addresses
}
}
/* Get the IP address of source and dest. from an IP packet.
* works with both IPv4 and IPv6 packets/addresses */
fn extract_ip(pkt: Packet) -> Option<(IpAddr, IpAddr)> {