changed the url and added the windows' code

This commit is contained in:
asxalex 2024-07-05 22:31:21 +08:00
parent edf5779af2
commit 7877737eb7
5 changed files with 103 additions and 13 deletions

56
Cargo.lock generated
View File

@ -196,6 +196,26 @@ version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9"
[[package]]
name = "c2rust-bitfields"
version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b43c3f07ab0ef604fa6f595aa46ec2f8a22172c975e186f6f5bf9829a3b72c41"
dependencies = [
"c2rust-bitfields-derive",
]
[[package]]
name = "c2rust-bitfields-derive"
version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3cbc102e2597c9744c8bd8c15915d554300601c91a079430d309816b0912545"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "cbc"
version = "0.1.2"
@ -796,6 +816,16 @@ version = "0.2.155"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c"
[[package]]
name = "libloading"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e310b3a6b5907f99202fcdb4960ff45b93735d7c7d96b760fcff8db2dc0e103d"
dependencies = [
"cfg-if",
"windows-targets 0.52.6",
]
[[package]]
name = "libm"
version = "0.2.8"
@ -1386,12 +1416,13 @@ dependencies = [
"tokio",
"tokio-util",
"tracing",
"wintun",
]
[[package]]
name = "sdlan-sn-rs"
version = "0.1.0"
source = "git+ssh://git@git.asxalex.pw/sdlan-v2/sdlan-rs.git#f320526f2dc1f6c3154b9ade81c556af0d068378"
source = "git+http://git.asxalex.pw/sdlan-v2/sdlan-rs.git#f320526f2dc1f6c3154b9ade81c556af0d068378"
dependencies = [
"aes",
"byteorder",
@ -2245,6 +2276,16 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e48a53791691ab099e5e2ad123536d0fff50652600abaf43bbf952894110d0be"
dependencies = [
"windows-core",
"windows-targets 0.52.6",
]
[[package]]
name = "windows-core"
version = "0.52.0"
@ -2402,6 +2443,19 @@ dependencies = [
"memchr",
]
[[package]]
name = "wintun"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b3c8c8876c686f8a2d6376999ac1c9a24c74d2968551c9394f7e89127783685"
dependencies = [
"c2rust-bitfields",
"libloading",
"log",
"thiserror",
"windows",
]
[[package]]
name = "zerocopy"
version = "0.7.35"

View File

@ -13,8 +13,12 @@ once_cell = "1.19.0"
prost = "0.12.6"
prost-build = "0.12.6"
rsa = "0.9.6"
sdlan-sn-rs = { git = "ssh://git@git.asxalex.pw/sdlan-v2/sdlan-rs.git" }
# sdlan-sn-rs = { git = "ssh://git@git.asxalex.pw/sdlan-v2/sdlan-rs.git" }
sdlan-sn-rs = { git = "http://git.asxalex.pw/sdlan-v2/sdlan-rs.git" }
structopt = "0.3.26"
tokio = { version = "1.38.0", futures = ["full"] }
tokio-util = "0.7.11"
tracing = "0.1.40"
[target.'cfg(windows)'.dependencies]
wintun = "0.4.0"

28
src/bin/sdlan/main.rs Normal file
View File

@ -0,0 +1,28 @@
use sdlan_rs::get_edge;
use sdlan_sn_rs::log;
use std::time::Duration;
use tokio::sync::mpsc::Sender;
use sdlan_rs::run_sdlan;
use sdlan_rs::CommandLine;
#[tokio::main]
async fn main() {
let _guard = log::init_log();
tokio::spawn(run_sdlan(CommandLine {
sn: "39.98.184.67:1265".to_owned(),
tcp: "39.98.184.67:18083".to_owned(),
_allow_routing: true,
register_ttl: 1,
mtu: 1290,
name: "tau".to_owned(),
tos: 0,
token: "".to_owned(),
}));
tokio::time::sleep(Duration::from_secs(3)).await;
let edge = get_edge();
edge.start("0".to_owned()).await;
tokio::time::sleep(Duration::from_secs(20)).await;
edge.stop().await;
}

View File

@ -13,9 +13,7 @@ mod tun;
mod device;
/*
pub trait ReadWriter {
fn send(&self, content: &[u8]) -> std::io::Result<usize>;
fn recv(&self, buf: &mut [u8]) -> std::io::Result<usize>;
}
*/

View File

@ -4,13 +4,15 @@ use std::io::{Error, ErrorKind};
use std::sync::Arc;
use wintun;
pub struct WinTun {
use super::device::{Mode, DeviceConfig};
pub struct Iface {
adapter: Arc<wintun::Adapter>,
session: Arc<wintun::Session>,
}
impl ReadWriter for WinTun {
fn recv(&self, buf: &mut [u8]) -> std::io::Result<usize> {
impl Iface {
pub fn recv(&self, buf: &mut [u8]) -> std::io::Result<usize> {
let Ok(pkt) = self.session.receive_blocking() else {
return Err(Error::new(ErrorKind::Other, "failed to receive"));
};
@ -25,7 +27,7 @@ impl ReadWriter for WinTun {
Ok(length)
}
fn send(&self, content: &[u8]) -> std::io::Result<usize> {
pub fn send(&self, content: &[u8]) -> std::io::Result<usize> {
let mut pkt = self
.session
.allocate_send_packet(content.len() as u16)
@ -35,20 +37,24 @@ impl ReadWriter for WinTun {
self.session.send_packet(pkt);
Ok(content.len())
}
pub fn reload_config(&self, device_config: &DeviceConfig) {
}
}
fn create_wintun(path: &str) -> WinTun {
fn create_wintun(path: &str) -> Iface {
let wt = unsafe { wintun::load_from_path(path) }.expect("failed to load wintun");
let adapter = match wintun::Adapter::open(&wt, "Demo") {
Ok(a) => a,
Err(_) => wintun::Adapter::create(&wt, "Demo", "Example", None)
Err(e) => wintun::Adapter::create(&wt, "Demo", "Example", None)
.expect("failed to create wintun adapter"),
};
let session = Arc::new(adapter.start_session(wintun::MAX_RING_CAPACITY).unwrap());
WinTun { adapter, session }
Iface { adapter, session }
}
pub fn create_tun() -> Result<Box<dyn ReadWriter>> {
Ok(Box::new(create_wintun("/path/to/file")))
pub fn new_iface(name: &str, mode: Mode) -> Iface {
create_wintun("./wintun.dll")
// Ok(Box::new(create_wintun("/path/to/file")))
}