Commit 6f983932 authored by nanahira's avatar nanahira

allow -c config.json

parent 1f97c198
#!/bin/bash #!/bin/bash
pid=0 pid=0
down=$(echo "$1" | jq -r '.routers | .[].down')
downs=()
if [ "${1:-}" = "-c" ] || [ "${1:-}" = "--config" ]; then
config_file="$2"
# 从文件直接 cat 管道给 jq
while IFS= read -r d; do
downs+=("$d")
done < <(cat "$config_file" | jq -r '.routers[].down')
else
# 直接把参数当作 JSON 字符串传给 jq,不存到变量
while IFS= read -r d; do
downs+=("$d")
done < <(printf '%s' "$1" | jq -r '.routers[].down')
fi
echo "$down" echo "$down"
run_stop() { run_stop() {
signalCode=$1 signalCode=$1
if [ -n "$down" ]; then for down in "${downs[@]}"; do
eval "$down" "$down"
fi done
if [ $pid -ne 0 ]; then if [ $pid -ne 0 ]; then
kill "-$signalCode" "$pid" kill "-$signalCode" "$pid"
wait "$pid" wait "$pid"
......
mod router; mod router;
use crate::router::{Router, RouterReader, RouterWriter, SECRET_LENGTH}; use crate::router::{Router, RouterReader, RouterWriter};
use std::collections::HashMap; use std::collections::HashMap;
use std::env; use std::env;
use std::error::Error; use std::error::Error;
...@@ -40,10 +40,28 @@ use crossbeam_utils::thread; ...@@ -40,10 +40,28 @@ use crossbeam_utils::thread;
use grouping_by::GroupingBy; use grouping_by::GroupingBy;
use pnet::packet::ipv4::Ipv4Packet; use pnet::packet::ipv4::Ipv4Packet;
use socket2::Socket; use socket2::Socket;
use std::fs;
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
println!("Init"); println!("Init");
let config: Config = serde_json::from_str(env::args().nth(1).ok_or("need param")?.as_str())?; let args: Vec<String> = env::args().collect();
if args.len() < 2 {
return Err("need JSON string or -c <config.json>".into());
}
let config: Config;
if args[1] == "-c" || args[1] == "--config" {
// 从文件读
if args.len() < 3 {
return Err("missing value for -c/--config".into());
}
let data = fs::read_to_string(&args[2])?;
config = serde_json::from_str(&data)?;
} else {
// 当作 JSON 字符串解析
config = serde_json::from_str(&args[1])?;
}
println!("Read config"); println!("Read config");
let mut sockets: HashMap<u16, Arc<Socket>> = HashMap::new(); let mut sockets: HashMap<u16, Arc<Socket>> = HashMap::new();
println!("Ready"); println!("Ready");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment