Removed askForRouterIP packet due to lack of need

Removed some comments
Changed some stuff
	modified:   Cargo.lock
	modified:   Cargo.toml
	modified:   src/client.rs
	modified:   src/server.rs
	modified:   src/udp.rs
This commit is contained in:
Michael Wain 2024-11-13 01:32:36 +03:00
parent 0684364121
commit 64d8601fff
5 changed files with 130 additions and 144 deletions

153
Cargo.lock generated
View File

@ -135,7 +135,7 @@ dependencies = [
"ndk-context", "ndk-context",
"ndk-sys 0.6.0+11769913", "ndk-sys 0.6.0+11769913",
"num_enum", "num_enum",
"thiserror", "thiserror 1.0.64",
] ]
[[package]] [[package]]
@ -321,7 +321,7 @@ checksum = "3b43422f69d8ff38f95f1b2bb76517c91589a924d1559a0e935d7c8ce0274c11"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.79", "syn 2.0.87",
] ]
[[package]] [[package]]
@ -361,7 +361,7 @@ checksum = "c7c24de15d275a1ecfd47a380fb4d5ec9bfe0933f309ed5e705b775596a3574d"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.79", "syn 2.0.87",
] ]
[[package]] [[package]]
@ -378,7 +378,7 @@ checksum = "721cae7de5c34fbb2acd27e21e6d2cf7b886dce0c27388d46c4e6c47ea4318dd"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.79", "syn 2.0.87",
] ]
[[package]] [[package]]
@ -587,7 +587,7 @@ checksum = "bcfcc3cd946cb52f0bbfdbbcfa2f4e24f75ebb6c0e1002f7c25904fada18b9ec"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.79", "syn 2.0.87",
] ]
[[package]] [[package]]
@ -604,18 +604,18 @@ checksum = "428d9aa8fbc0670b7b8d6030a7fadd0f86151cae55e4dbbece15f3780a3dfaf3"
[[package]] [[package]]
name = "c2rust-bitfields" name = "c2rust-bitfields"
version = "0.18.0" version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b43c3f07ab0ef604fa6f595aa46ec2f8a22172c975e186f6f5bf9829a3b72c41" checksum = "367e5d1b30f28be590b6b3868da1578361d29d9bfac516d22f497d28ed7c9055"
dependencies = [ dependencies = [
"c2rust-bitfields-derive", "c2rust-bitfields-derive",
] ]
[[package]] [[package]]
name = "c2rust-bitfields-derive" name = "c2rust-bitfields-derive"
version = "0.18.0" version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3cbc102e2597c9744c8bd8c15915d554300601c91a079430d309816b0912545" checksum = "a279db9c50c4024eeca1a763b6e0f033848ce74e83e47454bcf8a8a98f7b0b56"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -633,7 +633,7 @@ dependencies = [
"polling", "polling",
"rustix", "rustix",
"slab", "slab",
"thiserror", "thiserror 1.0.64",
] ]
[[package]] [[package]]
@ -766,7 +766,7 @@ version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4274ea815e013e0f9f04a2633423e14194e408a0576c943ce3d14ca56c50031c" checksum = "4274ea815e013e0f9f04a2633423e14194e408a0576c943ce3d14ca56c50031c"
dependencies = [ dependencies = [
"thiserror", "thiserror 1.0.64",
"x11rb", "x11rb",
] ]
@ -1109,7 +1109,7 @@ checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.79", "syn 2.0.87",
] ]
[[package]] [[package]]
@ -1370,7 +1370,7 @@ checksum = "de0d48a183585823424a4ce1aa132d174a6a81bd540895822eb4c8373a8e49e8"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.79", "syn 2.0.87",
] ]
[[package]] [[package]]
@ -1541,7 +1541,7 @@ checksum = "1a5c6c585bc94aaf2c7b51dd4c2ba22680844aba4c687be581871a6f518c5742"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.79", "syn 2.0.87",
] ]
[[package]] [[package]]
@ -1658,7 +1658,7 @@ checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.79", "syn 2.0.87",
] ]
[[package]] [[package]]
@ -1803,7 +1803,7 @@ checksum = "6f56f6318968d03c18e1bcf4857ff88c61157e9da8e47c5f29055d60e1228884"
dependencies = [ dependencies = [
"log", "log",
"presser", "presser",
"thiserror", "thiserror 1.0.64",
"winapi", "winapi",
"windows", "windows",
] ]
@ -1902,7 +1902,7 @@ dependencies = [
"com", "com",
"libc", "libc",
"libloading 0.8.5", "libloading 0.8.5",
"thiserror", "thiserror 1.0.64",
"widestring", "widestring",
"winapi", "winapi",
] ]
@ -2101,7 +2101,7 @@ dependencies = [
"iced_renderer", "iced_renderer",
"iced_widget", "iced_widget",
"iced_winit", "iced_winit",
"thiserror", "thiserror 1.0.64",
] ]
[[package]] [[package]]
@ -2120,7 +2120,7 @@ dependencies = [
"palette", "palette",
"rustc-hash 2.0.0", "rustc-hash 2.0.0",
"smol_str", "smol_str",
"thiserror", "thiserror 1.0.64",
"web-time", "web-time",
] ]
@ -2168,7 +2168,7 @@ dependencies = [
"once_cell", "once_cell",
"raw-window-handle", "raw-window-handle",
"rustc-hash 2.0.0", "rustc-hash 2.0.0",
"thiserror", "thiserror 1.0.64",
"unicode-segmentation", "unicode-segmentation",
] ]
@ -2182,7 +2182,7 @@ dependencies = [
"iced_tiny_skia", "iced_tiny_skia",
"iced_wgpu", "iced_wgpu",
"log", "log",
"thiserror", "thiserror 1.0.64",
] ]
[[package]] [[package]]
@ -2195,7 +2195,7 @@ dependencies = [
"iced_core", "iced_core",
"iced_futures", "iced_futures",
"raw-window-handle", "raw-window-handle",
"thiserror", "thiserror 1.0.64",
] ]
[[package]] [[package]]
@ -2230,7 +2230,7 @@ dependencies = [
"log", "log",
"once_cell", "once_cell",
"rustc-hash 2.0.0", "rustc-hash 2.0.0",
"thiserror", "thiserror 1.0.64",
"wgpu", "wgpu",
] ]
@ -2245,7 +2245,7 @@ dependencies = [
"num-traits", "num-traits",
"once_cell", "once_cell",
"rustc-hash 2.0.0", "rustc-hash 2.0.0",
"thiserror", "thiserror 1.0.64",
"unicode-segmentation", "unicode-segmentation",
] ]
@ -2260,7 +2260,7 @@ dependencies = [
"iced_runtime", "iced_runtime",
"log", "log",
"rustc-hash 2.0.0", "rustc-hash 2.0.0",
"thiserror", "thiserror 1.0.64",
"tracing", "tracing",
"wasm-bindgen-futures", "wasm-bindgen-futures",
"web-sys", "web-sys",
@ -2344,7 +2344,7 @@ dependencies = [
"combine", "combine",
"jni-sys", "jni-sys",
"log", "log",
"thiserror", "thiserror 1.0.64",
"walkdir", "walkdir",
] ]
@ -2358,7 +2358,7 @@ dependencies = [
"combine", "combine",
"jni-sys", "jni-sys",
"log", "log",
"thiserror", "thiserror 1.0.64",
"walkdir", "walkdir",
] ]
@ -2373,7 +2373,7 @@ dependencies = [
"combine", "combine",
"jni-sys", "jni-sys",
"log", "log",
"thiserror", "thiserror 1.0.64",
"walkdir", "walkdir",
"windows-sys 0.45.0", "windows-sys 0.45.0",
] ]
@ -2537,7 +2537,7 @@ dependencies = [
"serde-value", "serde-value",
"serde_json", "serde_json",
"serde_yaml", "serde_yaml",
"thiserror", "thiserror 1.0.64",
"thread-id", "thread-id",
"typemap-ors", "typemap-ors",
"winapi", "winapi",
@ -2662,7 +2662,7 @@ dependencies = [
"rustc-hash 1.1.0", "rustc-hash 1.1.0",
"spirv", "spirv",
"termcolor", "termcolor",
"thiserror", "thiserror 1.0.64",
"unicode-xid", "unicode-xid",
] ]
@ -2678,7 +2678,7 @@ dependencies = [
"ndk-sys 0.6.0+11769913", "ndk-sys 0.6.0+11769913",
"num_enum", "num_enum",
"raw-window-handle", "raw-window-handle",
"thiserror", "thiserror 1.0.64",
] ]
[[package]] [[package]]
@ -2713,7 +2713,7 @@ checksum = "433419f898328beca4f2c6c73a1b52540658d92b0a99f0269330457e0fd998d5"
dependencies = [ dependencies = [
"cc", "cc",
"libc", "libc",
"thiserror", "thiserror 1.0.64",
"winapi", "winapi",
] ]
@ -2786,7 +2786,7 @@ dependencies = [
"proc-macro-crate", "proc-macro-crate",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.79", "syn 2.0.87",
] ]
[[package]] [[package]]
@ -3114,7 +3114,7 @@ dependencies = [
"bitflags 1.3.2", "bitflags 1.3.2",
"byteorder", "byteorder",
"hwaddr", "hwaddr",
"thiserror", "thiserror 1.0.64",
] ]
[[package]] [[package]]
@ -3144,7 +3144,7 @@ dependencies = [
"by_address", "by_address",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.79", "syn 2.0.87",
] ]
[[package]] [[package]]
@ -3252,7 +3252,7 @@ dependencies = [
"phf_shared 0.11.2", "phf_shared 0.11.2",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.79", "syn 2.0.87",
] ]
[[package]] [[package]]
@ -3290,7 +3290,7 @@ checksum = "a4502d8515ca9f32f1fb543d987f63d95a14934883db45bdb48060b6b69257f8"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.79", "syn 2.0.87",
] ]
[[package]] [[package]]
@ -3444,7 +3444,7 @@ dependencies = [
"itertools", "itertools",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.79", "syn 2.0.87",
] ]
[[package]] [[package]]
@ -3587,7 +3587,7 @@ checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43"
dependencies = [ dependencies = [
"getrandom", "getrandom",
"libredox", "libredox",
"thiserror", "thiserror 1.0.64",
] ]
[[package]] [[package]]
@ -3825,7 +3825,7 @@ checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.79", "syn 2.0.87",
] ]
[[package]] [[package]]
@ -3848,7 +3848,7 @@ checksum = "6c64451ba24fc7a6a2d60fc75dd9c83c90903b19028d4eff35e88fc1e86564e9"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.79", "syn 2.0.87",
] ]
[[package]] [[package]]
@ -3968,7 +3968,7 @@ dependencies = [
"log", "log",
"memmap2", "memmap2",
"rustix", "rustix",
"thiserror", "thiserror 1.0.64",
"wayland-backend", "wayland-backend",
"wayland-client", "wayland-client",
"wayland-csd-frame", "wayland-csd-frame",
@ -4120,9 +4120,9 @@ dependencies = [
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.79" version = "2.0.87"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89132cd0bf050864e1d38dc3bbc07a0eb8e7530af26344d3d2bbbef83499f590" checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -4187,7 +4187,16 @@ version = "1.0.64"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d50af8abc119fb8bb6dbabcfa89656f46f84aa0ac7688088608076ad2b459a84" checksum = "d50af8abc119fb8bb6dbabcfa89656f46f84aa0ac7688088608076ad2b459a84"
dependencies = [ dependencies = [
"thiserror-impl", "thiserror-impl 1.0.64",
]
[[package]]
name = "thiserror"
version = "2.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c006c85c7651b3cf2ada4584faa36773bd07bac24acfb39f3c431b36d7e667aa"
dependencies = [
"thiserror-impl 2.0.3",
] ]
[[package]] [[package]]
@ -4198,7 +4207,18 @@ checksum = "08904e7672f5eb876eaaf87e0ce17857500934f4981c4a0ab2b4aa98baac7fc3"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.79", "syn 2.0.87",
]
[[package]]
name = "thiserror-impl"
version = "2.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f077553d607adc1caf65430528a576c757a71ed73944b66ebb58ef2bbd243568"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.87",
] ]
[[package]] [[package]]
@ -4302,7 +4322,7 @@ checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.79", "syn 2.0.87",
] ]
[[package]] [[package]]
@ -4458,7 +4478,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.79", "syn 2.0.87",
] ]
[[package]] [[package]]
@ -4528,20 +4548,19 @@ checksum = "5902c5d130972a0000f60860bfbf46f7ca3db5391eddfedd1b8728bd9dc96c0e"
[[package]] [[package]]
name = "tun2" name = "tun2"
version = "2.0.9" version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b139b40733b3e81560335ccad94948916c4d03ed1ded505a8675428879075b4" checksum = "21afe73d3d42581a7400fbf5aec057a646ffe3f8bb5ae3f61d88c7e7f4ac77be"
dependencies = [ dependencies = [
"bytes", "bytes",
"cfg-if", "cfg-if",
"futures",
"futures-core", "futures-core",
"ipnet", "ipnet",
"libc", "libc",
"libloading 0.8.5",
"log", "log",
"nix", "nix",
"rustversion", "thiserror 1.0.64",
"thiserror",
"tokio", "tokio",
"tokio-util", "tokio-util",
"windows-sys 0.59.0", "windows-sys 0.59.0",
@ -4744,7 +4763,7 @@ dependencies = [
"once_cell", "once_cell",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.79", "syn 2.0.87",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
@ -4778,7 +4797,7 @@ checksum = "26c6ab57572f7a24a4985830b120de1594465e5d500f24afe89e16b4e833ef68"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.79", "syn 2.0.87",
"wasm-bindgen-backend", "wasm-bindgen-backend",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
@ -4978,7 +4997,7 @@ dependencies = [
"raw-window-handle", "raw-window-handle",
"rustc-hash 1.1.0", "rustc-hash 1.1.0",
"smallvec", "smallvec",
"thiserror", "thiserror 1.0.64",
"web-sys", "web-sys",
"wgpu-hal", "wgpu-hal",
"wgpu-types", "wgpu-types",
@ -5022,7 +5041,7 @@ dependencies = [
"renderdoc-sys", "renderdoc-sys",
"rustc-hash 1.1.0", "rustc-hash 1.1.0",
"smallvec", "smallvec",
"thiserror", "thiserror 1.0.64",
"wasm-bindgen", "wasm-bindgen",
"web-sys", "web-sys",
"wgpu-types", "wgpu-types",
@ -5088,7 +5107,7 @@ dependencies = [
"clipboard_wayland", "clipboard_wayland",
"clipboard_x11", "clipboard_x11",
"raw-window-handle", "raw-window-handle",
"thiserror", "thiserror 1.0.64",
] ]
[[package]] [[package]]
@ -5406,14 +5425,16 @@ dependencies = [
[[package]] [[package]]
name = "wintun-bindings" name = "wintun-bindings"
version = "0.6.4" version = "0.7.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3af47a132f449a64ff858f9ad876a3d1812df30e9500cddfdcabb2266ab68c0d" checksum = "a8aed5bc5516ca7a52018b0a198911566edfa07584bdbfd71ea39161aa13806e"
dependencies = [ dependencies = [
"blocking",
"c2rust-bitfields", "c2rust-bitfields",
"futures",
"libloading 0.8.5", "libloading 0.8.5",
"log", "log",
"thiserror", "thiserror 2.0.3",
"windows-sys 0.59.0", "windows-sys 0.59.0",
] ]
@ -5555,7 +5576,7 @@ dependencies = [
"proc-macro-crate", "proc-macro-crate",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.79", "syn 2.0.87",
"zvariant_utils", "zvariant_utils",
] ]
@ -5594,7 +5615,7 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.79", "syn 2.0.87",
] ]
[[package]] [[package]]
@ -5614,7 +5635,7 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.79", "syn 2.0.87",
] ]
[[package]] [[package]]
@ -5639,7 +5660,7 @@ dependencies = [
"proc-macro-crate", "proc-macro-crate",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.79", "syn 2.0.87",
"zvariant_utils", "zvariant_utils",
] ]
@ -5651,5 +5672,5 @@ checksum = "c51bcff7cc3dbb5055396bcf774748c3dab426b4b8659046963523cee4808340"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.79", "syn 2.0.87",
] ]

View File

@ -38,7 +38,7 @@ socket2 = "0.4"
env_logger = "0.9" env_logger = "0.9"
log = "0.4.20" log = "0.4.20"
futures = "0.3.30" futures = "0.3.30"
tun2 = { version = "2", features = ["async"] } tun2 = { version = "4.0.0", features = ["async"] }
packet = "0.1.4" packet = "0.1.4"
crossbeam-channel = "0.5.13" crossbeam-channel = "0.5.13"
hex = "0.4" hex = "0.4"

View File

@ -1,16 +1,6 @@
use crossbeam_channel::unbounded; use crossbeam_channel::unbounded;
use socket2::SockAddr; use socket2::SockAddr;
/*
What the fuck I want to implement?
I need to make abstract class VPNClient which should be extended by several others:
AndroidClient
DesktopClient
Both of child classes should trigger the same "core vpn client" module
*/
pub mod general { pub mod general {
use crate::config::ClientConfiguration; use crate::config::ClientConfiguration;
use tokio_util::sync::CancellationToken; use tokio_util::sync::CancellationToken;
@ -24,8 +14,8 @@ pub mod general {
use std::net::Ipv4Addr; use std::net::Ipv4Addr;
use std::pin::pin; use std::pin::pin;
use x25519_dalek::{PublicKey, StaticSecret}; use x25519_dalek::{PublicKey, StaticSecret};
use crate::udp::{UDPVpnPacket, UDPVpnHandshake, UDPSerializable, UDPVpnRouterIP}; use crate::udp::{UDPVpnPacket, UDPVpnHandshake, UDPSerializable};
use tun2::{platform::Device, Configuration, DeviceReader, DeviceWriter}; use tun2::{AbstractDevice, AsyncDevice, Configuration, DeviceReader, DeviceWriter};
trait ReadWrapper { trait ReadWrapper {
async fn read(&mut self, buf: &mut [u8]) -> Result<usize, ()>; async fn read(&mut self, buf: &mut [u8]) -> Result<usize, ()>;
@ -61,20 +51,37 @@ pub mod general {
} }
trait WriteWrapper { trait WriteWrapper {
async fn write(&mut self, buf: &[u8]) -> Result<usize, ()>; async fn write(&mut self, msg: WriterMessage) -> Result<usize, ()>;
}
pub enum WriterMessage {
Plain(Vec<u8>),
Gateway(Ipv4Addr)
} }
pub struct DevWriter { pub struct DevWriter {
pub dr: DeviceWriter pub dr: DeviceWriter,
//pub dev: AsyncDevice
} }
// TODO: implement custom Error // TODO: implement custom Error
impl WriteWrapper for DevWriter { impl WriteWrapper for DevWriter {
async fn write(&mut self, buf: &[u8]) -> Result<usize, ()> { async fn write(&mut self, msg: WriterMessage) -> Result<usize, ()> {
if let Ok(a) = self.dr.write(buf).await { match msg {
return Ok(a); WriterMessage::Plain(buf) => {
if let Ok(a) = self.dr.write(&buf).await {
return Ok(a);
}
Err(())
},
WriterMessage::Gateway(addr) => {
/* if self.dev.set_destination(addr.into()).is_err() {
return Err(());
}*/
Ok(0)
}
} }
Err(())
} }
} }
@ -83,11 +90,16 @@ pub mod general {
} }
impl WriteWrapper for FdWriter { impl WriteWrapper for FdWriter {
async fn write(&mut self, buf: &[u8]) -> Result<usize, ()> { async fn write(&mut self, msg: WriterMessage) -> Result<usize, ()> {
if let Ok(a) = self.br.write(buf).await { match msg {
return Ok(a); WriterMessage::Plain(buf) => {
if let Ok(a) = self.br.write(&buf).await {
return Ok(a);
}
Err(())
},
WriterMessage::Gateway(_addr) => {Ok(0)}
} }
Err(())
} }
} }
@ -133,7 +145,7 @@ pub mod general {
let s_cipher = cipher_shared.clone(); let s_cipher = cipher_shared.clone();
self.dev_writer.write(&handshake.serialize()).await; self.dev_writer.write(WriterMessage::Plain(handshake.serialize())).await;
let mut buf = vec![0; 1400]; // mtu let mut buf = vec![0; 1400]; // mtu
let mut buf1 = vec![0; 4096]; let mut buf1 = vec![0; 4096];
@ -149,7 +161,7 @@ pub mod general {
rr = rx.recv() => { rr = rx.recv() => {
if let Some(bytes) = rr { if let Some(bytes) = rr {
info!("Write to tun."); info!("Write to tun.");
if let Err(e) = self.dev_writer.write(&bytes).await { if let Err(e) = self.dev_writer.write(WriterMessage::Plain(bytes)).await {
error!("Writing error: {:?}", e); error!("Writing error: {:?}", e);
} }
/* if let Err(e) = self.dev_writer.flush().await { /* if let Err(e) = self.dev_writer.flush().await {
@ -219,10 +231,6 @@ pub mod general {
} }
}, // payload }, // payload
2 => { info!("Got keepalive packet"); }, 2 => { info!("Got keepalive packet"); },
3 => {
let router_packet = UDPVpnRouterIP::deserialize(&(buf1[..l].to_vec()));
// todo: set of the router ip
},
_ => { error!("Unexpected header value."); } _ => { error!("Unexpected header value."); }
} }
}, },
@ -313,7 +321,7 @@ pub mod desktop {
if !ip_output.status.success() { if !ip_output.status.success() {
error!("Failed to route all traffic: {:?}", String::from_utf8_lossy(&ip_output.stderr)); error!("Failed to route all traffic: {:?}", String::from_utf8_lossy(&ip_output.stderr));
} }
// TODO: replace 192.168.0.1 with relative variable
ip_output = Command::new("sudo") ip_output = Command::new("sudo")
.arg("ip") .arg("ip")
.arg("route") .arg("route")
@ -342,7 +350,8 @@ pub mod desktop {
let mut config = tun2::Configuration::default(); let mut config = tun2::Configuration::default();
config.address(&self.client_config.client.address) config.address(&self.client_config.client.address)
.netmask("255.255.255.255") .netmask("255.255.255.255")
.destination("10.66.66.1") .destination(&self.client_config.client.address)
.mtu(1400)
.tun_name("tun0") .tun_name("tun0")
.up(); .up();
@ -351,7 +360,7 @@ pub mod desktop {
sock.connect(&self.client_config.server.endpoint).await.unwrap(); sock.connect(&self.client_config.server.endpoint).await.unwrap();
let dev = tun2::create_as_async(&config).unwrap(); let dev = tun2::create_as_async(&config).unwrap();
let (mut dev_writer, mut dev_reader) = dev.split().unwrap(); let (dev_writer, dev_reader) = dev.split().unwrap();
let mut client = CoreVpnClient{ client_config: self.client_config.clone(), dev_reader: DevReader{ dr: dev_reader }, dev_writer: DevWriter{dr: dev_writer}, close_token: tokio_util::sync::CancellationToken::new()}; let mut client = CoreVpnClient{ client_config: self.client_config.clone(), dev_reader: DevReader{ dr: dev_reader }, dev_writer: DevWriter{dr: dev_writer}, close_token: tokio_util::sync::CancellationToken::new()};
let s_a: SocketAddr = self.client_config.server.endpoint.parse().unwrap(); let s_a: SocketAddr = self.client_config.server.endpoint.parse().unwrap();

View File

@ -14,7 +14,7 @@ use network_interface::NetworkInterface;
use network_interface::NetworkInterfaceConfig; use network_interface::NetworkInterfaceConfig;
use crate::config::{ ServerConfiguration, ServerPeer}; use crate::config::{ ServerConfiguration, ServerPeer};
use crate::udp::{UDPKeepAlive, UDPSerializable, UDPVpnAskForIP, UDPVpnHandshake, UDPVpnPacket, UDPVpnRouterIP}; use crate::udp::{UDPKeepAlive, UDPSerializable, UDPVpnHandshake, UDPVpnPacket};
fn configure_routes(s_interface: Option<&str>) { fn configure_routes(s_interface: Option<&str>) {
let interfaces = NetworkInterface::show().unwrap(); let interfaces = NetworkInterface::show().unwrap();
@ -189,7 +189,7 @@ pub async fn server_mode(server_config: ServerConfiguration, s_interface: Option
Some(h) => { Some(h) => {
match h { match h {
0 => { 0 => {
let handshake = UDPVpnHandshake::deserialize(&buf); // todo: replace &buf reference with length dependent reference. let handshake = UDPVpnHandshake::deserialize(&buf);
info!("Got handshake from {:?}", handshake.request_ip); info!("Got handshake from {:?}", handshake.request_ip);
let skey = BASE64_STANDARD.encode(&handshake.public_key); let skey = BASE64_STANDARD.encode(&handshake.public_key);
if plp.iter().any(|c| c.ip == handshake.request_ip && c.public_key == skey) { if plp.iter().any(|c| c.ip == handshake.request_ip && c.public_key == skey) {
@ -228,13 +228,6 @@ pub async fn server_mode(server_config: ServerConfiguration, s_interface: Option
}); });
}, // payload }, // payload
2 => { }, // got keepalive packet 2 => { }, // got keepalive packet
3 => {
if let Ok(_packet) = UDPVpnAskForIP::deserialize(&(buf[..len].to_vec())) {
info!("Router address requested");
let response = UDPVpnRouterIP {router_ip: server_config.interface.internal_address.parse::<Ipv4Addr>().unwrap()};
let _ = send2hnd_ssr.send((response.serialize(), addr));
}
}, // fake router address request
_ => error!("Unexpected header value.") _ => error!("Unexpected header value.")
} }
}, },

View File

@ -30,43 +30,6 @@ impl UDPVpnPacket {
} }
} }
pub struct UDPVpnRouterIP {
pub router_ip: Ipv4Addr // [u8; 4]
}
impl UDPSerializable for UDPVpnRouterIP {
fn serialize(&self) -> Vec<u8> {
let h: &[u8] = &[3];
[h, &self.router_ip.octets()].concat() // [u8; 5]
}
}
impl UDPVpnRouterIP {
pub fn deserialize(data: &Vec<u8>) -> Self {
UDPVpnRouterIP { router_ip: Ipv4Addr::new(data[1], data[2], data[3], data[4]) }
}
}
pub struct UDPVpnAskForIP {}
impl UDPVpnAskForIP {
pub fn deserialize(data: &Vec<u8>) -> Result<UDPVpnAskForIP, ()> {
if data.len() == 33 {
return Ok(UDPVpnAskForIP {});
}
Err(())
}
}
impl UDPSerializable for UDPVpnAskForIP {
fn serialize(&self) -> Vec<u8> {
let mut rng = rand::thread_rng();
let h: &[u8] = &[3];
let a: [u8; 32] = rng.gen();
[h, &a].concat()
}
}
pub struct UDPVpnHandshake { pub struct UDPVpnHandshake {
pub public_key: Vec<u8>, pub public_key: Vec<u8>,
pub request_ip: Ipv4Addr // [u8; 4] pub request_ip: Ipv4Addr // [u8; 4]