Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server: support config reload #13

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ $ netguard-tool keygen --help
```


### Reload config

Reload `netguard-server` config file:

```shell
$ pkill -HUP netguard-server
```


## Build

Build release version.
Expand All @@ -87,7 +96,6 @@ $ cargo build --release

- Add query and reject connection Interfaces
- More certificate signing algorithms
- Reload configuration file
- Hot update bin executable program
- Audit log
- Knock SDK APIs
Expand Down
18 changes: 14 additions & 4 deletions server/src/bin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;

use anyhow::Result;
use clap::Parser;
use signal_hook::consts::{SIGINT, SIGQUIT, SIGTERM};
use signal_hook::consts::{SIGHUP, SIGINT, SIGQUIT, SIGTERM};
use signal_hook::iterator::Signals;
use tikv_jemallocator::Jemalloc;
use tracing::{debug, info};
Expand Down Expand Up @@ -59,7 +59,7 @@ fn main() -> Result<()> {

iptables::rules_create(&config)?;

wait_for_signal()?;
wait_for_signal(&args, &workers)?;

iptables::rules_destroy(&config)?;

Expand All @@ -68,15 +68,25 @@ fn main() -> Result<()> {
Ok(())
}

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

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

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

match signal {
SIGHUP => match Config::from_file(&args.config) {
Ok(new_config) => {
for worker in workers {
worker.update_config(new_config.clone());
}
}
Err(e) => {
info!("Failed to reload config: {e}")
}
},
term_sig => {
info!("Received a termination signal {:?}", term_sig);
break;
Expand Down