vpncloud/src/util.rs

442 lines
11 KiB
Rust
Raw Normal View History

// VpnCloud - Peer-to-Peer VPN
2021-02-08 09:11:20 +00:00
// Copyright (C) 2015-2021 Dennis Schwerdel
// This software is licensed under GPL-3 or newer (see LICENSE.md)
2021-01-23 19:29:15 +00:00
use std::process::Command;
2019-12-04 08:32:35 +00:00
use std::{
fmt,
net::{Ipv4Addr, SocketAddr, ToSocketAddrs, UdpSocket},
2021-01-23 19:29:15 +00:00
sync::atomic::{AtomicIsize, Ordering},
2019-12-04 08:32:35 +00:00
};
2016-06-27 13:43:30 +00:00
2020-09-24 17:48:13 +00:00
use crate::error::Error;
2016-06-27 13:43:30 +00:00
2021-01-23 19:29:15 +00:00
#[cfg(not(target_os = "linux"))]
use time;
2016-04-12 07:20:47 +00:00
2019-02-21 21:41:36 +00:00
use signal::{trap::Trap, Signal};
2020-11-03 17:49:35 +00:00
use smallvec::SmallVec;
2020-11-07 11:25:12 +00:00
use std::time::Instant;
2019-02-21 21:41:36 +00:00
2015-11-25 20:55:30 +00:00
pub type Duration = u32;
pub type Time = i64;
2020-09-24 17:48:13 +00:00
#[derive(Clone)]
pub struct MsgBuffer {
space_before: usize,
buffer: [u8; 65535],
start: usize,
2021-01-23 19:29:15 +00:00
end: usize,
2020-09-24 17:48:13 +00:00
}
impl MsgBuffer {
pub fn new(space_before: usize) -> Self {
Self { buffer: [0; 65535], space_before, start: space_before, end: space_before }
}
pub fn get_start(&self) -> usize {
self.start
}
pub fn set_start(&mut self, start: usize) {
self.start = start
}
pub fn prepend_byte(&mut self, byte: u8) {
self.start -= 1;
self.buffer[self.start] = byte
}
pub fn take_prefix(&mut self) -> u8 {
let byte = self.buffer[self.start];
self.start += 1;
byte
}
pub fn buffer(&mut self) -> &mut [u8] {
&mut self.buffer[self.start..]
}
pub fn message(&self) -> &[u8] {
&self.buffer[self.start..self.end]
}
pub fn take(&mut self) -> Option<&[u8]> {
if self.start != self.end {
let end = self.end;
self.end = self.start;
Some(&self.buffer[self.start..end])
} else {
None
}
}
pub fn message_mut(&mut self) -> &mut [u8] {
&mut self.buffer[self.start..self.end]
}
pub fn set_length(&mut self, length: usize) {
self.end = self.start + length
}
pub fn clone_from(&mut self, other: &[u8]) {
self.set_length(other.len());
self.message_mut().clone_from_slice(other);
}
pub fn len(&self) -> usize {
self.end - self.start
}
pub fn is_empty(&self) -> bool {
self.start == self.end
}
pub fn clear(&mut self) {
self.set_start(self.space_before);
self.set_length(0)
}
}
2019-01-01 23:35:14 +00:00
const HEX_CHARS: &[u8] = b"0123456789abcdef";
pub fn bytes_to_hex(bytes: &[u8]) -> String {
2019-12-05 17:29:12 +00:00
let mut s = String::with_capacity(bytes.len() * 2);
for &byte in bytes {
2019-12-05 17:29:12 +00:00
s.push(HEX_CHARS[(byte >> 4) as usize] as char);
s.push(HEX_CHARS[(byte & 0xf) as usize] as char);
}
2019-12-05 17:29:12 +00:00
s
}
2020-05-29 08:04:14 +00:00
pub fn addr_nice(addr: SocketAddr) -> SocketAddr {
if let SocketAddr::V6(v6addr) = addr {
if let Some(ip) = v6addr.ip().to_ipv4() {
2021-01-23 19:29:15 +00:00
return (ip, addr.port()).into();
2020-05-29 08:04:14 +00:00
}
}
addr
}
2015-11-26 09:45:25 +00:00
pub struct Encoder;
impl Encoder {
2016-06-11 14:08:57 +00:00
#[inline]
2015-11-26 09:45:25 +00:00
pub fn read_u16(data: &[u8]) -> u16 {
2019-01-01 23:35:14 +00:00
(u16::from(data[0]) << 8) | u16::from(data[1])
2015-11-26 09:45:25 +00:00
}
2016-06-11 14:08:57 +00:00
#[inline]
2015-11-26 09:45:25 +00:00
pub fn write_u16(val: u16, data: &mut [u8]) {
data[0] = ((val >> 8) & 0xff) as u8;
data[1] = (val & 0xff) as u8;
}
#[inline]
pub fn read_u32(data: &[u8]) -> u32 {
2019-12-04 08:32:35 +00:00
(u32::from(data[0]) << 24) | (u32::from(data[1]) << 16) | (u32::from(data[2]) << 8) | u32::from(data[3])
}
#[inline]
pub fn write_u32(val: u32, data: &mut [u8]) {
data[0] = ((val >> 24) & 0xff) as u8;
data[1] = ((val >> 16) & 0xff) as u8;
data[2] = ((val >> 8) & 0xff) as u8;
data[3] = (val & 0xff) as u8;
}
2016-06-11 14:08:57 +00:00
#[inline]
2015-11-26 09:45:25 +00:00
pub fn read_u64(data: &[u8]) -> u64 {
2019-12-04 08:32:35 +00:00
(u64::from(data[0]) << 56)
| (u64::from(data[1]) << 48)
| (u64::from(data[2]) << 40)
| (u64::from(data[3]) << 32)
| (u64::from(data[4]) << 24)
| (u64::from(data[5]) << 16)
| (u64::from(data[6]) << 8)
| u64::from(data[7])
2015-11-26 09:45:25 +00:00
}
2016-06-11 14:08:57 +00:00
#[inline]
2015-11-26 09:45:25 +00:00
pub fn write_u64(val: u64, data: &mut [u8]) {
data[0] = ((val >> 56) & 0xff) as u8;
data[1] = ((val >> 48) & 0xff) as u8;
data[2] = ((val >> 40) & 0xff) as u8;
data[3] = ((val >> 32) & 0xff) as u8;
data[4] = ((val >> 24) & 0xff) as u8;
data[5] = ((val >> 16) & 0xff) as u8;
data[6] = ((val >> 8) & 0xff) as u8;
data[7] = (val & 0xff) as u8;
}
}
macro_rules! fail {
($format:expr) => ( {
use std::process;
error!($format);
2019-12-04 12:09:20 +00:00
log::logger().flush();
process::exit(-1);
} );
($format:expr, $( $arg:expr ),+) => ( {
use std::process;
error!($format, $( $arg ),+ );
2019-12-04 12:09:20 +00:00
log::logger().flush();
process::exit(-1);
} );
}
macro_rules! try_fail {
($val:expr, $format:expr) => ( {
match $val {
Ok(val) => val,
Err(err) => fail!($format, err)
}
} );
($val:expr, $format:expr, $( $arg:expr ),+) => ( {
match $val {
Ok(val) => val,
Err(err) => fail!($format, $( $arg ),+, err)
}
} );
}
2016-06-27 13:43:30 +00:00
pub fn get_internal_ip() -> Ipv4Addr {
// Get the internal address (this trick gets the address by opening a UDP connection which
// does not really open anything but returns the correct address)
let dummy_sock = UdpSocket::bind("0.0.0.0:0").expect("Failed to bind");
dummy_sock.connect("8.8.8.8:53").expect("Failed to connect");
if let SocketAddr::V4(addr) = dummy_sock.local_addr().expect("Failed to get local address") {
*addr.ip()
} else {
unreachable!()
}
}
2019-12-04 08:32:35 +00:00
#[allow(unknown_lints, clippy::needless_pass_by_value)]
2020-12-19 10:54:55 +00:00
pub fn resolve<Addr: ToSocketAddrs + fmt::Debug>(addr: Addr) -> Result<SmallVec<[SocketAddr; 4]>, Error> {
2020-11-07 11:25:12 +00:00
let mut addrs =
addr.to_socket_addrs().map_err(|_| Error::NameUnresolvable(format!("{:?}", addr)))?.collect::<SmallVec<_>>();
2016-08-12 06:39:04 +00:00
// Try IPv4 first as it usually is faster
2021-01-23 19:29:15 +00:00
addrs.sort_by_key(|addr| match *addr {
SocketAddr::V4(_) => 4,
SocketAddr::V6(_) => 6,
2016-08-12 06:39:04 +00:00
});
2020-11-07 11:25:12 +00:00
// Remove duplicates in addrs (why are there duplicates???)
2016-06-27 13:43:30 +00:00
addrs.dedup();
Ok(addrs)
}
2019-01-09 16:45:12 +00:00
2019-03-01 15:36:18 +00:00
#[allow(unused_macros)]
2019-02-26 17:36:54 +00:00
macro_rules! addr {
2019-12-04 08:32:35 +00:00
($addr: expr) => {{
std::net::ToSocketAddrs::to_socket_addrs($addr).unwrap().next().unwrap()
}};
2019-02-26 17:36:54 +00:00
}
2019-01-09 16:45:12 +00:00
pub struct Bytes(pub u64);
impl fmt::Display for Bytes {
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
let mut size = self.0 as f32;
if size >= 512.0 {
size /= 1024.0;
} else {
2021-01-23 19:29:15 +00:00
return write!(formatter, "{:.0} B", size);
2019-01-09 16:45:12 +00:00
}
if size >= 512.0 {
size /= 1024.0;
} else {
2021-01-23 19:29:15 +00:00
return write!(formatter, "{:.1} KiB", size);
2019-01-09 16:45:12 +00:00
}
if size >= 512.0 {
size /= 1024.0;
} else {
2021-01-23 19:29:15 +00:00
return write!(formatter, "{:.1} MiB", size);
2019-01-09 16:45:12 +00:00
}
if size >= 512.0 {
size /= 1024.0;
} else {
2021-01-23 19:29:15 +00:00
return write!(formatter, "{:.1} GiB", size);
2019-01-09 16:45:12 +00:00
}
write!(formatter, "{:.1} TiB", size)
}
}
2019-02-21 21:41:36 +00:00
pub struct CtrlC {
dummy_time: Instant,
2021-01-23 19:29:15 +00:00
trap: Trap,
2019-02-21 21:41:36 +00:00
}
impl CtrlC {
pub fn new() -> Self {
2019-03-01 22:25:42 +00:00
Default::default()
2019-02-21 21:41:36 +00:00
}
pub fn was_pressed(&self) -> bool {
self.trap.wait(self.dummy_time).is_some()
}
2019-02-24 19:01:32 +00:00
}
2019-03-01 22:25:42 +00:00
impl Default for CtrlC {
fn default() -> Self {
let dummy_time = Instant::now();
let trap = Trap::trap(&[Signal::SIGINT, Signal::SIGTERM, Signal::SIGQUIT]);
Self { dummy_time, trap }
}
}
2019-02-24 19:01:32 +00:00
pub trait TimeSource: Sync + Copy + Send + 'static {
fn now() -> Time;
}
#[derive(Clone, Copy)]
pub struct SystemTimeSource;
impl TimeSource for SystemTimeSource {
#[cfg(target_os = "linux")]
fn now() -> Time {
let mut tv = libc::timespec { tv_sec: 0, tv_nsec: 0 };
2019-12-04 08:32:35 +00:00
unsafe {
libc::clock_gettime(6, &mut tv);
}
2019-02-24 19:01:32 +00:00
tv.tv_sec as Time
}
#[cfg(not(target_os = "linux"))]
fn now() -> Time {
time::get_time().sec
}
}
thread_local! {
static MOCK_TIME: AtomicIsize = AtomicIsize::new(0);
}
#[derive(Clone, Copy)]
pub struct MockTimeSource;
impl MockTimeSource {
pub fn set_time(time: Time) {
MOCK_TIME.with(|t| t.store(time as isize, Ordering::SeqCst))
}
}
impl TimeSource for MockTimeSource {
fn now() -> Time {
MOCK_TIME.with(|t| t.load(Ordering::SeqCst) as Time)
}
2019-12-04 08:32:35 +00:00
}
2019-12-04 15:33:42 +00:00
/// 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 {
let mut d: usize = m as usize;
2019-12-20 12:54:58 +00:00
for item in buf.iter_mut().take(buflen) {
d += *item as usize * 16;
*item = (d % 62) as u8;
2019-12-04 15:33:42 +00:00
d /= 62;
}
assert!(d < 62);
if d > 0 {
buf[buflen] = d as u8;
buflen += 1;
}
buflen
}
const BASE62: [char; 62] = [
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
2021-01-23 19:29:15 +00:00
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
2019-12-04 15:33:42 +00:00
];
pub fn to_base62(data: &[u8]) -> String {
let l = data.len();
let mut buf = vec![0; l * 2];
let mut buflen = 0;
for b in data {
buflen = base62_add_mult_16(&mut buf, buflen, b / 16);
buflen = base62_add_mult_16(&mut buf, buflen, b % 16);
}
buf[0..buflen].reverse();
let mut result = String::with_capacity(buflen);
for b in &buf[0..buflen] {
result.push(BASE62[*b as usize]);
}
result
}
pub fn from_base62(data: &str) -> Result<Vec<u8>, char> {
let mut buf = Vec::with_capacity(data.len() / 2 + data.len() / 4);
for c in data.chars() {
let mut val = match c {
'0'..='9' => ((c as usize) % ('0' as usize)),
'A'..='Z' => ((c as usize) % ('A' as usize)) + 10,
'a'..='z' => ((c as usize) % ('a' as usize)) + 36,
2021-01-23 19:29:15 +00:00
_ => return Err(c),
2019-12-04 15:33:42 +00:00
};
2019-12-20 12:54:58 +00:00
for item in &mut buf {
val += *item as usize * 62;
*item = (val % 256) as u8;
2019-12-04 15:33:42 +00:00
val /= 256;
}
if val > 0 {
buf.push(val as u8);
}
}
buf.reverse();
Ok(buf)
}
2020-05-30 14:12:54 +00:00
#[derive(Default)]
pub struct StatsdMsg {
entries: Vec<String>,
2021-01-23 19:29:15 +00:00
key: Vec<String>,
2020-05-30 14:12:54 +00:00
}
impl StatsdMsg {
pub fn new() -> Self {
Default::default()
}
pub fn add<T: fmt::Display>(&mut self, key: &str, val: T, type_: &str) -> &mut Self {
self.entries.push(format!("{}.{}:{}|{}", self.key.join("."), key, val, type_));
self
}
pub fn with_ns<F: FnOnce(&mut Self)>(&mut self, ns: &str, f: F) -> &mut Self {
self.key.push(ns.to_string());
f(self);
self.key.pop();
self
}
pub fn build(&self) -> String {
self.entries.join("\n")
}
}
2021-01-23 19:29:15 +00:00
pub fn run_cmd(mut cmd: Command) {
match cmd.status() {
Ok(status) => {
if !status.success() {
error!("Command returned error: {:?}", status.code())
}
}
Err(e) => error!("Failed to execute command {:?}: {}", cmd, e),
}
}
2020-05-30 14:12:54 +00:00
2019-12-04 15:33:42 +00:00
#[test]
fn base62() {
assert_eq!("", to_base62(&[0]));
assert_eq!("z", to_base62(&[61]));
assert_eq!("10", to_base62(&[62]));
assert_eq!("48", to_base62(&[1, 0]));
assert_eq!("1Xp7Ke", to_base62(b"Test"));
assert!(from_base62("").unwrap().is_empty());
assert_eq!(vec![61], from_base62("z").unwrap());
assert_eq!(vec![62], from_base62("10").unwrap());
assert_eq!(vec![1, 0], from_base62("48").unwrap());
assert_eq!(b"Test".to_vec(), from_base62("1Xp7Ke").unwrap());
}