Skip to content

Commit 32be8c1

Browse files
authored
server: support config reload (#13)
Signed-off-by: Xiaobo Liu <[email protected]>
1 parent 261c83b commit 32be8c1

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ $ netguard-tool keygen --help
6969
```
7070

7171

72+
### Reload config
73+
74+
Reload `netguard-server` config file:
75+
76+
```shell
77+
$ pkill -HUP netguard-server
78+
```
79+
80+
7281
## Build
7382

7483
Build release version.
@@ -87,7 +96,6 @@ $ cargo build --release
8796

8897
- Add query and reject connection Interfaces
8998
- More certificate signing algorithms
90-
- Reload configuration file
9199
- Hot update bin executable program
92100
- Audit log
93101
- Knock SDK APIs

server/src/bin/server.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::sync::Arc;
33

44
use anyhow::Result;
55
use clap::Parser;
6-
use signal_hook::consts::{SIGINT, SIGQUIT, SIGTERM};
6+
use signal_hook::consts::{SIGHUP, SIGINT, SIGQUIT, SIGTERM};
77
use signal_hook::iterator::Signals;
88
use tikv_jemallocator::Jemalloc;
99
use tracing::{debug, info};
@@ -59,7 +59,7 @@ fn main() -> Result<()> {
5959

6060
iptables::rules_create(&config)?;
6161

62-
wait_for_signal()?;
62+
wait_for_signal(&args, &workers)?;
6363

6464
iptables::rules_destroy(&config)?;
6565

@@ -68,15 +68,25 @@ fn main() -> Result<()> {
6868
Ok(())
6969
}
7070

71-
fn wait_for_signal() -> Result<()> {
72-
let sigs = vec![SIGTERM, SIGQUIT, SIGINT];
71+
fn wait_for_signal(args: &Args, workers: &[Worker]) -> Result<()> {
72+
let sigs = vec![SIGTERM, SIGQUIT, SIGINT, SIGHUP];
7373

7474
let mut signals = Signals::new(sigs)?;
7575

7676
for signal in &mut signals {
7777
debug!("Received a signal {:?}", signal);
7878

7979
match signal {
80+
SIGHUP => match Config::from_file(&args.config) {
81+
Ok(new_config) => {
82+
for worker in workers {
83+
worker.update_config(new_config.clone());
84+
}
85+
}
86+
Err(e) => {
87+
info!("Failed to reload config: {e}")
88+
}
89+
},
8090
term_sig => {
8191
info!("Received a termination signal {:?}", term_sig);
8292
break;

0 commit comments

Comments
 (0)