Commit 1f97c198 authored by nanahira's avatar nanahira

make local id inside routers

parent b63f8153
......@@ -20,11 +20,13 @@ use serde::Deserialize;
#[derive(Deserialize)]
pub struct ConfigRouter {
pub local_id: u16,
pub remote_id: u16,
pub proto: i32,
pub family: u8,
pub mark: u32,
pub endpoint: String,
pub local_secret: String,
pub remote_secret: String,
pub dev: String,
pub up: String,
......@@ -32,8 +34,6 @@ pub struct ConfigRouter {
#[derive(Deserialize)]
pub struct Config {
pub local_id: u16,
pub local_secret: String,
pub routers: Vec<ConfigRouter>,
}
use crossbeam_utils::thread;
......@@ -45,8 +45,6 @@ fn main() -> Result<(), Box<dyn Error>> {
println!("Init");
let config: Config = serde_json::from_str(env::args().nth(1).ok_or("need param")?.as_str())?;
println!("Read config");
let local_secret: [u8; SECRET_LENGTH] = Router::create_secret(config.local_secret.as_str())?;
println!("Created local secret");
let mut sockets: HashMap<u16, Arc<Socket>> = HashMap::new();
println!("Ready");
let routers: HashMap<u16, Router> = config
......@@ -87,7 +85,7 @@ fn main() -> Result<(), Box<dyn Error>> {
// Pre-initialize with our Meta header (local -> remote)
let meta = Meta {
src_id: config.local_id,
src_id: router.config.local_id,
dst_id: router.config.remote_id,
reversed: 0,
};
......@@ -124,13 +122,15 @@ fn main() -> Result<(), Box<dyn Error>> {
.split_at_mut_checked(size_of::<Meta>())
.ok_or("malformed packet")?;
let meta: &Meta = unsafe { transmute(meta_bytes.as_ptr()) };
if meta.dst_id == config.local_id && meta.reversed == 0 {
if meta.reversed == 0 {
let router = router_writers
.get_mut(&meta.src_id)
.ok_or("missing router")?;
*router.endpoint.write().unwrap() = Some(addr);
router.decrypt(payload, &local_secret);
router.tun_writer.write_all(payload)?;
if meta.dst_id == router.config.local_id {
*router.endpoint.write().unwrap() = Some(addr);
router.decrypt(payload);
router.tun_writer.write_all(payload)?;
}
}
Ok::<(), Box<dyn Error>>(())
......
......@@ -48,14 +48,15 @@ impl<'a> RouterReader<'a> {
// raw -> tun
pub struct RouterWriter<'a> {
pub config: &'a ConfigRouter,
pub secret: [u8; SECRET_LENGTH],
pub tun_writer: Writer,
pub endpoint: Arc<RwLock<Option<SockAddr>>>,
}
impl<'a> RouterWriter<'a> {
#[inline]
pub(crate) fn decrypt(&self, data: &mut [u8], secret: &[u8; SECRET_LENGTH]) {
xor_with_secret(data, secret);
pub(crate) fn decrypt(&self, data: &mut [u8]) {
xor_with_secret(data, &self.secret);
}
pub(crate) fn key(&self) -> u16 {
......@@ -65,7 +66,8 @@ impl<'a> RouterWriter<'a> {
pub struct Router<'a> {
pub config: &'a ConfigRouter,
pub secret: [u8; SECRET_LENGTH],
pub local_secret: [u8; SECRET_LENGTH],
pub remote_secret: [u8; SECRET_LENGTH],
pub tun_reader: Reader,
pub tun_writer: Writer,
pub socket: Arc<Socket>,
......@@ -134,7 +136,8 @@ impl<'a> Router<'a> {
config: &'a ConfigRouter,
sockets: &mut HashMap<u16, Arc<Socket>>,
) -> Result<Router<'a>, Box<dyn std::error::Error>> {
let secret = Self::create_secret(config.remote_secret.as_str())?;
let local_secret = Self::create_secret(config.local_secret.as_str())?;
let remote_secret = Self::create_secret(config.remote_secret.as_str())?;
let endpoint = Self::create_endpoint(&config)?;
let socket = Self::create_raw_socket(&config, sockets)?;
if (config.mark > 0) {
......@@ -146,7 +149,8 @@ impl<'a> Router<'a> {
let router = Router {
config,
secret,
local_secret,
remote_secret,
endpoint,
tun_reader,
tun_writer,
......@@ -159,13 +163,14 @@ impl<'a> Router<'a> {
pub fn split(self) -> (RouterReader<'a>, RouterWriter<'a>) {
let writer = RouterWriter {
config: self.config,
secret: self.local_secret,
endpoint: Arc::clone(&self.endpoint),
tun_writer: self.tun_writer,
};
let reader = RouterReader {
config: self.config,
secret: self.secret,
secret: self.remote_secret,
endpoint: self.endpoint,
tun_reader: self.tun_reader,
socket: self.socket,
......
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