mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-04-02 18:55:32 +00:00
aquatic_udp: move privdrop code to crate root, use in glommio impl
This commit is contained in:
parent
eafb88c345
commit
0e58347ac4
4 changed files with 47 additions and 30 deletions
1
TODO.md
1
TODO.md
|
|
@ -1,7 +1,6 @@
|
||||||
# TODO
|
# TODO
|
||||||
|
|
||||||
* aquatic_udp glommio
|
* aquatic_udp glommio
|
||||||
* privdrop
|
|
||||||
* disable by default!
|
* disable by default!
|
||||||
|
|
||||||
* access lists:
|
* access lists:
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ use glommio::channels::channel_mesh::MeshBuilder;
|
||||||
use glommio::prelude::*;
|
use glommio::prelude::*;
|
||||||
|
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
|
use crate::drop_privileges_after_socket_binding;
|
||||||
|
|
||||||
mod common;
|
mod common;
|
||||||
pub mod handlers;
|
pub mod handlers;
|
||||||
|
|
@ -87,6 +88,8 @@ pub fn run(config: Config) -> anyhow::Result<()> {
|
||||||
executors.push(executor);
|
executors.push(executor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
drop_privileges_after_socket_binding(&config, num_bound_sockets).unwrap();
|
||||||
|
|
||||||
for executor in executors {
|
for executor in executors {
|
||||||
executor
|
executor
|
||||||
.expect("failed to spawn local executor")
|
.expect("failed to spawn local executor")
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,11 @@
|
||||||
|
use std::{
|
||||||
|
sync::{
|
||||||
|
atomic::{AtomicUsize, Ordering},
|
||||||
|
Arc,
|
||||||
|
},
|
||||||
|
time::Duration,
|
||||||
|
};
|
||||||
|
|
||||||
use cfg_if::cfg_if;
|
use cfg_if::cfg_if;
|
||||||
|
|
||||||
pub mod common;
|
pub mod common;
|
||||||
|
|
@ -7,6 +15,7 @@ pub mod glommio;
|
||||||
pub mod mio;
|
pub mod mio;
|
||||||
|
|
||||||
use config::Config;
|
use config::Config;
|
||||||
|
use privdrop::PrivDrop;
|
||||||
|
|
||||||
pub const APP_NAME: &str = "aquatic_udp: UDP BitTorrent tracker";
|
pub const APP_NAME: &str = "aquatic_udp: UDP BitTorrent tracker";
|
||||||
|
|
||||||
|
|
@ -19,3 +28,35 @@ pub fn run(config: Config) -> ::anyhow::Result<()> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn drop_privileges_after_socket_binding(
|
||||||
|
config: &Config,
|
||||||
|
num_bound_sockets: Arc<AtomicUsize>,
|
||||||
|
) -> anyhow::Result<()> {
|
||||||
|
if config.privileges.drop_privileges {
|
||||||
|
let mut counter = 0usize;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let sockets = num_bound_sockets.load(Ordering::SeqCst);
|
||||||
|
|
||||||
|
if sockets == config.socket_workers {
|
||||||
|
PrivDrop::default()
|
||||||
|
.chroot(config.privileges.chroot_path.clone())
|
||||||
|
.user(config.privileges.user.clone())
|
||||||
|
.apply()?;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
::std::thread::sleep(Duration::from_millis(10));
|
||||||
|
|
||||||
|
counter += 1;
|
||||||
|
|
||||||
|
if counter == 500 {
|
||||||
|
panic!("Sockets didn't bind in time for privilege drop.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,15 +2,11 @@ use std::thread::Builder;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::{
|
use std::{
|
||||||
ops::Deref,
|
ops::Deref,
|
||||||
sync::{
|
sync::{atomic::AtomicUsize, Arc},
|
||||||
atomic::{AtomicUsize, Ordering},
|
|
||||||
Arc,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use crossbeam_channel::unbounded;
|
use crossbeam_channel::unbounded;
|
||||||
use privdrop::PrivDrop;
|
|
||||||
|
|
||||||
pub mod common;
|
pub mod common;
|
||||||
pub mod handlers;
|
pub mod handlers;
|
||||||
|
|
@ -20,6 +16,7 @@ pub mod tasks;
|
||||||
use aquatic_common::access_list::{AccessListArcSwap, AccessListMode, AccessListQuery};
|
use aquatic_common::access_list::{AccessListArcSwap, AccessListMode, AccessListQuery};
|
||||||
|
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
|
use crate::drop_privileges_after_socket_binding;
|
||||||
|
|
||||||
use common::State;
|
use common::State;
|
||||||
|
|
||||||
|
|
@ -38,30 +35,7 @@ pub fn run(config: Config) -> ::anyhow::Result<()> {
|
||||||
|
|
||||||
start_workers(config.clone(), state.clone(), num_bound_sockets.clone())?;
|
start_workers(config.clone(), state.clone(), num_bound_sockets.clone())?;
|
||||||
|
|
||||||
if config.privileges.drop_privileges {
|
drop_privileges_after_socket_binding(&config, num_bound_sockets).unwrap();
|
||||||
let mut counter = 0usize;
|
|
||||||
|
|
||||||
loop {
|
|
||||||
let sockets = num_bound_sockets.load(Ordering::SeqCst);
|
|
||||||
|
|
||||||
if sockets == config.socket_workers {
|
|
||||||
PrivDrop::default()
|
|
||||||
.chroot(config.privileges.chroot_path.clone())
|
|
||||||
.user(config.privileges.user.clone())
|
|
||||||
.apply()?;
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
::std::thread::sleep(Duration::from_millis(10));
|
|
||||||
|
|
||||||
counter += 1;
|
|
||||||
|
|
||||||
if counter == 500 {
|
|
||||||
panic!("Sockets didn't bind in time for privilege drop.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
::std::thread::sleep(Duration::from_secs(config.cleaning.interval));
|
::std::thread::sleep(Duration::from_secs(config.cleaning.interval));
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue