chacha20 is supported, need test aes

This commit is contained in:
alex 2026-03-18 15:58:28 +08:00
parent fa8a60a737
commit 321146f6ac
3 changed files with 93 additions and 73 deletions

View File

@ -11,6 +11,7 @@ use std::env;
use std::time::Duration; use std::time::Duration;
use clap::Parser; use clap::Parser;
use daemonize::Daemonize; use daemonize::Daemonize;
use etherparse::icmpv6::CODE_PARAM_PROBLEM_UNRECOG_NEXT_HEADER_BY_INTERMEDIATE_NODE;
use futures_util::io; use futures_util::io;
use libc::SIGTERM; use libc::SIGTERM;
use libc::kill; use libc::kill;
@ -345,6 +346,9 @@ fn main() {
} }
} }
let should_daemonize = true;
if should_daemonize {
let out = OpenOptions::new() let out = OpenOptions::new()
.create(true) .create(true)
.truncate(true) .truncate(true)
@ -367,6 +371,20 @@ fn main() {
match daemonize.start() { match daemonize.start() {
Ok(_) => { Ok(_) => {
run_it(cmd, client_id, mac, system, version);
}
Err(e) => {
eprintln!("failed to daemonize");
}
}
} else {
run_it(cmd, client_id, mac, system, version);
}
}
fn run_it(cmd: CommandLineInput2, client_id: String, mac: Mac, system: &str, version: &str) {
let rt = Runtime::new().unwrap(); let rt = Runtime::new().unwrap();
match &cmd.cmd { match &cmd.cmd {
Commands::Start => { Commands::Start => {
@ -417,11 +435,7 @@ fn main() {
} }
} }
}
Err(e) => {
eprintln!("failed to daemonize");
}
}
} }
pub fn delete_pid_file() { pub fn delete_pid_file() {

View File

@ -207,6 +207,7 @@ impl TunTapPacketHandler for Iface {
Ok(_) => return Ok(()), Ok(_) => return Ok(()),
} }
} }
async fn handle_packet_from_device( async fn handle_packet_from_device(
&self, &self,
data: Vec<u8>, data: Vec<u8>,
@ -288,7 +289,7 @@ impl TunTapPacketHandler for Iface {
} }
let size = data.len(); let size = data.len();
let Ok(encrypted) = edge.encryptor.read().unwrap().decrypt(&data) else { let Ok(encrypted) = edge.encryptor.read().unwrap().encrypt(&data) else {
// let Ok(encrypted) = aes_encrypt(encrypt_key, &data) else { // let Ok(encrypted) = aes_encrypt(encrypt_key, &data) else {
error!("failed to encrypt packet request"); error!("failed to encrypt packet request");
return Ok(()); return Ok(());

View File

@ -1,5 +1,7 @@
use std::{sync::{Arc, OnceLock, RwLock, atomic::{AtomicBool, AtomicU32, Ordering}}, time::{SystemTime, UNIX_EPOCH}}; use std::{sync::{Arc, OnceLock, RwLock, atomic::{AtomicBool, AtomicU32, Ordering}}, time::{SystemTime, UNIX_EPOCH}};
use tracing::debug;
use chacha20poly1305::{KeyInit, aead::Aead}; use chacha20poly1305::{KeyInit, aead::Aead};
use dashmap::DashSet; use dashmap::DashSet;
use sdlan_sn_rs::utils::{Result, SDLanError, aes_decrypt, aes_encrypt}; use sdlan_sn_rs::utils::{Result, SDLanError, aes_decrypt, aes_encrypt};
@ -115,7 +117,10 @@ impl Encryptor for Chacha20Encryptor {
nonce.extend_from_slice(&next_data.to_be_bytes()); nonce.extend_from_slice(&next_data.to_be_bytes());
match cipher.encrypt(nonce.as_slice().into(), data) { match cipher.encrypt(nonce.as_slice().into(), data) {
Ok(data) => Ok(data), Ok(data) => {
nonce.extend_from_slice(&data);
Ok(nonce)
},
Err(e) => { Err(e) => {
Err(SDLanError::EncryptError(e.to_string())) Err(SDLanError::EncryptError(e.to_string()))
} }
@ -127,11 +132,11 @@ impl Encryptor for Chacha20Encryptor {
return Err(SDLanError::EncryptError("ciphered text size error".to_owned())) return Err(SDLanError::EncryptError("ciphered text size error".to_owned()))
} }
let cipher = chacha20poly1305::ChaCha20Poly1305::new(self.key.as_slice().into()); let cipher = chacha20poly1305::ChaCha20Poly1305::new(self.key.as_slice().into());
let nonce = &ciphered[0..11]; let nonce = &ciphered[0..12];
match cipher.decrypt(nonce.into(), &ciphered[12..]) { match cipher.decrypt(nonce.into(), &ciphered[12..]) {
Ok(data) => Ok(data), Ok(data) => Ok(data),
Err(e) => { Err(e) => {
Err(SDLanError::EncryptError(e.to_string())) Err(SDLanError::EncryptError(format!("failed to decyrpt: {}", e.to_string())))
} }
} }
} }