Skip to content
Snippets Groups Projects
Commit d48de955 authored by Éloïs's avatar Éloïs
Browse files

[feat] launcher:wizard gva: add capability to update whitelist

parent 102cd8cd
No related branches found
No related tags found
1 merge request!1335Gva proto 2
...@@ -192,6 +192,7 @@ export class ConfDTO ...@@ -192,6 +192,7 @@ export class ConfDTO
remotePath?: string; remotePath?: string;
remoteSubscriptionsPath?: string; remoteSubscriptionsPath?: string;
remoteTls?: boolean; remoteTls?: boolean;
whitelist?: string[];
}, },
public ws2p?: { public ws2p?: {
privateAccess?: boolean; privateAccess?: boolean;
......
...@@ -21,6 +21,7 @@ export class GvaConf { ...@@ -21,6 +21,7 @@ export class GvaConf {
remotePath?: string; remotePath?: string;
remoteSubscriptionsPath?: string; remoteSubscriptionsPath?: string;
remoteTls?: boolean; remoteTls?: boolean;
whitelist?: string[];
} }
export class PeerCard { export class PeerCard {
......
...@@ -30,7 +30,7 @@ mod duniter_ts_args; ...@@ -30,7 +30,7 @@ mod duniter_ts_args;
mod sync; mod sync;
mod wizard_gva; mod wizard_gva;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Context, Result};
use daemonize_me::Daemon; use daemonize_me::Daemon;
use logwatcher::{LogWatcher, LogWatcherAction}; use logwatcher::{LogWatcher, LogWatcherAction};
use nix::{errno::Errno, sys::signal::Signal, unistd::Pid, Error}; use nix::{errno::Errno, sys::signal::Signal, unistd::Pid, Error};
......
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
use crate::*; use crate::*;
use read_input::prelude::*; use read_input::prelude::*;
use std::{ use std::{
collections::HashSet,
net::IpAddr,
net::{Ipv4Addr, Ipv6Addr}, net::{Ipv4Addr, Ipv6Addr},
str::FromStr, str::FromStr,
}; };
...@@ -61,6 +63,32 @@ pub(crate) fn wizard_gva(profile_name_opt: Option<&str>, profile_path: PathBuf) ...@@ -61,6 +63,32 @@ pub(crate) fn wizard_gva(profile_name_opt: Option<&str>, profile_path: PathBuf)
.as_object_mut() .as_object_mut()
.ok_or_else(|| anyhow::Error::msg("json conf must be an object"))?; .ok_or_else(|| anyhow::Error::msg("json conf must be an object"))?;
// Get existing whitelist
let mut whitelist = HashSet::new();
if let Some(gva_conf) = conf_json_obj.get("gva") {
let gva_conf_obj = gva_conf
.as_object()
.ok_or_else(|| anyhow::Error::msg("gva conf must be an object"))?;
if let Some(whitelist_json) = gva_conf_obj.get("whitelist") {
let whitelist_array = whitelist_json
.as_array()
.ok_or_else(|| anyhow::Error::msg("gva.whitelist must be an array"))?;
for ip_json in whitelist_array {
if let serde_json::Value::String(ip_str) = ip_json {
whitelist.insert(
IpAddr::from_str(ip_str).context("gva.whitelist contains invalid IP")?,
);
}
}
} else {
whitelist.insert(IpAddr::V4(Ipv4Addr::LOCALHOST));
whitelist.insert(IpAddr::V6(Ipv6Addr::LOCALHOST));
}
} else {
whitelist.insert(IpAddr::V4(Ipv4Addr::LOCALHOST));
whitelist.insert(IpAddr::V6(Ipv6Addr::LOCALHOST));
};
let mut gva_conf = serde_json::Map::new(); let mut gva_conf = serde_json::Map::new();
// Enable GVA API? // Enable GVA API?
...@@ -156,6 +184,41 @@ pub(crate) fn wizard_gva(profile_name_opt: Option<&str>, profile_path: PathBuf) ...@@ -156,6 +184,41 @@ pub(crate) fn wizard_gva(profile_name_opt: Option<&str>, profile_path: PathBuf)
serde_json::Value::String(remote_path), serde_json::Value::String(remote_path),
); );
} }
// whitelist
let res = input().msg("Update whitelist? [y/N]").default('N').get();
if res == 'y' || res == 'Y' {
loop {
println!("1. See whitelist content.");
println!("2. Add an IP to the whitelist.");
println!("3. Removing an IP from the whitelist.");
println!("4. Quit.");
match input().msg("Choose an action: ").default(1).get() {
2usize => {
whitelist.insert(input().msg("Enter a new IP address: ").get());
}
3 => {
whitelist
.remove(&input().msg("Indicate the IP address to be deleted: ").get());
}
4 => break,
_ => {
println!("--------------------------------");
println!("Whitelist content ({} IPs):", whitelist.len());
whitelist.iter().for_each(|ip| println!("{}", ip));
println!("--------------------------------");
}
}
}
}
gva_conf.insert(
"whitelist".to_owned(),
serde_json::Value::Array(
whitelist
.into_iter()
.map(|ip| serde_json::Value::String(ip.to_string()))
.collect(),
),
);
} }
// Insert GVA json conf in global json conf // Insert GVA json conf in global json conf
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment