mirror of
https://github.com/GyulyVGC/sniffnet.git
synced 2025-12-23 22:29:01 -05:00
improve services.txt validation
This commit is contained in:
22
build.rs
22
build.rs
@@ -1,6 +1,7 @@
|
||||
#[cfg(windows)]
|
||||
extern crate winres;
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader, BufWriter, Write};
|
||||
@@ -34,17 +35,25 @@ fn build_services_phf() {
|
||||
let mut services_map = phf_codegen::Map::new();
|
||||
|
||||
let input = BufReader::new(File::open(SERVICES_LIST_PATH).unwrap());
|
||||
let mut num_entries = 0;
|
||||
let mut distinct_services = HashSet::new();
|
||||
for line_res in input.lines() {
|
||||
// we want to panic if one of the lines is err...
|
||||
let line = line_res.unwrap();
|
||||
let mut parts = line.split('\t');
|
||||
// just to count and verify number of distinct services
|
||||
let service_str = parts.next().unwrap();
|
||||
distinct_services.insert(service_str.to_string());
|
||||
// we want to panic if one of the service names is invalid
|
||||
let val = get_valid_service_fmt_const(parts.next().unwrap());
|
||||
let val = get_valid_service_fmt_const(service_str);
|
||||
// we want to panic if port is not a u16, or protocol is not TCP or UDP
|
||||
let key = get_valid_service_query(parts.next().unwrap());
|
||||
assert!(parts.next().is_none());
|
||||
services_map.entry(key, &val);
|
||||
num_entries += 1;
|
||||
}
|
||||
assert_eq!(num_entries, 12066);
|
||||
assert_eq!(distinct_services.len(), 6438);
|
||||
|
||||
writeln!(
|
||||
&mut file,
|
||||
@@ -56,12 +65,13 @@ fn build_services_phf() {
|
||||
}
|
||||
|
||||
fn get_valid_service_fmt_const(s: &str) -> String {
|
||||
assert!(
|
||||
s.is_ascii(),
|
||||
"Service names must be ASCII strings, found: {s}"
|
||||
);
|
||||
match s.trim() {
|
||||
invalid if ["", "unknown", "?", "-"].contains(&invalid) || invalid.starts_with('#') => {
|
||||
invalid
|
||||
if ["", "unknown", "?", "-"].contains(&invalid)
|
||||
|| !invalid.is_ascii()
|
||||
|| invalid.starts_with('#')
|
||||
|| invalid.contains(' ') =>
|
||||
{
|
||||
panic!("Invalid service name found: {invalid}")
|
||||
}
|
||||
name => format!("Service::Name(\"{name}\")"),
|
||||
|
||||
Reference in New Issue
Block a user