-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimple.rs
65 lines (53 loc) · 2.09 KB
/
simple.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use fortun_cli::{create_async_tun, net_config};
use std::time::Duration;
use tokio::net::UdpSocket;
use windows::core::GUID;
// Run with Administrator Permission
#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt().init();
let guid = GUID::from("15D95261-C48F-428E-853F-FF080ACA23FA");
//let inf_path = std::env::current_dir().unwrap().join("driver\\fortun\\fortun.inf");
let inf_path = "C:/DriverTest/Drivers/ForTun.inf";
let (mut read, mut write, device) = create_async_tun(&guid, "ForTunTest", inf_path).unwrap();
net_config(device.instance_id.clone(), "10.0.0.2", "255.255.0.0", 1428).unwrap();
let mut read_buf: Vec<u8> = vec![0; 2048];
let task = tokio::spawn(async move {
while let Ok(size) = read.read(read_buf.as_mut_slice()) {
println!("receive: {size}");
tokio::time::sleep(Duration::from_secs(1)).await;
}
});
/*
let addr = "10.0.0.2:8080";
let socket = UdpSocket::bind(&addr).await?;
println!("Listening on: {}", socket.local_addr()?);
let mut to_send = None;
let mut buf = vec![0;1024];
tokio::spawn(async move {
loop {
if let Some((size, peer)) = to_send {
let amt = socket.send_to(b"pong", &peer).await.unwrap();
println!("Echoed {}/{} bytes to {}", amt, size, peer);
}
to_send = Some(socket.recv_from(&mut buf).await.unwrap());
}
});
tokio::time::sleep(Duration::from_secs(1)).await;
let socket = UdpSocket::bind("10.0.0.2:8089").await?;
const MAX_DATAGRAM_SIZE: usize = 1204;
socket.connect(addr).await?;
let data = b"ping";
socket.send(data).await?;
let mut data = vec![0u8; MAX_DATAGRAM_SIZE];
let len = socket.recv(&mut data).await?;
println!(
"Received {} bytes:\n{}",
len,
String::from_utf8_lossy(&data[..len])
);
tokio::time::sleep(Duration::from_secs(2)).await;
*/
let _ = task.await;
Ok(())
}