mirror of https://github.com/dswd/vpncloud.git
Some code cleanup
This commit is contained in:
parent
00858fe140
commit
246cd3bc2a
|
@ -445,7 +445,7 @@ impl<D: Device, P: Protocol, T: Table, S: Socket, TS: TimeSource> GenericCloud<D
|
||||||
debug!("Connecting to {:?}", addr);
|
debug!("Connecting to {:?}", addr);
|
||||||
let subnets = self.addresses.clone();
|
let subnets = self.addresses.clone();
|
||||||
let node_id = self.node_id;
|
let node_id = self.node_id;
|
||||||
let mut msg = Message::Init(0, node_id, subnets.clone(), self.peer_timeout_publish);
|
let mut msg = Message::Init(0, node_id, subnets, self.peer_timeout_publish);
|
||||||
self.send_msg(addr, &mut msg)
|
self.send_msg(addr, &mut msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -447,7 +447,6 @@ fn config_merge() {
|
||||||
group: Some("root".to_string()),
|
group: Some("root".to_string()),
|
||||||
pid_file: Some("/run/vpncloud-mynet.run".to_string()),
|
pid_file: Some("/run/vpncloud-mynet.run".to_string()),
|
||||||
stats_file: Some("/var/log/vpncloud-mynet.stats".to_string()),
|
stats_file: Some("/var/log/vpncloud-mynet.stats".to_string()),
|
||||||
daemonize: true,
|
daemonize: true
|
||||||
..Default::default()
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -171,9 +171,7 @@ fn encrypt_decrypt_chacha20poly1305() {
|
||||||
let msg_bytes = msg.as_bytes();
|
let msg_bytes = msg.as_bytes();
|
||||||
let mut buffer = [0u8; 1024];
|
let mut buffer = [0u8; 1024];
|
||||||
let header = [0u8; 8];
|
let header = [0u8; 8];
|
||||||
for i in 0..msg_bytes.len() {
|
buffer[..msg_bytes.len()].clone_from_slice(&msg_bytes);
|
||||||
buffer[i] = msg_bytes[i];
|
|
||||||
}
|
|
||||||
let mut nonce1 = [0u8; 12];
|
let mut nonce1 = [0u8; 12];
|
||||||
let size = sender.encrypt(&mut buffer, msg_bytes.len(), &mut nonce1, &header);
|
let size = sender.encrypt(&mut buffer, msg_bytes.len(), &mut nonce1, &header);
|
||||||
assert_eq!(size, msg_bytes.len() + sender.additional_bytes());
|
assert_eq!(size, msg_bytes.len() + sender.additional_bytes());
|
||||||
|
@ -195,9 +193,7 @@ fn encrypt_decrypt_aes256() {
|
||||||
let msg_bytes = msg.as_bytes();
|
let msg_bytes = msg.as_bytes();
|
||||||
let mut buffer = [0u8; 1024];
|
let mut buffer = [0u8; 1024];
|
||||||
let header = [0u8; 8];
|
let header = [0u8; 8];
|
||||||
for i in 0..msg_bytes.len() {
|
buffer[..msg_bytes.len()].clone_from_slice(&msg_bytes);
|
||||||
buffer[i] = msg_bytes[i];
|
|
||||||
}
|
|
||||||
let mut nonce1 = [0u8; 12];
|
let mut nonce1 = [0u8; 12];
|
||||||
let size = sender.encrypt(&mut buffer, msg_bytes.len(), &mut nonce1, &header);
|
let size = sender.encrypt(&mut buffer, msg_bytes.len(), &mut nonce1, &header);
|
||||||
assert_eq!(size, msg_bytes.len() + sender.additional_bytes());
|
assert_eq!(size, msg_bytes.len() + sender.additional_bytes());
|
||||||
|
|
|
@ -59,7 +59,7 @@ use crate::{
|
||||||
const VERSION: u8 = 1;
|
const VERSION: u8 = 1;
|
||||||
const MAGIC: HeaderMagic = *b"vpn\x01";
|
const MAGIC: HeaderMagic = *b"vpn\x01";
|
||||||
|
|
||||||
static USAGE: &'static str = include_str!("usage.txt");
|
static USAGE: &str = include_str!("usage.txt");
|
||||||
|
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Default)]
|
#[derive(Deserialize, Debug, Default)]
|
||||||
|
@ -240,6 +240,7 @@ impl<P: Protocol> AnyCloud<P> {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[allow(clippy::cognitive_complexity)]
|
||||||
fn run<P: Protocol>(config: Config) {
|
fn run<P: Protocol>(config: Config) {
|
||||||
let device = try_fail!(
|
let device = try_fail!(
|
||||||
TunTapDevice::new(&config.device_name, config.device_type, config.device_path.as_ref().map(|s| s as &str)),
|
TunTapDevice::new(&config.device_name, config.device_type, config.device_path.as_ref().map(|s| s as &str)),
|
||||||
|
|
|
@ -60,6 +60,7 @@ fn connect_nat_3_peers() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[allow(clippy::cognitive_complexity)]
|
||||||
fn nat_keepalive() {
|
fn nat_keepalive() {
|
||||||
init_debug_logger();
|
init_debug_logger();
|
||||||
MockTimeSource::set_time(0);
|
MockTimeSource::set_time(0);
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[allow(clippy::cognitive_complexity)]
|
||||||
fn connect_v4() {
|
fn connect_v4() {
|
||||||
let mut node1 = create_tap_node(false);
|
let mut node1 = create_tap_node(false);
|
||||||
let node1_addr = addr!("1.2.3.4:5678");
|
let node1_addr = addr!("1.2.3.4:5678");
|
||||||
|
@ -56,6 +57,7 @@ fn connect_v6() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[allow(clippy::cognitive_complexity)]
|
||||||
fn cross_connect() {
|
fn cross_connect() {
|
||||||
let mut node1 = create_tap_node(false);
|
let mut node1 = create_tap_node(false);
|
||||||
let node1_addr = addr!("1.1.1.1:1111");
|
let node1_addr = addr!("1.1.1.1:1111");
|
||||||
|
|
|
@ -190,11 +190,7 @@ pub fn decode<'a>(data: &'a mut [u8], magic: HeaderMagic, crypto: &Crypto) -> Re
|
||||||
pos += read;
|
pos += read;
|
||||||
addrs.push(range);
|
addrs.push(range);
|
||||||
}
|
}
|
||||||
let mut peer_timeout = 1800;
|
let peer_timeout = if data.len() >= pos + 2 { Encoder::read_u16(&data[pos..]) } else { 1800 };
|
||||||
if data.len() >= pos + 2 {
|
|
||||||
peer_timeout = Encoder::read_u16(&data[pos..]);
|
|
||||||
// pos += 2; never read
|
|
||||||
}
|
|
||||||
Message::Init(stage, node_id, addrs, peer_timeout)
|
Message::Init(stage, node_id, addrs, peer_timeout)
|
||||||
}
|
}
|
||||||
3 => Message::Close,
|
3 => Message::Close,
|
||||||
|
@ -353,12 +349,10 @@ fn udpmessage_packet() {
|
||||||
let res = encode(&mut msg, &mut [], MAGIC, &mut crypto);
|
let res = encode(&mut msg, &mut [], MAGIC, &mut crypto);
|
||||||
assert_eq!(res.len(), 13);
|
assert_eq!(res.len(), 13);
|
||||||
assert_eq!(&res[..8], &[118, 112, 110, 1, 0, 0, 0, 0]);
|
assert_eq!(&res[..8], &[118, 112, 110, 1, 0, 0, 0, 0]);
|
||||||
for i in 0..res.len() {
|
buf[..res.len()].clone_from_slice(&res);
|
||||||
buf[i] = res[i];
|
|
||||||
}
|
|
||||||
len = res.len();
|
len = res.len();
|
||||||
}
|
}
|
||||||
let msg2 = decode(&mut buf[..len], MAGIC, &mut crypto).unwrap();
|
let msg2 = decode(&mut buf[..len], MAGIC, &crypto).unwrap();
|
||||||
assert_eq!(msg, msg2);
|
assert_eq!(msg, msg2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -373,9 +367,7 @@ fn udpmessage_encrypted() {
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||||
];
|
];
|
||||||
let mut orig_payload = [0; 133];
|
let mut orig_payload = [0; 133];
|
||||||
for i in 0..payload.len() {
|
orig_payload[..payload.len()].clone_from_slice(&payload);
|
||||||
orig_payload[i] = payload[i];
|
|
||||||
}
|
|
||||||
let orig_msg = Message::Data(&mut orig_payload, 64, 69);
|
let orig_msg = Message::Data(&mut orig_payload, 64, 69);
|
||||||
let mut msg = Message::Data(&mut payload, 64, 69);
|
let mut msg = Message::Data(&mut payload, 64, 69);
|
||||||
let mut buf = [0; 1024];
|
let mut buf = [0; 1024];
|
||||||
|
@ -384,12 +376,10 @@ fn udpmessage_encrypted() {
|
||||||
let res = encode(&mut msg, &mut [], MAGIC, &mut crypto);
|
let res = encode(&mut msg, &mut [], MAGIC, &mut crypto);
|
||||||
assert_eq!(res.len(), 41);
|
assert_eq!(res.len(), 41);
|
||||||
assert_eq!(&res[..8], &[118, 112, 110, 1, 1, 0, 0, 0]);
|
assert_eq!(&res[..8], &[118, 112, 110, 1, 1, 0, 0, 0]);
|
||||||
for i in 0..res.len() {
|
buf[..res.len()].clone_from_slice(&res);
|
||||||
buf[i] = res[i];
|
|
||||||
}
|
|
||||||
len = res.len();
|
len = res.len();
|
||||||
}
|
}
|
||||||
let msg2 = decode(&mut buf[..len], MAGIC, &mut crypto).unwrap();
|
let msg2 = decode(&mut buf[..len], MAGIC, &crypto).unwrap();
|
||||||
assert_eq!(orig_msg, msg2);
|
assert_eq!(orig_msg, msg2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -414,16 +404,16 @@ fn udpmessage_peers() {
|
||||||
assert_eq!(res[i], should[i]);
|
assert_eq!(res[i], should[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let msg2 = decode(&mut should, MAGIC, &mut crypto).unwrap();
|
let msg2 = decode(&mut should, MAGIC, &crypto).unwrap();
|
||||||
assert_eq!(msg, msg2);
|
assert_eq!(msg, msg2);
|
||||||
// Missing IPv4 count
|
// Missing IPv4 count
|
||||||
assert!(decode(&mut [118, 112, 110, 1, 0, 0, 0, 1], MAGIC, &mut crypto).is_err());
|
assert!(decode(&mut [118, 112, 110, 1, 0, 0, 0, 1], MAGIC, &crypto).is_err());
|
||||||
// Truncated IPv4
|
// Truncated IPv4
|
||||||
assert!(decode(&mut [118, 112, 110, 1, 0, 0, 0, 1, 1], MAGIC, &mut crypto).is_err());
|
assert!(decode(&mut [118, 112, 110, 1, 0, 0, 0, 1, 1], MAGIC, &crypto).is_err());
|
||||||
// Missing IPv6 count
|
// Missing IPv6 count
|
||||||
assert!(decode(&mut [118, 112, 110, 1, 0, 0, 0, 1, 1, 1, 2, 3, 4, 0, 0], MAGIC, &mut crypto).is_err());
|
assert!(decode(&mut [118, 112, 110, 1, 0, 0, 0, 1, 1, 1, 2, 3, 4, 0, 0], MAGIC, &crypto).is_err());
|
||||||
// Truncated IPv6
|
// Truncated IPv6
|
||||||
assert!(decode(&mut [118, 112, 110, 1, 0, 0, 0, 1, 1, 1, 2, 3, 4, 0, 0, 1], MAGIC, &mut crypto).is_err());
|
assert!(decode(&mut [118, 112, 110, 1, 0, 0, 0, 1, 1, 1, 2, 3, 4, 0, 0, 1], MAGIC, &crypto).is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -448,7 +438,7 @@ fn udpmessage_init() {
|
||||||
assert_eq!(res[i], should[i]);
|
assert_eq!(res[i], should[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let msg2 = decode(&mut should, MAGIC, &mut crypto).unwrap();
|
let msg2 = decode(&mut should, MAGIC, &crypto).unwrap();
|
||||||
assert_eq!(msg, msg2);
|
assert_eq!(msg, msg2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -463,31 +453,31 @@ fn udpmessage_close() {
|
||||||
assert_eq!(res.len(), 8);
|
assert_eq!(res.len(), 8);
|
||||||
assert_eq!(&res, &should);
|
assert_eq!(&res, &should);
|
||||||
}
|
}
|
||||||
let msg2 = decode(&mut should, MAGIC, &mut crypto).unwrap();
|
let msg2 = decode(&mut should, MAGIC, &crypto).unwrap();
|
||||||
assert_eq!(msg, msg2);
|
assert_eq!(msg, msg2);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn udpmessage_invalid() {
|
fn udpmessage_invalid() {
|
||||||
let mut crypto = Crypto::None;
|
let crypto = Crypto::None;
|
||||||
assert!(decode(&mut [0x76, 0x70, 0x6e, 1, 0, 0, 0, 0], MAGIC, &mut crypto).is_ok());
|
assert!(decode(&mut [0x76, 0x70, 0x6e, 1, 0, 0, 0, 0], MAGIC, &crypto).is_ok());
|
||||||
// too short
|
// too short
|
||||||
assert!(decode(&mut [], MAGIC, &mut crypto).is_err());
|
assert!(decode(&mut [], MAGIC, &crypto).is_err());
|
||||||
// invalid protocol
|
// invalid protocol
|
||||||
assert!(decode(&mut [0, 1, 2, 0, 0, 0, 0, 0], MAGIC, &mut crypto).is_err());
|
assert!(decode(&mut [0, 1, 2, 0, 0, 0, 0, 0], MAGIC, &crypto).is_err());
|
||||||
// invalid version
|
// invalid version
|
||||||
assert!(decode(&mut [0x76, 0x70, 0x6e, 0xaa, 0, 0, 0, 0], MAGIC, &mut crypto).is_err());
|
assert!(decode(&mut [0x76, 0x70, 0x6e, 0xaa, 0, 0, 0, 0], MAGIC, &crypto).is_err());
|
||||||
// invalid crypto
|
// invalid crypto
|
||||||
assert!(decode(&mut [0x76, 0x70, 0x6e, 1, 0xaa, 0, 0, 0], MAGIC, &mut crypto).is_err());
|
assert!(decode(&mut [0x76, 0x70, 0x6e, 1, 0xaa, 0, 0, 0], MAGIC, &crypto).is_err());
|
||||||
// invalid msg type
|
// invalid msg type
|
||||||
assert!(decode(&mut [0x76, 0x70, 0x6e, 1, 0, 0, 0, 0xaa], MAGIC, &mut crypto).is_err());
|
assert!(decode(&mut [0x76, 0x70, 0x6e, 1, 0, 0, 0, 0xaa], MAGIC, &crypto).is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn udpmessage_invalid_crypto() {
|
fn udpmessage_invalid_crypto() {
|
||||||
let mut crypto = Crypto::from_shared_key(CryptoMethod::ChaCha20, "test");
|
let crypto = Crypto::from_shared_key(CryptoMethod::ChaCha20, "test");
|
||||||
// truncated crypto
|
// truncated crypto
|
||||||
assert!(decode(&mut [0x76, 0x70, 0x6e, 1, 1, 0, 0, 0], MAGIC, &mut crypto).is_err());
|
assert!(decode(&mut [0x76, 0x70, 0x6e, 1, 1, 0, 0, 0], MAGIC, &crypto).is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
12
src/util.rs
12
src/util.rs
|
@ -254,9 +254,9 @@ impl TimeSource for MockTimeSource {
|
||||||
/// Helper function that multiplies the base62 data in buf[0..buflen] by 16 and adds m to it
|
/// Helper function that multiplies the base62 data in buf[0..buflen] by 16 and adds m to it
|
||||||
fn base62_add_mult_16(buf: &mut [u8], mut buflen: usize, m: u8) -> usize {
|
fn base62_add_mult_16(buf: &mut [u8], mut buflen: usize, m: u8) -> usize {
|
||||||
let mut d: usize = m as usize;
|
let mut d: usize = m as usize;
|
||||||
for i in 0..buflen {
|
for item in buf.iter_mut().take(buflen) {
|
||||||
d += buf[i] as usize * 16;
|
d += *item as usize * 16;
|
||||||
buf[i] = (d % 62) as u8;
|
*item = (d % 62) as u8;
|
||||||
d /= 62;
|
d /= 62;
|
||||||
}
|
}
|
||||||
assert!(d < 62);
|
assert!(d < 62);
|
||||||
|
@ -298,9 +298,9 @@ pub fn from_base62(data: &str) -> Result<Vec<u8>, char> {
|
||||||
'a'..='z' => ((c as usize) % ('a' as usize)) + 36,
|
'a'..='z' => ((c as usize) % ('a' as usize)) + 36,
|
||||||
_ => return Err(c)
|
_ => return Err(c)
|
||||||
};
|
};
|
||||||
for i in 0..buf.len() {
|
for item in &mut buf {
|
||||||
val += buf[i] as usize * 62;
|
val += *item as usize * 62;
|
||||||
buf[i] = (val % 256) as u8;
|
*item = (val % 256) as u8;
|
||||||
val /= 256;
|
val /= 256;
|
||||||
}
|
}
|
||||||
if val > 0 {
|
if val > 0 {
|
||||||
|
|
Loading…
Reference in New Issue