Map all private IP to public IP for log-analyzer (#6907)
* Map all private IP to public IP for log-analyzer * fixes * shellcheck fixes
This commit is contained in:
@ -11,6 +11,12 @@ use std::fs;
|
|||||||
use std::ops::Sub;
|
use std::ops::Sub;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize, Debug)]
|
||||||
|
struct IPAddrMapping {
|
||||||
|
private: String,
|
||||||
|
public: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Debug)]
|
#[derive(Deserialize, Serialize, Debug)]
|
||||||
struct LogLine {
|
struct LogLine {
|
||||||
a: String,
|
a: String,
|
||||||
@ -84,29 +90,30 @@ impl Sub for &LogLine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn map_ip_address(mappings: &[IPAddrMapping], target: String) -> String {
|
||||||
|
for mapping in mappings {
|
||||||
|
if target.contains(&mapping.private) {
|
||||||
|
return target.replace(&mapping.private, mapping.public.as_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
target.to_string()
|
||||||
|
}
|
||||||
|
|
||||||
fn process_iftop_logs(matches: &ArgMatches) {
|
fn process_iftop_logs(matches: &ArgMatches) {
|
||||||
let map_address;
|
let mut map_list: Vec<IPAddrMapping> = vec![];
|
||||||
let private_address;
|
if let ("map-IP", Some(args_matches)) = matches.subcommand() {
|
||||||
let public_address;
|
let mut list = args_matches
|
||||||
match matches.subcommand() {
|
.value_of("list")
|
||||||
("map-IP", Some(args_matches)) => {
|
.expect("Missing list of IP address mappings")
|
||||||
map_address = true;
|
.to_string();
|
||||||
if let Some(addr) = args_matches.value_of("priv") {
|
list.insert(0, '[');
|
||||||
private_address = addr;
|
let terminate_at = list
|
||||||
} else {
|
.rfind('}')
|
||||||
panic!("Private IP address must be provided");
|
.expect("Didn't find a terminating '}' in IP list")
|
||||||
};
|
+ 1;
|
||||||
if let Some(addr) = args_matches.value_of("pub") {
|
let _ = list.split_off(terminate_at);
|
||||||
public_address = addr;
|
list.push(']');
|
||||||
} else {
|
map_list = serde_json::from_str(&list).expect("Failed to parse IP address mapping list");
|
||||||
panic!("Private IP address must be provided");
|
|
||||||
};
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
map_address = false;
|
|
||||||
private_address = "";
|
|
||||||
public_address = "";
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let log_path = PathBuf::from(value_t_or_exit!(matches, "file", String));
|
let log_path = PathBuf::from(value_t_or_exit!(matches, "file", String));
|
||||||
@ -128,15 +135,15 @@ fn process_iftop_logs(matches: &ArgMatches) {
|
|||||||
let output: Vec<LogLine> = unique_latest_logs
|
let output: Vec<LogLine> = unique_latest_logs
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(_, l)| {
|
.map(|(_, l)| {
|
||||||
if map_address {
|
if map_list.is_empty() {
|
||||||
|
l
|
||||||
|
} else {
|
||||||
LogLine {
|
LogLine {
|
||||||
a: l.a.replace(private_address, public_address),
|
a: map_ip_address(&map_list, l.a),
|
||||||
b: l.b.replace(private_address, public_address),
|
b: map_ip_address(&map_list, l.b),
|
||||||
a_to_b: l.a_to_b,
|
a_to_b: l.a_to_b,
|
||||||
b_to_a: l.b_to_a,
|
b_to_a: l.b_to_a,
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
l
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
@ -208,22 +215,15 @@ fn main() {
|
|||||||
)
|
)
|
||||||
.subcommand(
|
.subcommand(
|
||||||
SubCommand::with_name("map-IP")
|
SubCommand::with_name("map-IP")
|
||||||
.about("Public IP Address")
|
.about("Map private IP to public IP Address")
|
||||||
.arg(
|
.arg(
|
||||||
Arg::with_name("priv")
|
Arg::with_name("list")
|
||||||
.long("priv")
|
.short("l")
|
||||||
.value_name("IP Address")
|
.long("list")
|
||||||
|
.value_name("JSON string")
|
||||||
.takes_value(true)
|
.takes_value(true)
|
||||||
.required(true)
|
.required(true)
|
||||||
.help("The private IP address that should be mapped"),
|
.help("JSON string with a list of mapping"),
|
||||||
)
|
|
||||||
.arg(
|
|
||||||
Arg::with_name("pub")
|
|
||||||
.long("pub")
|
|
||||||
.value_name("IP Address")
|
|
||||||
.takes_value(true)
|
|
||||||
.required(true)
|
|
||||||
.help("The public IP address"),
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
set -e
|
set -e
|
||||||
|
|
||||||
usage() {
|
usage() {
|
||||||
echo "Usage: $0 <iftop log file> <temp file for interediate data> [optional public IP address]"
|
echo "Usage: $0 <iftop log file> <temp file for interediate data> [optional list of IP address mapping]"
|
||||||
echo
|
echo
|
||||||
echo Processes iftop log file, and extracts latest bandwidth used by each connection
|
echo Processes iftop log file, and extracts latest bandwidth used by each connection
|
||||||
echo
|
echo
|
||||||
@ -23,10 +23,11 @@ awk '{ if ($3 ~ "=>") { print $2, $7 } else if ($2 ~ "<=") { print $1, $6 }} ' <
|
|||||||
| awk 'NR%2{printf "%s ",$0;next;}1' \
|
| awk 'NR%2{printf "%s ",$0;next;}1' \
|
||||||
| awk '{ print "{ \"a\": \""$1"\", " "\"b\": \""$3"\", \"a_to_b\": \""$2"\", \"b_to_a\": \""$4"\"}," }' > "$2"
|
| awk '{ print "{ \"a\": \""$1"\", " "\"b\": \""$3"\", \"a_to_b\": \""$2"\", \"b_to_a\": \""$4"\"}," }' > "$2"
|
||||||
|
|
||||||
if [ "$#" -lt 4 ]; then
|
if [ "$#" -lt 3 ]; then
|
||||||
solana-log-analyzer iftop -f "$2"
|
solana-log-analyzer iftop -f "$2"
|
||||||
else
|
else
|
||||||
solana-log-analyzer iftop -f "$2" map-IP --priv "$3" --pub "$4"
|
list=$(cat "$3")
|
||||||
|
solana-log-analyzer iftop -f "$2" map-IP --list "$list"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
exit 1
|
exit 1
|
||||||
|
Reference in New Issue
Block a user