cargo fmt
This commit is contained in:
@@ -76,4 +76,4 @@ pub fn to_ip(mask: &str) -> Result<u32, String> {
|
||||
} else {
|
||||
Err("not netmask".to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
use std::process::Command;
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
@@ -7,8 +6,9 @@ pub fn get_unique_identifier() -> Option<String> {
|
||||
let output = match Command::new("wmic")
|
||||
.creation_flags(0x08000000)
|
||||
.args(&["csproduct", "get", "UUID"])
|
||||
.output() {
|
||||
Ok(output) => { output }
|
||||
.output()
|
||||
{
|
||||
Ok(output) => output,
|
||||
Err(_) => {
|
||||
return None;
|
||||
}
|
||||
@@ -27,8 +27,9 @@ pub fn get_unique_identifier() -> Option<String> {
|
||||
pub fn get_unique_identifier() -> Option<String> {
|
||||
let output = match Command::new("ioreg")
|
||||
.args(&["-rd1", "-c", "IOPlatformExpertDevice"])
|
||||
.output() {
|
||||
Ok(output) => { output }
|
||||
.output()
|
||||
{
|
||||
Ok(output) => output,
|
||||
Err(_) => {
|
||||
return None;
|
||||
}
|
||||
@@ -38,7 +39,8 @@ pub fn get_unique_identifier() -> Option<String> {
|
||||
let identifier = result
|
||||
.lines()
|
||||
.find(|line| line.contains("IOPlatformUUID"))
|
||||
.unwrap_or("").trim();
|
||||
.unwrap_or("")
|
||||
.trim();
|
||||
if identifier.is_empty() {
|
||||
None
|
||||
} else {
|
||||
@@ -51,8 +53,9 @@ pub fn get_unique_identifier() -> Option<String> {
|
||||
let output = match Command::new("dmidecode")
|
||||
.arg("-s")
|
||||
.arg("system-uuid")
|
||||
.output() {
|
||||
Ok(output) => { output }
|
||||
.output()
|
||||
{
|
||||
Ok(output) => output,
|
||||
Err(_) => {
|
||||
return None;
|
||||
}
|
||||
@@ -65,4 +68,4 @@ pub fn get_unique_identifier() -> Option<String> {
|
||||
} else {
|
||||
Some(identifier.to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,2 +1,2 @@
|
||||
pub mod identifier;
|
||||
pub mod args_parse;
|
||||
pub mod identifier;
|
||||
|
||||
+1
-1
@@ -7,4 +7,4 @@ fn main() {
|
||||
// embed_manifest(new_manifest("vnt")
|
||||
// .requested_execution_level(ExecutionLevel::RequireAdministrator)).expect("unable to embed manifest file");
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, UdpSocket};
|
||||
use std::str::FromStr;
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::command::entity::{DeviceItem, RouteItem, Info};
|
||||
use crate::command::entity::{DeviceItem, Info, RouteItem};
|
||||
|
||||
pub struct CommandClient {
|
||||
udp: UdpSocket,
|
||||
@@ -17,9 +17,12 @@ impl CommandClient {
|
||||
}
|
||||
let port = std::fs::read_to_string(path_buf)?;
|
||||
let port = match u16::from_str(&port) {
|
||||
Ok(port) => { port }
|
||||
Ok(port) => port,
|
||||
Err(_) => {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "'command-port' file error"));
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"'command-port' file error",
|
||||
));
|
||||
}
|
||||
};
|
||||
let udp = UdpSocket::bind("127.0.0.1:0")?;
|
||||
@@ -38,11 +41,9 @@ impl CommandClient {
|
||||
let mut buf = [0; 10240];
|
||||
let len = self.udp.recv(&mut buf)?;
|
||||
match serde_json::from_slice::<Vec<DeviceItem>>(&buf[..len]) {
|
||||
Ok(val) => {
|
||||
Ok(val)
|
||||
}
|
||||
Ok(val) => Ok(val),
|
||||
Err(e) => {
|
||||
log::error!("{:?}",e);
|
||||
log::error!("{:?}", e);
|
||||
Err(io::Error::new(io::ErrorKind::Other, "data error"))
|
||||
}
|
||||
}
|
||||
@@ -52,11 +53,9 @@ impl CommandClient {
|
||||
let mut buf = [0; 10240];
|
||||
let len = self.udp.recv(&mut buf)?;
|
||||
match serde_json::from_slice::<Vec<RouteItem>>(&buf[..len]) {
|
||||
Ok(val) => {
|
||||
Ok(val)
|
||||
}
|
||||
Ok(val) => Ok(val),
|
||||
Err(e) => {
|
||||
log::error!("{:?}",e);
|
||||
log::error!("{:?}", e);
|
||||
Err(io::Error::new(io::ErrorKind::Other, "data error"))
|
||||
}
|
||||
}
|
||||
@@ -66,11 +65,9 @@ impl CommandClient {
|
||||
let mut buf = [0; 10240];
|
||||
let len = self.udp.recv(&mut buf)?;
|
||||
match serde_json::from_slice::<Info>(&buf[..len]) {
|
||||
Ok(val) => {
|
||||
Ok(val)
|
||||
}
|
||||
Ok(val) => Ok(val),
|
||||
Err(e) => {
|
||||
log::error!("{:?},{:?}",&buf[..len],e);
|
||||
log::error!("{:?},{:?}", &buf[..len], e);
|
||||
Err(io::Error::new(io::ErrorKind::Other, "data error"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,5 +33,5 @@ pub struct DeviceItem {
|
||||
pub rt: String,
|
||||
pub status: String,
|
||||
pub client_secret: bool,
|
||||
pub current_client_secret:bool,
|
||||
}
|
||||
pub current_client_secret: bool,
|
||||
}
|
||||
|
||||
+19
-15
@@ -1,11 +1,11 @@
|
||||
use crate::command::entity::{DeviceItem, Info, RouteItem};
|
||||
use crate::console_out;
|
||||
use std::io;
|
||||
use vnt::core::Vnt;
|
||||
use crate::command::entity::{DeviceItem, RouteItem, Info};
|
||||
use crate::console_out;
|
||||
|
||||
pub mod client;
|
||||
pub mod server;
|
||||
pub mod entity;
|
||||
pub mod server;
|
||||
|
||||
pub enum CommandEnum {
|
||||
Route,
|
||||
@@ -51,7 +51,9 @@ pub fn command_route(vnt: &Vnt) -> Vec<RouteItem> {
|
||||
let route_table = vnt.route_table();
|
||||
let mut route_list = Vec::with_capacity(route_table.len());
|
||||
for (destination, route) in route_table {
|
||||
let next_hop = vnt.route_key(&route.route_key()).map_or(String::new(), |v| v.to_string());
|
||||
let next_hop = vnt
|
||||
.route_key(&route.route_key())
|
||||
.map_or(String::new(), |v| v.to_string());
|
||||
let metric = route.metric.to_string();
|
||||
let rt = if route.rt < 0 {
|
||||
"".to_string()
|
||||
@@ -79,15 +81,17 @@ pub fn command_list(vnt: &Vnt) -> Vec<DeviceItem> {
|
||||
for peer in device_list {
|
||||
let name = peer.name;
|
||||
let virtual_ip = peer.virtual_ip.to_string();
|
||||
let (nat_type, public_ips, local_ip) = if let Some(nat_info) = vnt.peer_nat_info(&peer.virtual_ip) {
|
||||
let nat_type = format!("{:?}", nat_info.nat_type);
|
||||
let public_ips: Vec<String> = nat_info.public_ips.iter().map(|v| v.to_string()).collect();
|
||||
let public_ips = public_ips.join(",");
|
||||
let local_ip = nat_info.local_ipv4_addr.ip().to_string();
|
||||
(nat_type, public_ips, local_ip)
|
||||
} else {
|
||||
("".to_string(), "".to_string(), "".to_string())
|
||||
};
|
||||
let (nat_type, public_ips, local_ip) =
|
||||
if let Some(nat_info) = vnt.peer_nat_info(&peer.virtual_ip) {
|
||||
let nat_type = format!("{:?}", nat_info.nat_type);
|
||||
let public_ips: Vec<String> =
|
||||
nat_info.public_ips.iter().map(|v| v.to_string()).collect();
|
||||
let public_ips = public_ips.join(",");
|
||||
let local_ip = nat_info.local_ipv4_addr.ip().to_string();
|
||||
(nat_type, public_ips, local_ip)
|
||||
} else {
|
||||
("".to_string(), "".to_string(), "".to_string())
|
||||
};
|
||||
let (nat_traversal_type, rt) = if let Some(route) = vnt.route(&peer.virtual_ip) {
|
||||
let nat_traversal_type = if route.metric == 1 {
|
||||
"p2p"
|
||||
@@ -95,7 +99,8 @@ pub fn command_list(vnt: &Vnt) -> Vec<DeviceItem> {
|
||||
"server-relay"
|
||||
} else {
|
||||
"client-relay"
|
||||
}.to_string();
|
||||
}
|
||||
.to_string();
|
||||
let rt = if route.rt < 0 {
|
||||
"".to_string()
|
||||
} else {
|
||||
@@ -155,4 +160,3 @@ pub fn command_info(vnt: &Vnt) -> Info {
|
||||
ipv6_addr,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ use tokio::net::UdpSocket;
|
||||
|
||||
use vnt::core::Vnt;
|
||||
|
||||
|
||||
pub struct CommandServer {}
|
||||
|
||||
impl CommandServer {
|
||||
@@ -41,39 +40,26 @@ impl CommandServer {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn command(cmd: &str, vnt: &Vnt) -> io::Result<String> {
|
||||
let out_str = match cmd {
|
||||
"route" => {
|
||||
match serde_json::to_string(&crate::command::command_route(vnt)) {
|
||||
Ok(str) => {
|
||||
str
|
||||
}
|
||||
Err(e) => {
|
||||
format!("{:?}", e)
|
||||
}
|
||||
"route" => match serde_json::to_string(&crate::command::command_route(vnt)) {
|
||||
Ok(str) => str,
|
||||
Err(e) => {
|
||||
format!("{:?}", e)
|
||||
}
|
||||
}
|
||||
"list" => {
|
||||
match serde_json::to_string(&crate::command::command_list(vnt)) {
|
||||
Ok(str) => {
|
||||
str
|
||||
}
|
||||
Err(e) => {
|
||||
format!("{:?}", e)
|
||||
}
|
||||
},
|
||||
"list" => match serde_json::to_string(&crate::command::command_list(vnt)) {
|
||||
Ok(str) => str,
|
||||
Err(e) => {
|
||||
format!("{:?}", e)
|
||||
}
|
||||
}
|
||||
"info" => {
|
||||
match serde_json::to_string(&crate::command::command_info(vnt)) {
|
||||
Ok(str) => {
|
||||
str
|
||||
}
|
||||
Err(e) => {
|
||||
format!("{:?}", e)
|
||||
}
|
||||
},
|
||||
"info" => match serde_json::to_string(&crate::command::command_info(vnt)) {
|
||||
Ok(str) => str,
|
||||
Err(e) => {
|
||||
format!("{:?}", e)
|
||||
}
|
||||
}
|
||||
},
|
||||
"stop" => {
|
||||
vnt.stop()?;
|
||||
"stopped".to_string()
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use console::{style, Style};
|
||||
|
||||
use crate::command::entity::{DeviceItem, RouteItem, Info};
|
||||
use crate::command::entity::{DeviceItem, Info, RouteItem};
|
||||
|
||||
pub mod table;
|
||||
|
||||
@@ -9,7 +9,10 @@ pub fn console_info(status: Info) {
|
||||
println!("Virtual ip: {}", style(status.virtual_ip).green());
|
||||
println!("Virtual gateway: {}", style(status.virtual_gateway).green());
|
||||
println!("Virtual netmask: {}", style(status.virtual_netmask).green());
|
||||
println!("Connection status: {}", style(status.connect_status).green());
|
||||
println!(
|
||||
"Connection status: {}",
|
||||
style(status.connect_status).green()
|
||||
);
|
||||
println!("NAT type: {}", style(status.nat_type).green());
|
||||
println!("Relay server: {}", style(status.relay_server).green());
|
||||
println!("Public ips: {}", style(status.public_ips).green());
|
||||
@@ -25,17 +28,21 @@ pub fn console_route_table(mut list: Vec<RouteItem>) {
|
||||
list.sort_by(|t1, t2| t1.destination.cmp(&t2.destination));
|
||||
let mut out_list = Vec::with_capacity(list.len());
|
||||
|
||||
out_list.push(vec![("Destination".to_string(), Style::new()),
|
||||
("Next Hop".to_string(), Style::new()),
|
||||
("Metric".to_string(), Style::new()),
|
||||
("Rt".to_string(), Style::new()),
|
||||
("Interface".to_string(), Style::new()), ]);
|
||||
out_list.push(vec![
|
||||
("Destination".to_string(), Style::new()),
|
||||
("Next Hop".to_string(), Style::new()),
|
||||
("Metric".to_string(), Style::new()),
|
||||
("Rt".to_string(), Style::new()),
|
||||
("Interface".to_string(), Style::new()),
|
||||
]);
|
||||
for item in list {
|
||||
out_list.push(vec![(item.destination, Style::new().green()),
|
||||
(item.next_hop, Style::new().green()),
|
||||
(item.metric, Style::new().green()),
|
||||
(item.rt, Style::new().green()),
|
||||
(item.interface, Style::new().green())]);
|
||||
out_list.push(vec![
|
||||
(item.destination, Style::new().green()),
|
||||
(item.next_hop, Style::new().green()),
|
||||
(item.metric, Style::new().green()),
|
||||
(item.rt, Style::new().green()),
|
||||
(item.interface, Style::new().green()),
|
||||
]);
|
||||
}
|
||||
|
||||
table::println_table(out_list)
|
||||
@@ -50,41 +57,51 @@ pub fn console_device_list(mut list: Vec<DeviceItem>) {
|
||||
list.sort_by(|t1, t2| t1.status.cmp(&t2.status));
|
||||
let mut out_list = Vec::with_capacity(list.len());
|
||||
//表头
|
||||
out_list.push(vec![("Name".to_string(), Style::new()),
|
||||
("Virtual Ip".to_string(), Style::new()),
|
||||
("Status".to_string(), Style::new()),
|
||||
("P2P/Relay".to_string(), Style::new()),
|
||||
("Rt".to_string(), Style::new())]);
|
||||
out_list.push(vec![
|
||||
("Name".to_string(), Style::new()),
|
||||
("Virtual Ip".to_string(), Style::new()),
|
||||
("Status".to_string(), Style::new()),
|
||||
("P2P/Relay".to_string(), Style::new()),
|
||||
("Rt".to_string(), Style::new()),
|
||||
]);
|
||||
for item in list {
|
||||
if &item.status == "Online" {
|
||||
if item.client_secret != item.current_client_secret {
|
||||
//加密状态不一致,无法通信的
|
||||
out_list.push(vec![(item.name, Style::new().red()),
|
||||
(item.virtual_ip, Style::new().red()),
|
||||
(item.status, Style::new().red()),
|
||||
("".to_string(), Style::new().red()),
|
||||
("".to_string(), Style::new().red())]);
|
||||
out_list.push(vec![
|
||||
(item.name, Style::new().red()),
|
||||
(item.virtual_ip, Style::new().red()),
|
||||
(item.status, Style::new().red()),
|
||||
("".to_string(), Style::new().red()),
|
||||
("".to_string(), Style::new().red()),
|
||||
]);
|
||||
} else {
|
||||
if &item.nat_traversal_type == "p2p" {
|
||||
out_list.push(vec![(item.name, Style::new().green()),
|
||||
(item.virtual_ip, Style::new().green()),
|
||||
(item.status, Style::new().green()),
|
||||
(item.nat_traversal_type, Style::new().green()),
|
||||
(item.rt, Style::new().green())]);
|
||||
out_list.push(vec![
|
||||
(item.name, Style::new().green()),
|
||||
(item.virtual_ip, Style::new().green()),
|
||||
(item.status, Style::new().green()),
|
||||
(item.nat_traversal_type, Style::new().green()),
|
||||
(item.rt, Style::new().green()),
|
||||
]);
|
||||
} else {
|
||||
out_list.push(vec![(item.name, Style::new().yellow()),
|
||||
(item.virtual_ip, Style::new().yellow()),
|
||||
(item.status, Style::new().yellow()),
|
||||
(item.nat_traversal_type, Style::new().yellow()),
|
||||
(item.rt, Style::new().yellow())]);
|
||||
out_list.push(vec![
|
||||
(item.name, Style::new().yellow()),
|
||||
(item.virtual_ip, Style::new().yellow()),
|
||||
(item.status, Style::new().yellow()),
|
||||
(item.nat_traversal_type, Style::new().yellow()),
|
||||
(item.rt, Style::new().yellow()),
|
||||
]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
out_list.push(vec![(item.name, Style::new().color256(102)),
|
||||
(item.virtual_ip, Style::new().color256(102)),
|
||||
(item.status, Style::new().color256(102)),
|
||||
("".to_string(), Style::new().color256(102)),
|
||||
("".to_string(), Style::new().color256(102))]);
|
||||
out_list.push(vec![
|
||||
(item.name, Style::new().color256(102)),
|
||||
(item.virtual_ip, Style::new().color256(102)),
|
||||
(item.status, Style::new().color256(102)),
|
||||
("".to_string(), Style::new().color256(102)),
|
||||
("".to_string(), Style::new().color256(102)),
|
||||
]);
|
||||
}
|
||||
}
|
||||
table::println_table(out_list)
|
||||
@@ -99,45 +116,53 @@ pub fn console_device_list_all(mut list: Vec<DeviceItem>) {
|
||||
list.sort_by(|t1, t2| t1.status.cmp(&t2.status));
|
||||
let mut out_list = Vec::with_capacity(list.len());
|
||||
//表头
|
||||
out_list.push(vec![("Name".to_string(), Style::new()),
|
||||
("Virtual Ip".to_string(), Style::new()),
|
||||
("Status".to_string(), Style::new()),
|
||||
("P2P/Relay".to_string(), Style::new()),
|
||||
("Rt".to_string(), Style::new()),
|
||||
("NAT Type".to_string(), Style::new()),
|
||||
("Public Ips".to_string(), Style::new()),
|
||||
("Local Ip".to_string(), Style::new())]);
|
||||
out_list.push(vec![
|
||||
("Name".to_string(), Style::new()),
|
||||
("Virtual Ip".to_string(), Style::new()),
|
||||
("Status".to_string(), Style::new()),
|
||||
("P2P/Relay".to_string(), Style::new()),
|
||||
("Rt".to_string(), Style::new()),
|
||||
("NAT Type".to_string(), Style::new()),
|
||||
("Public Ips".to_string(), Style::new()),
|
||||
("Local Ip".to_string(), Style::new()),
|
||||
]);
|
||||
for item in list {
|
||||
if &item.status == "Online" {
|
||||
if &item.nat_traversal_type == "p2p" {
|
||||
out_list.push(vec![(item.name, Style::new().green()),
|
||||
(item.virtual_ip, Style::new().green()),
|
||||
(item.status, Style::new().green()),
|
||||
(item.nat_traversal_type, Style::new().green()),
|
||||
(item.rt, Style::new().green()),
|
||||
(item.nat_type, Style::new().green()),
|
||||
(item.public_ips, Style::new().green()),
|
||||
(item.local_ip, Style::new().green())]);
|
||||
out_list.push(vec![
|
||||
(item.name, Style::new().green()),
|
||||
(item.virtual_ip, Style::new().green()),
|
||||
(item.status, Style::new().green()),
|
||||
(item.nat_traversal_type, Style::new().green()),
|
||||
(item.rt, Style::new().green()),
|
||||
(item.nat_type, Style::new().green()),
|
||||
(item.public_ips, Style::new().green()),
|
||||
(item.local_ip, Style::new().green()),
|
||||
]);
|
||||
} else {
|
||||
out_list.push(vec![(item.name, Style::new().yellow()),
|
||||
(item.virtual_ip, Style::new().yellow()),
|
||||
(item.status, Style::new().yellow()),
|
||||
(item.nat_traversal_type, Style::new().yellow()),
|
||||
(item.rt, Style::new().yellow()),
|
||||
(item.nat_type, Style::new().yellow()),
|
||||
(item.public_ips, Style::new().yellow()),
|
||||
(item.local_ip, Style::new().yellow()), ]);
|
||||
out_list.push(vec![
|
||||
(item.name, Style::new().yellow()),
|
||||
(item.virtual_ip, Style::new().yellow()),
|
||||
(item.status, Style::new().yellow()),
|
||||
(item.nat_traversal_type, Style::new().yellow()),
|
||||
(item.rt, Style::new().yellow()),
|
||||
(item.nat_type, Style::new().yellow()),
|
||||
(item.public_ips, Style::new().yellow()),
|
||||
(item.local_ip, Style::new().yellow()),
|
||||
]);
|
||||
}
|
||||
} else {
|
||||
out_list.push(vec![(item.name, Style::new().color256(102)),
|
||||
(item.virtual_ip, Style::new().color256(102)),
|
||||
(item.status, Style::new().color256(102)),
|
||||
("".to_string(), Style::new().color256(102)),
|
||||
("".to_string(), Style::new().color256(102)),
|
||||
("".to_string(), Style::new().color256(102)),
|
||||
("".to_string(), Style::new().color256(102)),
|
||||
("".to_string(), Style::new().color256(102)), ]);
|
||||
out_list.push(vec![
|
||||
(item.name, Style::new().color256(102)),
|
||||
(item.virtual_ip, Style::new().color256(102)),
|
||||
(item.status, Style::new().color256(102)),
|
||||
("".to_string(), Style::new().color256(102)),
|
||||
("".to_string(), Style::new().color256(102)),
|
||||
("".to_string(), Style::new().color256(102)),
|
||||
("".to_string(), Style::new().color256(102)),
|
||||
("".to_string(), Style::new().color256(102)),
|
||||
]);
|
||||
}
|
||||
}
|
||||
table::println_table(out_list)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,4 +20,4 @@ pub fn println_table(table: Vec<Vec<(String, Style)>>) {
|
||||
}
|
||||
println!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+131
-91
@@ -21,7 +21,9 @@ mod console_out;
|
||||
mod root_check;
|
||||
|
||||
pub fn app_home() -> io::Result<PathBuf> {
|
||||
let path = dirs::home_dir().ok_or(io::Error::new(io::ErrorKind::Other, "not home"))?.join(".vnt-cli");
|
||||
let path = dirs::home_dir()
|
||||
.ok_or(io::Error::new(io::ErrorKind::Other, "not home"))?
|
||||
.join(".vnt-cli");
|
||||
if !path.exists() {
|
||||
std::fs::create_dir_all(&path)?;
|
||||
}
|
||||
@@ -52,7 +54,7 @@ fn main() {
|
||||
opts.optopt("", "par", "任务并行度(必须为正整数)", "<parallel>");
|
||||
opts.optopt("", "thread", "线程数(必须为正整数)", "<thread>");
|
||||
opts.optopt("", "model", "加密模式", "<model>");
|
||||
opts.optflag("", "finger", "指纹校验", );
|
||||
opts.optflag("", "finger", "指纹校验");
|
||||
//"后台运行时,查看其他设备列表"
|
||||
opts.optflag("", "list", "后台运行时,查看其他设备列表");
|
||||
opts.optflag("", "all", "后台运行时,查看其他设备完整信息");
|
||||
@@ -61,7 +63,7 @@ fn main() {
|
||||
opts.optflag("", "stop", "停止后台运行");
|
||||
opts.optflag("h", "help", "帮助");
|
||||
let matches = match opts.parse(&args[1..]) {
|
||||
Ok(m) => { m }
|
||||
Ok(m) => m,
|
||||
Err(f) => {
|
||||
print_usage(&program, opts);
|
||||
println!("{}", f.to_string());
|
||||
@@ -123,8 +125,12 @@ fn main() {
|
||||
println!("parameter -d not found .");
|
||||
return;
|
||||
}
|
||||
let name = matches.opt_get_default("n", os_info::get().to_string()).unwrap();
|
||||
let server_address_str = matches.opt_get_default("s", "nat1.wherewego.top:29872".to_string()).unwrap();
|
||||
let name = matches
|
||||
.opt_get_default("n", os_info::get().to_string())
|
||||
.unwrap();
|
||||
let server_address_str = matches
|
||||
.opt_get_default("s", "nat1.wherewego.top:29872".to_string())
|
||||
.unwrap();
|
||||
let server_address = match server_address_str.to_socket_addrs() {
|
||||
Ok(mut addr) => {
|
||||
if let Some(addr) = addr.next() {
|
||||
@@ -148,7 +154,7 @@ fn main() {
|
||||
|
||||
let in_ip = matches.opt_strs("i");
|
||||
let in_ip = match ips_parse(&in_ip) {
|
||||
Ok(in_ip) => { in_ip }
|
||||
Ok(in_ip) => in_ip,
|
||||
Err(e) => {
|
||||
print_usage(&program, opts);
|
||||
println!();
|
||||
@@ -159,7 +165,7 @@ fn main() {
|
||||
};
|
||||
let out_ip = matches.opt_strs("o");
|
||||
let out_ip = match out_ips_parse(&out_ip) {
|
||||
Ok(out_ip) => { out_ip }
|
||||
Ok(out_ip) => out_ip,
|
||||
Err(e) => {
|
||||
print_usage(&program, opts);
|
||||
println!();
|
||||
@@ -175,9 +181,7 @@ fn main() {
|
||||
let mtu: Option<String> = matches.opt_get("u").unwrap();
|
||||
let mtu = if let Some(mtu) = mtu {
|
||||
match u16::from_str(&mtu) {
|
||||
Ok(mtu) => {
|
||||
Some(mtu)
|
||||
}
|
||||
Ok(mtu) => Some(mtu),
|
||||
Err(e) => {
|
||||
print_usage(&program, opts);
|
||||
println!();
|
||||
@@ -203,21 +207,46 @@ fn main() {
|
||||
println!("--par invalid");
|
||||
return;
|
||||
}
|
||||
let thread_num = matches.opt_get::<usize>("thread").unwrap().unwrap_or(std::thread::available_parallelism().unwrap().get() * 2);
|
||||
let cipher_model = matches.opt_get::<CipherModel>("model").unwrap().unwrap_or(CipherModel::AesGcm);
|
||||
let thread_num = matches
|
||||
.opt_get::<usize>("thread")
|
||||
.unwrap()
|
||||
.unwrap_or(std::thread::available_parallelism().unwrap().get() * 2);
|
||||
let cipher_model = matches
|
||||
.opt_get::<CipherModel>("model")
|
||||
.unwrap()
|
||||
.unwrap_or(CipherModel::AesGcm);
|
||||
if thread_num == 0 {
|
||||
println!("--thread invalid");
|
||||
return;
|
||||
}
|
||||
let finger = matches.opt_present("finger");
|
||||
println!("version {}",vnt::VNT_VERSION);
|
||||
let config = Config::new(tap,
|
||||
token, device_id, name,
|
||||
server_address, server_address_str,
|
||||
stun_server, in_ip,
|
||||
out_ip, password, simulate_multicast, mtu,
|
||||
tcp_channel, virtual_ip, relay, server_encrypt, parallel, cipher_model,finger);
|
||||
let runtime = tokio::runtime::Builder::new_multi_thread().enable_all().worker_threads(thread_num).build().unwrap();
|
||||
println!("version {}", vnt::VNT_VERSION);
|
||||
let config = Config::new(
|
||||
tap,
|
||||
token,
|
||||
device_id,
|
||||
name,
|
||||
server_address,
|
||||
server_address_str,
|
||||
stun_server,
|
||||
in_ip,
|
||||
out_ip,
|
||||
password,
|
||||
simulate_multicast,
|
||||
mtu,
|
||||
tcp_channel,
|
||||
virtual_ip,
|
||||
relay,
|
||||
server_encrypt,
|
||||
parallel,
|
||||
cipher_model,
|
||||
finger,
|
||||
);
|
||||
let runtime = tokio::runtime::Builder::new_multi_thread()
|
||||
.enable_all()
|
||||
.worker_threads(thread_num)
|
||||
.build()
|
||||
.unwrap();
|
||||
runtime.block_on(main0(config, !unused_cmd));
|
||||
std::process::exit(0);
|
||||
}
|
||||
@@ -264,55 +293,51 @@ async fn main0(config: Config, show_cmd: bool) {
|
||||
Ok(response) => {
|
||||
break response;
|
||||
}
|
||||
Err(e) => {
|
||||
match e {
|
||||
ReqEnum::TokenError => {
|
||||
println!("token error");
|
||||
return;
|
||||
}
|
||||
ReqEnum::AddressExhausted => {
|
||||
println!("address exhausted");
|
||||
return;
|
||||
}
|
||||
ReqEnum::Timeout => {
|
||||
println!("timeout...");
|
||||
}
|
||||
ReqEnum::ServerError(str) => {
|
||||
println!("error:{}", str);
|
||||
}
|
||||
ReqEnum::Other(str) => {
|
||||
println!("error:{}", str);
|
||||
}
|
||||
ReqEnum::IpAlreadyExists => {
|
||||
println!("ip already exists");
|
||||
return;
|
||||
}
|
||||
ReqEnum::InvalidIp => {
|
||||
println!("invalid ip");
|
||||
return;
|
||||
}
|
||||
Err(e) => match e {
|
||||
ReqEnum::TokenError => {
|
||||
println!("token error");
|
||||
return;
|
||||
}
|
||||
}
|
||||
ReqEnum::AddressExhausted => {
|
||||
println!("address exhausted");
|
||||
return;
|
||||
}
|
||||
ReqEnum::Timeout => {
|
||||
println!("timeout...");
|
||||
}
|
||||
ReqEnum::ServerError(str) => {
|
||||
println!("error:{}", str);
|
||||
}
|
||||
ReqEnum::Other(str) => {
|
||||
println!("error:{}", str);
|
||||
}
|
||||
ReqEnum::IpAlreadyExists => {
|
||||
println!("ip already exists");
|
||||
return;
|
||||
}
|
||||
ReqEnum::InvalidIp => {
|
||||
println!("invalid ip");
|
||||
return;
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
match e {
|
||||
HandshakeEnum::NotSecret => {
|
||||
println!("The server does not support encryption");
|
||||
return;
|
||||
}
|
||||
HandshakeEnum::KeyError => {}
|
||||
HandshakeEnum::Timeout => {
|
||||
println!("handshake timeout")
|
||||
}
|
||||
HandshakeEnum::ServerError(str) => {
|
||||
println!("error:{}", str);
|
||||
}
|
||||
HandshakeEnum::Other(str) => {
|
||||
println!("error:{}", str);
|
||||
}
|
||||
Err(e) => match e {
|
||||
HandshakeEnum::NotSecret => {
|
||||
println!("The server does not support encryption");
|
||||
return;
|
||||
}
|
||||
}
|
||||
HandshakeEnum::KeyError => {}
|
||||
HandshakeEnum::Timeout => {
|
||||
println!("handshake timeout")
|
||||
}
|
||||
HandshakeEnum::ServerError(str) => {
|
||||
println!("error:{}", str);
|
||||
}
|
||||
HandshakeEnum::Other(str) => {
|
||||
println!("error:{}", str);
|
||||
}
|
||||
},
|
||||
}
|
||||
};
|
||||
println!(" ====== Connect Successfully ====== ");
|
||||
@@ -323,9 +348,7 @@ async fn main0(config: Config, show_cmd: bool) {
|
||||
println!("name:{}", driver_info.name);
|
||||
println!("version:{}", driver_info.version);
|
||||
let mut vnt = match vnt_util.build().await {
|
||||
Ok(vnt) => {
|
||||
vnt
|
||||
}
|
||||
Ok(vnt) => vnt,
|
||||
Err(e) => {
|
||||
println!("error:{}", e);
|
||||
return;
|
||||
@@ -343,7 +366,7 @@ async fn main0(config: Config, show_cmd: bool) {
|
||||
let mut cmd = String::new();
|
||||
let mut reader = BufReader::new(stdin);
|
||||
#[cfg(unix)]
|
||||
let mut sigterm = signal(SignalKind::terminate()).expect("Error setting SIGTERM handler");
|
||||
let mut sigterm = signal(SignalKind::terminate()).expect("Error setting SIGTERM handler");
|
||||
loop {
|
||||
cmd.clear();
|
||||
println!("input:list,info,route,all,stop");
|
||||
@@ -403,20 +426,20 @@ async fn main0(config: Config, show_cmd: bool) {
|
||||
}
|
||||
#[cfg(unix)]
|
||||
tokio::select! {
|
||||
_ = vnt.wait_stop()=>{
|
||||
return;
|
||||
}
|
||||
_ = signal::ctrl_c()=>{
|
||||
let _ = vnt.stop();
|
||||
vnt.wait_stop_ms(std::time::Duration::from_secs(3)).await;
|
||||
return;
|
||||
}
|
||||
_ = sigterm.recv()=>{
|
||||
let _ = vnt.stop();
|
||||
vnt.wait_stop_ms(std::time::Duration::from_secs(3)).await;
|
||||
return;
|
||||
}
|
||||
}
|
||||
_ = vnt.wait_stop()=>{
|
||||
return;
|
||||
}
|
||||
_ = signal::ctrl_c()=>{
|
||||
let _ = vnt.stop();
|
||||
vnt.wait_stop_ms(std::time::Duration::from_secs(3)).await;
|
||||
return;
|
||||
}
|
||||
_ = sigterm.recv()=>{
|
||||
let _ = vnt.stop();
|
||||
vnt.wait_stop_ms(std::time::Duration::from_secs(3)).await;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
vnt.wait_stop().await;
|
||||
}
|
||||
@@ -454,9 +477,12 @@ fn command(cmd: &str, vnt: &Vnt) -> bool {
|
||||
|
||||
fn print_usage(program: &str, _opts: Options) {
|
||||
println!("Usage: {} [options]", program);
|
||||
println!("version:{}",vnt::VNT_VERSION);
|
||||
println!("version:{}", vnt::VNT_VERSION);
|
||||
println!("Options:");
|
||||
println!(" -k <token> {}", green("必选,使用相同的token,就能组建一个局域网络".to_string()));
|
||||
println!(
|
||||
" -k <token> {}",
|
||||
green("必选,使用相同的token,就能组建一个局域网络".to_string())
|
||||
);
|
||||
println!(" -n <name> 给设备一个名字,便于区分不同设备,默认使用系统版本");
|
||||
println!(" -d <id> 设备唯一标识符,不使用--ip参数时,服务端凭此参数分配虚拟ip");
|
||||
println!(" -c 关闭交互式命令,使用此参数禁用控制台输入");
|
||||
@@ -478,11 +504,26 @@ fn print_usage(program: &str, _opts: Options) {
|
||||
println!(" --model <model> 加密模式(默认aes_gcm),可选值aes_gcm/aes_cbc/aes_ecb,通常性能aes_ecb>aes_cbc>aes_gcm,安全性则相反");
|
||||
println!(" --finger 增加数据指纹校验,可增加安全性,如果服务端开启指纹校验,则客户端也必须开启");
|
||||
println!();
|
||||
println!(" --list {}", yellow("后台运行时,查看其他设备列表".to_string()));
|
||||
println!(" --all {}", yellow("后台运行时,查看其他设备完整信息".to_string()));
|
||||
println!(" --info {}", yellow("后台运行时,查看当前设备信息".to_string()));
|
||||
println!(" --route {}", yellow("后台运行时,查看数据转发路径".to_string()));
|
||||
println!(" --stop {}", yellow("停止后台运行".to_string()));
|
||||
println!(
|
||||
" --list {}",
|
||||
yellow("后台运行时,查看其他设备列表".to_string())
|
||||
);
|
||||
println!(
|
||||
" --all {}",
|
||||
yellow("后台运行时,查看其他设备完整信息".to_string())
|
||||
);
|
||||
println!(
|
||||
" --info {}",
|
||||
yellow("后台运行时,查看当前设备信息".to_string())
|
||||
);
|
||||
println!(
|
||||
" --route {}",
|
||||
yellow("后台运行时,查看数据转发路径".to_string())
|
||||
);
|
||||
println!(
|
||||
" --stop {}",
|
||||
yellow("停止后台运行".to_string())
|
||||
);
|
||||
println!(" -h, --help 帮助");
|
||||
}
|
||||
|
||||
@@ -493,4 +534,3 @@ fn green(str: String) -> impl std::fmt::Display {
|
||||
fn yellow(str: String) -> impl std::fmt::Display {
|
||||
style(str).yellow()
|
||||
}
|
||||
|
||||
|
||||
@@ -8,4 +8,4 @@ pub use windows::is_app_elevated;
|
||||
mod unix;
|
||||
|
||||
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
||||
pub use unix::is_app_elevated;
|
||||
pub use unix::is_app_elevated;
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
pub fn is_app_elevated() -> bool {
|
||||
sudo::RunningAs::Root == sudo::check()
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,2 +1,2 @@
|
||||
pub mod vnt;
|
||||
pub mod vnt_util;
|
||||
pub mod vnt;
|
||||
+19
-15
@@ -1,8 +1,8 @@
|
||||
use std::ptr;
|
||||
use jni::errors::Error;
|
||||
use jni::JNIEnv;
|
||||
use jni::objects::{JClass, JObject, JValue};
|
||||
use jni::sys::{jboolean, jbyte, jint, jlong, jobject, jobjectArray, jsize};
|
||||
use jni::JNIEnv;
|
||||
use std::ptr;
|
||||
use vnt::channel::Route;
|
||||
use vnt::core::sync::VntSync;
|
||||
use vnt::handle::PeerDeviceInfo;
|
||||
@@ -67,7 +67,7 @@ pub unsafe extern "C" fn Java_top_wherewego_vnt_jni_Vnt_list0(
|
||||
"top/wherewego/vnt/jni/PeerDeviceInfo",
|
||||
JObject::null(),
|
||||
) {
|
||||
Ok(arr) => { arr }
|
||||
Ok(arr) => arr,
|
||||
Err(e) => {
|
||||
env.throw_new("java/lang/RuntimeException", format!("error:{:?}", e))
|
||||
.expect("throw");
|
||||
@@ -77,12 +77,8 @@ pub unsafe extern "C" fn Java_top_wherewego_vnt_jni_Vnt_list0(
|
||||
for (index, peer) in list.into_iter().enumerate() {
|
||||
let route = if let Some(route) = vnt.route(&peer.virtual_ip) {
|
||||
match route_parse(&mut env, route) {
|
||||
Ok(route) => {
|
||||
JObject::from_raw(route)
|
||||
}
|
||||
Err(_) => {
|
||||
JObject::null()
|
||||
}
|
||||
Ok(route) => JObject::from_raw(route),
|
||||
Err(_) => JObject::null(),
|
||||
}
|
||||
} else {
|
||||
JObject::null()
|
||||
@@ -115,24 +111,32 @@ fn route_parse(env: &mut JNIEnv, route: Route) -> Result<jobject, Error> {
|
||||
let rs = env.new_object(
|
||||
"top/wherewego/vnt/jni/Route",
|
||||
"(Ljava/lang/String;BI)V",
|
||||
&[JValue::Object(&env.new_string(address)?.into()),
|
||||
&[
|
||||
JValue::Object(&env.new_string(address)?.into()),
|
||||
JValue::Byte(metric as jbyte),
|
||||
JValue::Int(rt as jint)],
|
||||
JValue::Int(rt as jint),
|
||||
],
|
||||
)?;
|
||||
Ok(rs.as_raw())
|
||||
}
|
||||
|
||||
fn peer_device_info_parse(env: &mut JNIEnv, peer: PeerDeviceInfo, route: JObject) -> Result<jobject, Error> {
|
||||
fn peer_device_info_parse(
|
||||
env: &mut JNIEnv,
|
||||
peer: PeerDeviceInfo,
|
||||
route: JObject,
|
||||
) -> Result<jobject, Error> {
|
||||
let virtual_ip = u32::from(peer.virtual_ip);
|
||||
let name = peer.name.to_string();
|
||||
let status = format!("{:?}", peer.status);
|
||||
let rs = env.new_object(
|
||||
"top/wherewego/vnt/jni/PeerDeviceInfo",
|
||||
"(ILjava/lang/String;Ljava/lang/String;Ltop/wherewego/vnt/jni/Route;)V",
|
||||
&[JValue::Int(virtual_ip as jint),
|
||||
&[
|
||||
JValue::Int(virtual_ip as jint),
|
||||
JValue::Object(&env.new_string(name)?.into()),
|
||||
JValue::Object(&env.new_string(status)?.into()),
|
||||
JValue::Object(&route)],
|
||||
JValue::Object(&route),
|
||||
],
|
||||
)?;
|
||||
Ok(rs.as_raw())
|
||||
}
|
||||
}
|
||||
|
||||
+128
-76
@@ -9,13 +9,17 @@ use jni::sys::jboolean;
|
||||
use jni::sys::{jint, jlong, jobject};
|
||||
use jni::JNIEnv;
|
||||
use vnt::cipher::CipherModel;
|
||||
use vnt::core::Config;
|
||||
use vnt::core::sync::VntUtilSync;
|
||||
use vnt::core::Config;
|
||||
use vnt::handle::registration_handler::{RegResponse, ReqEnum};
|
||||
#[cfg(not(target_os = "android"))]
|
||||
use vnt::tun_tap_device::DriverInfo;
|
||||
|
||||
fn to_string_not_null(env: &mut JNIEnv, config: &JObject, name: &'static str) -> Result<String, Error> {
|
||||
fn to_string_not_null(
|
||||
env: &mut JNIEnv,
|
||||
config: &JObject,
|
||||
name: &'static str,
|
||||
) -> Result<String, Error> {
|
||||
let value = env.get_field(config, name, "Ljava/lang/String;")?.l()?;
|
||||
if value.is_null() {
|
||||
env.throw_new("java/lang/NullPointerException", name)
|
||||
@@ -73,13 +77,16 @@ fn new_sync(env: &mut JNIEnv, config: JObject) -> Result<VntUtilSync, Error> {
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
env.throw_new("java/lang/RuntimeException", format!("server address {}", e))
|
||||
.expect("throw");
|
||||
env.throw_new(
|
||||
"java/lang/RuntimeException",
|
||||
format!("server address {}", e),
|
||||
)
|
||||
.expect("throw");
|
||||
return Err(Error::JavaException);
|
||||
}
|
||||
};
|
||||
let cipher_model = match CipherModel::from_str(&cipher_model) {
|
||||
Ok(cipher_model) => {cipher_model}
|
||||
Ok(cipher_model) => cipher_model,
|
||||
Err(e) => {
|
||||
env.throw_new("java/lang/RuntimeException", format!("cipher_model {}", e))
|
||||
.expect("throw");
|
||||
@@ -90,20 +97,35 @@ fn new_sync(env: &mut JNIEnv, config: JObject) -> Result<VntUtilSync, Error> {
|
||||
for addr in stun_server_str.split(",") {
|
||||
stun_server.push(addr.trim().to_string());
|
||||
}
|
||||
let config = Config::new(false,
|
||||
token, device_id, name,
|
||||
server_address, server_address_str,
|
||||
stun_server, vec![],
|
||||
vec![], password,
|
||||
false, None, tcp, None,
|
||||
false, false, 1, cipher_model,finger);
|
||||
let config = Config::new(
|
||||
false,
|
||||
token,
|
||||
device_id,
|
||||
name,
|
||||
server_address,
|
||||
server_address_str,
|
||||
stun_server,
|
||||
vec![],
|
||||
vec![],
|
||||
password,
|
||||
false,
|
||||
None,
|
||||
tcp,
|
||||
None,
|
||||
false,
|
||||
false,
|
||||
1,
|
||||
cipher_model,
|
||||
finger,
|
||||
);
|
||||
match VntUtilSync::new(config) {
|
||||
Ok(vnt_util) => {
|
||||
Ok(vnt_util)
|
||||
}
|
||||
Ok(vnt_util) => Ok(vnt_util),
|
||||
Err(e) => {
|
||||
env.throw_new("java/lang/RuntimeException", format!("vnt start error {}", e))
|
||||
.expect("throw");
|
||||
env.throw_new(
|
||||
"java/lang/RuntimeException",
|
||||
format!("vnt start error {}", e),
|
||||
)
|
||||
.expect("throw");
|
||||
return Err(Error::JavaException);
|
||||
}
|
||||
}
|
||||
@@ -135,8 +157,11 @@ pub unsafe extern "C" fn Java_top_wherewego_vnt_jni_VntUtil_connect0(
|
||||
match (&mut *raw_vnt_util).connect() {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
env.throw_new("java/lang/RuntimeException", format!("vnt connect error {}", e))
|
||||
.expect("throw");
|
||||
env.throw_new(
|
||||
"java/lang/RuntimeException",
|
||||
format!("vnt connect error {}", e),
|
||||
)
|
||||
.expect("throw");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -149,48 +174,66 @@ pub unsafe extern "C" fn Java_top_wherewego_vnt_jni_VntUtil_register0(
|
||||
) -> jobject {
|
||||
let raw_vnt_util = raw_vnt_util as *mut VntUtilSync;
|
||||
match (&mut *raw_vnt_util).register() {
|
||||
Ok(response) => {
|
||||
match reg_response(&mut env, response) {
|
||||
Ok(res) => {
|
||||
return res;
|
||||
}
|
||||
Err(e) => {
|
||||
env.throw(format!("vnt register error {}", e)).expect("throw");
|
||||
}
|
||||
Ok(response) => match reg_response(&mut env, response) {
|
||||
Ok(res) => {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
match e {
|
||||
ReqEnum::TokenError => {
|
||||
env.throw_new("top/wherewego/vnt/jni/exception/TokenErrorException", "TokenError")
|
||||
.expect("throw");
|
||||
}
|
||||
ReqEnum::AddressExhausted => {
|
||||
env.throw_new("top/wherewego/vnt/jni/exception/AddressExhaustedException", "AddressExhausted")
|
||||
.expect("throw");
|
||||
}
|
||||
ReqEnum::Timeout => {
|
||||
env.throw_new("top/wherewego/vnt/jni/exception/TimeoutException", "Timeout")
|
||||
.expect("throw");
|
||||
}
|
||||
ReqEnum::ServerError(str) => {
|
||||
env.throw_new("java/lang/RuntimeException", format!("vnt register error {}", str))
|
||||
.expect("throw");
|
||||
}
|
||||
ReqEnum::Other(str) => {
|
||||
env.throw_new("java/lang/RuntimeException", format!("vnt register error {}", str))
|
||||
.expect("throw");
|
||||
}
|
||||
ReqEnum::IpAlreadyExists => {
|
||||
env.throw_new("top/wherewego/vnt/jni/exception/IpAlreadyExistsException", "IpAlreadyExists")
|
||||
.expect("throw");
|
||||
}
|
||||
ReqEnum::InvalidIp => {
|
||||
env.throw_new("top/wherewego/vnt/jni/exception/InvalidIpException", "InvalidIp")
|
||||
.expect("throw");
|
||||
}
|
||||
Err(e) => {
|
||||
env.throw(format!("vnt register error {}", e))
|
||||
.expect("throw");
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(e) => match e {
|
||||
ReqEnum::TokenError => {
|
||||
env.throw_new(
|
||||
"top/wherewego/vnt/jni/exception/TokenErrorException",
|
||||
"TokenError",
|
||||
)
|
||||
.expect("throw");
|
||||
}
|
||||
ReqEnum::AddressExhausted => {
|
||||
env.throw_new(
|
||||
"top/wherewego/vnt/jni/exception/AddressExhaustedException",
|
||||
"AddressExhausted",
|
||||
)
|
||||
.expect("throw");
|
||||
}
|
||||
ReqEnum::Timeout => {
|
||||
env.throw_new(
|
||||
"top/wherewego/vnt/jni/exception/TimeoutException",
|
||||
"Timeout",
|
||||
)
|
||||
.expect("throw");
|
||||
}
|
||||
ReqEnum::ServerError(str) => {
|
||||
env.throw_new(
|
||||
"java/lang/RuntimeException",
|
||||
format!("vnt register error {}", str),
|
||||
)
|
||||
.expect("throw");
|
||||
}
|
||||
ReqEnum::Other(str) => {
|
||||
env.throw_new(
|
||||
"java/lang/RuntimeException",
|
||||
format!("vnt register error {}", str),
|
||||
)
|
||||
.expect("throw");
|
||||
}
|
||||
ReqEnum::IpAlreadyExists => {
|
||||
env.throw_new(
|
||||
"top/wherewego/vnt/jni/exception/IpAlreadyExistsException",
|
||||
"IpAlreadyExists",
|
||||
)
|
||||
.expect("throw");
|
||||
}
|
||||
ReqEnum::InvalidIp => {
|
||||
env.throw_new(
|
||||
"top/wherewego/vnt/jni/exception/InvalidIpException",
|
||||
"InvalidIp",
|
||||
)
|
||||
.expect("throw");
|
||||
}
|
||||
},
|
||||
}
|
||||
return ptr::null_mut();
|
||||
}
|
||||
@@ -218,19 +261,21 @@ pub unsafe extern "C" fn Java_top_wherewego_vnt_jni_VntUtil_createIface0(
|
||||
let raw_vnt_util = raw_vnt_util as *mut VntUtilSync;
|
||||
let rs = (&mut *raw_vnt_util).create_iface();
|
||||
match rs {
|
||||
Ok(driver_info) => {
|
||||
match driver_info_e(&mut env, driver_info) {
|
||||
Ok(res) => {
|
||||
return res;
|
||||
}
|
||||
Err(e) => {
|
||||
env.throw(format!("vnt create iface error {}", e)).expect("throw");
|
||||
}
|
||||
Ok(driver_info) => match driver_info_e(&mut env, driver_info) {
|
||||
Ok(res) => {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
env.throw(format!("vnt create iface error {}", e))
|
||||
.expect("throw");
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
env.throw_new("java/lang/RuntimeException", format!("vnt create iface error {}", e))
|
||||
.expect("throw");
|
||||
env.throw_new(
|
||||
"java/lang/RuntimeException",
|
||||
format!("vnt create iface error {}", e),
|
||||
)
|
||||
.expect("throw");
|
||||
}
|
||||
}
|
||||
return ptr::null_mut();
|
||||
@@ -248,8 +293,11 @@ pub unsafe extern "C" fn Java_top_wherewego_vnt_jni_VntUtil_build0(
|
||||
return Box::into_raw(Box::new(rs)) as jlong;
|
||||
}
|
||||
Err(e) => {
|
||||
env.throw_new("java/lang/RuntimeException", format!("vnt start error:{:?}", e))
|
||||
.expect("throw");
|
||||
env.throw_new(
|
||||
"java/lang/RuntimeException",
|
||||
format!("vnt start error:{:?}", e),
|
||||
)
|
||||
.expect("throw");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
@@ -262,9 +310,11 @@ fn reg_response(env: &mut JNIEnv, response: RegResponse) -> Result<jobject, Erro
|
||||
let response = env.new_object(
|
||||
"top/wherewego/vnt/jni/RegResponse",
|
||||
"(III)V",
|
||||
&[JValue::Int(virtual_ip as jint),
|
||||
&[
|
||||
JValue::Int(virtual_ip as jint),
|
||||
JValue::Int(virtual_gateway as jint),
|
||||
JValue::Int(virtual_netmask as jint)],
|
||||
JValue::Int(virtual_netmask as jint),
|
||||
],
|
||||
)?;
|
||||
Ok(response.into_raw())
|
||||
}
|
||||
@@ -278,10 +328,12 @@ fn driver_info_e(env: &mut JNIEnv, driver_info: DriverInfo) -> Result<jobject, E
|
||||
let response = env.new_object(
|
||||
"top/wherewego/vnt/jni/DriverInfo",
|
||||
"(ZLjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V",
|
||||
&[JValue::Bool(is_tun as jboolean),
|
||||
&[
|
||||
JValue::Bool(is_tun as jboolean),
|
||||
JValue::Object(&env.new_string(name)?.into()),
|
||||
JValue::Object(&env.new_string(version)?.into()),
|
||||
JValue::Object(&env.new_string(mac)?.into()), ],
|
||||
JValue::Object(&env.new_string(mac)?.into()),
|
||||
],
|
||||
)?;
|
||||
Ok(response.into_raw())
|
||||
}
|
||||
|
||||
@@ -3,12 +3,12 @@ use std::{fmt, io};
|
||||
/// 地址解析协议,由IP地址找到MAC地址
|
||||
/// https://www.ietf.org/rfc/rfc6747.txt
|
||||
/*
|
||||
0 2 4 5 6 8 10 (字节)
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| 硬件类型|协议类型|硬件地址长度|协议地址长度|操作类型|
|
||||
| 源MAC地址 | 源ip地址 |
|
||||
| 目的MAC地址 | 目的ip地址 |
|
||||
*/
|
||||
0 2 4 5 6 8 10 (字节)
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| 硬件类型|协议类型|硬件地址长度|协议地址长度|操作类型|
|
||||
| 源MAC地址 | 源ip地址 |
|
||||
| 目的MAC地址 | 目的ip地址 |
|
||||
*/
|
||||
|
||||
pub struct ArpPacket<B> {
|
||||
buffer: B,
|
||||
@@ -119,4 +119,4 @@ impl<B: AsRef<[u8]>> fmt::Debug for ArpPacket<B> {
|
||||
.field("target_protocol_addr", &self.target_protocol_addr())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1 +1 @@
|
||||
pub mod arp;
|
||||
pub mod arp;
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
pub mod packet;
|
||||
pub mod protocol;
|
||||
pub mod protocol;
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
use std::{fmt, io};
|
||||
use crate::ethernet::protocol::Protocol;
|
||||
use std::{fmt, io};
|
||||
|
||||
/// 以太网帧协议
|
||||
/// https://www.ietf.org/rfc/rfc894.txt
|
||||
/*
|
||||
0 6 12 14 (字节)
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| 目的地址 | 源地址 | 类型 |
|
||||
*/
|
||||
0 6 12 14 (字节)
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| 目的地址 | 源地址 | 类型 |
|
||||
*/
|
||||
pub struct EthernetPacket<B> {
|
||||
pub buffer: B,
|
||||
}
|
||||
@@ -74,4 +74,4 @@ impl<B: AsRef<[u8]>> fmt::Debug for EthernetPacket<B> {
|
||||
.field("payload", &self.payload())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@ impl From<u16> for Protocol {
|
||||
0x88f7 => Ptp,
|
||||
0x8902 => Cfm,
|
||||
0x9100 => QinQ,
|
||||
n => Unknown(n),
|
||||
n => Unknown(n),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -112,30 +112,30 @@ impl Into<u16> for Protocol {
|
||||
use self::Protocol::*;
|
||||
|
||||
match self {
|
||||
Ipv4 => 0x0800,
|
||||
Arp => 0x0806,
|
||||
WakeOnLan => 0x0842,
|
||||
Trill => 0x22f3,
|
||||
DecNet => 0x6003,
|
||||
Rarp => 0x8035,
|
||||
AppleTalk => 0x809b,
|
||||
Aarp => 0x80f3,
|
||||
Ipx => 0x8137,
|
||||
Qnx => 0x8204,
|
||||
Ipv6 => 0x86dd,
|
||||
FlowControl => 0x8808,
|
||||
CobraNet => 0x8819,
|
||||
Mpls => 0x8847,
|
||||
MplsMulticast => 0x8848,
|
||||
Ipv4 => 0x0800,
|
||||
Arp => 0x0806,
|
||||
WakeOnLan => 0x0842,
|
||||
Trill => 0x22f3,
|
||||
DecNet => 0x6003,
|
||||
Rarp => 0x8035,
|
||||
AppleTalk => 0x809b,
|
||||
Aarp => 0x80f3,
|
||||
Ipx => 0x8137,
|
||||
Qnx => 0x8204,
|
||||
Ipv6 => 0x86dd,
|
||||
FlowControl => 0x8808,
|
||||
CobraNet => 0x8819,
|
||||
Mpls => 0x8847,
|
||||
MplsMulticast => 0x8848,
|
||||
PppoeDiscovery => 0x8863,
|
||||
PppoeSession => 0x8864,
|
||||
Vlan => 0x8100,
|
||||
PBridge => 0x88a8,
|
||||
Lldp => 0x88cc,
|
||||
Ptp => 0x88f7,
|
||||
Cfm => 0x8902,
|
||||
QinQ => 0x9100,
|
||||
Unknown(n) => n,
|
||||
PppoeSession => 0x8864,
|
||||
Vlan => 0x8100,
|
||||
PBridge => 0x88a8,
|
||||
Lldp => 0x88cc,
|
||||
Ptp => 0x88f7,
|
||||
Cfm => 0x8902,
|
||||
QinQ => 0x9100,
|
||||
Unknown(n) => n,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use std::{fmt, io};
|
||||
use byteorder::{BigEndian, ReadBytesExt};
|
||||
use crate::cal_checksum;
|
||||
use crate::icmp::{Code, Kind};
|
||||
use crate::ip::ipv4::packet::IpV4Packet;
|
||||
use byteorder::{BigEndian, ReadBytesExt};
|
||||
use std::{fmt, io};
|
||||
|
||||
/// icmp 协议
|
||||
/* https://www.rfc-editor.org/rfc/rfc792
|
||||
@@ -67,7 +67,7 @@ impl<B: AsRef<[u8]>> IcmpPacket<B> {
|
||||
| Kind::TimestampReply
|
||||
| Kind::InformationRequest
|
||||
| Kind::InformationReply => {
|
||||
let ide =u16::from_be_bytes(self.buffer.as_ref()[4..6].try_into().unwrap());
|
||||
let ide = u16::from_be_bytes(self.buffer.as_ref()[4..6].try_into().unwrap());
|
||||
let seq = u16::from_be_bytes(self.buffer.as_ref()[6..8].try_into().unwrap());
|
||||
HeaderOther::Identifier(ide, seq)
|
||||
}
|
||||
@@ -121,11 +121,11 @@ impl<B: AsRef<[u8]>> fmt::Debug for IcmpPacket<B> {
|
||||
} else {
|
||||
"icmp::Packet!"
|
||||
})
|
||||
.field("kind", &self.kind())
|
||||
.field("code", &self.code())
|
||||
.field("checksum", &self.checksum())
|
||||
.field("payload", &self.payload())
|
||||
.finish()
|
||||
.field("kind", &self.kind())
|
||||
.field("code", &self.code())
|
||||
.field("checksum", &self.checksum())
|
||||
.field("payload", &self.payload())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
use std::{fmt, io};
|
||||
use std::net::Ipv4Addr;
|
||||
use crate::cal_checksum;
|
||||
use std::net::Ipv4Addr;
|
||||
use std::{fmt, io};
|
||||
|
||||
/// igmp v1
|
||||
/* https://datatracker.ietf.org/doc/html/rfc1112
|
||||
0 1 2 3
|
||||
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
|Version| Type | Unused | Checksum |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| Group Address |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
*/
|
||||
0 1 2 3
|
||||
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
|Version| Type | Unused | Checksum |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| Group Address |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
*/
|
||||
/// v1版本的报文
|
||||
pub struct IgmpV1Packet<B> {
|
||||
pub buffer: B,
|
||||
@@ -43,7 +43,7 @@ impl Into<u8> for IgmpV1Type {
|
||||
match self {
|
||||
IgmpV1Type::Query => 0x11,
|
||||
IgmpV1Type::ReportV1 => 0x12,
|
||||
IgmpV1Type::Unknown(v) => v
|
||||
IgmpV1Type::Unknown(v) => v,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -114,4 +114,4 @@ impl<B: AsRef<[u8]>> fmt::Debug for IgmpV1Packet<B> {
|
||||
.field("group_address", &self.group_address())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
use std::{fmt, io};
|
||||
use std::net::Ipv4Addr;
|
||||
use crate::cal_checksum;
|
||||
use std::net::Ipv4Addr;
|
||||
use std::{fmt, io};
|
||||
|
||||
/// igmp v2
|
||||
/* https://www.rfc-editor.org/rfc/rfc2236.html
|
||||
|
||||
0 1 2 3
|
||||
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| Type | Max Resp Time | Checksum |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| Group Address |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
*/
|
||||
0 1 2 3
|
||||
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| Type | Max Resp Time | Checksum |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| Group Address |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
*/
|
||||
|
||||
/// v2版本的报文
|
||||
pub struct IgmpV2Packet<B> {
|
||||
@@ -48,7 +48,7 @@ impl Into<u8> for IgmpV2Type {
|
||||
IgmpV2Type::Query => 0x11,
|
||||
IgmpV2Type::ReportV2 => 0x16,
|
||||
IgmpV2Type::LeaveV2 => 0x17,
|
||||
IgmpV2Type::Unknown(v) => v
|
||||
IgmpV2Type::Unknown(v) => v,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use std::{fmt, io};
|
||||
use std::net::Ipv4Addr;
|
||||
use std::{fmt, io};
|
||||
|
||||
use crate::cal_checksum;
|
||||
|
||||
@@ -116,7 +116,7 @@ impl Into<u8> for IgmpV3Type {
|
||||
match self {
|
||||
IgmpV3Type::Query => 0x11,
|
||||
IgmpV3Type::ReportV3 => 0x22,
|
||||
IgmpV3Type::Unknown(v) => v
|
||||
IgmpV3Type::Unknown(v) => v,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -203,7 +203,7 @@ impl<B: AsRef<[u8]> + AsMut<[u8]>> IgmpV3QueryPacket<B> {
|
||||
self.buffer.as_mut()[2..4].copy_from_slice(&checksum.to_be_bytes())
|
||||
}
|
||||
pub fn set_qrv(&mut self, qrv: u8) {
|
||||
self.buffer.as_mut()[8] = (self.buffer.as_ref()[8]&(!0x07)) | (qrv & 0x07)
|
||||
self.buffer.as_mut()[8] = (self.buffer.as_ref()[8] & (!0x07)) | (qrv & 0x07)
|
||||
}
|
||||
pub fn set_qqic(&mut self, qqic: u8) {
|
||||
self.buffer.as_mut()[9] = qqic
|
||||
@@ -349,7 +349,10 @@ impl<B: AsRef<[u8]>> IgmpV3ReportPacket<B> {
|
||||
return None;
|
||||
}
|
||||
if let Ok(record) = IgmpV3RecordPacket::new(&buf[start..]) {
|
||||
let end = start + 8 + record.aux_data_len() as usize * 4 + record.source_number() as usize * 4;
|
||||
let end = start
|
||||
+ 8
|
||||
+ record.aux_data_len() as usize * 4
|
||||
+ record.source_number() as usize * 4;
|
||||
if end > len {
|
||||
return None;
|
||||
}
|
||||
@@ -364,7 +367,6 @@ impl<B: AsRef<[u8]>> IgmpV3ReportPacket<B> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// group record
|
||||
pub struct IgmpV3RecordPacket<B> {
|
||||
pub buffer: B,
|
||||
@@ -488,4 +490,4 @@ impl<B: AsRef<[u8]>> fmt::Debug for IgmpV3RecordPacket<B> {
|
||||
.field("auxiliary_data", &self.auxiliary_data())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ pub mod igmp_v1;
|
||||
pub mod igmp_v2;
|
||||
pub mod igmp_v3;
|
||||
|
||||
#[derive(Debug,Copy, Clone,Eq, PartialEq)]
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||
pub enum IgmpType {
|
||||
/// 0x11 所有组224.0.0.1或者特定组
|
||||
Query,
|
||||
@@ -40,7 +40,7 @@ impl Into<u8> for IgmpType {
|
||||
IgmpType::ReportV2 => 0x16,
|
||||
IgmpType::ReportV3 => 0x22,
|
||||
IgmpType::LeaveV2 => 0x17,
|
||||
IgmpType::Unknown(v) => v
|
||||
IgmpType::Unknown(v) => v,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use std::{fmt, io};
|
||||
use std::net::Ipv4Addr;
|
||||
|
||||
use std::{fmt, io};
|
||||
|
||||
use crate::cal_checksum;
|
||||
use crate::ip::ipv4::protocol::Protocol;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#[derive(Eq, PartialEq,Ord, PartialOrd, Copy, Clone, Debug)]
|
||||
#[derive(Eq, PartialEq, Ord, PartialOrd, Copy, Clone, Debug)]
|
||||
pub enum Protocol {
|
||||
///
|
||||
Hopopt,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use std::io;
|
||||
use ipv4::packet::IpV4Packet;
|
||||
use std::io;
|
||||
|
||||
pub mod ipv4;
|
||||
|
||||
|
||||
@@ -3,13 +3,13 @@ use std::net::Ipv4Addr;
|
||||
use byteorder::BigEndian;
|
||||
use byteorder::ReadBytesExt;
|
||||
|
||||
pub mod arp;
|
||||
pub mod ethernet;
|
||||
pub mod icmp;
|
||||
pub mod igmp;
|
||||
pub mod ip;
|
||||
pub mod tcp;
|
||||
pub mod udp;
|
||||
pub mod ethernet;
|
||||
pub mod arp;
|
||||
// pub enum IpUpperLayer<B> {
|
||||
// UDP(UdpPacket<B>),
|
||||
// Unknown(B),
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use std::{fmt, io};
|
||||
use std::net::Ipv4Addr;
|
||||
use std::{fmt, io};
|
||||
|
||||
use crate::tcp::Flags;
|
||||
|
||||
@@ -58,7 +58,11 @@ impl<B: AsRef<[u8]>> TcpPacket<B> {
|
||||
buffer,
|
||||
}
|
||||
}
|
||||
pub fn new(source_ip: Ipv4Addr, destination_ip: Ipv4Addr, buffer: B) -> io::Result<TcpPacket<B>> {
|
||||
pub fn new(
|
||||
source_ip: Ipv4Addr,
|
||||
destination_ip: Ipv4Addr,
|
||||
buffer: B,
|
||||
) -> io::Result<TcpPacket<B>> {
|
||||
let packet = TcpPacket::unchecked(source_ip, destination_ip, buffer);
|
||||
|
||||
if packet.buffer.as_ref().len() < 20 {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use std::{fmt, io};
|
||||
use std::net::Ipv4Addr;
|
||||
use std::{fmt, io};
|
||||
|
||||
/// udp协议
|
||||
///
|
||||
@@ -60,7 +60,11 @@ impl<B: AsRef<[u8]>> UdpPacket<B> {
|
||||
buffer,
|
||||
}
|
||||
}
|
||||
pub fn new(source_ip: Ipv4Addr, destination_ip: Ipv4Addr, buffer: B) -> io::Result<UdpPacket<B>> {
|
||||
pub fn new(
|
||||
source_ip: Ipv4Addr,
|
||||
destination_ip: Ipv4Addr,
|
||||
buffer: B,
|
||||
) -> io::Result<UdpPacket<B>> {
|
||||
if buffer.as_ref().len() < 8 {
|
||||
Err(io::Error::from(io::ErrorKind::InvalidData))?;
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ use crate::error::*;
|
||||
|
||||
/// A TUN device.
|
||||
pub trait Device {
|
||||
type Queue ;
|
||||
type Queue;
|
||||
|
||||
/// Reconfigure the device.
|
||||
fn configure(&mut self, config: &Configuration) -> Result<()> {
|
||||
|
||||
@@ -77,10 +77,10 @@ impl Device {
|
||||
|
||||
req.ifru.flags = device_type
|
||||
| if config.platform.packet_information {
|
||||
0
|
||||
} else {
|
||||
IFF_NO_PI
|
||||
}
|
||||
0
|
||||
} else {
|
||||
IFF_NO_PI
|
||||
}
|
||||
| if queues_num > 1 { IFF_MULTI_QUEUE } else { 0 };
|
||||
|
||||
for _ in 0..queues_num {
|
||||
|
||||
@@ -22,7 +22,7 @@ use std::ptr;
|
||||
use std::sync::Arc;
|
||||
|
||||
use libc;
|
||||
use libc::{AF_INET, c_char, c_uint, c_void, SOCK_DGRAM, sockaddr, socklen_t};
|
||||
use libc::{c_char, c_uint, c_void, sockaddr, socklen_t, AF_INET, SOCK_DGRAM};
|
||||
|
||||
use crate::configuration::{Configuration, Layer};
|
||||
use crate::device::Device as D;
|
||||
|
||||
@@ -27,7 +27,6 @@ pub mod macos;
|
||||
#[cfg(target_os = "macos")]
|
||||
pub use self::macos::{create, Configuration, Device, Queue};
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::configuration::Configuration;
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
use std::io;
|
||||
use std::mem;
|
||||
use std::os::unix::io::{AsRawFd,RawFd};
|
||||
use std::os::unix::io::{AsRawFd, RawFd};
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::platform::posix::Fd;
|
||||
@@ -72,7 +72,6 @@ impl Writer {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn write_vectored(&self, bufs: &[io::IoSlice<'_>]) -> io::Result<usize> {
|
||||
unsafe {
|
||||
let mut msg: libc::msghdr = mem::zeroed();
|
||||
|
||||
+148
-87
@@ -7,15 +7,15 @@ use byte_pool::{Block, BytePool};
|
||||
use crossbeam_utils::atomic::AtomicCell;
|
||||
use dashmap::DashMap;
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
use tokio::net::{TcpStream, UdpSocket};
|
||||
use tokio::net::tcp::OwnedReadHalf;
|
||||
use tokio::net::{TcpStream, UdpSocket};
|
||||
use tokio::sync::watch::{channel, Receiver, Sender};
|
||||
|
||||
use crate::channel::{Route, RouteKey, Status};
|
||||
use crate::channel::punch::NatType;
|
||||
use crate::channel::{Route, RouteKey, Status};
|
||||
use crate::core::status::VntWorker;
|
||||
use crate::handle::CurrentDeviceInfo;
|
||||
use crate::handle::recv_handler::ChannelDataHandler;
|
||||
use crate::handle::CurrentDeviceInfo;
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
static ref POOL:BytePool = BytePool::new();
|
||||
@@ -41,7 +41,13 @@ pub struct Context {
|
||||
}
|
||||
|
||||
impl Context {
|
||||
pub fn new(main_channel: Arc<UdpSocket>, main_channel_ipv6: Option<Arc<UdpSocket>>, main_tcp_channel: Option<tokio::sync::mpsc::Sender<Vec<u8>>>, current_device: Arc<AtomicCell<CurrentDeviceInfo>>, _channel_num: usize) -> Self {
|
||||
pub fn new(
|
||||
main_channel: Arc<UdpSocket>,
|
||||
main_channel_ipv6: Option<Arc<UdpSocket>>,
|
||||
main_tcp_channel: Option<tokio::sync::mpsc::Sender<Vec<u8>>>,
|
||||
current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
|
||||
_channel_num: usize,
|
||||
) -> Self {
|
||||
//当前版本只支持一个通道
|
||||
let channel_num = 1;
|
||||
let (status_sender, status_receiver) = channel(Status::Cone);
|
||||
@@ -57,9 +63,7 @@ impl Context {
|
||||
channel_num,
|
||||
current_device,
|
||||
});
|
||||
Self {
|
||||
inner
|
||||
}
|
||||
Self { inner }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,9 +100,9 @@ impl Context {
|
||||
self.inner.main_channel.local_addr().map(|k| k.port())
|
||||
}
|
||||
pub fn main_local_ipv6_port(&self) -> io::Result<u16> {
|
||||
if let Some(ipv6) = &self.inner.main_channel_ipv6{
|
||||
if let Some(ipv6) = &self.inner.main_channel_ipv6 {
|
||||
ipv6.local_addr().map(|k| k.port())
|
||||
}else{
|
||||
} else {
|
||||
Err(io::Error::new(io::ErrorKind::Other, "not ipv6"))
|
||||
}
|
||||
}
|
||||
@@ -172,7 +176,7 @@ impl Context {
|
||||
}
|
||||
}
|
||||
}
|
||||
return self.send_by_key(buf,&route.route_key()).await;
|
||||
return self.send_by_key(buf, &route.route_key()).await;
|
||||
}
|
||||
Err(io::Error::new(io::ErrorKind::NotFound, "route not found"))
|
||||
}
|
||||
@@ -234,7 +238,11 @@ impl Context {
|
||||
}
|
||||
fn add_route_(&self, id: Ipv4Addr, route: Route, only_if_absent: bool) {
|
||||
let key = route.route_key();
|
||||
let mut list = self.inner.route_table.entry(id).or_insert_with(|| Vec::with_capacity(4));
|
||||
let mut list = self
|
||||
.inner
|
||||
.route_table
|
||||
.entry(id)
|
||||
.or_insert_with(|| Vec::with_capacity(4));
|
||||
let mut exist = false;
|
||||
for x in list.iter_mut() {
|
||||
if x.metric < route.metric {
|
||||
@@ -265,7 +273,9 @@ impl Context {
|
||||
list.truncate(max_len);
|
||||
}
|
||||
}
|
||||
self.inner.route_table_time.insert((key, id), Instant::now());
|
||||
self.inner
|
||||
.route_table_time
|
||||
.insert((key, id), Instant::now());
|
||||
}
|
||||
pub fn route(&self, id: &Ipv4Addr) -> Option<Vec<Route>> {
|
||||
if let Some(v) = self.inner.route_table.get(id) {
|
||||
@@ -300,7 +310,11 @@ impl Context {
|
||||
true
|
||||
}
|
||||
pub fn route_table(&self) -> Vec<(Ipv4Addr, Vec<Route>)> {
|
||||
self.inner.route_table.iter().map(|k| (k.key().clone(), k.value().clone())).collect()
|
||||
self.inner
|
||||
.route_table
|
||||
.iter()
|
||||
.map(|k| (k.key().clone(), k.value().clone()))
|
||||
.collect()
|
||||
}
|
||||
pub fn route_table_one(&self) -> Vec<(Ipv4Addr, Route)> {
|
||||
let mut v = Vec::with_capacity(8);
|
||||
@@ -351,17 +365,16 @@ pub struct Channel {
|
||||
}
|
||||
|
||||
impl Channel {
|
||||
pub fn new(context: Context,
|
||||
handler: ChannelDataHandler, ) -> Self {
|
||||
Self {
|
||||
context,
|
||||
handler,
|
||||
}
|
||||
pub fn new(context: Context, handler: ChannelDataHandler) -> Self {
|
||||
Self { context, handler }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct BufSenderGroup(usize, Vec<tokio::sync::mpsc::Sender<(Block<'static>, usize, usize, RouteKey)>>);
|
||||
struct BufSenderGroup(
|
||||
usize,
|
||||
Vec<tokio::sync::mpsc::Sender<(Block<'static>, usize, usize, RouteKey)>>,
|
||||
);
|
||||
|
||||
struct BufReceiverGroup(Vec<tokio::sync::mpsc::Receiver<(Block<'static>, usize, usize, RouteKey)>>);
|
||||
|
||||
@@ -377,15 +390,23 @@ fn buf_channel_group(size: usize) -> (BufSenderGroup, BufReceiverGroup) {
|
||||
let mut buf_sender_group = Vec::with_capacity(size);
|
||||
let mut buf_receiver_group = Vec::with_capacity(size);
|
||||
for _ in 0..size {
|
||||
let (buf_sender, buf_receiver) = tokio::sync::mpsc::channel::<(Block<'static, Vec<u8>>, usize, usize, RouteKey)>(10);
|
||||
let (buf_sender, buf_receiver) =
|
||||
tokio::sync::mpsc::channel::<(Block<'static, Vec<u8>>, usize, usize, RouteKey)>(10);
|
||||
buf_sender_group.push(buf_sender);
|
||||
buf_receiver_group.push(buf_receiver);
|
||||
}
|
||||
(BufSenderGroup(0, buf_sender_group), BufReceiverGroup(buf_receiver_group))
|
||||
(
|
||||
BufSenderGroup(0, buf_sender_group),
|
||||
BufReceiverGroup(buf_receiver_group),
|
||||
)
|
||||
}
|
||||
|
||||
impl Channel {
|
||||
async fn tcp_handle(mut tcp_r: OwnedReadHalf, mut buf_sender: BufSenderGroup, head_reserve: usize) -> io::Result<()> {
|
||||
async fn tcp_handle(
|
||||
mut tcp_r: OwnedReadHalf,
|
||||
mut buf_sender: BufSenderGroup,
|
||||
head_reserve: usize,
|
||||
) -> io::Result<()> {
|
||||
let mut head = [0; 4];
|
||||
let addr = tcp_r.peer_addr()?;
|
||||
let key = RouteKey::new(0, addr);
|
||||
@@ -399,21 +420,34 @@ impl Channel {
|
||||
"length overflow",
|
||||
));
|
||||
}
|
||||
tcp_r.read_exact(&mut buf[head_reserve..head_reserve + len]).await?;
|
||||
if !buf_sender.send((buf, head_reserve, head_reserve + len, key)).await {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "buf_sender发送数据失败"));
|
||||
tcp_r
|
||||
.read_exact(&mut buf[head_reserve..head_reserve + len])
|
||||
.await?;
|
||||
if !buf_sender
|
||||
.send((buf, head_reserve, head_reserve + len, key))
|
||||
.await
|
||||
{
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"buf_sender发送数据失败",
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
async fn start_tcp(mut worker: VntWorker, tcp_stream: TcpStream, mut receiver: tokio::sync::mpsc::Receiver<Vec<u8>>,
|
||||
current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
|
||||
buf_sender: BufSenderGroup, head_reserve: usize) {
|
||||
async fn start_tcp(
|
||||
mut worker: VntWorker,
|
||||
tcp_stream: TcpStream,
|
||||
mut receiver: tokio::sync::mpsc::Receiver<Vec<u8>>,
|
||||
current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
|
||||
buf_sender: BufSenderGroup,
|
||||
head_reserve: usize,
|
||||
) {
|
||||
let (tcp_r, mut tcp_w) = tcp_stream.into_split();
|
||||
{
|
||||
let buf_sender = buf_sender.clone();
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = Self::tcp_handle(tcp_r, buf_sender, head_reserve).await {
|
||||
log::info!("tcp链接断开:{:?}",e);
|
||||
log::info!("tcp链接断开:{:?}", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -463,13 +497,14 @@ impl Channel {
|
||||
worker.stop_all();
|
||||
}
|
||||
|
||||
pub async fn start(self,
|
||||
mut worker: VntWorker,
|
||||
tcp: Option<(TcpStream, tokio::sync::mpsc::Receiver<Vec<u8>>)>,
|
||||
head_reserve: usize,//头部预留字节
|
||||
symmetric_channel_num: usize,//对称网络,则再加一组监听,提升打洞成功率
|
||||
relay: bool,
|
||||
parallel: usize,
|
||||
pub async fn start(
|
||||
self,
|
||||
mut worker: VntWorker,
|
||||
tcp: Option<(TcpStream, tokio::sync::mpsc::Receiver<Vec<u8>>)>,
|
||||
head_reserve: usize, //头部预留字节
|
||||
symmetric_channel_num: usize, //对称网络,则再加一组监听,提升打洞成功率
|
||||
relay: bool,
|
||||
parallel: usize,
|
||||
) {
|
||||
let handler = self.handler.clone();
|
||||
let context = self.context;
|
||||
@@ -481,7 +516,9 @@ impl Channel {
|
||||
let handler = handler.clone();
|
||||
tokio::spawn(async move {
|
||||
while let Some((mut buf, start, end, route_key)) = buf_receiver.recv().await {
|
||||
handler.handle(&mut buf, start, end, route_key, &context).await;
|
||||
handler
|
||||
.handle(&mut buf, start, end, route_key, &context)
|
||||
.await;
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -490,12 +527,35 @@ impl Channel {
|
||||
None
|
||||
};
|
||||
if let Some((tcp_stream, receiver)) = tcp {
|
||||
tokio::spawn(Self::start_tcp(worker.worker("main_channel_tcp"), tcp_stream, receiver, context.inner.current_device.clone(), buf_sender.clone().unwrap(), head_reserve));
|
||||
tokio::spawn(Self::start_tcp(
|
||||
worker.worker("main_channel_tcp"),
|
||||
tcp_stream,
|
||||
receiver,
|
||||
context.inner.current_device.clone(),
|
||||
buf_sender.clone().unwrap(),
|
||||
head_reserve,
|
||||
));
|
||||
}
|
||||
if let Some(main_channel_ipv6) = &context.inner.main_channel_ipv6 {
|
||||
tokio::spawn(Self::start_(worker.worker("main_channel_ipv6"), context.clone(), main_channel_ipv6.clone(), handler.clone(), buf_sender.clone(), head_reserve, true));
|
||||
tokio::spawn(Self::start_(
|
||||
worker.worker("main_channel_ipv6"),
|
||||
context.clone(),
|
||||
main_channel_ipv6.clone(),
|
||||
handler.clone(),
|
||||
buf_sender.clone(),
|
||||
head_reserve,
|
||||
true,
|
||||
));
|
||||
}
|
||||
tokio::spawn(Self::start_(worker.worker("main_channel_1"), context.clone(), main_channel.clone(), handler.clone(), buf_sender.clone(), head_reserve, true));
|
||||
tokio::spawn(Self::start_(
|
||||
worker.worker("main_channel_1"),
|
||||
context.clone(),
|
||||
main_channel.clone(),
|
||||
handler.clone(),
|
||||
buf_sender.clone(),
|
||||
head_reserve,
|
||||
true,
|
||||
));
|
||||
if relay {
|
||||
worker.stop_wait().await;
|
||||
return;
|
||||
@@ -547,21 +607,24 @@ impl Channel {
|
||||
}
|
||||
worker.stop_all();
|
||||
}
|
||||
async fn start_(mut worker: VntWorker, context: Context,
|
||||
udp: Arc<UdpSocket>,
|
||||
handler: ChannelDataHandler,
|
||||
buf_sender: Option<BufSenderGroup>,
|
||||
head_reserve: usize,
|
||||
is_core: bool) {
|
||||
async fn start_(
|
||||
mut worker: VntWorker,
|
||||
context: Context,
|
||||
udp: Arc<UdpSocket>,
|
||||
handler: ChannelDataHandler,
|
||||
buf_sender: Option<BufSenderGroup>,
|
||||
head_reserve: usize,
|
||||
is_core: bool,
|
||||
) {
|
||||
let mut status_receiver = context.inner.status_receiver.clone();
|
||||
#[cfg(target_os = "windows")]
|
||||
use std::os::windows::io::AsRawSocket;
|
||||
#[cfg(target_os = "windows")]
|
||||
let id = 1 + udp.as_raw_socket() as usize;
|
||||
let id = 1 + udp.as_raw_socket() as usize;
|
||||
#[cfg(any(unix))]
|
||||
use std::os::fd::AsRawFd;
|
||||
#[cfg(any(unix))]
|
||||
let id = 1 + udp.as_raw_fd() as usize;
|
||||
let id = 1 + udp.as_raw_fd() as usize;
|
||||
context.inner.udp_map.insert(id, udp.clone());
|
||||
match buf_sender {
|
||||
None => {
|
||||
@@ -604,49 +667,47 @@ impl Channel {
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(mut buf_sender) => {
|
||||
loop {
|
||||
let mut buf = POOL.alloc(4096);
|
||||
tokio::select! {
|
||||
rs=udp.recv_from(&mut buf[head_reserve..])=>{
|
||||
match rs {
|
||||
Ok((len, addr)) => {
|
||||
if !buf_sender.send((buf,head_reserve,head_reserve+len,RouteKey::new(id, addr))).await{
|
||||
log::error!("udp buf_sender发送数据失败");
|
||||
break;
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("{:?}",e)
|
||||
Some(mut buf_sender) => loop {
|
||||
let mut buf = POOL.alloc(4096);
|
||||
tokio::select! {
|
||||
rs=udp.recv_from(&mut buf[head_reserve..])=>{
|
||||
match rs {
|
||||
Ok((len, addr)) => {
|
||||
if !buf_sender.send((buf,head_reserve,head_reserve+len,RouteKey::new(id, addr))).await{
|
||||
log::error!("udp buf_sender发送数据失败");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
changed=status_receiver.changed()=>{
|
||||
match changed {
|
||||
Ok(_) => {
|
||||
match *status_receiver.borrow() {
|
||||
Status::Cone => {
|
||||
if !is_core{
|
||||
break;
|
||||
}
|
||||
}
|
||||
Status::Close=>{
|
||||
break;
|
||||
}
|
||||
Status::Symmetric => {}
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
_=worker.stop_wait()=>{
|
||||
break;
|
||||
Err(e) => {
|
||||
log::error!("{:?}",e)
|
||||
}
|
||||
}
|
||||
}
|
||||
changed=status_receiver.changed()=>{
|
||||
match changed {
|
||||
Ok(_) => {
|
||||
match *status_receiver.borrow() {
|
||||
Status::Cone => {
|
||||
if !is_core{
|
||||
break;
|
||||
}
|
||||
}
|
||||
Status::Close=>{
|
||||
break;
|
||||
}
|
||||
Status::Symmetric => {}
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
_=worker.stop_wait()=>{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
context.inner.udp_map.remove(&id);
|
||||
if is_core {
|
||||
|
||||
+5
-10
@@ -1,10 +1,9 @@
|
||||
use crate::channel::channel::Context;
|
||||
use crate::channel::RouteKey;
|
||||
use std::io;
|
||||
use std::io::{Error, ErrorKind};
|
||||
use std::net::Ipv4Addr;
|
||||
use std::time::Duration;
|
||||
use crate::channel::channel::Context;
|
||||
use crate::channel::RouteKey;
|
||||
|
||||
|
||||
pub struct Idle {
|
||||
read_idle: Duration,
|
||||
@@ -12,12 +11,8 @@ pub struct Idle {
|
||||
}
|
||||
|
||||
impl Idle {
|
||||
pub fn new(read_idle: Duration,
|
||||
context: Context, ) -> Self {
|
||||
Self {
|
||||
read_idle,
|
||||
context,
|
||||
}
|
||||
pub fn new(read_idle: Duration, context: Context) -> Self {
|
||||
Self { read_idle, context }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,4 +40,4 @@ impl Idle {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+5
-10
@@ -1,8 +1,8 @@
|
||||
use std::net::SocketAddr;
|
||||
|
||||
pub mod channel;
|
||||
pub mod punch;
|
||||
pub mod idle;
|
||||
pub mod punch;
|
||||
pub mod sender;
|
||||
|
||||
#[derive(Copy, Clone, Eq, PartialEq)]
|
||||
@@ -27,8 +27,7 @@ pub struct RouteSortKey {
|
||||
}
|
||||
|
||||
impl Route {
|
||||
pub fn new(index: usize,
|
||||
addr: SocketAddr, metric: u8, rt: i64, ) -> Self {
|
||||
pub fn new(index: usize, addr: SocketAddr, metric: u8, rt: i64) -> Self {
|
||||
Self {
|
||||
index,
|
||||
addr,
|
||||
@@ -68,11 +67,7 @@ pub struct RouteKey {
|
||||
}
|
||||
|
||||
impl RouteKey {
|
||||
pub(crate) fn new(index: usize,
|
||||
addr: SocketAddr, ) -> Self {
|
||||
Self {
|
||||
index,
|
||||
addr,
|
||||
}
|
||||
pub(crate) fn new(index: usize, addr: SocketAddr) -> Self {
|
||||
Self { index, addr }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+37
-20
@@ -24,15 +24,15 @@ pub enum NatType {
|
||||
}
|
||||
|
||||
impl NatInfo {
|
||||
pub fn new(mut public_ips: Vec<Ipv4Addr>,
|
||||
public_port: u16,
|
||||
public_port_range: u16,
|
||||
local_ipv4_addr: SocketAddrV4,
|
||||
ipv6_addr: SocketAddrV6,
|
||||
nat_type: NatType, ) -> Self {
|
||||
public_ips.retain(|ip| {
|
||||
!ip.is_loopback() && !ip.is_private()
|
||||
});
|
||||
pub fn new(
|
||||
mut public_ips: Vec<Ipv4Addr>,
|
||||
public_port: u16,
|
||||
public_port_range: u16,
|
||||
local_ipv4_addr: SocketAddrV4,
|
||||
ipv6_addr: SocketAddrV6,
|
||||
nat_type: NatType,
|
||||
) -> Self {
|
||||
public_ips.retain(|ip| !ip.is_loopback() && !ip.is_private());
|
||||
Self {
|
||||
public_ips,
|
||||
public_port,
|
||||
@@ -71,10 +71,16 @@ impl Punch {
|
||||
return Ok(());
|
||||
}
|
||||
if !nat_info.local_ipv4_addr.ip().is_unspecified() && nat_info.local_ipv4_addr.port() != 0 {
|
||||
let _ = self.context.send_main_udp(buf, SocketAddr::V4(nat_info.local_ipv4_addr)).await;
|
||||
let _ = self
|
||||
.context
|
||||
.send_main_udp(buf, SocketAddr::V4(nat_info.local_ipv4_addr))
|
||||
.await;
|
||||
}
|
||||
if !nat_info.ipv6_addr.ip().is_unspecified() && nat_info.ipv6_addr.port() != 0 {
|
||||
let _ = self.context.send_main_udp(buf, SocketAddr::V6(nat_info.ipv6_addr)).await;
|
||||
let _ = self
|
||||
.context
|
||||
.send_main_udp(buf, SocketAddr::V6(nat_info.ipv6_addr))
|
||||
.await;
|
||||
}
|
||||
match nat_info.nat_type {
|
||||
NatType::Symmetric => {
|
||||
@@ -94,12 +100,10 @@ impl Punch {
|
||||
} else {
|
||||
1
|
||||
};
|
||||
let (max_port, overflow) = nat_info.public_port.overflowing_add(nat_info.public_port_range);
|
||||
let max_port = if overflow {
|
||||
65535
|
||||
} else {
|
||||
max_port
|
||||
};
|
||||
let (max_port, overflow) = nat_info
|
||||
.public_port
|
||||
.overflowing_add(nat_info.public_port_range);
|
||||
let max_port = if overflow { 65535 } else { max_port };
|
||||
let k = if max_port - min_port + 1 > max_k1 {
|
||||
max_k1 as usize
|
||||
} else {
|
||||
@@ -111,7 +115,8 @@ impl Punch {
|
||||
let mut rng = rand::thread_rng();
|
||||
nums.shuffle(&mut rng);
|
||||
}
|
||||
self.punch_symmetric(&nums[..k], buf, &nat_info.public_ips, max_k1 as usize).await?;
|
||||
self.punch_symmetric(&nums[..k], buf, &nat_info.public_ips, max_k1 as usize)
|
||||
.await?;
|
||||
}
|
||||
let start = *self.port_index.entry(id.clone()).or_insert(0);
|
||||
let mut end = start + max_k2;
|
||||
@@ -120,7 +125,13 @@ impl Punch {
|
||||
end = self.port_vec.len();
|
||||
index = 0
|
||||
}
|
||||
self.punch_symmetric(&self.port_vec[start..end], buf, &nat_info.public_ips, max_k2).await?;
|
||||
self.punch_symmetric(
|
||||
&self.port_vec[start..end],
|
||||
buf,
|
||||
&nat_info.public_ips,
|
||||
max_k2,
|
||||
)
|
||||
.await?;
|
||||
self.port_index.insert(id, index);
|
||||
}
|
||||
NatType::Cone => {
|
||||
@@ -140,7 +151,13 @@ impl Punch {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn punch_symmetric(&self, ports: &[u16], buf: &[u8], ips: &Vec<Ipv4Addr>, max: usize) -> io::Result<()> {
|
||||
async fn punch_symmetric(
|
||||
&self,
|
||||
ports: &[u16],
|
||||
buf: &[u8],
|
||||
ips: &Vec<Ipv4Addr>,
|
||||
max: usize,
|
||||
) -> io::Result<()> {
|
||||
let mut count = 0;
|
||||
for port in ports {
|
||||
for pub_ip in ips {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use std::ops::Deref;
|
||||
use crate::channel::channel::Context;
|
||||
use std::ops::Deref;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ChannelSender {
|
||||
@@ -8,9 +8,7 @@ pub struct ChannelSender {
|
||||
|
||||
impl ChannelSender {
|
||||
pub fn new(context: Context) -> Self {
|
||||
Self {
|
||||
context,
|
||||
}
|
||||
Self { context }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+34
-20
@@ -5,7 +5,7 @@ use rand::RngCore;
|
||||
|
||||
use crate::cipher::Finger;
|
||||
use crate::protocol::body::AesCbcSecretBody;
|
||||
use crate::protocol::{HEAD_LEN, NetPacket};
|
||||
use crate::protocol::{NetPacket, HEAD_LEN};
|
||||
|
||||
type Aes128CbcEnc = cbc::Encryptor<aes::Aes128>;
|
||||
type Aes128CbcDec = cbc::Decryptor<aes::Aes128>;
|
||||
@@ -27,8 +27,8 @@ pub enum AesCbcEnum {
|
||||
impl AesCbcCipher {
|
||||
pub fn key(&self) -> &[u8] {
|
||||
match &self.cipher {
|
||||
AesCbcEnum::AES128CBC(key) => { key }
|
||||
AesCbcEnum::AES256CBC(key) => { key }
|
||||
AesCbcEnum::AES128CBC(key) => key,
|
||||
AesCbcEnum::AES256CBC(key) => key,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -47,13 +47,16 @@ impl AesCbcCipher {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decrypt_ipv4<B: AsRef<[u8]> + AsMut<[u8]>>(&self, net_packet: &mut NetPacket<B>) -> io::Result<()> {
|
||||
pub fn decrypt_ipv4<B: AsRef<[u8]> + AsMut<[u8]>>(
|
||||
&self,
|
||||
net_packet: &mut NetPacket<B>,
|
||||
) -> io::Result<()> {
|
||||
if !net_packet.is_encrypt() {
|
||||
//未加密的数据直接丢弃
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "not encrypt"));
|
||||
}
|
||||
if net_packet.payload().len() < 16 {
|
||||
log::error!("数据异常,长度{}小于{}",net_packet.payload().len(),16);
|
||||
log::error!("数据异常,长度{}小于{}", net_packet.payload().len(), 16);
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "data err"));
|
||||
}
|
||||
let mut iv = [0; 16];
|
||||
@@ -67,7 +70,8 @@ impl AesCbcCipher {
|
||||
iv[12..16].copy_from_slice(&finger.hash[0..4]);
|
||||
}
|
||||
|
||||
let mut secret_body = AesCbcSecretBody::new(net_packet.payload_mut(), self.finger.is_some())?;
|
||||
let mut secret_body =
|
||||
AesCbcSecretBody::new(net_packet.payload_mut(), self.finger.is_some())?;
|
||||
if let Some(finger) = &self.finger {
|
||||
let finger = finger.calculate_finger(&iv[..12], secret_body.en_body());
|
||||
if &finger != secret_body.finger() {
|
||||
@@ -75,8 +79,10 @@ impl AesCbcCipher {
|
||||
}
|
||||
}
|
||||
let rs = match &self.cipher {
|
||||
AesCbcEnum::AES128CBC(key) => { Aes128CbcDec::new(&(*key).into(), &iv.into()).decrypt_padded_mut::<Pkcs7>(secret_body.en_body_mut()) }
|
||||
AesCbcEnum::AES256CBC(key) => { Aes256CbcDec::new(&(*key).into(), &iv.into()).decrypt_padded_mut::<Pkcs7>(secret_body.en_body_mut()) }
|
||||
AesCbcEnum::AES128CBC(key) => Aes128CbcDec::new(&(*key).into(), &iv.into())
|
||||
.decrypt_padded_mut::<Pkcs7>(secret_body.en_body_mut()),
|
||||
AesCbcEnum::AES256CBC(key) => Aes256CbcDec::new(&(*key).into(), &iv.into())
|
||||
.decrypt_padded_mut::<Pkcs7>(secret_body.en_body_mut()),
|
||||
};
|
||||
match rs {
|
||||
Ok(buf) => {
|
||||
@@ -86,14 +92,18 @@ impl AesCbcCipher {
|
||||
net_packet.set_data_len(HEAD_LEN + len - 4)?;
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
Err(io::Error::new(io::ErrorKind::Other, format!("解密失败:{}", e)))
|
||||
}
|
||||
Err(e) => Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("解密失败:{}", e),
|
||||
)),
|
||||
}
|
||||
}
|
||||
/// net_packet 必须预留足够长度
|
||||
/// data_len是有效载荷的长度
|
||||
pub fn encrypt_ipv4<B: AsRef<[u8]> + AsMut<[u8]>>(&self, net_packet: &mut NetPacket<B>) -> io::Result<()> {
|
||||
pub fn encrypt_ipv4<B: AsRef<[u8]> + AsMut<[u8]>>(
|
||||
&self,
|
||||
net_packet: &mut NetPacket<B>,
|
||||
) -> io::Result<()> {
|
||||
let data_len = net_packet.data_len();
|
||||
let mut iv = [0; 16];
|
||||
iv[0..4].copy_from_slice(&net_packet.source().octets());
|
||||
@@ -105,17 +115,20 @@ impl AesCbcCipher {
|
||||
if let Some(finger) = &self.finger {
|
||||
iv[12..16].copy_from_slice(&finger.hash[0..4]);
|
||||
net_packet.set_data_len(data_len + 16)?;
|
||||
}else{
|
||||
} else {
|
||||
net_packet.set_data_len(data_len + 4)?;
|
||||
}
|
||||
//先扩充随机数
|
||||
let mut secret_body = AesCbcSecretBody::new(net_packet.payload_mut(), self.finger.is_some())?;
|
||||
let mut secret_body =
|
||||
AesCbcSecretBody::new(net_packet.payload_mut(), self.finger.is_some())?;
|
||||
secret_body.set_random(rand::thread_rng().next_u32());
|
||||
let p_len = secret_body.en_body().len();
|
||||
net_packet.set_data_len_max();
|
||||
let rs = match &self.cipher {
|
||||
AesCbcEnum::AES128CBC(key) => { Aes128CbcEnc::new(&(*key).into(), &iv.into()).encrypt_padded_mut::<Pkcs7>(net_packet.payload_mut(), p_len) }
|
||||
AesCbcEnum::AES256CBC(key) => { Aes256CbcEnc::new(&(*key).into(), &iv.into()).encrypt_padded_mut::<Pkcs7>(net_packet.payload_mut(), p_len) }
|
||||
AesCbcEnum::AES128CBC(key) => Aes128CbcEnc::new(&(*key).into(), &iv.into())
|
||||
.encrypt_padded_mut::<Pkcs7>(net_packet.payload_mut(), p_len),
|
||||
AesCbcEnum::AES256CBC(key) => Aes256CbcEnc::new(&(*key).into(), &iv.into())
|
||||
.encrypt_padded_mut::<Pkcs7>(net_packet.payload_mut(), p_len),
|
||||
};
|
||||
return match rs {
|
||||
Ok(buf) => {
|
||||
@@ -133,9 +146,10 @@ impl AesCbcCipher {
|
||||
net_packet.set_encrypt_flag(true);
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
Err(io::Error::new(io::ErrorKind::Other, format!("加密失败:{}", e)))
|
||||
}
|
||||
Err(e) => Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("加密失败:{}", e),
|
||||
)),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+35
-21
@@ -1,9 +1,9 @@
|
||||
use std::io;
|
||||
use crate::cipher::Finger;
|
||||
use crate::protocol::body::AesCbcSecretBody;
|
||||
use crate::protocol::{HEAD_LEN, NetPacket};
|
||||
use crate::protocol::{NetPacket, HEAD_LEN};
|
||||
use aes::cipher::{block_padding::Pkcs7, BlockDecryptMut, BlockEncryptMut, KeyInit};
|
||||
use rand::RngCore;
|
||||
use std::io;
|
||||
|
||||
type Aes128EcbEnc = ecb::Encryptor<aes::Aes128>;
|
||||
type Aes128EcbDec = ecb::Decryptor<aes::Aes128>;
|
||||
@@ -25,8 +25,8 @@ pub enum AesEcbEnum {
|
||||
impl AesEcbCipher {
|
||||
pub fn key(&self) -> &[u8] {
|
||||
match &self.cipher {
|
||||
AesEcbEnum::AES128ECB(key) => { key }
|
||||
AesEcbEnum::AES256ECB(key) => { key }
|
||||
AesEcbEnum::AES128ECB(key) => key,
|
||||
AesEcbEnum::AES256ECB(key) => key,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -45,13 +45,16 @@ impl AesEcbCipher {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decrypt_ipv4<B: AsRef<[u8]> + AsMut<[u8]>>(&self, net_packet: &mut NetPacket<B>) -> io::Result<()> {
|
||||
pub fn decrypt_ipv4<B: AsRef<[u8]> + AsMut<[u8]>>(
|
||||
&self,
|
||||
net_packet: &mut NetPacket<B>,
|
||||
) -> io::Result<()> {
|
||||
if !net_packet.is_encrypt() {
|
||||
//未加密的数据直接丢弃
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "not encrypt"));
|
||||
}
|
||||
if net_packet.payload().len() < 16 {
|
||||
log::error!("数据异常,长度{}小于{}",net_packet.payload().len(),16);
|
||||
log::error!("数据异常,长度{}小于{}", net_packet.payload().len(), 16);
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "data err"));
|
||||
}
|
||||
let mut iv = [0; 16];
|
||||
@@ -65,7 +68,8 @@ impl AesEcbCipher {
|
||||
iv[12..16].copy_from_slice(&finger.hash[0..4]);
|
||||
}
|
||||
|
||||
let mut secret_body = AesCbcSecretBody::new(net_packet.payload_mut(), self.finger.is_some())?;
|
||||
let mut secret_body =
|
||||
AesCbcSecretBody::new(net_packet.payload_mut(), self.finger.is_some())?;
|
||||
if let Some(finger) = &self.finger {
|
||||
let finger = finger.calculate_finger(&iv[..12], secret_body.en_body());
|
||||
if &finger != secret_body.finger() {
|
||||
@@ -73,8 +77,10 @@ impl AesEcbCipher {
|
||||
}
|
||||
}
|
||||
let rs = match &self.cipher {
|
||||
AesEcbEnum::AES128ECB(key) => { Aes128EcbDec::new(&(*key).into()).decrypt_padded_mut::<Pkcs7>(secret_body.en_body_mut()) }
|
||||
AesEcbEnum::AES256ECB(key) => { Aes256EcbDec::new(&(*key).into()).decrypt_padded_mut::<Pkcs7>(secret_body.en_body_mut()) }
|
||||
AesEcbEnum::AES128ECB(key) => Aes128EcbDec::new(&(*key).into())
|
||||
.decrypt_padded_mut::<Pkcs7>(secret_body.en_body_mut()),
|
||||
AesEcbEnum::AES256ECB(key) => Aes256EcbDec::new(&(*key).into())
|
||||
.decrypt_padded_mut::<Pkcs7>(secret_body.en_body_mut()),
|
||||
};
|
||||
match rs {
|
||||
Ok(buf) => {
|
||||
@@ -84,14 +90,18 @@ impl AesEcbCipher {
|
||||
net_packet.set_data_len(HEAD_LEN + len - 4)?;
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
Err(io::Error::new(io::ErrorKind::Other, format!("解密失败:{}", e)))
|
||||
}
|
||||
Err(e) => Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("解密失败:{}", e),
|
||||
)),
|
||||
}
|
||||
}
|
||||
/// net_packet 必须预留足够长度
|
||||
/// data_len是有效载荷的长度
|
||||
pub fn encrypt_ipv4<B: AsRef<[u8]> + AsMut<[u8]>>(&self, net_packet: &mut NetPacket<B>) -> io::Result<()> {
|
||||
pub fn encrypt_ipv4<B: AsRef<[u8]> + AsMut<[u8]>>(
|
||||
&self,
|
||||
net_packet: &mut NetPacket<B>,
|
||||
) -> io::Result<()> {
|
||||
let data_len = net_packet.data_len();
|
||||
let mut iv = [0; 16];
|
||||
iv[0..4].copy_from_slice(&net_packet.source().octets());
|
||||
@@ -103,18 +113,21 @@ impl AesEcbCipher {
|
||||
if let Some(finger) = &self.finger {
|
||||
iv[12..16].copy_from_slice(&finger.hash[0..4]);
|
||||
net_packet.set_data_len(data_len + 16)?;
|
||||
}else{
|
||||
} else {
|
||||
net_packet.set_data_len(data_len + 4)?;
|
||||
}
|
||||
//先扩充随机数
|
||||
|
||||
let mut secret_body = AesCbcSecretBody::new(net_packet.payload_mut(), self.finger.is_some())?;
|
||||
let mut secret_body =
|
||||
AesCbcSecretBody::new(net_packet.payload_mut(), self.finger.is_some())?;
|
||||
secret_body.set_random(rand::thread_rng().next_u32());
|
||||
let p_len = secret_body.en_body().len();
|
||||
net_packet.set_data_len_max();
|
||||
let rs = match &self.cipher {
|
||||
AesEcbEnum::AES128ECB(key) => { Aes128EcbEnc::new(&(*key).into()).encrypt_padded_mut::<Pkcs7>(net_packet.payload_mut(), p_len) }
|
||||
AesEcbEnum::AES256ECB(key) => { Aes256EcbEnc::new(&(*key).into()).encrypt_padded_mut::<Pkcs7>(net_packet.payload_mut(), p_len) }
|
||||
AesEcbEnum::AES128ECB(key) => Aes128EcbEnc::new(&(*key).into())
|
||||
.encrypt_padded_mut::<Pkcs7>(net_packet.payload_mut(), p_len),
|
||||
AesEcbEnum::AES256ECB(key) => Aes256EcbEnc::new(&(*key).into())
|
||||
.encrypt_padded_mut::<Pkcs7>(net_packet.payload_mut(), p_len),
|
||||
};
|
||||
return match rs {
|
||||
Ok(buf) => {
|
||||
@@ -132,9 +145,10 @@ impl AesEcbCipher {
|
||||
net_packet.set_encrypt_flag(true);
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
Err(io::Error::new(io::ErrorKind::Other, format!("加密失败:{}", e)))
|
||||
}
|
||||
Err(e) => Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("加密失败:{}", e),
|
||||
)),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
use std::io;
|
||||
|
||||
use aes_gcm::{AeadInPlace, Aes128Gcm, Aes256Gcm, Key, KeyInit, Nonce, Tag};
|
||||
use aes_gcm::aead::consts::{U12, U16};
|
||||
use aes_gcm::aead::generic_array::GenericArray;
|
||||
use aes_gcm::{AeadInPlace, Aes128Gcm, Aes256Gcm, Key, KeyInit, Nonce, Tag};
|
||||
use rand::RngCore;
|
||||
|
||||
use crate::cipher::finger::Finger;
|
||||
use crate::protocol::{body::ENCRYPTION_RESERVED, body::SecretBody, NetPacket};
|
||||
|
||||
use crate::protocol::{body::SecretBody, body::ENCRYPTION_RESERVED, NetPacket};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AesGcmCipher {
|
||||
@@ -37,13 +36,16 @@ impl AesGcmCipher {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decrypt_ipv4<B: AsRef<[u8]> + AsMut<[u8]>>(&self, net_packet: &mut NetPacket<B>) -> io::Result<()> {
|
||||
pub fn decrypt_ipv4<B: AsRef<[u8]> + AsMut<[u8]>>(
|
||||
&self,
|
||||
net_packet: &mut NetPacket<B>,
|
||||
) -> io::Result<()> {
|
||||
if !net_packet.is_encrypt() {
|
||||
//未加密的数据直接丢弃
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "not encrypt"));
|
||||
}
|
||||
if net_packet.payload().len() < ENCRYPTION_RESERVED {
|
||||
log::error!("数据异常,长度小于{}",ENCRYPTION_RESERVED);
|
||||
log::error!("数据异常,长度小于{}", ENCRYPTION_RESERVED);
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "data err"));
|
||||
}
|
||||
let mut nonce_raw = [0; 12];
|
||||
@@ -65,11 +67,18 @@ impl AesGcmCipher {
|
||||
}
|
||||
let tag: GenericArray<u8, U16> = Tag::clone_from_slice(tag);
|
||||
let rs = match &self.cipher {
|
||||
AesGcmEnum::AES128GCM(aes_gcm) => { aes_gcm.decrypt_in_place_detached(nonce, &[], secret_body.body_mut(), &tag) }
|
||||
AesGcmEnum::AES256GCM(aes_gcm) => { aes_gcm.decrypt_in_place_detached(nonce, &[], secret_body.body_mut(), &tag) }
|
||||
AesGcmEnum::AES128GCM(aes_gcm) => {
|
||||
aes_gcm.decrypt_in_place_detached(nonce, &[], secret_body.body_mut(), &tag)
|
||||
}
|
||||
AesGcmEnum::AES256GCM(aes_gcm) => {
|
||||
aes_gcm.decrypt_in_place_detached(nonce, &[], secret_body.body_mut(), &tag)
|
||||
}
|
||||
};
|
||||
if let Err(e) = rs {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, format!("解密失败:{}", e)));
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("解密失败:{}", e),
|
||||
));
|
||||
}
|
||||
net_packet.set_encrypt_flag(false);
|
||||
net_packet.set_data_len(net_packet.data_len() - ENCRYPTION_RESERVED)?;
|
||||
@@ -77,7 +86,10 @@ impl AesGcmCipher {
|
||||
}
|
||||
/// net_packet 必须预留足够长度
|
||||
/// data_len是有效载荷的长度
|
||||
pub fn encrypt_ipv4<B: AsRef<[u8]> + AsMut<[u8]>>(&self, net_packet: &mut NetPacket<B>) -> io::Result<()> {
|
||||
pub fn encrypt_ipv4<B: AsRef<[u8]> + AsMut<[u8]>>(
|
||||
&self,
|
||||
net_packet: &mut NetPacket<B>,
|
||||
) -> io::Result<()> {
|
||||
if net_packet.reserve() < ENCRYPTION_RESERVED {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "too short"));
|
||||
}
|
||||
@@ -94,8 +106,12 @@ impl AesGcmCipher {
|
||||
let mut secret_body = SecretBody::new(net_packet.payload_mut(), self.finger.is_some())?;
|
||||
secret_body.set_random(rand::thread_rng().next_u32());
|
||||
let rs = match &self.cipher {
|
||||
AesGcmEnum::AES128GCM(aes_gcm) => { aes_gcm.encrypt_in_place_detached(nonce, &[], secret_body.body_mut()) }
|
||||
AesGcmEnum::AES256GCM(aes_gcm) => { aes_gcm.encrypt_in_place_detached(nonce, &[], secret_body.body_mut()) }
|
||||
AesGcmEnum::AES128GCM(aes_gcm) => {
|
||||
aes_gcm.encrypt_in_place_detached(nonce, &[], secret_body.body_mut())
|
||||
}
|
||||
AesGcmEnum::AES256GCM(aes_gcm) => {
|
||||
aes_gcm.encrypt_in_place_detached(nonce, &[], secret_body.body_mut())
|
||||
}
|
||||
};
|
||||
return match rs {
|
||||
Ok(tag) => {
|
||||
@@ -107,9 +123,10 @@ impl AesGcmCipher {
|
||||
net_packet.set_encrypt_flag(true);
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
Err(io::Error::new(io::ErrorKind::Other, format!("加密失败:{}", e)))
|
||||
}
|
||||
Err(e) => Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("加密失败:{}", e),
|
||||
)),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+42
-68
@@ -1,14 +1,14 @@
|
||||
use std::io;
|
||||
use std::str::FromStr;
|
||||
use crate::cipher::{aes_cbc, Finger};
|
||||
use crate::protocol::NetPacket;
|
||||
use sha2::Digest;
|
||||
#[cfg(feature = "ring-cipher")]
|
||||
use crate::cipher::ring_aes_gcm_cipher::AesGcmCipher;
|
||||
use crate::cipher::aes_ecb::AesEcbCipher;
|
||||
#[cfg(not(feature = "ring-cipher"))]
|
||||
use crate::cipher::aes_gcm_cipher::AesGcmCipher;
|
||||
#[cfg(feature = "ring-cipher")]
|
||||
use crate::cipher::ring_aes_gcm_cipher::AesGcmCipher;
|
||||
use crate::cipher::{aes_cbc, Finger};
|
||||
use crate::protocol::NetPacket;
|
||||
use aes_cbc::AesCbcCipher;
|
||||
use crate::cipher::aes_ecb::AesEcbCipher;
|
||||
use sha2::Digest;
|
||||
use std::io;
|
||||
use std::str::FromStr;
|
||||
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
||||
pub enum CipherModel {
|
||||
@@ -22,14 +22,10 @@ impl FromStr for CipherModel {
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s {
|
||||
"aes_gcm" => {
|
||||
Ok(CipherModel::AesGcm)
|
||||
}
|
||||
"aes_cbc" => { Ok(CipherModel::AesCbc) }
|
||||
"aes_ecb" => { Ok(CipherModel::AesEcb) }
|
||||
_ => {
|
||||
Err(format!("not match '{}'", s))
|
||||
}
|
||||
"aes_gcm" => Ok(CipherModel::AesGcm),
|
||||
"aes_cbc" => Ok(CipherModel::AesCbc),
|
||||
"aes_ecb" => Ok(CipherModel::AesEcb),
|
||||
_ => Err(format!("not match '{}'", s)),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,7 +39,11 @@ pub enum Cipher {
|
||||
}
|
||||
|
||||
impl Cipher {
|
||||
pub fn new_password(model: CipherModel, password: Option<String>, token: Option<String>) -> Self {
|
||||
pub fn new_password(
|
||||
model: CipherModel,
|
||||
password: Option<String>,
|
||||
token: Option<String>,
|
||||
) -> Self {
|
||||
let finger = token.map(|token| Finger::new(&token));
|
||||
if let Some(password) = password {
|
||||
let mut hasher = sha2::Sha256::new();
|
||||
@@ -93,22 +93,17 @@ impl Cipher {
|
||||
let aes = AesGcmCipher::new_256(key, finger);
|
||||
Ok(Cipher::AesGcm((aes, key.to_vec())))
|
||||
}
|
||||
_ => {
|
||||
Err(io::Error::new(io::ErrorKind::Other, "key error"))
|
||||
}
|
||||
_ => Err(io::Error::new(io::ErrorKind::Other, "key error")),
|
||||
}
|
||||
}
|
||||
pub fn decrypt_ipv4<B: AsRef<[u8]> + AsMut<[u8]>>(&self, net_packet: &mut NetPacket<B>) -> io::Result<()> {
|
||||
pub fn decrypt_ipv4<B: AsRef<[u8]> + AsMut<[u8]>>(
|
||||
&self,
|
||||
net_packet: &mut NetPacket<B>,
|
||||
) -> io::Result<()> {
|
||||
match self {
|
||||
Cipher::AesGcm((aes_gcm, _)) => {
|
||||
aes_gcm.decrypt_ipv4(net_packet)
|
||||
}
|
||||
Cipher::AesCbc(aes_cbc) => {
|
||||
aes_cbc.decrypt_ipv4(net_packet)
|
||||
}
|
||||
Cipher::AesEcb(aes_ecb) => {
|
||||
aes_ecb.decrypt_ipv4(net_packet)
|
||||
}
|
||||
Cipher::AesGcm((aes_gcm, _)) => aes_gcm.decrypt_ipv4(net_packet),
|
||||
Cipher::AesCbc(aes_cbc) => aes_cbc.decrypt_ipv4(net_packet),
|
||||
Cipher::AesEcb(aes_ecb) => aes_ecb.decrypt_ipv4(net_packet),
|
||||
Cipher::None => {
|
||||
if net_packet.is_encrypt() {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "not key"));
|
||||
@@ -117,36 +112,23 @@ impl Cipher {
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn encrypt_ipv4<B: AsRef<[u8]> + AsMut<[u8]>>(&self, net_packet: &mut NetPacket<B>) -> io::Result<()> {
|
||||
pub fn encrypt_ipv4<B: AsRef<[u8]> + AsMut<[u8]>>(
|
||||
&self,
|
||||
net_packet: &mut NetPacket<B>,
|
||||
) -> io::Result<()> {
|
||||
match self {
|
||||
Cipher::AesGcm((aes_gcm, _)) => {
|
||||
aes_gcm.encrypt_ipv4(net_packet)
|
||||
}
|
||||
Cipher::AesCbc(aes_cbc) => {
|
||||
aes_cbc.encrypt_ipv4(net_packet)
|
||||
}
|
||||
Cipher::AesEcb(aes_ecb) => {
|
||||
aes_ecb.encrypt_ipv4(net_packet)
|
||||
}
|
||||
Cipher::None => {
|
||||
Ok(())
|
||||
}
|
||||
Cipher::AesGcm((aes_gcm, _)) => aes_gcm.encrypt_ipv4(net_packet),
|
||||
Cipher::AesCbc(aes_cbc) => aes_cbc.encrypt_ipv4(net_packet),
|
||||
Cipher::AesEcb(aes_ecb) => aes_ecb.encrypt_ipv4(net_packet),
|
||||
Cipher::None => Ok(()),
|
||||
}
|
||||
}
|
||||
pub fn check_finger<B: AsRef<[u8]>>(&self, net_packet: &NetPacket<B>) -> io::Result<()> {
|
||||
let finger = match self {
|
||||
Cipher::AesGcm((aes_gcm, _)) => {
|
||||
aes_gcm.finger.as_ref()
|
||||
}
|
||||
Cipher::AesCbc(aes_cbc) => {
|
||||
aes_cbc.finger.as_ref()
|
||||
}
|
||||
Cipher::AesEcb(aes_ecb) => {
|
||||
aes_ecb.finger.as_ref()
|
||||
}
|
||||
Cipher::None => {
|
||||
None
|
||||
}
|
||||
Cipher::AesGcm((aes_gcm, _)) => aes_gcm.finger.as_ref(),
|
||||
Cipher::AesCbc(aes_cbc) => aes_cbc.finger.as_ref(),
|
||||
Cipher::AesEcb(aes_ecb) => aes_ecb.finger.as_ref(),
|
||||
Cipher::None => None,
|
||||
};
|
||||
if let Some(finger) = finger {
|
||||
finger.check_finger(net_packet)
|
||||
@@ -156,18 +138,10 @@ impl Cipher {
|
||||
}
|
||||
pub fn key(&self) -> Option<&[u8]> {
|
||||
match self {
|
||||
Cipher::AesGcm((_, key)) => {
|
||||
Some(key)
|
||||
}
|
||||
Cipher::AesCbc(aes_cbc) => {
|
||||
Some(aes_cbc.key())
|
||||
}
|
||||
Cipher::AesEcb(aes_ecb) => {
|
||||
Some(aes_ecb.key())
|
||||
}
|
||||
Cipher::None => {
|
||||
None
|
||||
}
|
||||
Cipher::AesGcm((_, key)) => Some(key),
|
||||
Cipher::AesCbc(aes_cbc) => Some(aes_cbc.key()),
|
||||
Cipher::AesEcb(aes_ecb) => Some(aes_ecb.key()),
|
||||
Cipher::None => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ impl Finger {
|
||||
}
|
||||
let payload_len = net_packet.payload().len();
|
||||
if payload_len < 12 {
|
||||
log::error!("数据异常,长度小于{}",12);
|
||||
log::error!("数据异常,长度小于{}", 12);
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "data err"));
|
||||
}
|
||||
let mut nonce_raw = [0; 12];
|
||||
@@ -48,4 +48,4 @@ impl Finger {
|
||||
let key: [u8; 32] = hasher.finalize().into();
|
||||
return key[20..].try_into().unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
#[cfg(feature = "ring-cipher")]
|
||||
mod ring_aes_gcm_cipher;
|
||||
#[cfg(not(feature = "ring-cipher"))]
|
||||
mod aes_gcm_cipher;
|
||||
mod rsa_cipher;
|
||||
mod aes_cbc;
|
||||
mod aes_ecb;
|
||||
mod finger;
|
||||
#[cfg(not(feature = "ring-cipher"))]
|
||||
mod aes_gcm_cipher;
|
||||
mod cipher;
|
||||
mod finger;
|
||||
#[cfg(feature = "ring-cipher")]
|
||||
mod ring_aes_gcm_cipher;
|
||||
mod rsa_cipher;
|
||||
|
||||
pub use cipher::Cipher;
|
||||
pub use cipher::CipherModel;
|
||||
pub use finger::Finger;
|
||||
pub use rsa_cipher::RsaCipher;
|
||||
pub use cipher::CipherModel;
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use std::io;
|
||||
use crate::cipher::Finger;
|
||||
use rand::RngCore;
|
||||
use ring::aead;
|
||||
use ring::aead::{LessSafeKey, UnboundKey};
|
||||
use crate::cipher::Finger;
|
||||
use std::io;
|
||||
|
||||
use crate::protocol::body::{SecretBody, ENCRYPTION_RESERVED};
|
||||
use crate::protocol::NetPacket;
|
||||
use crate::protocol::body::{ENCRYPTION_RESERVED, SecretBody};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AesGcmCipher {
|
||||
@@ -22,11 +22,13 @@ impl Clone for AesGcmEnum {
|
||||
fn clone(&self) -> Self {
|
||||
match &self {
|
||||
AesGcmEnum::AesGCM128(_, key) => {
|
||||
let c = LessSafeKey::new(UnboundKey::new(&aead::AES_128_GCM, key.as_slice()).unwrap());
|
||||
let c =
|
||||
LessSafeKey::new(UnboundKey::new(&aead::AES_128_GCM, key.as_slice()).unwrap());
|
||||
AesGcmEnum::AesGCM128(c, *key)
|
||||
}
|
||||
AesGcmEnum::AesGCM256(_, key) => {
|
||||
let c = LessSafeKey::new(UnboundKey::new(&aead::AES_256_GCM, key.as_slice()).unwrap());
|
||||
let c =
|
||||
LessSafeKey::new(UnboundKey::new(&aead::AES_256_GCM, key.as_slice()).unwrap());
|
||||
AesGcmEnum::AesGCM256(c, *key)
|
||||
}
|
||||
}
|
||||
@@ -48,13 +50,16 @@ impl AesGcmCipher {
|
||||
finger,
|
||||
}
|
||||
}
|
||||
pub fn decrypt_ipv4<B: AsRef<[u8]> + AsMut<[u8]>>(&self, net_packet: &mut NetPacket<B>) -> io::Result<()> {
|
||||
pub fn decrypt_ipv4<B: AsRef<[u8]> + AsMut<[u8]>>(
|
||||
&self,
|
||||
net_packet: &mut NetPacket<B>,
|
||||
) -> io::Result<()> {
|
||||
if !net_packet.is_encrypt() {
|
||||
//未加密的数据直接丢弃
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "not encrypt"));
|
||||
}
|
||||
if net_packet.payload().len() < ENCRYPTION_RESERVED {
|
||||
log::error!("数据异常,长度小于{}",ENCRYPTION_RESERVED);
|
||||
log::error!("数据异常,长度小于{}", ENCRYPTION_RESERVED);
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "data err"));
|
||||
}
|
||||
let mut nonce_raw = [0; 12];
|
||||
@@ -82,7 +87,10 @@ impl AesGcmCipher {
|
||||
}
|
||||
};
|
||||
if let Err(e) = rs {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, format!("解密失败:{}", e)));
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("解密失败:{}", e),
|
||||
));
|
||||
}
|
||||
net_packet.set_encrypt_flag(false);
|
||||
net_packet.set_data_len(net_packet.data_len() - ENCRYPTION_RESERVED)?;
|
||||
@@ -91,7 +99,10 @@ impl AesGcmCipher {
|
||||
/// net_packet 必须预留足够长度
|
||||
/// data_len是有效载荷的长度
|
||||
/// 返回加密后载荷的长度
|
||||
pub fn encrypt_ipv4<B: AsRef<[u8]> + AsMut<[u8]>>(&self, net_packet: &mut NetPacket<B>) -> io::Result<()> {
|
||||
pub fn encrypt_ipv4<B: AsRef<[u8]> + AsMut<[u8]>>(
|
||||
&self,
|
||||
net_packet: &mut NetPacket<B>,
|
||||
) -> io::Result<()> {
|
||||
let mut nonce_raw = [0; 12];
|
||||
nonce_raw[0..4].copy_from_slice(&net_packet.source().octets());
|
||||
nonce_raw[4..8].copy_from_slice(&net_packet.destination().octets());
|
||||
@@ -117,7 +128,10 @@ impl AesGcmCipher {
|
||||
Ok(tag) => {
|
||||
let tag = tag.as_ref();
|
||||
if tag.len() != 16 {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, format!("加密tag长度错误:{}", tag.len())));
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("加密tag长度错误:{}", tag.len()),
|
||||
));
|
||||
}
|
||||
secret_body.set_tag(tag)?;
|
||||
if let Some(finger) = &self.finger {
|
||||
@@ -127,9 +141,10 @@ impl AesGcmCipher {
|
||||
net_packet.set_encrypt_flag(true);
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
Err(io::Error::new(io::ErrorKind::Other, format!("加密失败:{}", e)))
|
||||
}
|
||||
Err(e) => Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("加密失败:{}", e),
|
||||
)),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use std::io;
|
||||
use crate::protocol::body::{RsaSecretBody, ENCRYPTION_RESERVED};
|
||||
use crate::protocol::NetPacket;
|
||||
use rand::Rng;
|
||||
use rsa::pkcs8::der::Decode;
|
||||
use rsa::{PublicKey, RsaPublicKey};
|
||||
use spki::{DecodePublicKey, EncodePublicKey};
|
||||
use crate::protocol::body::{ENCRYPTION_RESERVED, RsaSecretBody};
|
||||
use crate::protocol::NetPacket;
|
||||
use sha2::Digest;
|
||||
use spki::{DecodePublicKey, EncodePublicKey};
|
||||
use std::io;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RsaCipher {
|
||||
@@ -21,47 +21,44 @@ impl RsaCipher {
|
||||
pub fn new(der: &[u8]) -> io::Result<Self> {
|
||||
match RsaPublicKey::from_public_key_der(der) {
|
||||
Ok(public_key) => {
|
||||
let inner = Inner {
|
||||
public_key,
|
||||
};
|
||||
Ok(Self {
|
||||
inner
|
||||
})
|
||||
}
|
||||
Err(e) => {
|
||||
Err(io::Error::new(io::ErrorKind::Other, format!("from_public_key_der failed {}", e)))
|
||||
let inner = Inner { public_key };
|
||||
Ok(Self { inner })
|
||||
}
|
||||
Err(e) => Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("from_public_key_der failed {}", e),
|
||||
)),
|
||||
}
|
||||
}
|
||||
pub fn finger(&self) -> io::Result<String> {
|
||||
match self.inner.public_key.to_public_key_der() {
|
||||
Ok(der) => {
|
||||
match rsa::pkcs8::SubjectPublicKeyInfo::from_der(der.as_bytes()) {
|
||||
Ok(spki) => {
|
||||
match spki.fingerprint_base64() {
|
||||
Ok(finger) => {
|
||||
Ok(finger)
|
||||
}
|
||||
Err(e) => {
|
||||
Err(io::Error::new(io::ErrorKind::Other, format!("fingerprint_base64 error {}", e)))
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
Err(io::Error::new(io::ErrorKind::Other, format!("from_der error {}", e)))
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
Err(io::Error::new(io::ErrorKind::Other, format!("to_public_key_der error {}", e)))
|
||||
}
|
||||
Ok(der) => match rsa::pkcs8::SubjectPublicKeyInfo::from_der(der.as_bytes()) {
|
||||
Ok(spki) => match spki.fingerprint_base64() {
|
||||
Ok(finger) => Ok(finger),
|
||||
Err(e) => Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("fingerprint_base64 error {}", e),
|
||||
)),
|
||||
},
|
||||
Err(e) => Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("from_der error {}", e),
|
||||
)),
|
||||
},
|
||||
Err(e) => Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("to_public_key_der error {}", e),
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RsaCipher {
|
||||
/// net_packet 必须预留足够长度
|
||||
pub fn encrypt<B: AsRef<[u8]> + AsMut<[u8]>>(&self, net_packet: &mut NetPacket<B>) -> io::Result<NetPacket<Vec<u8>>> {
|
||||
pub fn encrypt<B: AsRef<[u8]> + AsMut<[u8]>>(
|
||||
&self,
|
||||
net_packet: &mut NetPacket<B>,
|
||||
) -> io::Result<NetPacket<Vec<u8>>> {
|
||||
if net_packet.reserve() < ENCRYPTION_RESERVED {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "too short"));
|
||||
}
|
||||
@@ -84,16 +81,21 @@ impl RsaCipher {
|
||||
hasher.update(nonce_raw);
|
||||
let key: [u8; 32] = hasher.finalize().into();
|
||||
secret_body.set_finger(&key[16..])?;
|
||||
match self.inner.public_key.encrypt(&mut rng, rsa::PaddingScheme::PKCS1v15Encrypt, secret_body.buffer()) {
|
||||
match self.inner.public_key.encrypt(
|
||||
&mut rng,
|
||||
rsa::PaddingScheme::PKCS1v15Encrypt,
|
||||
secret_body.buffer(),
|
||||
) {
|
||||
Ok(enc_data) => {
|
||||
let mut net_packet_e = NetPacket::new(vec![0; 12 + enc_data.len()])?;
|
||||
net_packet_e.buffer_mut()[..12].copy_from_slice(&net_packet.buffer()[..12]);
|
||||
net_packet_e.set_payload(&enc_data)?;
|
||||
Ok(net_packet_e)
|
||||
}
|
||||
Err(e) => {
|
||||
Err(io::Error::new(io::ErrorKind::Other, format!("encrypt failed {}", e)))
|
||||
}
|
||||
Err(e) => Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("encrypt failed {}", e),
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+220
-78
@@ -10,22 +10,25 @@ use rand::Rng;
|
||||
use tokio::net::{TcpStream, UdpSocket};
|
||||
use tokio::sync::mpsc::channel;
|
||||
|
||||
use crate::channel::{Route, RouteKey};
|
||||
use crate::channel::channel::{Channel, Context};
|
||||
use crate::channel::idle::Idle;
|
||||
use crate::channel::punch::{NatInfo, Punch};
|
||||
use crate::channel::sender::ChannelSender;
|
||||
use crate::channel::{Route, RouteKey};
|
||||
use crate::cipher::{Cipher, CipherModel, RsaCipher};
|
||||
use crate::core::status::VntStatusManger;
|
||||
use crate::error::Error;
|
||||
use crate::external_route::{AllowExternalRoute, ExternalRoute};
|
||||
use crate::handle::{ConnectStatus, CurrentDeviceInfo, handshake_handler, heartbeat_handler, PeerDeviceInfo, punch_handler, registration_handler};
|
||||
use crate::handle::handshake_handler::HandshakeEnum;
|
||||
use crate::handle::recv_handler::ChannelDataHandler;
|
||||
use crate::handle::registration_handler::{RegResponse, ReqEnum};
|
||||
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
|
||||
use crate::handle::tun_tap::tap_handler;
|
||||
use crate::handle::tun_tap::tun_handler;
|
||||
use crate::handle::{
|
||||
handshake_handler, heartbeat_handler, punch_handler, registration_handler, ConnectStatus,
|
||||
CurrentDeviceInfo, PeerDeviceInfo,
|
||||
};
|
||||
use crate::igmp_server::IgmpServer;
|
||||
use crate::nat::NatTest;
|
||||
use crate::tun_tap_device;
|
||||
@@ -34,7 +37,6 @@ use crate::tun_tap_device::{DeviceReader, DeviceWriter};
|
||||
pub mod status;
|
||||
pub mod sync;
|
||||
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Vnt {
|
||||
config: Config,
|
||||
@@ -66,11 +68,9 @@ impl VntUtil {
|
||||
pub async fn new(config: Config) -> io::Result<VntUtil> {
|
||||
let main_channel = UdpSocket::bind("0.0.0.0:0").await?;
|
||||
let main_channel_ipv6 = match UdpSocket::bind("[::]:0").await {
|
||||
Ok(main_channel_ipv6) => {
|
||||
Some(main_channel_ipv6)
|
||||
}
|
||||
Ok(main_channel_ipv6) => Some(main_channel_ipv6),
|
||||
Err(e) => {
|
||||
log::warn!("绑定ipv6地址失败:{}",e);
|
||||
log::warn!("绑定ipv6地址失败:{}", e);
|
||||
None
|
||||
}
|
||||
};
|
||||
@@ -103,26 +103,48 @@ impl VntUtil {
|
||||
|
||||
///握手 用于获取公钥
|
||||
pub async fn handshake(&mut self) -> Result<Option<RsaCipher>, HandshakeEnum> {
|
||||
let rsa_cipher = handshake_handler::handshake(&self.main_channel, self.main_tcp_channel.as_mut(), self.config.server_address, self.config.server_encrypt).await?;
|
||||
let rsa_cipher = handshake_handler::handshake(
|
||||
&self.main_channel,
|
||||
self.main_tcp_channel.as_mut(),
|
||||
self.config.server_address,
|
||||
self.config.server_encrypt,
|
||||
)
|
||||
.await?;
|
||||
self.rsa_cipher = rsa_cipher.clone();
|
||||
Ok(rsa_cipher)
|
||||
}
|
||||
/// 加密握手 用于同步密钥
|
||||
pub async fn secret_handshake(&mut self) -> Result<(), HandshakeEnum> {
|
||||
handshake_handler::secret_handshake(&self.main_channel, self.main_tcp_channel.as_mut(), self.config.server_address, self.rsa_cipher.as_ref().unwrap(), &self.server_cipher, self.config.token.clone()).await
|
||||
handshake_handler::secret_handshake(
|
||||
&self.main_channel,
|
||||
self.main_tcp_channel.as_mut(),
|
||||
self.config.server_address,
|
||||
self.rsa_cipher.as_ref().unwrap(),
|
||||
&self.server_cipher,
|
||||
self.config.token.clone(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
/// 注册
|
||||
pub async fn register(&mut self) -> Result<RegResponse, ReqEnum> {
|
||||
match registration_handler::registration(&self.main_channel, self.main_tcp_channel.as_mut(), &self.server_cipher, self.config.server_address,
|
||||
self.config.token.clone(), self.config.device_id.clone(),
|
||||
self.config.name.clone(), self.config.ip.unwrap_or(Ipv4Addr::UNSPECIFIED), self.config.password.is_some()).await {
|
||||
match registration_handler::registration(
|
||||
&self.main_channel,
|
||||
self.main_tcp_channel.as_mut(),
|
||||
&self.server_cipher,
|
||||
self.config.server_address,
|
||||
self.config.token.clone(),
|
||||
self.config.device_id.clone(),
|
||||
self.config.name.clone(),
|
||||
self.config.ip.unwrap_or(Ipv4Addr::UNSPECIFIED),
|
||||
self.config.password.is_some(),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(res) => {
|
||||
let _ = self.response.insert(res.clone());
|
||||
Ok(res)
|
||||
}
|
||||
Err(e) => {
|
||||
Err(e)
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
#[cfg(any(target_os = "android"))]
|
||||
@@ -139,9 +161,7 @@ impl VntUtil {
|
||||
None => {
|
||||
return Err(io::Error::from(io::ErrorKind::AlreadyExists));
|
||||
}
|
||||
Some(res) => {
|
||||
res
|
||||
}
|
||||
Some(res) => res,
|
||||
};
|
||||
let device_type = if self.config.tap {
|
||||
#[cfg(windows)]
|
||||
@@ -166,14 +186,23 @@ impl VntUtil {
|
||||
1420
|
||||
}
|
||||
}
|
||||
Some(mtu) => {
|
||||
mtu
|
||||
}
|
||||
Some(mtu) => mtu,
|
||||
};
|
||||
let in_ips = self.config.in_ips.iter().map(|(dest, mask, _)| { (Ipv4Addr::from(*dest & *mask), Ipv4Addr::from(*mask)) }).collect::<Vec<(Ipv4Addr, Ipv4Addr)>>();
|
||||
let in_ips = self
|
||||
.config
|
||||
.in_ips
|
||||
.iter()
|
||||
.map(|(dest, mask, _)| (Ipv4Addr::from(*dest & *mask), Ipv4Addr::from(*mask)))
|
||||
.collect::<Vec<(Ipv4Addr, Ipv4Addr)>>();
|
||||
|
||||
let (device_writer, device_reader, driver_info) = tun_tap_device::create_device(device_type, response.virtual_ip,
|
||||
response.virtual_netmask, response.virtual_gateway, in_ips, mtu)?;
|
||||
let (device_writer, device_reader, driver_info) = tun_tap_device::create_device(
|
||||
device_type,
|
||||
response.virtual_ip,
|
||||
response.virtual_netmask,
|
||||
response.virtual_gateway,
|
||||
in_ips,
|
||||
mtu,
|
||||
)?;
|
||||
let _ = self.iface.insert((device_writer, device_reader));
|
||||
Ok(driver_info)
|
||||
}
|
||||
@@ -182,17 +211,13 @@ impl VntUtil {
|
||||
None => {
|
||||
return Err(Error::Stop("response None".to_string()));
|
||||
}
|
||||
Some(res) => {
|
||||
res
|
||||
}
|
||||
Some(res) => res,
|
||||
};
|
||||
let (device_writer, device_reader) = match self.iface {
|
||||
None => {
|
||||
return Err(Error::Stop("iface None".to_string()));
|
||||
}
|
||||
Some(res) => {
|
||||
res
|
||||
}
|
||||
Some(res) => res,
|
||||
};
|
||||
let config = self.config.clone();
|
||||
let vnt_status_manager = VntStatusManger::new();
|
||||
@@ -201,11 +226,17 @@ impl VntUtil {
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let client_cipher = Cipher::new_password(config.cipher_model, config.password.clone(), finger);
|
||||
let client_cipher =
|
||||
Cipher::new_password(config.cipher_model, config.password.clone(), finger);
|
||||
let virtual_ip = response.virtual_ip;
|
||||
let virtual_gateway = response.virtual_gateway;
|
||||
let virtual_netmask = response.virtual_netmask;
|
||||
let current_device = Arc::new(AtomicCell::new(CurrentDeviceInfo::new(virtual_ip, virtual_gateway, virtual_netmask, config.server_address)));
|
||||
let current_device = Arc::new(AtomicCell::new(CurrentDeviceInfo::new(
|
||||
virtual_ip,
|
||||
virtual_gateway,
|
||||
virtual_netmask,
|
||||
config.server_address,
|
||||
)));
|
||||
|
||||
let (cone_sender, cone_receiver) = channel(3);
|
||||
let (symmetric_sender, symmetric_receiver) = channel(2);
|
||||
@@ -215,17 +246,28 @@ impl VntUtil {
|
||||
} else {
|
||||
(None, None)
|
||||
};
|
||||
let context = Context::new(Arc::new(self.main_channel),
|
||||
self.main_channel_ipv6.map(|v| Arc::new(v)),
|
||||
tcp_sender, current_device.clone(), 1);
|
||||
let context = Context::new(
|
||||
Arc::new(self.main_channel),
|
||||
self.main_channel_ipv6.map(|v| Arc::new(v)),
|
||||
tcp_sender,
|
||||
current_device.clone(),
|
||||
1,
|
||||
);
|
||||
let punch = Punch::new(context.clone());
|
||||
let idle = Idle::new(Duration::from_secs(16), context.clone());
|
||||
let channel_sender = ChannelSender::new(context.clone());
|
||||
|
||||
let register = Arc::new(registration_handler::Register::new(self.server_cipher.clone(), channel_sender.clone(),
|
||||
config.server_address, config.token.clone(),
|
||||
config.device_id.clone(), config.name.clone(), config.password.is_some()));
|
||||
let device_list: Arc<Mutex<(u16, Vec<PeerDeviceInfo>)>> = Arc::new(Mutex::new((response.epoch, response.device_info_list)));
|
||||
let register = Arc::new(registration_handler::Register::new(
|
||||
self.server_cipher.clone(),
|
||||
channel_sender.clone(),
|
||||
config.server_address,
|
||||
config.token.clone(),
|
||||
config.device_id.clone(),
|
||||
config.name.clone(),
|
||||
config.password.is_some(),
|
||||
));
|
||||
let device_list: Arc<Mutex<(u16, Vec<PeerDeviceInfo>)>> =
|
||||
Arc::new(Mutex::new((response.epoch, response.device_info_list)));
|
||||
let peer_nat_info_map: Arc<DashMap<Ipv4Addr, NatInfo>> = Arc::new(DashMap::new());
|
||||
let connect_status = Arc::new(AtomicCell::new(ConnectStatus::Connected));
|
||||
|
||||
@@ -235,8 +277,14 @@ impl VntUtil {
|
||||
let ipv6_port = context.main_local_ipv6_port().unwrap_or(0);
|
||||
let ipv6_addr = crate::nat::local_ipv6_addr(ipv6_port);
|
||||
// NAT检测
|
||||
let nat_test = NatTest::new(config.stun_server.clone(), response.public_ip,
|
||||
response.public_port, local_ipv4_addr, ipv6_addr).await;
|
||||
let nat_test = NatTest::new(
|
||||
config.stun_server.clone(),
|
||||
response.public_ip,
|
||||
response.public_port,
|
||||
local_ipv4_addr,
|
||||
ipv6_addr,
|
||||
)
|
||||
.await;
|
||||
let in_external_route = if config.in_ips.is_empty() {
|
||||
None
|
||||
} else {
|
||||
@@ -245,7 +293,12 @@ impl VntUtil {
|
||||
let (tcp_proxy, udp_proxy, ip_proxy_map) = if config.out_ips.is_empty() {
|
||||
(None, None, None)
|
||||
} else {
|
||||
let (tcp_proxy, udp_proxy, ip_proxy_map) = crate::ip_proxy::init_proxy(channel_sender.clone(), current_device.clone(), client_cipher.clone()).await?;
|
||||
let (tcp_proxy, udp_proxy, ip_proxy_map) = crate::ip_proxy::init_proxy(
|
||||
channel_sender.clone(),
|
||||
current_device.clone(),
|
||||
client_cipher.clone(),
|
||||
)
|
||||
.await?;
|
||||
(Some(tcp_proxy), Some(udp_proxy), Some(ip_proxy_map))
|
||||
};
|
||||
let out_external_route = AllowExternalRoute::new(config.out_ips);
|
||||
@@ -257,31 +310,79 @@ impl VntUtil {
|
||||
};
|
||||
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
|
||||
if config.tap {
|
||||
tap_handler::start(vnt_status_manager.worker("tap_handler"), channel_sender.clone(), device_reader, device_writer.clone(),
|
||||
igmp_server.clone(), current_device.clone(), in_external_route, ip_proxy_map.clone(),
|
||||
client_cipher.clone(), self.server_cipher.clone(), config.parallel);
|
||||
tap_handler::start(
|
||||
vnt_status_manager.worker("tap_handler"),
|
||||
channel_sender.clone(),
|
||||
device_reader,
|
||||
device_writer.clone(),
|
||||
igmp_server.clone(),
|
||||
current_device.clone(),
|
||||
in_external_route,
|
||||
ip_proxy_map.clone(),
|
||||
client_cipher.clone(),
|
||||
self.server_cipher.clone(),
|
||||
config.parallel,
|
||||
);
|
||||
} else {
|
||||
tun_handler::start(vnt_status_manager.worker("tun_handler"), channel_sender.clone(), device_reader, device_writer.clone(),
|
||||
igmp_server.clone(), current_device.clone(), in_external_route, ip_proxy_map.clone(),
|
||||
client_cipher.clone(), self.server_cipher.clone(), config.parallel).await;
|
||||
tun_handler::start(
|
||||
vnt_status_manager.worker("tun_handler"),
|
||||
channel_sender.clone(),
|
||||
device_reader,
|
||||
device_writer.clone(),
|
||||
igmp_server.clone(),
|
||||
current_device.clone(),
|
||||
in_external_route,
|
||||
ip_proxy_map.clone(),
|
||||
client_cipher.clone(),
|
||||
self.server_cipher.clone(),
|
||||
config.parallel,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
#[cfg(any(target_os = "android"))]
|
||||
tun_handler::start(vnt_status_manager.worker("android tun_handler"), channel_sender.clone(), device_reader, device_writer.clone(),
|
||||
igmp_server.clone(), current_device.clone(), in_external_route, ip_proxy_map.clone(), client_cipher.clone(), self.server_cipher.clone(), config.parallel).await;
|
||||
tun_handler::start(
|
||||
vnt_status_manager.worker("android tun_handler"),
|
||||
channel_sender.clone(),
|
||||
device_reader,
|
||||
device_writer.clone(),
|
||||
igmp_server.clone(),
|
||||
current_device.clone(),
|
||||
in_external_route,
|
||||
ip_proxy_map.clone(),
|
||||
client_cipher.clone(),
|
||||
self.server_cipher.clone(),
|
||||
config.parallel,
|
||||
)
|
||||
.await;
|
||||
|
||||
//外部数据接收处理
|
||||
let channel_recv_handler = ChannelDataHandler::new(current_device.clone(), device_list.clone(),
|
||||
register.clone(), nat_test.clone(), igmp_server,
|
||||
device_writer.clone(), connect_status.clone(),
|
||||
peer_nat_info_map.clone(), ip_proxy_map, out_external_route,
|
||||
cone_sender, symmetric_sender, client_cipher.clone(),
|
||||
self.server_cipher.clone(), self.rsa_cipher.clone(), config.relay, config.token.clone());
|
||||
let channel_recv_handler = ChannelDataHandler::new(
|
||||
current_device.clone(),
|
||||
device_list.clone(),
|
||||
register.clone(),
|
||||
nat_test.clone(),
|
||||
igmp_server,
|
||||
device_writer.clone(),
|
||||
connect_status.clone(),
|
||||
peer_nat_info_map.clone(),
|
||||
ip_proxy_map,
|
||||
out_external_route,
|
||||
cone_sender,
|
||||
symmetric_sender,
|
||||
client_cipher.clone(),
|
||||
self.server_cipher.clone(),
|
||||
self.rsa_cipher.clone(),
|
||||
config.relay,
|
||||
config.token.clone(),
|
||||
);
|
||||
{
|
||||
let channel = Channel::new(context.clone(), channel_recv_handler);
|
||||
let channel_worker = vnt_status_manager.worker("channel_worker");
|
||||
let relay = config.relay;
|
||||
tokio::spawn(async move {
|
||||
channel.start(channel_worker, tcp, 14, 65, relay, config.parallel).await
|
||||
channel
|
||||
.start(channel_worker, tcp, 14, 65, relay, config.parallel)
|
||||
.await
|
||||
});
|
||||
}
|
||||
{
|
||||
@@ -289,16 +390,45 @@ impl VntUtil {
|
||||
let device_list = device_list.clone();
|
||||
let current_device = current_device.clone();
|
||||
// 定时心跳
|
||||
heartbeat_handler::start_heartbeat(vnt_status_manager.worker("heartbeat"), channel_sender.clone(), device_list.clone(),
|
||||
current_device.clone(), config.server_address_str, client_cipher.clone(), self.server_cipher.clone());
|
||||
heartbeat_handler::start_heartbeat(
|
||||
vnt_status_manager.worker("heartbeat"),
|
||||
channel_sender.clone(),
|
||||
device_list.clone(),
|
||||
current_device.clone(),
|
||||
config.server_address_str,
|
||||
client_cipher.clone(),
|
||||
self.server_cipher.clone(),
|
||||
);
|
||||
// 空闲检查
|
||||
heartbeat_handler::start_idle(vnt_status_manager.worker("idle"), idle, channel_sender.clone());
|
||||
heartbeat_handler::start_idle(
|
||||
vnt_status_manager.worker("idle"),
|
||||
idle,
|
||||
channel_sender.clone(),
|
||||
);
|
||||
if !config.relay {
|
||||
// 打洞处理
|
||||
punch_handler::start(vnt_status_manager.worker("cone_receiver"), cone_receiver, punch.clone(), current_device.clone(), client_cipher.clone());
|
||||
punch_handler::start(vnt_status_manager.worker("symmetric_receiver"), symmetric_receiver, punch, current_device.clone(), client_cipher.clone());
|
||||
tokio::spawn(punch_handler::start_punch(vnt_status_manager.worker("punch_handler"), nat_test,
|
||||
device_list, channel_sender, current_device, client_cipher.clone()));
|
||||
punch_handler::start(
|
||||
vnt_status_manager.worker("cone_receiver"),
|
||||
cone_receiver,
|
||||
punch.clone(),
|
||||
current_device.clone(),
|
||||
client_cipher.clone(),
|
||||
);
|
||||
punch_handler::start(
|
||||
vnt_status_manager.worker("symmetric_receiver"),
|
||||
symmetric_receiver,
|
||||
punch,
|
||||
current_device.clone(),
|
||||
client_cipher.clone(),
|
||||
);
|
||||
tokio::spawn(punch_handler::start_punch(
|
||||
vnt_status_manager.worker("punch_handler"),
|
||||
nat_test,
|
||||
device_list,
|
||||
channel_sender,
|
||||
current_device,
|
||||
client_cipher.clone(),
|
||||
));
|
||||
}
|
||||
}
|
||||
{
|
||||
@@ -367,8 +497,10 @@ impl Vnt {
|
||||
self.vnt_status_manager.stop_all();
|
||||
self.device_writer.close()?;
|
||||
let virtual_gateway = self.current_device.load().virtual_gateway;
|
||||
let _ = std::net::UdpSocket::bind("0.0.0.0:0")?.send_to(&[0],
|
||||
SocketAddr::V4(SocketAddrV4::new(virtual_gateway, 10000)));
|
||||
let _ = std::net::UdpSocket::bind("0.0.0.0:0")?.send_to(
|
||||
&[0],
|
||||
SocketAddr::V4(SocketAddrV4::new(virtual_gateway, 10000)),
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
pub async fn wait_stop(&mut self) {
|
||||
@@ -417,18 +549,28 @@ pub struct Config {
|
||||
pub finger: bool,
|
||||
}
|
||||
|
||||
|
||||
impl Config {
|
||||
pub fn new(tap: bool, token: String,
|
||||
device_id: String,
|
||||
name: String,
|
||||
server_address: SocketAddr,
|
||||
server_address_str: String,
|
||||
mut stun_server: Vec<String>,
|
||||
in_ips: Vec<(u32, u32, Ipv4Addr)>, out_ips: Vec<(u32, u32)>,
|
||||
password: Option<String>, simulate_multicast: bool, mtu: Option<u16>, tcp: bool,
|
||||
ip: Option<Ipv4Addr>,
|
||||
relay: bool, server_encrypt: bool, parallel: usize, cipher_model: CipherModel, finger: bool, ) -> Self {
|
||||
pub fn new(
|
||||
tap: bool,
|
||||
token: String,
|
||||
device_id: String,
|
||||
name: String,
|
||||
server_address: SocketAddr,
|
||||
server_address_str: String,
|
||||
mut stun_server: Vec<String>,
|
||||
in_ips: Vec<(u32, u32, Ipv4Addr)>,
|
||||
out_ips: Vec<(u32, u32)>,
|
||||
password: Option<String>,
|
||||
simulate_multicast: bool,
|
||||
mtu: Option<u16>,
|
||||
tcp: bool,
|
||||
ip: Option<Ipv4Addr>,
|
||||
relay: bool,
|
||||
server_encrypt: bool,
|
||||
parallel: usize,
|
||||
cipher_model: CipherModel,
|
||||
finger: bool,
|
||||
) -> Self {
|
||||
for x in stun_server.iter_mut() {
|
||||
if !x.contains(":") {
|
||||
x.push_str(":3478");
|
||||
@@ -456,4 +598,4 @@ impl Config {
|
||||
finger,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::util::wait::WaitGroup;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::watch;
|
||||
use tokio::sync::watch::{Receiver, Sender};
|
||||
use crate::util::wait::WaitGroup;
|
||||
|
||||
#[derive(Copy, Clone, Eq, PartialEq)]
|
||||
pub enum VntStatus {
|
||||
@@ -49,7 +49,9 @@ impl VntWorker {
|
||||
return;
|
||||
}
|
||||
}
|
||||
Err(_) => { return; }
|
||||
Err(_) => {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+16
-15
@@ -1,11 +1,11 @@
|
||||
use std::io;
|
||||
use std::ops::Deref;
|
||||
use std::time::Duration;
|
||||
use tokio::runtime::Runtime;
|
||||
use crate::cipher::RsaCipher;
|
||||
use crate::core::{Config, Vnt, VntUtil};
|
||||
use crate::handle::handshake_handler::HandshakeEnum;
|
||||
use crate::handle::registration_handler::{RegResponse, ReqEnum};
|
||||
use std::io;
|
||||
use std::ops::Deref;
|
||||
use std::time::Duration;
|
||||
use tokio::runtime::Runtime;
|
||||
|
||||
pub struct VntUtilSync {
|
||||
vnt_util: VntUtil,
|
||||
@@ -19,12 +19,11 @@ pub struct VntSync {
|
||||
|
||||
impl VntUtilSync {
|
||||
pub fn new(config: Config) -> io::Result<VntUtilSync> {
|
||||
let runtime = tokio::runtime::Builder::new_multi_thread().enable_all().build()?;
|
||||
let runtime = tokio::runtime::Builder::new_multi_thread()
|
||||
.enable_all()
|
||||
.build()?;
|
||||
let vnt_util = runtime.block_on(VntUtil::new(config))?;
|
||||
Ok(VntUtilSync {
|
||||
vnt_util,
|
||||
runtime,
|
||||
})
|
||||
Ok(VntUtilSync { vnt_util, runtime })
|
||||
}
|
||||
pub fn connect(&mut self) -> io::Result<()> {
|
||||
self.runtime.block_on(self.vnt_util.connect())
|
||||
@@ -51,13 +50,14 @@ impl VntUtilSync {
|
||||
let vnt = runtime.block_on(self.vnt_util.build())?;
|
||||
{
|
||||
let mut vnt = vnt.clone();
|
||||
std::thread::spawn(move || {
|
||||
runtime.block_on(vnt.wait_stop())
|
||||
});
|
||||
std::thread::spawn(move || runtime.block_on(vnt.wait_stop()));
|
||||
}
|
||||
Ok(VntSync {
|
||||
vnt,
|
||||
runtime: tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap(),
|
||||
runtime: tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.unwrap(),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -67,7 +67,8 @@ impl VntSync {
|
||||
self.runtime.block_on(self.vnt.wait_stop())
|
||||
}
|
||||
pub fn wait_stop_ms(&mut self, ms: u64) -> bool {
|
||||
self.runtime.block_on(self.vnt.wait_stop_ms(Duration::from_millis(ms)))
|
||||
self.runtime
|
||||
.block_on(self.vnt.wait_stop_ms(Duration::from_millis(ms)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,4 +78,4 @@ impl Deref for VntSync {
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.vnt
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ pub struct ExternalRoute {
|
||||
impl ExternalRoute {
|
||||
pub fn new(route_table: Vec<(u32, u32, Ipv4Addr)>) -> Self {
|
||||
Self {
|
||||
route_table: Arc::new(route_table)
|
||||
route_table: Arc::new(route_table),
|
||||
}
|
||||
}
|
||||
pub fn route(&self, ip: &Ipv4Addr) -> Option<Ipv4Addr> {
|
||||
@@ -33,7 +33,7 @@ pub struct AllowExternalRoute {
|
||||
impl AllowExternalRoute {
|
||||
pub fn new(route_table: Vec<(u32, u32)>) -> Self {
|
||||
Self {
|
||||
route_table: Arc::new(route_table)
|
||||
route_table: Arc::new(route_table),
|
||||
}
|
||||
}
|
||||
pub fn allow(&self, ip: &Ipv4Addr) -> bool {
|
||||
@@ -45,4 +45,4 @@ impl AllowExternalRoute {
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,9 +8,8 @@ use tokio::net::{TcpStream, UdpSocket};
|
||||
use crate::channel::channel::Context;
|
||||
use crate::cipher::{Cipher, RsaCipher};
|
||||
use crate::proto::message::{HandshakeRequest, HandshakeResponse, SecretHandshakeRequest};
|
||||
use crate::protocol::{MAX_TTL, NetPacket, Protocol, service_packet, Version};
|
||||
use crate::protocol::body::ENCRYPTION_RESERVED;
|
||||
|
||||
use crate::protocol::{service_packet, NetPacket, Protocol, Version, MAX_TTL};
|
||||
|
||||
pub enum HandshakeEnum {
|
||||
NotSecret,
|
||||
@@ -36,7 +35,11 @@ fn handshake_request_packet(secret: bool) -> crate::Result<NetPacket<Vec<u8>>> {
|
||||
Ok(net_packet)
|
||||
}
|
||||
|
||||
fn secret_handshake_request_packet(rsa_cipher: &RsaCipher, token: String, key: &[u8]) -> crate::Result<NetPacket<Vec<u8>>> {
|
||||
fn secret_handshake_request_packet(
|
||||
rsa_cipher: &RsaCipher,
|
||||
token: String,
|
||||
key: &[u8],
|
||||
) -> crate::Result<NetPacket<Vec<u8>>> {
|
||||
let mut request = SecretHandshakeRequest::new();
|
||||
request.token = token;
|
||||
request.key = key.to_vec();
|
||||
@@ -52,16 +55,25 @@ fn secret_handshake_request_packet(rsa_cipher: &RsaCipher, token: String, key: &
|
||||
}
|
||||
|
||||
/// 第一次握手,拿到公钥
|
||||
pub async fn handshake(main_channel: &UdpSocket, main_tcp_channel: Option<&mut TcpStream>,
|
||||
server_address: SocketAddr, secret: bool) -> Result<Option<RsaCipher>, HandshakeEnum> {
|
||||
pub async fn handshake(
|
||||
main_channel: &UdpSocket,
|
||||
main_tcp_channel: Option<&mut TcpStream>,
|
||||
server_address: SocketAddr,
|
||||
secret: bool,
|
||||
) -> Result<Option<RsaCipher>, HandshakeEnum> {
|
||||
let request_packet = handshake_request_packet(secret).unwrap();
|
||||
let send_buf = request_packet.buffer();
|
||||
let mut recv_buf = [0u8; 10240];
|
||||
let len = send_recv(main_channel, main_tcp_channel, server_address, send_buf, &mut recv_buf).await?;
|
||||
let len = send_recv(
|
||||
main_channel,
|
||||
main_tcp_channel,
|
||||
server_address,
|
||||
send_buf,
|
||||
&mut recv_buf,
|
||||
)
|
||||
.await?;
|
||||
let net_packet = match NetPacket::new(&recv_buf[..len]) {
|
||||
Ok(net_packet) => {
|
||||
net_packet
|
||||
}
|
||||
Ok(net_packet) => net_packet,
|
||||
Err(e) => {
|
||||
return Err(HandshakeEnum::Other(format!("net_packet {}", e)));
|
||||
}
|
||||
@@ -77,23 +89,31 @@ pub async fn handshake(main_channel: &UdpSocket, main_tcp_channel: Option<&mut T
|
||||
return Err(HandshakeEnum::NotSecret);
|
||||
}
|
||||
if secret {
|
||||
//转换公钥
|
||||
//转换公钥
|
||||
match RsaCipher::new(&response.public_key) {
|
||||
Ok(rsa) => {
|
||||
match rsa.finger() {
|
||||
Ok(finger) => {
|
||||
if finger != response.key_finger {
|
||||
return Err(HandshakeEnum::Other("finger error".to_string()));
|
||||
return Err(HandshakeEnum::Other(
|
||||
"finger error".to_string(),
|
||||
));
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(HandshakeEnum::Other(format!("finger {}", e)));
|
||||
return Err(HandshakeEnum::Other(format!(
|
||||
"finger {}",
|
||||
e
|
||||
)));
|
||||
}
|
||||
}
|
||||
Ok(Some(rsa))
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(HandshakeEnum::Other(format!("RsaCipher {}", e)));
|
||||
return Err(HandshakeEnum::Other(format!(
|
||||
"RsaCipher {}",
|
||||
e
|
||||
)));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -116,8 +136,13 @@ pub async fn handshake(main_channel: &UdpSocket, main_tcp_channel: Option<&mut T
|
||||
}
|
||||
}
|
||||
|
||||
async fn send_recv(main_channel: &UdpSocket, main_tcp_channel: Option<&mut TcpStream>,
|
||||
server_address: SocketAddr, send_buf: &[u8], recv_buf: &mut [u8]) -> Result<usize, HandshakeEnum> {
|
||||
async fn send_recv(
|
||||
main_channel: &UdpSocket,
|
||||
main_tcp_channel: Option<&mut TcpStream>,
|
||||
server_address: SocketAddr,
|
||||
send_buf: &[u8],
|
||||
recv_buf: &mut [u8],
|
||||
) -> Result<usize, HandshakeEnum> {
|
||||
if let Some(main_tcp_channel) = main_tcp_channel {
|
||||
let mut head = [0; 4];
|
||||
let len = send_buf.len();
|
||||
@@ -144,20 +169,20 @@ async fn send_recv(main_channel: &UdpSocket, main_tcp_channel: Option<&mut TcpSt
|
||||
if let Err(e) = main_channel.send_to(send_buf, server_address).await {
|
||||
return Err(HandshakeEnum::Other(format!("send error:{}", e)));
|
||||
}
|
||||
match tokio::time::timeout(Duration::from_millis(300), main_channel.recv_from(recv_buf)).await {
|
||||
Ok(rs) => {
|
||||
match rs {
|
||||
Ok((len, addr)) => {
|
||||
if server_address != addr {
|
||||
return Err(HandshakeEnum::Other(format!("invalid data,from {}", addr)));
|
||||
}
|
||||
Ok(len)
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(HandshakeEnum::Other(format!("receiver error:{}", e)));
|
||||
match tokio::time::timeout(Duration::from_millis(300), main_channel.recv_from(recv_buf))
|
||||
.await
|
||||
{
|
||||
Ok(rs) => match rs {
|
||||
Ok((len, addr)) => {
|
||||
if server_address != addr {
|
||||
return Err(HandshakeEnum::Other(format!("invalid data,from {}", addr)));
|
||||
}
|
||||
Ok(len)
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(HandshakeEnum::Other(format!("receiver error:{}", e)));
|
||||
}
|
||||
},
|
||||
Err(_) => {
|
||||
return Err(HandshakeEnum::Timeout);
|
||||
}
|
||||
@@ -166,48 +191,72 @@ async fn send_recv(main_channel: &UdpSocket, main_tcp_channel: Option<&mut TcpSt
|
||||
}
|
||||
|
||||
/// 第二次握手,同步对称密钥,后续将使用对称加密
|
||||
pub async fn secret_handshake(main_channel: &UdpSocket, main_tcp_channel: Option<&mut TcpStream>,
|
||||
server_address: SocketAddr, rsa_cipher: &RsaCipher, server_cipher: &Cipher, token: String)
|
||||
-> Result<(), HandshakeEnum> {
|
||||
let secret_packet = match secret_handshake_request_packet(rsa_cipher, token, server_cipher.key().unwrap()) {
|
||||
Ok(secret_packet) => {
|
||||
secret_packet
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(HandshakeEnum::Other(format!("secret_handshake_request_packet {}", e)));
|
||||
}
|
||||
};
|
||||
pub async fn secret_handshake(
|
||||
main_channel: &UdpSocket,
|
||||
main_tcp_channel: Option<&mut TcpStream>,
|
||||
server_address: SocketAddr,
|
||||
rsa_cipher: &RsaCipher,
|
||||
server_cipher: &Cipher,
|
||||
token: String,
|
||||
) -> Result<(), HandshakeEnum> {
|
||||
let secret_packet =
|
||||
match secret_handshake_request_packet(rsa_cipher, token, server_cipher.key().unwrap()) {
|
||||
Ok(secret_packet) => secret_packet,
|
||||
Err(e) => {
|
||||
return Err(HandshakeEnum::Other(format!(
|
||||
"secret_handshake_request_packet {}",
|
||||
e
|
||||
)));
|
||||
}
|
||||
};
|
||||
let send_buf = secret_packet.buffer();
|
||||
let mut recv_buf = [0u8; 10240];
|
||||
let len = send_recv(main_channel, main_tcp_channel, server_address, send_buf, &mut recv_buf).await?;
|
||||
let len = send_recv(
|
||||
main_channel,
|
||||
main_tcp_channel,
|
||||
server_address,
|
||||
send_buf,
|
||||
&mut recv_buf,
|
||||
)
|
||||
.await?;
|
||||
let mut net_packet = match NetPacket::new(&mut recv_buf[..len]) {
|
||||
Ok(net_packet) => { net_packet }
|
||||
Ok(net_packet) => net_packet,
|
||||
Err(e) => {
|
||||
return Err(HandshakeEnum::Other(format!("secret_net_packet {}", e)));
|
||||
}
|
||||
};
|
||||
match server_cipher.decrypt_ipv4(&mut net_packet) {
|
||||
Ok(_) => {
|
||||
if net_packet.is_gateway() && net_packet.protocol() == Protocol::Service
|
||||
&& service_packet::Protocol::from(net_packet.transport_protocol()) ==
|
||||
service_packet::Protocol::SecretHandshakeResponse {
|
||||
if net_packet.is_gateway()
|
||||
&& net_packet.protocol() == Protocol::Service
|
||||
&& service_packet::Protocol::from(net_packet.transport_protocol())
|
||||
== service_packet::Protocol::SecretHandshakeResponse
|
||||
{
|
||||
Ok(())
|
||||
} else {
|
||||
Err(HandshakeEnum::Other("not match".to_string()))
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
Err(HandshakeEnum::Other(format!("decrypt_ipv4 {}", e)))
|
||||
}
|
||||
Err(e) => Err(HandshakeEnum::Other(format!("decrypt_ipv4 {}", e))),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn secret_handshake_req(context: &Context,
|
||||
server_address: SocketAddr, rsa_cipher: &RsaCipher, server_cipher: &Cipher, token: String, ) -> crate::Result<()> {
|
||||
let secret_packet = secret_handshake_request_packet(rsa_cipher, token, server_cipher.key().unwrap())?;
|
||||
context.send_main(secret_packet.buffer(), server_address).await?;
|
||||
if context.is_main_tcp(){
|
||||
context.send_main_udp(secret_packet.buffer(),server_address).await?;
|
||||
pub async fn secret_handshake_req(
|
||||
context: &Context,
|
||||
server_address: SocketAddr,
|
||||
rsa_cipher: &RsaCipher,
|
||||
server_cipher: &Cipher,
|
||||
token: String,
|
||||
) -> crate::Result<()> {
|
||||
let secret_packet =
|
||||
secret_handshake_request_packet(rsa_cipher, token, server_cipher.key().unwrap())?;
|
||||
context
|
||||
.send_main(secret_packet.buffer(), server_address)
|
||||
.await?;
|
||||
if context.is_main_tcp() {
|
||||
context
|
||||
.send_main_udp(secret_packet.buffer(), server_address)
|
||||
.await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,22 +1,21 @@
|
||||
use std::io;
|
||||
use std::net::{Ipv4Addr, ToSocketAddrs};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use std::io;
|
||||
|
||||
use crate::channel::idle::Idle;
|
||||
use crate::channel::sender::ChannelSender;
|
||||
use crate::channel::Route;
|
||||
use crate::cipher::Cipher;
|
||||
use crate::core::status::VntWorker;
|
||||
use crossbeam_utils::atomic::AtomicCell;
|
||||
use parking_lot::Mutex;
|
||||
use rand::prelude::SliceRandom;
|
||||
use crate::channel::idle::Idle;
|
||||
use crate::channel::Route;
|
||||
use crate::channel::sender::ChannelSender;
|
||||
use crate::cipher::Cipher;
|
||||
use crate::core::status::VntWorker;
|
||||
|
||||
|
||||
use crate::handle::{CurrentDeviceInfo, PeerDeviceInfo};
|
||||
use crate::protocol::control_packet::PingPacket;
|
||||
use crate::protocol::{control_packet, MAX_TTL, NetPacket, Protocol, Version};
|
||||
use crate::protocol::body::ENCRYPTION_RESERVED;
|
||||
use crate::protocol::control_packet::PingPacket;
|
||||
use crate::protocol::{control_packet, NetPacket, Protocol, Version, MAX_TTL};
|
||||
|
||||
pub fn start_idle(mut worker: VntWorker, idle: Idle, sender: ChannelSender) {
|
||||
tokio::spawn(async move {
|
||||
@@ -38,11 +37,7 @@ async fn start_idle_(idle: Idle, sender: ChannelSender) -> io::Result<()> {
|
||||
log::info!("启动空闲检查任务");
|
||||
loop {
|
||||
let (peer_ip, route) = idle.next_idle().await?;
|
||||
log::info!(
|
||||
"peer_ip:{:?},route:{:?}",
|
||||
peer_ip,
|
||||
route
|
||||
);
|
||||
log::info!("peer_ip:{:?},route:{:?}", peer_ip, route);
|
||||
sender.remove_route(&peer_ip, route);
|
||||
}
|
||||
}
|
||||
@@ -71,8 +66,15 @@ pub fn start_heartbeat(
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
fn heartbeat_packet(ttl: u8, device_list: &Mutex<(u16, Vec<PeerDeviceInfo>)>, client_cipher: &Cipher, server_cipher: &Cipher, gateway: bool, src: Ipv4Addr, dest: Ipv4Addr) -> NetPacket<[u8; 48]> {
|
||||
fn heartbeat_packet(
|
||||
ttl: u8,
|
||||
device_list: &Mutex<(u16, Vec<PeerDeviceInfo>)>,
|
||||
client_cipher: &Cipher,
|
||||
server_cipher: &Cipher,
|
||||
gateway: bool,
|
||||
src: Ipv4Addr,
|
||||
dest: Ipv4Addr,
|
||||
) -> NetPacket<[u8; 48]> {
|
||||
let mut net_packet = NetPacket::new_encrypt([0u8; 12 + 4 + ENCRYPTION_RESERVED]).unwrap();
|
||||
net_packet.set_version(Version::V1);
|
||||
net_packet.set_protocol(Protocol::Control);
|
||||
@@ -116,14 +118,14 @@ async fn start_heartbeat_(
|
||||
packet.set_version(Version::V1);
|
||||
packet.set_gateway_flag(true);
|
||||
packet.set_protocol(Protocol::Control);
|
||||
packet.set_transport_protocol(
|
||||
control_packet::Protocol::AddrRequest.into(),
|
||||
);
|
||||
packet.set_transport_protocol(control_packet::Protocol::AddrRequest.into());
|
||||
packet.first_set_ttl(MAX_TTL);
|
||||
packet.set_source(current_dev.virtual_ip());
|
||||
packet.set_destination(current_dev.virtual_gateway);
|
||||
server_cipher.encrypt_ipv4(&mut packet)?;
|
||||
let _ = sender.send_main_udp(packet.buffer(), current_dev.connect_server).await;
|
||||
let _ = sender
|
||||
.send_main_udp(packet.buffer(), current_dev.connect_server)
|
||||
.await;
|
||||
}
|
||||
if count % 20 == 19 {
|
||||
if let Ok(mut addr) = server_address_str.to_socket_addrs() {
|
||||
@@ -131,7 +133,11 @@ async fn start_heartbeat_(
|
||||
if addr != current_dev.connect_server {
|
||||
let mut tmp = current_dev.clone();
|
||||
tmp.connect_server = addr;
|
||||
log::info!("服务端地址变化,旧地址:{},新地址:{}",current_dev.connect_server,addr);
|
||||
log::info!(
|
||||
"服务端地址变化,旧地址:{},新地址:{}",
|
||||
current_dev.connect_server,
|
||||
addr
|
||||
);
|
||||
if current_device.compare_exchange(current_dev, tmp).is_ok() {
|
||||
current_dev.connect_server = addr;
|
||||
}
|
||||
@@ -140,14 +146,20 @@ async fn start_heartbeat_(
|
||||
}
|
||||
}
|
||||
let src = current_dev.virtual_ip();
|
||||
let server_packet = heartbeat_packet(MAX_TTL, &device_list, &client_cipher, &server_cipher, true, src, current_dev.virtual_gateway);
|
||||
if let Err(e) = sender.send_main(server_packet.buffer(), current_dev.connect_server).await
|
||||
let server_packet = heartbeat_packet(
|
||||
MAX_TTL,
|
||||
&device_list,
|
||||
&client_cipher,
|
||||
&server_cipher,
|
||||
true,
|
||||
src,
|
||||
current_dev.virtual_gateway,
|
||||
);
|
||||
if let Err(e) = sender
|
||||
.send_main(server_packet.buffer(), current_dev.connect_server)
|
||||
.await
|
||||
{
|
||||
log::warn!(
|
||||
"connect_server:{:?},e:{:?}",
|
||||
current_dev.connect_server,
|
||||
e
|
||||
);
|
||||
log::warn!("connect_server:{:?},e:{:?}", current_dev.connect_server, e);
|
||||
}
|
||||
if count < 7 || count % 7 == 0 {
|
||||
let mut route_list: Option<Vec<(Ipv4Addr, Vec<Route>)>> = None;
|
||||
@@ -156,15 +168,27 @@ async fn start_heartbeat_(
|
||||
if peer.virtual_ip == current_dev.virtual_ip {
|
||||
continue;
|
||||
}
|
||||
let client_packet = heartbeat_packet(MAX_TTL, &device_list, &client_cipher, &server_cipher, false, src, peer.virtual_ip);
|
||||
let client_packet = heartbeat_packet(
|
||||
MAX_TTL,
|
||||
&device_list,
|
||||
&client_cipher,
|
||||
&server_cipher,
|
||||
false,
|
||||
src,
|
||||
peer.virtual_ip,
|
||||
);
|
||||
if let Some(route) = sender.route_one(&peer.virtual_ip) {
|
||||
let _ = sender.send_by_key(client_packet.buffer(), &route.route_key()).await;
|
||||
let _ = sender
|
||||
.send_by_key(client_packet.buffer(), &route.route_key())
|
||||
.await;
|
||||
if route.is_p2p() {
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
//没有直连路由则发送到网关
|
||||
let _ = sender.send_main(client_packet.buffer(), current_dev.connect_server).await;
|
||||
let _ = sender
|
||||
.send_main(client_packet.buffer(), current_dev.connect_server)
|
||||
.await;
|
||||
}
|
||||
|
||||
//再随机发送到其他地址,看有没有客户端符合转发条件
|
||||
@@ -177,7 +201,8 @@ async fn start_heartbeat_(
|
||||
'a: for (peer_ip, route_list) in route_list.iter() {
|
||||
for route in route_list {
|
||||
if peer_ip != &peer.virtual_ip && route.is_p2p() {
|
||||
let _ = sender.try_send_by_key(client_packet.buffer(), &route.route_key());
|
||||
let _ =
|
||||
sender.try_send_by_key(client_packet.buffer(), &route.route_key());
|
||||
num += 1;
|
||||
break;
|
||||
}
|
||||
@@ -193,9 +218,20 @@ async fn start_heartbeat_(
|
||||
if peer_ip == ¤t_dev.virtual_gateway {
|
||||
continue;
|
||||
}
|
||||
let client_packet = heartbeat_packet(MAX_TTL, &device_list, &client_cipher, &server_cipher, false, src, *peer_ip);
|
||||
let client_packet = heartbeat_packet(
|
||||
MAX_TTL,
|
||||
&device_list,
|
||||
&client_cipher,
|
||||
&server_cipher,
|
||||
false,
|
||||
src,
|
||||
*peer_ip,
|
||||
);
|
||||
for route in route_list {
|
||||
if let Err(e) = sender.send_by_key(client_packet.buffer(), &route.route_key()).await {
|
||||
if let Err(e) = sender
|
||||
.send_by_key(client_packet.buffer(), &route.route_key())
|
||||
.await
|
||||
{
|
||||
log::warn!("peer_ip:{:?},route:{:?},e:{:?}", peer_ip, route, e);
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(2)).await;
|
||||
|
||||
@@ -31,17 +31,17 @@ pub struct PeerDeviceInfo {
|
||||
}
|
||||
|
||||
impl PeerDeviceInfo {
|
||||
pub fn new(virtual_ip: Ipv4Addr, name: String, status: u8,client_secret: bool) -> Self {
|
||||
pub fn new(virtual_ip: Ipv4Addr, name: String, status: u8, client_secret: bool) -> Self {
|
||||
Self {
|
||||
virtual_ip,
|
||||
name,
|
||||
status: PeerDeviceStatus::from(status),
|
||||
client_secret
|
||||
client_secret,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq,Ord, PartialOrd)]
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
|
||||
pub enum PeerDeviceStatus {
|
||||
Online,
|
||||
Offline,
|
||||
@@ -82,7 +82,6 @@ pub struct CurrentDeviceInfo {
|
||||
pub broadcast_address: Ipv4Addr,
|
||||
//链接的服务器地址
|
||||
pub connect_server: SocketAddr,
|
||||
|
||||
}
|
||||
|
||||
impl CurrentDeviceInfo {
|
||||
|
||||
@@ -1,25 +1,29 @@
|
||||
use crate::channel::punch::{NatInfo, Punch};
|
||||
use crate::channel::sender::ChannelSender;
|
||||
use crate::cipher::Cipher;
|
||||
use crate::core::status::VntWorker;
|
||||
use crate::handle::{CurrentDeviceInfo, PeerDeviceInfo};
|
||||
use crate::nat::NatTest;
|
||||
use crate::proto::message::{PunchInfo, PunchNatType};
|
||||
use crate::protocol::body::ENCRYPTION_RESERVED;
|
||||
use crate::protocol::{control_packet, other_turn_packet, NetPacket, Protocol, Version, MAX_TTL};
|
||||
use crossbeam_utils::atomic::AtomicCell;
|
||||
use parking_lot::Mutex;
|
||||
use protobuf::Message;
|
||||
use rand::prelude::SliceRandom;
|
||||
use std::io;
|
||||
use std::net::Ipv4Addr;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use std::io;
|
||||
use tokio::sync::mpsc::Receiver;
|
||||
use crate::channel::punch::{NatInfo, Punch};
|
||||
use crate::channel::sender::ChannelSender;
|
||||
use crate::cipher::Cipher;
|
||||
use crate::core::status::VntWorker;
|
||||
use crate::protocol::body::ENCRYPTION_RESERVED;
|
||||
|
||||
pub fn start(mut worker: VntWorker, receiver: Receiver<(Ipv4Addr, NatInfo)>,
|
||||
punch: Punch, current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
|
||||
client_cipher: Cipher, ) {
|
||||
pub fn start(
|
||||
mut worker: VntWorker,
|
||||
receiver: Receiver<(Ipv4Addr, NatInfo)>,
|
||||
punch: Punch,
|
||||
current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
|
||||
client_cipher: Cipher,
|
||||
) {
|
||||
tokio::spawn(async move {
|
||||
tokio::select! {
|
||||
_=start0(receiver, punch, current_device,client_cipher)=>{}
|
||||
@@ -31,12 +35,23 @@ pub fn start(mut worker: VntWorker, receiver: Receiver<(Ipv4Addr, NatInfo)>,
|
||||
});
|
||||
}
|
||||
|
||||
pub async fn start0(mut receiver: Receiver<(Ipv4Addr, NatInfo)>,
|
||||
mut punch: Punch, current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
|
||||
client_cipher: Cipher, ) {
|
||||
pub async fn start0(
|
||||
mut receiver: Receiver<(Ipv4Addr, NatInfo)>,
|
||||
mut punch: Punch,
|
||||
current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
|
||||
client_cipher: Cipher,
|
||||
) {
|
||||
log::info!("启动打洞任务");
|
||||
while let Some((peer_ip, nat_info)) = receiver.recv().await {
|
||||
if let Err(e) = start_(&client_cipher, &mut punch, ¤t_device, peer_ip, nat_info).await {
|
||||
if let Err(e) = start_(
|
||||
&client_cipher,
|
||||
&mut punch,
|
||||
¤t_device,
|
||||
peer_ip,
|
||||
nat_info,
|
||||
)
|
||||
.await
|
||||
{
|
||||
log::warn!("网络打洞异常 {:?}", e);
|
||||
}
|
||||
}
|
||||
@@ -115,8 +130,15 @@ async fn start_punch_(
|
||||
if count > 2 {
|
||||
break;
|
||||
}
|
||||
let packet = punch_packet(client_cipher, current_device.virtual_ip(), &nat_info, info.virtual_ip)?;
|
||||
let _ = sender.send_main(packet.buffer(), current_device.connect_server).await;
|
||||
let packet = punch_packet(
|
||||
client_cipher,
|
||||
current_device.virtual_ip(),
|
||||
&nat_info,
|
||||
info.virtual_ip,
|
||||
)?;
|
||||
let _ = sender
|
||||
.send_main(packet.buffer(), current_device.connect_server)
|
||||
.await;
|
||||
}
|
||||
tokio::time::sleep(sleep_time).await;
|
||||
Ok(())
|
||||
|
||||
+296
-125
@@ -7,29 +7,32 @@ use parking_lot::Mutex;
|
||||
use protobuf::Message;
|
||||
use tokio::sync::mpsc::Sender;
|
||||
|
||||
use packet::icmp::{icmp, Kind};
|
||||
use packet::icmp::icmp::HeaderOther;
|
||||
use packet::icmp::{icmp, Kind};
|
||||
use packet::ip::ipv4;
|
||||
use packet::ip::ipv4::packet::IpV4Packet;
|
||||
|
||||
use crate::channel::{Route, RouteKey};
|
||||
use crate::channel::channel::Context;
|
||||
use crate::channel::punch::{NatInfo, NatType};
|
||||
use crate::channel::{Route, RouteKey};
|
||||
use crate::cipher::{Cipher, RsaCipher};
|
||||
use crate::error::Error;
|
||||
use crate::external_route::AllowExternalRoute;
|
||||
use crate::handle::{ConnectStatus, CurrentDeviceInfo, PeerDeviceInfo, PeerDeviceStatus};
|
||||
use crate::handle::handshake_handler::secret_handshake_req;
|
||||
use crate::handle::registration_handler::Register;
|
||||
use crate::handle::{ConnectStatus, CurrentDeviceInfo, PeerDeviceInfo, PeerDeviceStatus};
|
||||
use crate::igmp_server::IgmpServer;
|
||||
use crate::ip_proxy::IpProxyMap;
|
||||
use crate::nat;
|
||||
use crate::nat::NatTest;
|
||||
use crate::proto::message::{DeviceList, PunchInfo, PunchNatType, RegistrationResponse};
|
||||
use crate::protocol::{control_packet, ip_turn_packet, MAX_TTL, NetPacket, other_turn_packet, Protocol, service_packet, Version};
|
||||
use crate::protocol::body::ENCRYPTION_RESERVED;
|
||||
use crate::protocol::control_packet::ControlPacket;
|
||||
use crate::protocol::error_packet::InErrorPacket;
|
||||
use crate::protocol::{
|
||||
control_packet, ip_turn_packet, other_turn_packet, service_packet, NetPacket, Protocol,
|
||||
Version, MAX_TTL,
|
||||
};
|
||||
use crate::tun_tap_device::DeviceWriter;
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -54,22 +57,25 @@ pub struct ChannelDataHandler {
|
||||
}
|
||||
|
||||
impl ChannelDataHandler {
|
||||
pub fn new(current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
|
||||
device_list: Arc<Mutex<(u16, Vec<PeerDeviceInfo>)>>,
|
||||
register: Arc<Register>,
|
||||
nat_test: NatTest,
|
||||
igmp_server: Option<IgmpServer>,
|
||||
device_writer: DeviceWriter,
|
||||
connect_status: Arc<AtomicCell<ConnectStatus>>,
|
||||
peer_nat_info_map: Arc<DashMap<Ipv4Addr, NatInfo>>,
|
||||
ip_proxy_map: Option<IpProxyMap>,
|
||||
out_external_route: AllowExternalRoute,
|
||||
cone_sender: Sender<(Ipv4Addr, NatInfo)>,
|
||||
symmetric_sender: Sender<(Ipv4Addr, NatInfo)>,
|
||||
client_cipher: Cipher,
|
||||
server_cipher: Cipher,
|
||||
rsa_cipher: Option<RsaCipher>,
|
||||
relay: bool, token: String, ) -> Self {
|
||||
pub fn new(
|
||||
current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
|
||||
device_list: Arc<Mutex<(u16, Vec<PeerDeviceInfo>)>>,
|
||||
register: Arc<Register>,
|
||||
nat_test: NatTest,
|
||||
igmp_server: Option<IgmpServer>,
|
||||
device_writer: DeviceWriter,
|
||||
connect_status: Arc<AtomicCell<ConnectStatus>>,
|
||||
peer_nat_info_map: Arc<DashMap<Ipv4Addr, NatInfo>>,
|
||||
ip_proxy_map: Option<IpProxyMap>,
|
||||
out_external_route: AllowExternalRoute,
|
||||
cone_sender: Sender<(Ipv4Addr, NatInfo)>,
|
||||
symmetric_sender: Sender<(Ipv4Addr, NatInfo)>,
|
||||
client_cipher: Cipher,
|
||||
server_cipher: Cipher,
|
||||
rsa_cipher: Option<RsaCipher>,
|
||||
relay: bool,
|
||||
token: String,
|
||||
) -> Self {
|
||||
Self {
|
||||
current_device,
|
||||
device_list,
|
||||
@@ -92,18 +98,29 @@ impl ChannelDataHandler {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl ChannelDataHandler {
|
||||
pub async fn handle(&self, buf: &mut [u8], start: usize, end: usize, route_key: RouteKey, context: &Context) {
|
||||
pub async fn handle(
|
||||
&self,
|
||||
buf: &mut [u8],
|
||||
start: usize,
|
||||
end: usize,
|
||||
route_key: RouteKey,
|
||||
context: &Context,
|
||||
) {
|
||||
assert_eq!(start, 14);
|
||||
match self.handle0(&mut buf[..end], &route_key, context).await {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
log::warn!("{:?}",e);
|
||||
log::warn!("{:?}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
async fn handle0(&self, buf: &mut [u8], route_key: &RouteKey, context: &Context) -> crate::Result<()> {
|
||||
async fn handle0(
|
||||
&self,
|
||||
buf: &mut [u8],
|
||||
route_key: &RouteKey,
|
||||
context: &Context,
|
||||
) -> crate::Result<()> {
|
||||
let mut net_packet = NetPacket::new(&mut buf[14..])?;
|
||||
if net_packet.ttl() == 0 || net_packet.source_ttl() < net_packet.ttl() {
|
||||
return Ok(());
|
||||
@@ -112,9 +129,13 @@ impl ChannelDataHandler {
|
||||
context.update_read_time(&source, route_key);
|
||||
let current_device = self.current_device.load();
|
||||
let destination = net_packet.destination();
|
||||
let not_broadcast = !destination.is_broadcast() && !destination.is_multicast() && destination != current_device.broadcast_address;
|
||||
let not_broadcast = !destination.is_broadcast()
|
||||
&& !destination.is_multicast()
|
||||
&& destination != current_device.broadcast_address;
|
||||
if current_device.virtual_ip() != destination
|
||||
&& not_broadcast && !destination.is_unspecified() {
|
||||
&& not_broadcast
|
||||
&& !destination.is_unspecified()
|
||||
{
|
||||
//校验指纹,不需要解密
|
||||
self.client_cipher.check_finger(&net_packet)?;
|
||||
net_packet.set_ttl(net_packet.ttl() - 1);
|
||||
@@ -123,26 +144,42 @@ impl ChannelDataHandler {
|
||||
// 转发
|
||||
if let Some(route) = context.route_one(&destination) {
|
||||
if route.metric <= net_packet.ttl() {
|
||||
context.send_by_key(net_packet.buffer(), &route.route_key()).await?;
|
||||
context
|
||||
.send_by_key(net_packet.buffer(), &route.route_key())
|
||||
.await?;
|
||||
}
|
||||
} else if (ttl > 1 || destination == current_device.virtual_gateway())
|
||||
&& source != current_device.virtual_gateway() {
|
||||
&& source != current_device.virtual_gateway()
|
||||
{
|
||||
//网关默认要转发一次,生存时间不够的发到网关也会被丢弃
|
||||
context.send_main(net_packet.buffer(), current_device.connect_server).await?;
|
||||
context
|
||||
.send_main(net_packet.buffer(), current_device.connect_server)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
if net_packet.is_gateway() {
|
||||
if net_packet.protocol() == Protocol::Error && net_packet.transport_protocol() == crate::protocol::error_packet::Protocol::NoKey.into() {
|
||||
if net_packet.protocol() == Protocol::Error
|
||||
&& net_packet.transport_protocol()
|
||||
== crate::protocol::error_packet::Protocol::NoKey.into()
|
||||
{
|
||||
if let Some(rsa_cipher) = &self.rsa_cipher {
|
||||
secret_handshake_req(context, current_device.connect_server, rsa_cipher, &self.server_cipher, self.token.clone()).await?;
|
||||
secret_handshake_req(
|
||||
context,
|
||||
current_device.connect_server,
|
||||
rsa_cipher,
|
||||
&self.server_cipher,
|
||||
self.token.clone(),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
} else {
|
||||
//服务端解密
|
||||
self.server_cipher.decrypt_ipv4(&mut net_packet)?;
|
||||
let data_len = net_packet.data_len();
|
||||
self.server_packet_handle(context, current_device, buf, data_len, route_key).await?;
|
||||
self.server_packet_handle(context, current_device, buf, data_len, route_key)
|
||||
.await?;
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
@@ -161,7 +198,8 @@ impl ChannelDataHandler {
|
||||
}
|
||||
ipv4::protocol::Protocol::Icmp => {
|
||||
if ipv4.destination_ip() == destination {
|
||||
let mut icmp_packet = icmp::IcmpPacket::new(ipv4.payload_mut())?;
|
||||
let mut icmp_packet =
|
||||
icmp::IcmpPacket::new(ipv4.payload_mut())?;
|
||||
if icmp_packet.kind() == Kind::EchoRequest {
|
||||
//开启ping
|
||||
icmp_packet.set_kind(Kind::EchoReply);
|
||||
@@ -187,56 +225,81 @@ impl ChannelDataHandler {
|
||||
ipv4::protocol::Protocol::Tcp => {
|
||||
let dest_ip = ipv4.destination_ip();
|
||||
//转发到代理目标地址
|
||||
let mut tcp_packet = packet::tcp::tcp::TcpPacket::new(source, destination, ipv4.payload_mut())?;
|
||||
let mut tcp_packet = packet::tcp::tcp::TcpPacket::new(
|
||||
source,
|
||||
destination,
|
||||
ipv4.payload_mut(),
|
||||
)?;
|
||||
let source_port = tcp_packet.source_port();
|
||||
let dest_port = tcp_packet.destination_port();
|
||||
tcp_packet.set_destination_port(ip_proxy_map.tcp_proxy_port);
|
||||
tcp_packet
|
||||
.set_destination_port(ip_proxy_map.tcp_proxy_port);
|
||||
tcp_packet.update_checksum();
|
||||
ipv4.set_destination_ip(destination);
|
||||
ipv4.update_checksum();
|
||||
let key = SocketAddrV4::new(source, source_port);
|
||||
//https://github.com/crossbeam-rs/crossbeam/issues/1023
|
||||
ip_proxy_map.tcp_proxy_map.insert(key, SocketAddrV4::new(dest_ip, dest_port));
|
||||
ip_proxy_map
|
||||
.tcp_proxy_map
|
||||
.insert(key, SocketAddrV4::new(dest_ip, dest_port));
|
||||
}
|
||||
ipv4::protocol::Protocol::Udp => {
|
||||
let dest_ip = ipv4.destination_ip();
|
||||
//转发到代理目标地址
|
||||
let mut udp_packet = packet::udp::udp::UdpPacket::new(source, destination, ipv4.payload_mut())?;
|
||||
let mut udp_packet = packet::udp::udp::UdpPacket::new(
|
||||
source,
|
||||
destination,
|
||||
ipv4.payload_mut(),
|
||||
)?;
|
||||
let source_port = udp_packet.source_port();
|
||||
let dest_port = udp_packet.destination_port();
|
||||
udp_packet.set_destination_port(ip_proxy_map.udp_proxy_port);
|
||||
udp_packet
|
||||
.set_destination_port(ip_proxy_map.udp_proxy_port);
|
||||
udp_packet.update_checksum();
|
||||
ipv4.set_destination_ip(destination);
|
||||
ipv4.update_checksum();
|
||||
let key = SocketAddrV4::new(source, source_port);
|
||||
ip_proxy_map.udp_proxy_map.insert(key, SocketAddrV4::new(dest_ip, dest_port));
|
||||
ip_proxy_map
|
||||
.udp_proxy_map
|
||||
.insert(key, SocketAddrV4::new(dest_ip, dest_port));
|
||||
}
|
||||
ipv4::protocol::Protocol::Icmp => {
|
||||
let dest_ip = ipv4.destination_ip();
|
||||
//转发到代理目标地址
|
||||
let icmp_packet = icmp::IcmpPacket::new(ipv4.payload())?;
|
||||
let icmp_packet =
|
||||
icmp::IcmpPacket::new(ipv4.payload())?;
|
||||
match icmp_packet.header_other() {
|
||||
HeaderOther::Identifier(id, seq) => {
|
||||
ip_proxy_map.icmp_proxy_map.insert((dest_ip, id, seq), source);
|
||||
ip_proxy_map.send_icmp(ipv4.payload(), &dest_ip)?;
|
||||
ip_proxy_map
|
||||
.icmp_proxy_map
|
||||
.insert((dest_ip, id, seq), source);
|
||||
ip_proxy_map
|
||||
.send_icmp(ipv4.payload(), &dest_ip)?;
|
||||
}
|
||||
_ => {
|
||||
log::warn!("不支持的ip代理Icmp协议:{}",destination);
|
||||
return Err(Error::Warn("不支持的ip代理Icmp协议".to_string()));
|
||||
log::warn!(
|
||||
"不支持的ip代理Icmp协议:{}",
|
||||
destination
|
||||
);
|
||||
return Err(Error::Warn(
|
||||
"不支持的ip代理Icmp协议".to_string(),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
log::warn!("不支持的ip代理ipv4协议:{}",destination);
|
||||
return Err(Error::Warn("不支持的ip代理ipv4协议".to_string()));
|
||||
log::warn!("不支持的ip代理ipv4协议:{}", destination);
|
||||
return Err(Error::Warn(
|
||||
"不支持的ip代理ipv4协议".to_string(),
|
||||
));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
log::warn!("没有ip代理规则:{}",destination);
|
||||
log::warn!("没有ip代理规则:{}", destination);
|
||||
return Err(Error::Warn("没有ip代理规则".to_string()));
|
||||
}
|
||||
} else {
|
||||
log::warn!("不支持ip代理:{}",destination);
|
||||
log::warn!("不支持ip代理:{}", destination);
|
||||
return Err(Error::Warn("不支持ip代理".to_string()));
|
||||
}
|
||||
}
|
||||
@@ -254,19 +317,30 @@ impl ChannelDataHandler {
|
||||
Protocol::Service => {}
|
||||
Protocol::Error => {}
|
||||
Protocol::Control => {
|
||||
self.control(context, current_device, source, net_packet, route_key).await?;
|
||||
self.control(context, current_device, source, net_packet, route_key)
|
||||
.await?;
|
||||
}
|
||||
Protocol::OtherTurn => {
|
||||
self.other_turn(context, current_device, source, net_packet, route_key).await?;
|
||||
self.other_turn(context, current_device, source, net_packet, route_key)
|
||||
.await?;
|
||||
}
|
||||
Protocol::UnKnow(e) => {
|
||||
log::info!("不支持的协议:{}",e);
|
||||
log::info!("不支持的协议:{}", e);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn pong_packet(&self, gateway: bool, metric: u8, context: &Context, current_device: CurrentDeviceInfo, source: Ipv4Addr, pong_packet: control_packet::PongPacket<&[u8]>, route_key: &RouteKey) -> crate::Result<()> {
|
||||
async fn pong_packet(
|
||||
&self,
|
||||
gateway: bool,
|
||||
metric: u8,
|
||||
context: &Context,
|
||||
current_device: CurrentDeviceInfo,
|
||||
source: Ipv4Addr,
|
||||
pong_packet: control_packet::PongPacket<&[u8]>,
|
||||
route_key: &RouteKey,
|
||||
) -> crate::Result<()> {
|
||||
let current_time = crate::handle::now_time() as u16;
|
||||
if current_time < pong_packet.time() {
|
||||
return Ok(());
|
||||
@@ -286,12 +360,21 @@ impl ChannelDataHandler {
|
||||
poll_device.set_protocol(Protocol::Service);
|
||||
poll_device.set_transport_protocol(service_packet::Protocol::PollDeviceList.into());
|
||||
self.server_cipher.encrypt_ipv4(&mut poll_device)?;
|
||||
context.send_main(poll_device.buffer(), current_device.connect_server).await?;
|
||||
context
|
||||
.send_main(poll_device.buffer(), current_device.connect_server)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
async fn control(&self, context: &Context, current_device: CurrentDeviceInfo, source: Ipv4Addr, mut net_packet: NetPacket<&mut [u8]>, route_key: &RouteKey) -> crate::Result<()> {
|
||||
async fn control(
|
||||
&self,
|
||||
context: &Context,
|
||||
current_device: CurrentDeviceInfo,
|
||||
source: Ipv4Addr,
|
||||
mut net_packet: NetPacket<&mut [u8]>,
|
||||
route_key: &RouteKey,
|
||||
) -> crate::Result<()> {
|
||||
let metric = net_packet.source_ttl() - net_packet.ttl() + 1;
|
||||
match ControlPacket::new(net_packet.transport_protocol(), net_packet.payload())? {
|
||||
ControlPacket::PingPacket(_) => {
|
||||
@@ -305,7 +388,16 @@ impl ChannelDataHandler {
|
||||
context.add_route_if_absent(source, route);
|
||||
}
|
||||
ControlPacket::PongPacket(pong_packet) => {
|
||||
self.pong_packet(false, metric, context, current_device, source, pong_packet, route_key).await?;
|
||||
self.pong_packet(
|
||||
false,
|
||||
metric,
|
||||
context,
|
||||
current_device,
|
||||
source,
|
||||
pong_packet,
|
||||
route_key,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
ControlPacket::PunchRequest => {
|
||||
if self.relay {
|
||||
@@ -328,49 +420,61 @@ impl ChannelDataHandler {
|
||||
let route = Route::from(*route_key, 1, 199);
|
||||
context.add_route_if_absent(source, route);
|
||||
}
|
||||
ControlPacket::AddrRequest => {
|
||||
match route_key.addr.ip() {
|
||||
std::net::IpAddr::V4(ipv4) => {
|
||||
let mut packet = NetPacket::new_encrypt([0; 12 + 6 + ENCRYPTION_RESERVED])?;
|
||||
packet.set_version(Version::V1);
|
||||
packet.set_protocol(Protocol::Control);
|
||||
packet.set_transport_protocol(
|
||||
control_packet::Protocol::AddrResponse.into(),
|
||||
);
|
||||
packet.first_set_ttl(MAX_TTL);
|
||||
packet.set_source(current_device.virtual_ip());
|
||||
packet.set_destination(source);
|
||||
let mut addr_packet = control_packet::AddrPacket::new(packet.payload_mut())?;
|
||||
addr_packet.set_ipv4(ipv4);
|
||||
addr_packet.set_port(route_key.addr.port());
|
||||
self.client_cipher.encrypt_ipv4(&mut packet)?;
|
||||
context.send_by_key(packet.buffer(), route_key).await?;
|
||||
}
|
||||
std::net::IpAddr::V6(_) => {}
|
||||
ControlPacket::AddrRequest => match route_key.addr.ip() {
|
||||
std::net::IpAddr::V4(ipv4) => {
|
||||
let mut packet = NetPacket::new_encrypt([0; 12 + 6 + ENCRYPTION_RESERVED])?;
|
||||
packet.set_version(Version::V1);
|
||||
packet.set_protocol(Protocol::Control);
|
||||
packet.set_transport_protocol(control_packet::Protocol::AddrResponse.into());
|
||||
packet.first_set_ttl(MAX_TTL);
|
||||
packet.set_source(current_device.virtual_ip());
|
||||
packet.set_destination(source);
|
||||
let mut addr_packet = control_packet::AddrPacket::new(packet.payload_mut())?;
|
||||
addr_packet.set_ipv4(ipv4);
|
||||
addr_packet.set_port(route_key.addr.port());
|
||||
self.client_cipher.encrypt_ipv4(&mut packet)?;
|
||||
context.send_by_key(packet.buffer(), route_key).await?;
|
||||
}
|
||||
}
|
||||
std::net::IpAddr::V6(_) => {}
|
||||
},
|
||||
ControlPacket::AddrResponse(addr_packet) => {
|
||||
if !addr_packet.ipv4().is_multicast()
|
||||
&& !addr_packet.ipv4().is_broadcast()
|
||||
&& !addr_packet.ipv4().is_unspecified()
|
||||
&& !addr_packet.ipv4().is_loopback()
|
||||
&& !addr_packet.ipv4().is_private() && addr_packet.port() != 0 {
|
||||
self.nat_test.update_addr(addr_packet.ipv4(), addr_packet.port())
|
||||
&& !addr_packet.ipv4().is_private()
|
||||
&& addr_packet.port() != 0
|
||||
{
|
||||
self.nat_test
|
||||
.update_addr(addr_packet.ipv4(), addr_packet.port())
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
async fn other_turn(&self, context: &Context, current_device: CurrentDeviceInfo, source: Ipv4Addr, net_packet: NetPacket<&mut [u8]>, route_key: &RouteKey) -> crate::Result<()> {
|
||||
async fn other_turn(
|
||||
&self,
|
||||
context: &Context,
|
||||
current_device: CurrentDeviceInfo,
|
||||
source: Ipv4Addr,
|
||||
net_packet: NetPacket<&mut [u8]>,
|
||||
route_key: &RouteKey,
|
||||
) -> crate::Result<()> {
|
||||
if self.relay {
|
||||
return Ok(());
|
||||
}
|
||||
match other_turn_packet::Protocol::from(net_packet.transport_protocol()) {
|
||||
other_turn_packet::Protocol::Punch => {
|
||||
let punch_info = PunchInfo::parse_from_bytes(net_packet.payload())?;
|
||||
let public_ips = punch_info.public_ip_list.
|
||||
iter().map(|v| { Ipv4Addr::from(v.to_be_bytes()) }).collect();
|
||||
let local_ipv4_addr = SocketAddrV4::new(Ipv4Addr::from(punch_info.local_ip.to_be_bytes()), punch_info.local_port as u16);
|
||||
let public_ips = punch_info
|
||||
.public_ip_list
|
||||
.iter()
|
||||
.map(|v| Ipv4Addr::from(v.to_be_bytes()))
|
||||
.collect();
|
||||
let local_ipv4_addr = SocketAddrV4::new(
|
||||
Ipv4Addr::from(punch_info.local_ip.to_be_bytes()),
|
||||
punch_info.local_port as u16,
|
||||
);
|
||||
let ipv6_addr = if punch_info.ipv6.len() == 16 {
|
||||
let ipv6: [u8; 16] = punch_info.ipv6.try_into().unwrap();
|
||||
SocketAddrV6::new(Ipv6Addr::from(ipv6), punch_info.ipv6_port as u16, 0, 0)
|
||||
@@ -378,23 +482,30 @@ impl ChannelDataHandler {
|
||||
SocketAddrV6::new(Ipv6Addr::UNSPECIFIED, 0, 0, 0)
|
||||
};
|
||||
|
||||
let peer_nat_info = NatInfo::new(public_ips,
|
||||
punch_info.public_port as u16,
|
||||
punch_info.public_port_range as u16,
|
||||
local_ipv4_addr,
|
||||
ipv6_addr,
|
||||
punch_info.nat_type.enum_value_or_default().into());
|
||||
let peer_nat_info = NatInfo::new(
|
||||
public_ips,
|
||||
punch_info.public_port as u16,
|
||||
punch_info.public_port_range as u16,
|
||||
local_ipv4_addr,
|
||||
ipv6_addr,
|
||||
punch_info.nat_type.enum_value_or_default().into(),
|
||||
);
|
||||
self.peer_nat_info_map.insert(source, peer_nat_info.clone());
|
||||
if !punch_info.reply {
|
||||
let mut punch_reply = PunchInfo::new();
|
||||
punch_reply.reply = true;
|
||||
let nat_info = self.nat_test.nat_info();
|
||||
punch_reply.public_ip_list = nat_info.public_ips.iter().map(|ip| u32::from_be_bytes(ip.octets())).collect();
|
||||
punch_reply.public_ip_list = nat_info
|
||||
.public_ips
|
||||
.iter()
|
||||
.map(|ip| u32::from_be_bytes(ip.octets()))
|
||||
.collect();
|
||||
punch_reply.public_port = nat_info.public_port as u32;
|
||||
punch_reply.public_port_range = nat_info.public_port_range as u32;
|
||||
punch_reply.nat_type =
|
||||
protobuf::EnumOrUnknown::new(PunchNatType::from(nat_info.nat_type));
|
||||
punch_reply.local_ip = u32::from_be_bytes(nat_info.local_ipv4_addr.ip().octets());
|
||||
punch_reply.local_ip =
|
||||
u32::from_be_bytes(nat_info.local_ipv4_addr.ip().octets());
|
||||
punch_reply.local_port = nat_info.local_ipv4_addr.port() as u32;
|
||||
if !nat_info.ipv6_addr.ip().is_unspecified() {
|
||||
punch_reply.ipv6 = nat_info.ipv6_addr.ip().octets().to_vec();
|
||||
@@ -405,9 +516,7 @@ impl ChannelDataHandler {
|
||||
NetPacket::new_encrypt(vec![0u8; 12 + bytes.len() + ENCRYPTION_RESERVED])?;
|
||||
punch_packet.set_version(Version::V1);
|
||||
punch_packet.set_protocol(Protocol::OtherTurn);
|
||||
punch_packet.set_transport_protocol(
|
||||
other_turn_packet::Protocol::Punch.into(),
|
||||
);
|
||||
punch_packet.set_transport_protocol(other_turn_packet::Protocol::Punch.into());
|
||||
punch_packet.first_set_ttl(MAX_TTL);
|
||||
punch_packet.set_source(current_device.virtual_ip());
|
||||
punch_packet.set_destination(source);
|
||||
@@ -426,44 +535,55 @@ impl ChannelDataHandler {
|
||||
// }
|
||||
if self.punch(source, peer_nat_info).await {
|
||||
self.client_cipher.encrypt_ipv4(&mut punch_packet)?;
|
||||
context.send_by_key(punch_packet.buffer(), route_key).await?;
|
||||
context
|
||||
.send_by_key(punch_packet.buffer(), route_key)
|
||||
.await?;
|
||||
}
|
||||
} else {
|
||||
self.punch(source, peer_nat_info).await;
|
||||
}
|
||||
}
|
||||
other_turn_packet::Protocol::Unknown(e) => {
|
||||
log::warn!("不支持的转发协议 {:?},source:{:?}",e,source);
|
||||
log::warn!("不支持的转发协议 {:?},source:{:?}", e, source);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
async fn punch(&self, peer_ip: Ipv4Addr, peer_nat_info: NatInfo) -> bool {
|
||||
match peer_nat_info.nat_type {
|
||||
NatType::Symmetric => {
|
||||
self.symmetric_sender.try_send((peer_ip, peer_nat_info)).is_ok()
|
||||
}
|
||||
NatType::Cone => {
|
||||
self.cone_sender.try_send((peer_ip, peer_nat_info)).is_ok()
|
||||
}
|
||||
NatType::Symmetric => self
|
||||
.symmetric_sender
|
||||
.try_send((peer_ip, peer_nat_info))
|
||||
.is_ok(),
|
||||
NatType::Cone => self.cone_sender.try_send((peer_ip, peer_nat_info)).is_ok(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 处理服务端数据
|
||||
impl ChannelDataHandler {
|
||||
async fn server_packet_handle(&self, context: &Context, current_device: CurrentDeviceInfo, buf: &mut [u8], data_len: usize, route_key: &RouteKey) -> crate::Result<()> {
|
||||
async fn server_packet_handle(
|
||||
&self,
|
||||
context: &Context,
|
||||
current_device: CurrentDeviceInfo,
|
||||
buf: &mut [u8],
|
||||
data_len: usize,
|
||||
route_key: &RouteKey,
|
||||
) -> crate::Result<()> {
|
||||
let net_packet = NetPacket::new0(data_len, &buf[14..])?;
|
||||
let source = net_packet.source();
|
||||
match net_packet.protocol() {
|
||||
Protocol::Service => {
|
||||
self.service(context, current_device, net_packet, route_key).await?;
|
||||
self.service(context, current_device, net_packet, route_key)
|
||||
.await?;
|
||||
}
|
||||
Protocol::Error => {
|
||||
self.error(context, current_device, source, net_packet, route_key).await?;
|
||||
self.error(context, current_device, source, net_packet, route_key)
|
||||
.await?;
|
||||
}
|
||||
Protocol::Control => {
|
||||
self.control_gateway(context, current_device, net_packet, route_key).await?;
|
||||
self.control_gateway(context, current_device, net_packet, route_key)
|
||||
.await?;
|
||||
}
|
||||
Protocol::IpTurn => {
|
||||
match ip_turn_packet::Protocol::from(net_packet.transport_protocol()) {
|
||||
@@ -497,14 +617,29 @@ impl ChannelDataHandler {
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
async fn control_gateway(&self, context: &Context, current_device: CurrentDeviceInfo, net_packet: NetPacket<&[u8]>, route_key: &RouteKey) -> crate::Result<()> {
|
||||
async fn control_gateway(
|
||||
&self,
|
||||
context: &Context,
|
||||
current_device: CurrentDeviceInfo,
|
||||
net_packet: NetPacket<&[u8]>,
|
||||
route_key: &RouteKey,
|
||||
) -> crate::Result<()> {
|
||||
if net_packet.source() != current_device.virtual_gateway {
|
||||
return Ok(());
|
||||
}
|
||||
match ControlPacket::new(net_packet.transport_protocol(), net_packet.payload())? {
|
||||
ControlPacket::PongPacket(pong_packet) => {
|
||||
let metric = net_packet.source_ttl() - net_packet.ttl() + 1;
|
||||
self.pong_packet(true, metric, context, current_device, net_packet.source(), pong_packet, route_key).await?;
|
||||
self.pong_packet(
|
||||
true,
|
||||
metric,
|
||||
context,
|
||||
current_device,
|
||||
net_packet.source(),
|
||||
pong_packet,
|
||||
route_key,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
ControlPacket::AddrResponse(addr_packet) => {
|
||||
if addr_packet.port() != 0
|
||||
@@ -512,15 +647,23 @@ impl ChannelDataHandler {
|
||||
&& !addr_packet.ipv4().is_broadcast()
|
||||
&& !addr_packet.ipv4().is_unspecified()
|
||||
&& !addr_packet.ipv4().is_loopback()
|
||||
&& !addr_packet.ipv4().is_private() {
|
||||
self.nat_test.update_addr(addr_packet.ipv4(), addr_packet.port())
|
||||
&& !addr_packet.ipv4().is_private()
|
||||
{
|
||||
self.nat_test
|
||||
.update_addr(addr_packet.ipv4(), addr_packet.port())
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
async fn service(&self, context: &Context, current_device: CurrentDeviceInfo, net_packet: NetPacket<&[u8]>, route_key: &RouteKey) -> crate::Result<()> {
|
||||
async fn service(
|
||||
&self,
|
||||
context: &Context,
|
||||
current_device: CurrentDeviceInfo,
|
||||
net_packet: NetPacket<&[u8]>,
|
||||
route_key: &RouteKey,
|
||||
) -> crate::Result<()> {
|
||||
match service_packet::Protocol::from(net_packet.transport_protocol()) {
|
||||
service_packet::Protocol::RegistrationRequest => {}
|
||||
service_packet::Protocol::RegistrationResponse => {
|
||||
@@ -529,28 +672,47 @@ impl ChannelDataHandler {
|
||||
let local_ipv4_addr = nat::local_ipv4_addr(local_port);
|
||||
let local_port = context.main_local_ipv6_port().unwrap_or(0);
|
||||
let ipv6_addr = nat::local_ipv6_addr(local_port);
|
||||
let nat_info = self.nat_test.re_test(Ipv4Addr::from(response.public_ip),
|
||||
response.public_port as u16,
|
||||
local_ipv4_addr, ipv6_addr).await;
|
||||
let nat_info = self
|
||||
.nat_test
|
||||
.re_test(
|
||||
Ipv4Addr::from(response.public_ip),
|
||||
response.public_port as u16,
|
||||
local_ipv4_addr,
|
||||
ipv6_addr,
|
||||
)
|
||||
.await;
|
||||
context.switch(nat_info.nat_type);
|
||||
let new_ip = Ipv4Addr::from(response.virtual_ip);
|
||||
let current_ip = current_device.virtual_ip();
|
||||
if current_ip != new_ip {
|
||||
// ip发生变化
|
||||
log::info!("ip发生变化,old_ip:{:?},new_ip:{:?}",current_ip,new_ip);
|
||||
log::info!("ip发生变化,old_ip:{:?},new_ip:{:?}", current_ip, new_ip);
|
||||
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
|
||||
let old_netmask = current_device.virtual_netmask;
|
||||
let old_netmask = current_device.virtual_netmask;
|
||||
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
|
||||
let old_gateway = current_device.virtual_gateway();
|
||||
let old_gateway = current_device.virtual_gateway();
|
||||
let virtual_ip = Ipv4Addr::from(response.virtual_ip);
|
||||
let virtual_gateway = Ipv4Addr::from(response.virtual_gateway);
|
||||
let virtual_netmask = Ipv4Addr::from(response.virtual_netmask);
|
||||
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
|
||||
self.device_writer.change_ip(virtual_ip, virtual_netmask, virtual_gateway, old_netmask, old_gateway)?;
|
||||
let new_current_device = CurrentDeviceInfo::new(virtual_ip, virtual_gateway,
|
||||
virtual_netmask, current_device.connect_server);
|
||||
if let Err(e) = self.current_device.compare_exchange(current_device, new_current_device) {
|
||||
log::warn!("替换失败:{:?}",e);
|
||||
self.device_writer.change_ip(
|
||||
virtual_ip,
|
||||
virtual_netmask,
|
||||
virtual_gateway,
|
||||
old_netmask,
|
||||
old_gateway,
|
||||
)?;
|
||||
let new_current_device = CurrentDeviceInfo::new(
|
||||
virtual_ip,
|
||||
virtual_gateway,
|
||||
virtual_netmask,
|
||||
current_device.connect_server,
|
||||
);
|
||||
if let Err(e) = self
|
||||
.current_device
|
||||
.compare_exchange(current_device, new_current_device)
|
||||
{
|
||||
log::warn!("替换失败:{:?}", e);
|
||||
}
|
||||
}
|
||||
self.connect_status.store(ConnectStatus::Connected);
|
||||
@@ -583,14 +745,21 @@ impl ChannelDataHandler {
|
||||
}
|
||||
}
|
||||
service_packet::Protocol::Unknown(u) => {
|
||||
log::warn!("未知服务协议:{}",u);
|
||||
log::warn!("未知服务协议:{}", u);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
async fn error(&self, _context: &Context, current_device: CurrentDeviceInfo, _source: Ipv4Addr, net_packet: NetPacket<&[u8]>, _route_key: &RouteKey) -> crate::Result<()> {
|
||||
log::info!("current_device:{:?}",current_device);
|
||||
async fn error(
|
||||
&self,
|
||||
_context: &Context,
|
||||
current_device: CurrentDeviceInfo,
|
||||
_source: Ipv4Addr,
|
||||
net_packet: NetPacket<&[u8]>,
|
||||
_route_key: &RouteKey,
|
||||
) -> crate::Result<()> {
|
||||
log::info!("current_device:{:?}", current_device);
|
||||
match InErrorPacket::new(net_packet.transport_protocol(), net_packet.payload())? {
|
||||
InErrorPacket::TokenError => {
|
||||
return Err(Error::Stop("Token error".to_string()));
|
||||
@@ -603,7 +772,9 @@ impl ChannelDataHandler {
|
||||
}
|
||||
|
||||
self.connect_status.store(ConnectStatus::Connecting);
|
||||
self.register.fast_register(current_device.virtual_ip).await?;
|
||||
self.register
|
||||
.fast_register(current_device.virtual_ip)
|
||||
.await?;
|
||||
}
|
||||
InErrorPacket::AddressExhausted => {
|
||||
//地址用尽
|
||||
@@ -622,4 +793,4 @@ impl ChannelDataHandler {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
use crossbeam_utils::atomic::AtomicCell;
|
||||
use std::net::{Ipv4Addr, SocketAddr};
|
||||
use std::time::{Duration, Instant};
|
||||
use crossbeam_utils::atomic::AtomicCell;
|
||||
|
||||
use protobuf::Message;
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
use tokio::net::{TcpStream, UdpSocket};
|
||||
use crate::channel::sender::ChannelSender;
|
||||
use crate::cipher::Cipher;
|
||||
use crate::handle::PeerDeviceInfo;
|
||||
use protobuf::Message;
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
use tokio::net::{TcpStream, UdpSocket};
|
||||
|
||||
use crate::proto::message::{RegistrationRequest, RegistrationResponse};
|
||||
use crate::protocol::body::ENCRYPTION_RESERVED;
|
||||
use crate::protocol::error_packet::InErrorPacket;
|
||||
use crate::protocol::{service_packet, NetPacket, Protocol, Version, MAX_TTL};
|
||||
use crate::protocol::body::ENCRYPTION_RESERVED;
|
||||
|
||||
pub enum ReqEnum {
|
||||
TokenError,
|
||||
@@ -47,8 +47,17 @@ pub async fn registration(
|
||||
ip: Ipv4Addr,
|
||||
client_secret: bool,
|
||||
) -> Result<RegResponse, ReqEnum> {
|
||||
let request_packet =
|
||||
registration_request_packet(server_cipher, token.clone(), device_id.clone(), name.clone(), ip, false, false, client_secret).unwrap();
|
||||
let request_packet = registration_request_packet(
|
||||
server_cipher,
|
||||
token.clone(),
|
||||
device_id.clone(),
|
||||
name.clone(),
|
||||
ip,
|
||||
false,
|
||||
false,
|
||||
client_secret,
|
||||
)
|
||||
.unwrap();
|
||||
let buf = request_packet.buffer();
|
||||
let mut recv_buf = [0u8; 10240];
|
||||
let recv_buf = if let Some(main_tcp_channel) = main_tcp_channel {
|
||||
@@ -75,29 +84,30 @@ pub async fn registration(
|
||||
if let Err(e) = main_channel.send_to(buf, server_address).await {
|
||||
return Err(ReqEnum::Other(format!("send error:{}", e)));
|
||||
}
|
||||
match tokio::time::timeout(Duration::from_millis(300), main_channel.recv_from(&mut recv_buf)).await {
|
||||
Ok(rs) => {
|
||||
match rs {
|
||||
Ok((len, addr)) => {
|
||||
if server_address != addr {
|
||||
return Err(ReqEnum::Other(format!("invalid data,from {}", addr)));
|
||||
}
|
||||
&mut recv_buf[..len]
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(ReqEnum::Other(format!("receiver error:{}", e)));
|
||||
match tokio::time::timeout(
|
||||
Duration::from_millis(300),
|
||||
main_channel.recv_from(&mut recv_buf),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(rs) => match rs {
|
||||
Ok((len, addr)) => {
|
||||
if server_address != addr {
|
||||
return Err(ReqEnum::Other(format!("invalid data,from {}", addr)));
|
||||
}
|
||||
&mut recv_buf[..len]
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(ReqEnum::Other(format!("receiver error:{}", e)));
|
||||
}
|
||||
},
|
||||
Err(_) => {
|
||||
return Err(ReqEnum::Timeout);
|
||||
}
|
||||
}
|
||||
};
|
||||
let mut net_packet = match NetPacket::new(recv_buf) {
|
||||
Ok(net_packet) => {
|
||||
net_packet
|
||||
}
|
||||
Ok(net_packet) => net_packet,
|
||||
Err(e) => {
|
||||
return Err(ReqEnum::ServerError(format!("{}", e)));
|
||||
}
|
||||
@@ -133,14 +143,10 @@ pub async fn registration(
|
||||
public_port: response.public_port as u16,
|
||||
})
|
||||
}
|
||||
Err(_) => {
|
||||
Err(ReqEnum::ServerError("invalid data".to_string()))
|
||||
}
|
||||
Err(_) => Err(ReqEnum::ServerError("invalid data".to_string())),
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
Err(ReqEnum::ServerError("invalid data".to_string()))
|
||||
}
|
||||
_ => Err(ReqEnum::ServerError("invalid data".to_string())),
|
||||
}
|
||||
}
|
||||
Protocol::Error => {
|
||||
@@ -150,24 +156,14 @@ pub async fn registration(
|
||||
InErrorPacket::Disconnect => {
|
||||
Err(ReqEnum::ServerError("disconnect".to_string()))
|
||||
}
|
||||
InErrorPacket::AddressExhausted => {
|
||||
Err(ReqEnum::AddressExhausted)
|
||||
}
|
||||
InErrorPacket::AddressExhausted => Err(ReqEnum::AddressExhausted),
|
||||
InErrorPacket::OtherError(e) => match e.message() {
|
||||
Ok(str) => {
|
||||
Err(ReqEnum::ServerError(str))
|
||||
}
|
||||
Ok(str) => Err(ReqEnum::ServerError(str)),
|
||||
Err(e) => Err(ReqEnum::Other(format!("{}", e))),
|
||||
},
|
||||
InErrorPacket::IpAlreadyExists => {
|
||||
Err(ReqEnum::IpAlreadyExists)
|
||||
}
|
||||
InErrorPacket::InvalidIp => {
|
||||
Err(ReqEnum::InvalidIp)
|
||||
}
|
||||
InErrorPacket::NoKey => {
|
||||
Err(ReqEnum::ServerError("no key".to_string()))
|
||||
}
|
||||
InErrorPacket::IpAlreadyExists => Err(ReqEnum::IpAlreadyExists),
|
||||
InErrorPacket::InvalidIp => Err(ReqEnum::InvalidIp),
|
||||
InErrorPacket::NoKey => Err(ReqEnum::ServerError("no key".to_string())),
|
||||
},
|
||||
Err(e) => Err(ReqEnum::Other(format!("{}", e))),
|
||||
}
|
||||
@@ -243,10 +239,7 @@ impl Register {
|
||||
pub async fn fast_register(&self, ip: Ipv4Addr) -> crate::Result<()> {
|
||||
let last = self.time.load();
|
||||
if last.elapsed() < Duration::from_secs(2)
|
||||
|| self
|
||||
.time
|
||||
.compare_exchange(last, Instant::now())
|
||||
.is_err()
|
||||
|| self.time.compare_exchange(last, Instant::now()).is_err()
|
||||
{
|
||||
//短时间不重复注册
|
||||
return Ok(());
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
use byte_pool::Block;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct BufSenderGroup(usize, Vec<tokio::sync::mpsc::Sender<(Block<'static>, usize, usize)>>);
|
||||
pub struct BufSenderGroup(
|
||||
usize,
|
||||
Vec<tokio::sync::mpsc::Sender<(Block<'static>, usize, usize)>>,
|
||||
);
|
||||
|
||||
pub struct BufReceiverGroup(pub Vec<tokio::sync::mpsc::Receiver<(Block<'static>, usize, usize)>>);
|
||||
|
||||
@@ -17,9 +20,13 @@ pub fn buf_channel_group(size: usize) -> (BufSenderGroup, BufReceiverGroup) {
|
||||
let mut buf_sender_group = Vec::with_capacity(size);
|
||||
let mut buf_receiver_group = Vec::with_capacity(size);
|
||||
for _ in 0..size {
|
||||
let (buf_sender, buf_receiver) = tokio::sync::mpsc::channel::<(Block<'static>, usize, usize)>(10);
|
||||
let (buf_sender, buf_receiver) =
|
||||
tokio::sync::mpsc::channel::<(Block<'static>, usize, usize)>(10);
|
||||
buf_sender_group.push(buf_sender);
|
||||
buf_receiver_group.push(buf_receiver);
|
||||
}
|
||||
(BufSenderGroup(0, buf_sender_group), BufReceiverGroup(buf_receiver_group))
|
||||
}
|
||||
(
|
||||
BufSenderGroup(0, buf_sender_group),
|
||||
BufReceiverGroup(buf_receiver_group),
|
||||
)
|
||||
}
|
||||
|
||||
+104
-44
@@ -1,28 +1,34 @@
|
||||
use std::io;
|
||||
use std::net::{Ipv4Addr, SocketAddrV4};
|
||||
use std::sync::Arc;
|
||||
use parking_lot::RwLock;
|
||||
use crate::channel::sender::ChannelSender;
|
||||
use crate::cipher::Cipher;
|
||||
use crate::error::*;
|
||||
use crate::external_route::ExternalRoute;
|
||||
use crate::handle::{check_dest, CurrentDeviceInfo};
|
||||
use crate::igmp_server::{IgmpServer, Multicast};
|
||||
use crate::ip_proxy::IpProxyMap;
|
||||
use crate::protocol;
|
||||
use crate::protocol::body::ENCRYPTION_RESERVED;
|
||||
use crate::protocol::ip_turn_packet::BroadcastPacket;
|
||||
use crate::protocol::{ip_turn_packet, NetPacket, Version, MAX_TTL};
|
||||
use packet::ip::ipv4::packet::IpV4Packet;
|
||||
use packet::ip::ipv4::protocol::Protocol;
|
||||
use packet::tcp::tcp::TcpPacket;
|
||||
use packet::udp::udp::UdpPacket;
|
||||
use crate::channel::sender::ChannelSender;
|
||||
use crate::cipher::Cipher;
|
||||
use crate::external_route::ExternalRoute;
|
||||
use crate::handle::{check_dest, CurrentDeviceInfo};
|
||||
use crate::ip_proxy::IpProxyMap;
|
||||
use crate::protocol::{ip_turn_packet, MAX_TTL, NetPacket, Version};
|
||||
use crate::error::*;
|
||||
use crate::igmp_server::{IgmpServer, Multicast};
|
||||
use crate::protocol;
|
||||
use crate::protocol::body::ENCRYPTION_RESERVED;
|
||||
use crate::protocol::ip_turn_packet::BroadcastPacket;
|
||||
use parking_lot::RwLock;
|
||||
use std::io;
|
||||
use std::net::{Ipv4Addr, SocketAddrV4};
|
||||
use std::sync::Arc;
|
||||
pub mod channel_group;
|
||||
pub mod tun_handler;
|
||||
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
|
||||
pub mod tap_handler;
|
||||
pub mod tun_handler;
|
||||
|
||||
async fn broadcast(server_cipher: &Cipher, multicast_members: Option<Arc<RwLock<Multicast>>>, sender: &ChannelSender, net_packet: &mut NetPacket<&mut [u8]>, current_device: &CurrentDeviceInfo) -> Result<()> {
|
||||
async fn broadcast(
|
||||
server_cipher: &Cipher,
|
||||
multicast_members: Option<Arc<RwLock<Multicast>>>,
|
||||
sender: &ChannelSender,
|
||||
net_packet: &mut NetPacket<&mut [u8]>,
|
||||
current_device: &CurrentDeviceInfo,
|
||||
) -> Result<()> {
|
||||
let mut peer_ips = Vec::with_capacity(8);
|
||||
let vec = sender.route_table_one();
|
||||
let mut relay_count = 0;
|
||||
@@ -40,7 +46,11 @@ async fn broadcast(server_cipher: &Cipher, multicast_members: Option<Arc<RwLock<
|
||||
}
|
||||
}
|
||||
if route.is_p2p()
|
||||
&& sender.send_by_key(net_packet.buffer(), &route.route_key()).await.is_ok() {
|
||||
&& sender
|
||||
.send_by_key(net_packet.buffer(), &route.route_key())
|
||||
.await
|
||||
.is_ok()
|
||||
{
|
||||
peer_ips.push(peer_ip);
|
||||
} else {
|
||||
relay_count += 1;
|
||||
@@ -52,9 +62,14 @@ async fn broadcast(server_cipher: &Cipher, multicast_members: Option<Arc<RwLock<
|
||||
}
|
||||
//转发到服务端的可选择广播,还要进行服务端加密
|
||||
if peer_ips.is_empty() {
|
||||
sender.send_main(net_packet.buffer(), current_device.connect_server).await?;
|
||||
sender
|
||||
.send_main(net_packet.buffer(), current_device.connect_server)
|
||||
.await?;
|
||||
} else {
|
||||
let buf = vec![0 as u8; 12 + 1 + peer_ips.len() * 4 + net_packet.data_len() + ENCRYPTION_RESERVED];
|
||||
let buf = vec![
|
||||
0 as u8;
|
||||
12 + 1 + peer_ips.len() * 4 + net_packet.data_len() + ENCRYPTION_RESERVED
|
||||
];
|
||||
//剩余的发送到服务端,需要告知哪些已发送过
|
||||
let mut server_packet = NetPacket::new_encrypt(buf)?;
|
||||
server_packet.set_version(Version::V1);
|
||||
@@ -70,7 +85,9 @@ async fn broadcast(server_cipher: &Cipher, multicast_members: Option<Arc<RwLock<
|
||||
broadcast.set_address(&peer_ips)?;
|
||||
broadcast.set_data(net_packet.buffer())?;
|
||||
server_cipher.encrypt_ipv4(&mut server_packet)?;
|
||||
sender.send_main(server_packet.buffer(), current_device.connect_server).await?;
|
||||
sender
|
||||
.send_main(server_packet.buffer(), current_device.connect_server)
|
||||
.await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -79,14 +96,17 @@ async fn broadcast(server_cipher: &Cipher, multicast_members: Option<Arc<RwLock<
|
||||
/// |12字节开头|ip报文|至少1024字节结尾|
|
||||
///
|
||||
#[inline]
|
||||
pub async fn base_handle(sender: &ChannelSender, buf: &mut [u8],
|
||||
data_len: usize,//数据总长度=12+ip包长度
|
||||
igmp_server: &Option<IgmpServer>,
|
||||
current_device: CurrentDeviceInfo,
|
||||
ip_route: &Option<ExternalRoute>,
|
||||
proxy_map: &Option<IpProxyMap>,
|
||||
client_cipher: &Cipher,
|
||||
server_cipher: &Cipher) -> Result<()> {
|
||||
pub async fn base_handle(
|
||||
sender: &ChannelSender,
|
||||
buf: &mut [u8],
|
||||
data_len: usize, //数据总长度=12+ip包长度
|
||||
igmp_server: &Option<IgmpServer>,
|
||||
current_device: CurrentDeviceInfo,
|
||||
ip_route: &Option<ExternalRoute>,
|
||||
proxy_map: &Option<IpProxyMap>,
|
||||
client_cipher: &Cipher,
|
||||
server_cipher: &Cipher,
|
||||
) -> Result<()> {
|
||||
let ipv4_packet = IpV4Packet::new(&buf[12..data_len])?;
|
||||
let protocol = ipv4_packet.protocol();
|
||||
let ip_head_len = ipv4_packet.header_len() as usize * 4;
|
||||
@@ -106,7 +126,9 @@ pub async fn base_handle(sender: &ChannelSender, buf: &mut [u8],
|
||||
if protocol == Protocol::Icmp {
|
||||
net_packet.set_gateway_flag(true);
|
||||
server_cipher.encrypt_ipv4(&mut net_packet)?;
|
||||
sender.send_main(net_packet.buffer(), current_device.connect_server).await?;
|
||||
sender
|
||||
.send_main(net_packet.buffer(), current_device.connect_server)
|
||||
.await?;
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
@@ -118,7 +140,9 @@ pub async fn base_handle(sender: &ChannelSender, buf: &mut [u8],
|
||||
net_packet.set_destination(current_device.virtual_gateway);
|
||||
net_packet.set_gateway_flag(true);
|
||||
server_cipher.encrypt_ipv4(&mut net_packet)?;
|
||||
sender.send_main(net_packet.buffer(), current_device.connect_server).await?;
|
||||
sender
|
||||
.send_main(net_packet.buffer(), current_device.connect_server)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
Protocol::Udp => {
|
||||
@@ -130,7 +154,14 @@ pub async fn base_handle(sender: &ChannelSender, buf: &mut [u8],
|
||||
None
|
||||
};
|
||||
client_cipher.encrypt_ipv4(&mut net_packet)?;
|
||||
broadcast(server_cipher, multicast_members, sender, &mut net_packet, ¤t_device).await?;
|
||||
broadcast(
|
||||
server_cipher,
|
||||
multicast_members,
|
||||
sender,
|
||||
&mut net_packet,
|
||||
¤t_device,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
@@ -139,10 +170,21 @@ pub async fn base_handle(sender: &ChannelSender, buf: &mut [u8],
|
||||
if dest_ip.is_broadcast() || current_device.broadcast_address == dest_ip {
|
||||
// 广播 发送到直连目标
|
||||
client_cipher.encrypt_ipv4(&mut net_packet)?;
|
||||
broadcast(server_cipher, None, sender, &mut net_packet, ¤t_device).await?;
|
||||
broadcast(
|
||||
server_cipher,
|
||||
None,
|
||||
sender,
|
||||
&mut net_packet,
|
||||
¤t_device,
|
||||
)
|
||||
.await?;
|
||||
return Ok(());
|
||||
}
|
||||
if !check_dest(dest_ip, current_device.virtual_netmask, current_device.virtual_network) {
|
||||
if !check_dest(
|
||||
dest_ip,
|
||||
current_device.virtual_netmask,
|
||||
current_device.virtual_network,
|
||||
) {
|
||||
if let Some(ip_route) = ip_route {
|
||||
if let Some(r_dest_ip) = ip_route.route(&dest_ip) {
|
||||
//路由的目标不能是自己
|
||||
@@ -162,15 +204,21 @@ pub async fn base_handle(sender: &ChannelSender, buf: &mut [u8],
|
||||
match protocol {
|
||||
Protocol::Tcp => {
|
||||
let dest_addr = {
|
||||
let tcp_packet = TcpPacket::new(src_ip, dest_ip,
|
||||
&mut net_packet.payload_mut()[ip_head_len..])?;
|
||||
let tcp_packet = TcpPacket::new(
|
||||
src_ip,
|
||||
dest_ip,
|
||||
&mut net_packet.payload_mut()[ip_head_len..],
|
||||
)?;
|
||||
SocketAddrV4::new(dest_ip, tcp_packet.destination_port())
|
||||
};
|
||||
if let Some(entry) = proxy_map.tcp_proxy_map.get(&dest_addr) {
|
||||
let source_addr = entry.value();
|
||||
let source_ip = *source_addr.ip();
|
||||
let mut tcp_packet = TcpPacket::new(source_ip, dest_ip,
|
||||
&mut net_packet.payload_mut()[ip_head_len..])?;
|
||||
let mut tcp_packet = TcpPacket::new(
|
||||
source_ip,
|
||||
dest_ip,
|
||||
&mut net_packet.payload_mut()[ip_head_len..],
|
||||
)?;
|
||||
tcp_packet.set_source_port(source_addr.port());
|
||||
tcp_packet.update_checksum();
|
||||
let mut ipv4_packet = IpV4Packet::new(net_packet.payload_mut())?;
|
||||
@@ -180,15 +228,21 @@ pub async fn base_handle(sender: &ChannelSender, buf: &mut [u8],
|
||||
}
|
||||
Protocol::Udp => {
|
||||
let dest_addr = {
|
||||
let udp_packet = UdpPacket::new(src_ip, dest_ip,
|
||||
&mut net_packet.payload_mut()[ip_head_len..])?;
|
||||
let udp_packet = UdpPacket::new(
|
||||
src_ip,
|
||||
dest_ip,
|
||||
&mut net_packet.payload_mut()[ip_head_len..],
|
||||
)?;
|
||||
SocketAddrV4::new(dest_ip, udp_packet.destination_port())
|
||||
};
|
||||
if let Some(entry) = proxy_map.udp_proxy_map.get(&dest_addr) {
|
||||
let source_addr = entry.value();
|
||||
let source_ip = *source_addr.ip();
|
||||
let mut udp_packet = UdpPacket::new(source_ip, dest_ip,
|
||||
&mut net_packet.payload_mut()[ip_head_len..])?;
|
||||
let mut udp_packet = UdpPacket::new(
|
||||
source_ip,
|
||||
dest_ip,
|
||||
&mut net_packet.payload_mut()[ip_head_len..],
|
||||
)?;
|
||||
udp_packet.set_source_port(source_addr.port());
|
||||
udp_packet.update_checksum();
|
||||
let mut ipv4_packet = IpV4Packet::new(net_packet.payload_mut())?;
|
||||
@@ -201,8 +255,14 @@ pub async fn base_handle(sender: &ChannelSender, buf: &mut [u8],
|
||||
}
|
||||
client_cipher.encrypt_ipv4(&mut net_packet)?;
|
||||
//优先发到直连到地址
|
||||
if sender.send_by_id(net_packet.buffer(), &dest_ip).await.is_err() {
|
||||
sender.send_main(net_packet.buffer(), current_device.connect_server).await?;
|
||||
if sender
|
||||
.send_by_id(net_packet.buffer(), &dest_ip)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
sender
|
||||
.send_main(net_packet.buffer(), current_device.connect_server)
|
||||
.await?;
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::{io, thread};
|
||||
use std::sync::Arc;
|
||||
use byte_pool::BytePool;
|
||||
use std::sync::Arc;
|
||||
use std::{io, thread};
|
||||
|
||||
use crossbeam_utils::atomic::AtomicCell;
|
||||
use lazy_static::lazy_static;
|
||||
@@ -17,36 +17,56 @@ use crate::channel::sender::ChannelSender;
|
||||
use crate::cipher::Cipher;
|
||||
use crate::core::status::VntWorker;
|
||||
use crate::external_route::ExternalRoute;
|
||||
use crate::handle::CurrentDeviceInfo;
|
||||
use crate::handle::tun_tap::channel_group::{buf_channel_group, BufSenderGroup};
|
||||
use crate::handle::CurrentDeviceInfo;
|
||||
use crate::igmp_server::IgmpServer;
|
||||
use crate::ip_proxy::IpProxyMap;
|
||||
use crate::tun_tap_device::{DeviceReader, DeviceWriter};
|
||||
lazy_static! {
|
||||
static ref POOL:BytePool<Vec<u8>> = BytePool::<Vec<u8>>::new();
|
||||
static ref POOL: BytePool<Vec<u8>> = BytePool::<Vec<u8>>::new();
|
||||
}
|
||||
|
||||
pub fn start(worker: VntWorker, sender: ChannelSender,
|
||||
device_reader: DeviceReader,
|
||||
device_writer: DeviceWriter,
|
||||
igmp_server: Option<IgmpServer>,
|
||||
current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
|
||||
ip_route: Option<ExternalRoute>,
|
||||
ip_proxy_map: Option<IpProxyMap>,
|
||||
client_cipher: Cipher, server_cipher: Cipher, parallel: usize) {
|
||||
pub fn start(
|
||||
worker: VntWorker,
|
||||
sender: ChannelSender,
|
||||
device_reader: DeviceReader,
|
||||
device_writer: DeviceWriter,
|
||||
igmp_server: Option<IgmpServer>,
|
||||
current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
|
||||
ip_route: Option<ExternalRoute>,
|
||||
ip_proxy_map: Option<IpProxyMap>,
|
||||
client_cipher: Cipher,
|
||||
server_cipher: Cipher,
|
||||
parallel: usize,
|
||||
) {
|
||||
if parallel == 1 {
|
||||
thread::Builder::new().name("tap_handler".into()).spawn(move || {
|
||||
tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all().build().unwrap()
|
||||
.block_on(async move {
|
||||
if let Err(e) = start_simple(sender, device_reader,
|
||||
device_writer, igmp_server,
|
||||
current_device, ip_route, ip_proxy_map, client_cipher, server_cipher).await {
|
||||
log::warn!("tap:{:?}",e);
|
||||
}
|
||||
worker.stop_all();
|
||||
});
|
||||
}).unwrap();
|
||||
thread::Builder::new()
|
||||
.name("tap_handler".into())
|
||||
.spawn(move || {
|
||||
tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.unwrap()
|
||||
.block_on(async move {
|
||||
if let Err(e) = start_simple(
|
||||
sender,
|
||||
device_reader,
|
||||
device_writer,
|
||||
igmp_server,
|
||||
current_device,
|
||||
ip_route,
|
||||
ip_proxy_map,
|
||||
client_cipher,
|
||||
server_cipher,
|
||||
)
|
||||
.await
|
||||
{
|
||||
log::warn!("tap:{:?}", e);
|
||||
}
|
||||
worker.stop_all();
|
||||
});
|
||||
})
|
||||
.unwrap();
|
||||
} else {
|
||||
let (buf_sender, buf_receiver) = buf_channel_group(parallel);
|
||||
for mut buf_receiver in buf_receiver.0 {
|
||||
@@ -60,8 +80,20 @@ pub fn start(worker: VntWorker, sender: ChannelSender,
|
||||
let server_cipher = server_cipher.clone();
|
||||
tokio::spawn(async move {
|
||||
while let Some((mut buf, _, len)) = buf_receiver.recv().await {
|
||||
match handle(&mut buf, len, &igmp_server, ¤t_device, &device_writer, &sender,
|
||||
&ip_route, &ip_proxy_map, &client_cipher, &server_cipher).await {
|
||||
match handle(
|
||||
&mut buf,
|
||||
len,
|
||||
&igmp_server,
|
||||
¤t_device,
|
||||
&device_writer,
|
||||
&sender,
|
||||
&ip_route,
|
||||
&ip_proxy_map,
|
||||
&client_cipher,
|
||||
&server_cipher,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
log::warn!("{:?}", e)
|
||||
@@ -70,22 +102,29 @@ pub fn start(worker: VntWorker, sender: ChannelSender,
|
||||
}
|
||||
});
|
||||
}
|
||||
thread::Builder::new().name("tap_handler".into()).spawn(move || {
|
||||
tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all().build().unwrap()
|
||||
.block_on(async move {
|
||||
if let Err(e) = start_(sender, device_reader, buf_sender).await {
|
||||
log::warn!("tap:{:?}",e);
|
||||
}
|
||||
worker.stop_all();
|
||||
});
|
||||
}).unwrap();
|
||||
thread::Builder::new()
|
||||
.name("tap_handler".into())
|
||||
.spawn(move || {
|
||||
tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.unwrap()
|
||||
.block_on(async move {
|
||||
if let Err(e) = start_(sender, device_reader, buf_sender).await {
|
||||
log::warn!("tap:{:?}", e);
|
||||
}
|
||||
worker.stop_all();
|
||||
});
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
async fn start_(sender: ChannelSender,
|
||||
device_reader: DeviceReader,
|
||||
mut buf_sender: BufSenderGroup) -> io::Result<()> {
|
||||
async fn start_(
|
||||
sender: ChannelSender,
|
||||
device_reader: DeviceReader,
|
||||
mut buf_sender: BufSenderGroup,
|
||||
) -> io::Result<()> {
|
||||
loop {
|
||||
let mut buf = POOL.alloc(4096);
|
||||
if sender.is_close() {
|
||||
@@ -94,36 +133,65 @@ async fn start_(sender: ChannelSender,
|
||||
let start = 0;
|
||||
let len = device_reader.read(&mut buf)?;
|
||||
if !buf_sender.send((buf, start, len)).await {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "tap buf_sender发送失败"));
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"tap buf_sender发送失败",
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn start_simple(sender: ChannelSender,
|
||||
device_reader: DeviceReader,
|
||||
device_writer: DeviceWriter,
|
||||
igmp_server: Option<IgmpServer>,
|
||||
current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
|
||||
ip_route: Option<ExternalRoute>,
|
||||
ip_proxy_map: Option<IpProxyMap>,
|
||||
client_cipher: Cipher, server_cipher: Cipher) -> io::Result<()> {
|
||||
async fn start_simple(
|
||||
sender: ChannelSender,
|
||||
device_reader: DeviceReader,
|
||||
device_writer: DeviceWriter,
|
||||
igmp_server: Option<IgmpServer>,
|
||||
current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
|
||||
ip_route: Option<ExternalRoute>,
|
||||
ip_proxy_map: Option<IpProxyMap>,
|
||||
client_cipher: Cipher,
|
||||
server_cipher: Cipher,
|
||||
) -> io::Result<()> {
|
||||
let mut buf = [0; 4096];
|
||||
loop {
|
||||
let len = device_reader.read(&mut buf)?;
|
||||
if let Err(e) = handle(&mut buf, len, &igmp_server, ¤t_device, &device_writer, &sender, &ip_route, &ip_proxy_map, &client_cipher, &server_cipher).await {
|
||||
log::warn!("tap handle{:?}",e);
|
||||
if let Err(e) = handle(
|
||||
&mut buf,
|
||||
len,
|
||||
&igmp_server,
|
||||
¤t_device,
|
||||
&device_writer,
|
||||
&sender,
|
||||
&ip_route,
|
||||
&ip_proxy_map,
|
||||
&client_cipher,
|
||||
&server_cipher,
|
||||
)
|
||||
.await
|
||||
{
|
||||
log::warn!("tap handle{:?}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle(buf: &mut [u8], len: usize, igmp_server: &Option<IgmpServer>, current_device: &AtomicCell<CurrentDeviceInfo>,
|
||||
device_writer: &DeviceWriter, sender: &ChannelSender, ip_route: &Option<ExternalRoute>,
|
||||
proxy_map: &Option<IpProxyMap>, client_cipher: &Cipher, server_cipher: &Cipher) -> crate::Result<()> {
|
||||
async fn handle(
|
||||
buf: &mut [u8],
|
||||
len: usize,
|
||||
igmp_server: &Option<IgmpServer>,
|
||||
current_device: &AtomicCell<CurrentDeviceInfo>,
|
||||
device_writer: &DeviceWriter,
|
||||
sender: &ChannelSender,
|
||||
ip_route: &Option<ExternalRoute>,
|
||||
proxy_map: &Option<IpProxyMap>,
|
||||
client_cipher: &Cipher,
|
||||
server_cipher: &Cipher,
|
||||
) -> crate::Result<()> {
|
||||
let mut ethernet_packet = EthernetPacket::new(&mut buf[..len])?;
|
||||
let current_device = current_device.load();
|
||||
match ethernet_packet.protocol() {
|
||||
ethernet::protocol::Protocol::Arp => {
|
||||
let mut out_ethernet_packet = EthernetPacket::unchecked(ethernet_packet.buffer.to_vec());
|
||||
let mut out_ethernet_packet =
|
||||
EthernetPacket::unchecked(ethernet_packet.buffer.to_vec());
|
||||
let arp_packet = ArpPacket::unchecked(ethernet_packet.payload());
|
||||
let mut out_arp_packet = ArpPacket::unchecked(out_ethernet_packet.payload_mut());
|
||||
let sender_h = arp_packet.sender_hardware_addr();
|
||||
@@ -133,12 +201,26 @@ async fn handle(buf: &mut [u8], len: usize, igmp_server: &Option<IgmpServer>, cu
|
||||
return Ok(());
|
||||
}
|
||||
//回复一个虚假的MAC地址
|
||||
out_arp_packet.set_sender_hardware_addr(&[target_p[0], target_p[1], target_p[2], target_p[3], !sender_h[5], 234]);
|
||||
out_arp_packet.set_sender_hardware_addr(&[
|
||||
target_p[0],
|
||||
target_p[1],
|
||||
target_p[2],
|
||||
target_p[3],
|
||||
!sender_h[5],
|
||||
234,
|
||||
]);
|
||||
out_arp_packet.set_sender_protocol_addr(target_p);
|
||||
out_arp_packet.set_target_hardware_addr(sender_h);
|
||||
out_arp_packet.set_target_protocol_addr(sender_p);
|
||||
out_arp_packet.set_op_code(2);
|
||||
out_ethernet_packet.set_source(&[target_p[0], target_p[1], target_p[2], target_p[3], !sender_h[5], 234]);
|
||||
out_ethernet_packet.set_source(&[
|
||||
target_p[0],
|
||||
target_p[1],
|
||||
target_p[2],
|
||||
target_p[3],
|
||||
!sender_h[5],
|
||||
234,
|
||||
]);
|
||||
out_ethernet_packet.set_destination(sender_h);
|
||||
device_writer.write_ethernet_tap(&out_ethernet_packet.buffer)?;
|
||||
}
|
||||
@@ -169,8 +251,18 @@ async fn handle(buf: &mut [u8], len: usize, igmp_server: &Option<IgmpServer>, cu
|
||||
return Ok(());
|
||||
}
|
||||
// 以太网帧头部14字节,预留12字节
|
||||
return crate::handle::tun_tap::base_handle(sender, &mut buf[2..], len - 2, igmp_server, current_device,
|
||||
ip_route, proxy_map, client_cipher, server_cipher).await;
|
||||
return crate::handle::tun_tap::base_handle(
|
||||
sender,
|
||||
&mut buf[2..],
|
||||
len - 2,
|
||||
igmp_server,
|
||||
current_device,
|
||||
ip_route,
|
||||
proxy_map,
|
||||
client_cipher,
|
||||
server_cipher,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
_ => {
|
||||
// log::warn!("不支持的二层协议:{:?}",p)
|
||||
@@ -178,4 +270,3 @@ async fn handle(buf: &mut [u8], len: usize, igmp_server: &Option<IgmpServer>, cu
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
use std::{io, thread};
|
||||
use std::sync::Arc;
|
||||
use byte_pool::BytePool;
|
||||
use std::sync::Arc;
|
||||
use std::{io, thread};
|
||||
|
||||
use crossbeam_utils::atomic::AtomicCell;
|
||||
|
||||
use packet::icmp::Kind;
|
||||
use packet::icmp::icmp::IcmpPacket;
|
||||
use packet::ip::ipv4;
|
||||
use packet::ip::ipv4::packet::IpV4Packet;
|
||||
use crate::channel::sender::ChannelSender;
|
||||
use crate::cipher::Cipher;
|
||||
use crate::core::status::VntWorker;
|
||||
use packet::icmp::icmp::IcmpPacket;
|
||||
use packet::icmp::Kind;
|
||||
use packet::ip::ipv4;
|
||||
use packet::ip::ipv4::packet::IpV4Packet;
|
||||
|
||||
use crate::error::*;
|
||||
use crate::external_route::ExternalRoute;
|
||||
use crate::handle::CurrentDeviceInfo;
|
||||
use crate::handle::tun_tap::channel_group::{buf_channel_group, BufSenderGroup};
|
||||
use crate::handle::CurrentDeviceInfo;
|
||||
use crate::igmp_server::IgmpServer;
|
||||
use crate::ip_proxy::IpProxyMap;
|
||||
use crate::tun_tap_device::{DeviceReader, DeviceWriter};
|
||||
@@ -40,10 +40,18 @@ fn icmp(device_writer: &DeviceWriter, mut ipv4_packet: IpV4Packet<&mut [u8]>) ->
|
||||
|
||||
/// 接收tun数据,并且转发到udp上
|
||||
#[inline]
|
||||
async fn handle(sender: &ChannelSender, data: &mut [u8], len: usize, device_writer: &DeviceWriter,
|
||||
igmp_server: &Option<IgmpServer>, current_device: CurrentDeviceInfo,
|
||||
ip_route: &Option<ExternalRoute>, proxy_map: &Option<IpProxyMap>,
|
||||
client_cipher: &Cipher, server_cipher: &Cipher) -> Result<()> {
|
||||
async fn handle(
|
||||
sender: &ChannelSender,
|
||||
data: &mut [u8],
|
||||
len: usize,
|
||||
device_writer: &DeviceWriter,
|
||||
igmp_server: &Option<IgmpServer>,
|
||||
current_device: CurrentDeviceInfo,
|
||||
ip_route: &Option<ExternalRoute>,
|
||||
proxy_map: &Option<IpProxyMap>,
|
||||
client_cipher: &Cipher,
|
||||
server_cipher: &Cipher,
|
||||
) -> Result<()> {
|
||||
let ipv4_packet = if let Ok(ipv4_packet) = IpV4Packet::new(&mut data[12..len]) {
|
||||
ipv4_packet
|
||||
} else {
|
||||
@@ -57,30 +65,62 @@ async fn handle(sender: &ChannelSender, data: &mut [u8], len: usize, device_writ
|
||||
if src_ip == dest_ip {
|
||||
return icmp(&device_writer, ipv4_packet);
|
||||
}
|
||||
return crate::handle::tun_tap::base_handle(sender, data, len, igmp_server,
|
||||
current_device, ip_route, proxy_map, client_cipher, server_cipher).await;
|
||||
return crate::handle::tun_tap::base_handle(
|
||||
sender,
|
||||
data,
|
||||
len,
|
||||
igmp_server,
|
||||
current_device,
|
||||
ip_route,
|
||||
proxy_map,
|
||||
client_cipher,
|
||||
server_cipher,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
pub async fn start(worker: VntWorker, sender: ChannelSender,
|
||||
device_reader: DeviceReader,
|
||||
device_writer: DeviceWriter,
|
||||
igmp_server: Option<IgmpServer>,
|
||||
current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
|
||||
ip_route: Option<ExternalRoute>,
|
||||
ip_proxy_map: Option<IpProxyMap>,
|
||||
client_cipher: Cipher, server_cipher: Cipher, parallel: usize) {
|
||||
pub async fn start(
|
||||
worker: VntWorker,
|
||||
sender: ChannelSender,
|
||||
device_reader: DeviceReader,
|
||||
device_writer: DeviceWriter,
|
||||
igmp_server: Option<IgmpServer>,
|
||||
current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
|
||||
ip_route: Option<ExternalRoute>,
|
||||
ip_proxy_map: Option<IpProxyMap>,
|
||||
client_cipher: Cipher,
|
||||
server_cipher: Cipher,
|
||||
parallel: usize,
|
||||
) {
|
||||
if parallel == 1 {
|
||||
thread::Builder::new().name("tun_handler".into()).spawn(move || {
|
||||
tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all().build().unwrap()
|
||||
.block_on(async move {
|
||||
if let Err(e) = start_simple(sender, device_reader, &device_writer, igmp_server, current_device, ip_route, ip_proxy_map, client_cipher, server_cipher).await {
|
||||
log::warn!("stop:{}",e);
|
||||
}
|
||||
let _ = device_writer.close();
|
||||
worker.stop_all();
|
||||
})
|
||||
}).unwrap();
|
||||
thread::Builder::new()
|
||||
.name("tun_handler".into())
|
||||
.spawn(move || {
|
||||
tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.unwrap()
|
||||
.block_on(async move {
|
||||
if let Err(e) = start_simple(
|
||||
sender,
|
||||
device_reader,
|
||||
&device_writer,
|
||||
igmp_server,
|
||||
current_device,
|
||||
ip_route,
|
||||
ip_proxy_map,
|
||||
client_cipher,
|
||||
server_cipher,
|
||||
)
|
||||
.await
|
||||
{
|
||||
log::warn!("stop:{}", e);
|
||||
}
|
||||
let _ = device_writer.close();
|
||||
worker.stop_all();
|
||||
})
|
||||
})
|
||||
.unwrap();
|
||||
} else {
|
||||
let (buf_sender, buf_receiver) = buf_channel_group(parallel);
|
||||
for mut buf_receiver in buf_receiver.0 {
|
||||
@@ -94,8 +134,20 @@ pub async fn start(worker: VntWorker, sender: ChannelSender,
|
||||
let server_cipher = server_cipher.clone();
|
||||
tokio::spawn(async move {
|
||||
while let Some((mut buf, start, len)) = buf_receiver.recv().await {
|
||||
match handle(&sender, &mut buf[start..], len, &device_writer, &igmp_server, current_device.load(),
|
||||
&ip_route, &ip_proxy_map, &client_cipher, &server_cipher).await {
|
||||
match handle(
|
||||
&sender,
|
||||
&mut buf[start..],
|
||||
len,
|
||||
&device_writer,
|
||||
&igmp_server,
|
||||
current_device.load(),
|
||||
&ip_route,
|
||||
&ip_proxy_map,
|
||||
&client_cipher,
|
||||
&server_cipher,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
log::warn!("{:?}", e)
|
||||
@@ -105,21 +157,30 @@ pub async fn start(worker: VntWorker, sender: ChannelSender,
|
||||
});
|
||||
}
|
||||
|
||||
thread::Builder::new().name("tun_handler".into()).spawn(move || {
|
||||
tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all().build().unwrap()
|
||||
.block_on(async move {
|
||||
if let Err(e) = start_(sender, device_reader, buf_sender).await {
|
||||
log::warn!("stop:{}",e);
|
||||
}
|
||||
let _ = device_writer.close();
|
||||
worker.stop_all();
|
||||
})
|
||||
}).unwrap();
|
||||
thread::Builder::new()
|
||||
.name("tun_handler".into())
|
||||
.spawn(move || {
|
||||
tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.unwrap()
|
||||
.block_on(async move {
|
||||
if let Err(e) = start_(sender, device_reader, buf_sender).await {
|
||||
log::warn!("stop:{}", e);
|
||||
}
|
||||
let _ = device_writer.close();
|
||||
worker.stop_all();
|
||||
})
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
async fn start_(sender: ChannelSender, device_reader: DeviceReader, mut buf_sender: BufSenderGroup) -> io::Result<()> {
|
||||
async fn start_(
|
||||
sender: ChannelSender,
|
||||
device_reader: DeviceReader,
|
||||
mut buf_sender: BufSenderGroup,
|
||||
) -> io::Result<()> {
|
||||
loop {
|
||||
let mut buf = POOL.alloc(4096);
|
||||
buf[..12].fill(0);
|
||||
@@ -129,21 +190,27 @@ async fn start_(sender: ChannelSender, device_reader: DeviceReader, mut buf_send
|
||||
let start = 0;
|
||||
let len = device_reader.read(&mut buf[12..])? + 12;
|
||||
#[cfg(any(target_os = "macos"))]
|
||||
let start = 4;
|
||||
let start = 4;
|
||||
if !buf_sender.send((buf, start, len)).await {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "tun buf_sender发送失败"));
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"tun buf_sender发送失败",
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn start_simple(sender: ChannelSender,
|
||||
device_reader: DeviceReader,
|
||||
device_writer: &DeviceWriter,
|
||||
igmp_server: Option<IgmpServer>,
|
||||
current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
|
||||
ip_route: Option<ExternalRoute>,
|
||||
ip_proxy_map: Option<IpProxyMap>,
|
||||
client_cipher: Cipher, server_cipher: Cipher) -> io::Result<()> {
|
||||
async fn start_simple(
|
||||
sender: ChannelSender,
|
||||
device_reader: DeviceReader,
|
||||
device_writer: &DeviceWriter,
|
||||
igmp_server: Option<IgmpServer>,
|
||||
current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
|
||||
ip_route: Option<ExternalRoute>,
|
||||
ip_proxy_map: Option<IpProxyMap>,
|
||||
client_cipher: Cipher,
|
||||
server_cipher: Cipher,
|
||||
) -> io::Result<()> {
|
||||
let mut buf = [0; 4096];
|
||||
loop {
|
||||
if sender.is_close() {
|
||||
@@ -152,8 +219,21 @@ async fn start_simple(sender: ChannelSender,
|
||||
buf[..12].fill(0);
|
||||
let len = device_reader.read(&mut buf[12..])? + 12;
|
||||
#[cfg(any(target_os = "macos"))]
|
||||
let mut buf = &mut buf[4..];
|
||||
match handle(&sender, &mut buf, len, device_writer, &igmp_server, current_device.load(), &ip_route, &ip_proxy_map, &client_cipher, &server_cipher).await {
|
||||
let mut buf = &mut buf[4..];
|
||||
match handle(
|
||||
&sender,
|
||||
&mut buf,
|
||||
len,
|
||||
device_writer,
|
||||
&igmp_server,
|
||||
current_device.load(),
|
||||
&ip_route,
|
||||
&ip_proxy_map,
|
||||
&client_cipher,
|
||||
&server_cipher,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
log::warn!("{:?}", e)
|
||||
|
||||
+41
-40
@@ -1,14 +1,14 @@
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::net::Ipv4Addr;
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
use crate::tun_tap_device::DeviceWriter;
|
||||
use dashmap::DashMap;
|
||||
use parking_lot::RwLock;
|
||||
use packet::igmp::igmp_v2::IgmpV2Packet;
|
||||
use packet::igmp::igmp_v3::{IgmpV3QueryPacket, IgmpV3RecordType, IgmpV3ReportPacket};
|
||||
use packet::igmp::IgmpType;
|
||||
use packet::ip::ipv4::protocol::Protocol;
|
||||
use crate::tun_tap_device::DeviceWriter;
|
||||
use parking_lot::RwLock;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::net::Ipv4Addr;
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
//1. 定时发送query,启动时20秒一次,连发3次,之后8分钟一次
|
||||
//2. 接收网关的igmp report 维护组播源信息
|
||||
@@ -90,9 +90,7 @@ impl IgmpServer {
|
||||
std::thread::sleep(Duration::from_secs(20))
|
||||
}
|
||||
});
|
||||
Self {
|
||||
multicast,
|
||||
}
|
||||
Self { multicast }
|
||||
}
|
||||
pub fn load(&self, multicast_addr: &Ipv4Addr) -> Option<Arc<RwLock<Multicast>>> {
|
||||
if let Some(entry) = self.multicast.get(multicast_addr) {
|
||||
@@ -125,9 +123,11 @@ impl IgmpServer {
|
||||
return Ok(());
|
||||
}
|
||||
let multi = {
|
||||
self.multicast.entry(multicast_addr).or_insert_with(|| {
|
||||
Arc::new(RwLock::new(Multicast::new()))
|
||||
}).value().clone()
|
||||
self.multicast
|
||||
.entry(multicast_addr)
|
||||
.or_insert_with(|| Arc::new(RwLock::new(Multicast::new())))
|
||||
.value()
|
||||
.clone()
|
||||
};
|
||||
let mut guard = multi.write();
|
||||
guard.members.insert(source, Instant::now());
|
||||
@@ -153,13 +153,17 @@ impl IgmpServer {
|
||||
if !multicast_addr.is_multicast() {
|
||||
return Ok(());
|
||||
}
|
||||
let multi = self.multicast.entry(multicast_addr).or_insert_with(|| {
|
||||
Arc::new(RwLock::new(Multicast::new()))
|
||||
}).value().clone();
|
||||
let multi = self
|
||||
.multicast
|
||||
.entry(multicast_addr)
|
||||
.or_insert_with(|| Arc::new(RwLock::new(Multicast::new())))
|
||||
.value()
|
||||
.clone();
|
||||
let mut guard = multi.write();
|
||||
|
||||
match group_record.record_type() {
|
||||
IgmpV3RecordType::ModeIsInclude | IgmpV3RecordType::ChangeToIncludeMode => {
|
||||
IgmpV3RecordType::ModeIsInclude
|
||||
| IgmpV3RecordType::ChangeToIncludeMode => {
|
||||
match group_record.source_addresses() {
|
||||
None => {
|
||||
//不接收所有
|
||||
@@ -173,7 +177,8 @@ impl IgmpServer {
|
||||
}
|
||||
}
|
||||
|
||||
IgmpV3RecordType::ModeIsExclude | IgmpV3RecordType::ChangeToExcludeMode => {
|
||||
IgmpV3RecordType::ModeIsExclude
|
||||
| IgmpV3RecordType::ChangeToExcludeMode => {
|
||||
match group_record.source_addresses() {
|
||||
None => {
|
||||
//接收所有
|
||||
@@ -190,40 +195,36 @@ impl IgmpServer {
|
||||
//在已有源的基础上,接收目标源,如果是排除模式,则删除;是包含模式则添加
|
||||
match group_record.source_addresses() {
|
||||
None => {}
|
||||
Some(src) => {
|
||||
match guard.map.get_mut(&source) {
|
||||
None => {}
|
||||
Some((is_include, set)) => {
|
||||
for ip in src {
|
||||
if *is_include {
|
||||
set.insert(ip);
|
||||
} else {
|
||||
set.remove(&ip);
|
||||
}
|
||||
Some(src) => match guard.map.get_mut(&source) {
|
||||
None => {}
|
||||
Some((is_include, set)) => {
|
||||
for ip in src {
|
||||
if *is_include {
|
||||
set.insert(ip);
|
||||
} else {
|
||||
set.remove(&ip);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
IgmpV3RecordType::BlockOldSources => {
|
||||
//在已有源的基础上,不接收目标源
|
||||
match group_record.source_addresses() {
|
||||
None => {}
|
||||
Some(src) => {
|
||||
match guard.map.get_mut(&source) {
|
||||
None => {}
|
||||
Some((is_include, set)) => {
|
||||
for ip in src {
|
||||
if *is_include {
|
||||
set.remove(&ip);
|
||||
} else {
|
||||
set.insert(ip);
|
||||
}
|
||||
Some(src) => match guard.map.get_mut(&source) {
|
||||
None => {}
|
||||
Some((is_include, set)) => {
|
||||
for ip in src {
|
||||
if *is_include {
|
||||
set.remove(&ip);
|
||||
} else {
|
||||
set.insert(ip);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
IgmpV3RecordType::Unknown(_) => {}
|
||||
@@ -235,4 +236,4 @@ impl IgmpServer {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
use crossbeam_utils::atomic::AtomicCell;
|
||||
use dashmap::DashMap;
|
||||
use std::io;
|
||||
use std::mem::MaybeUninit;
|
||||
use std::net::{IpAddr, Ipv4Addr, SocketAddrV4};
|
||||
use std::sync::Arc;
|
||||
use crossbeam_utils::atomic::AtomicCell;
|
||||
use dashmap::DashMap;
|
||||
|
||||
use socket2::{Domain, SockAddr, Socket, Type};
|
||||
|
||||
use packet::icmp::icmp;
|
||||
use packet::icmp::icmp::HeaderOther;
|
||||
use packet::ip::ipv4;
|
||||
use crate::channel::sender::ChannelSender;
|
||||
use crate::cipher::Cipher;
|
||||
use crate::handle::CurrentDeviceInfo;
|
||||
use crate::protocol::{MAX_TTL, NetPacket, Protocol, Version};
|
||||
use crate::protocol::body::ENCRYPTION_RESERVED;
|
||||
use crate::protocol::{NetPacket, Protocol, Version, MAX_TTL};
|
||||
use packet::icmp::icmp;
|
||||
use packet::icmp::icmp::HeaderOther;
|
||||
use packet::ip::ipv4;
|
||||
|
||||
pub struct IcmpProxy {
|
||||
icmp_socket: Arc<Socket>,
|
||||
@@ -26,9 +26,18 @@ pub struct IcmpProxy {
|
||||
}
|
||||
|
||||
impl IcmpProxy {
|
||||
pub fn new(addr: SocketAddrV4, icmp_proxy_map: Arc<DashMap<(Ipv4Addr, u16, u16), Ipv4Addr>>,
|
||||
sender: ChannelSender, current_device: Arc<AtomicCell<CurrentDeviceInfo>>, client_cipher: Cipher) -> io::Result<IcmpProxy> {
|
||||
let icmp_socket = Arc::new(Socket::new(Domain::IPV4, Type::RAW, Some(socket2::Protocol::ICMPV4))?);
|
||||
pub fn new(
|
||||
addr: SocketAddrV4,
|
||||
icmp_proxy_map: Arc<DashMap<(Ipv4Addr, u16, u16), Ipv4Addr>>,
|
||||
sender: ChannelSender,
|
||||
current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
|
||||
client_cipher: Cipher,
|
||||
) -> io::Result<IcmpProxy> {
|
||||
let icmp_socket = Arc::new(Socket::new(
|
||||
Domain::IPV4,
|
||||
Type::RAW,
|
||||
Some(socket2::Protocol::ICMPV4),
|
||||
)?);
|
||||
icmp_socket.bind(&SockAddr::from(addr))?;
|
||||
Ok(IcmpProxy {
|
||||
icmp_socket,
|
||||
@@ -43,8 +52,7 @@ impl IcmpProxy {
|
||||
}
|
||||
pub fn start(self) {
|
||||
let mut buf = [0 as u8; 1500];
|
||||
let data: &mut [MaybeUninit<u8>] =
|
||||
unsafe { std::mem::transmute(&mut buf[..]) };
|
||||
let data: &mut [MaybeUninit<u8>] = unsafe { std::mem::transmute(&mut buf[..]) };
|
||||
|
||||
loop {
|
||||
match self.recv(data) {
|
||||
@@ -57,29 +65,54 @@ impl IcmpProxy {
|
||||
Ok(icmp_packet) => {
|
||||
match icmp_packet.header_other() {
|
||||
HeaderOther::Identifier(id, seq) => {
|
||||
if let Some(entry) = self.icmp_proxy_map.get(&(peer_ip, id, seq)) {
|
||||
if let Some(entry) =
|
||||
self.icmp_proxy_map.get(&(peer_ip, id, seq))
|
||||
{
|
||||
//将数据发送到真实的来源
|
||||
let dest_ip = *entry.value();
|
||||
drop(entry);
|
||||
ipv4_packet.set_destination_ip(dest_ip);
|
||||
ipv4_packet.update_checksum();
|
||||
let current_device = self.current_device.load();
|
||||
let virtual_ip = current_device.virtual_ip();
|
||||
let connect_server = current_device.connect_server;
|
||||
let mut net_packet = NetPacket::new_encrypt(vec![0u8; 12 + len + ENCRYPTION_RESERVED]).unwrap();
|
||||
let current_device =
|
||||
self.current_device.load();
|
||||
let virtual_ip =
|
||||
current_device.virtual_ip();
|
||||
let connect_server =
|
||||
current_device.connect_server;
|
||||
let mut net_packet =
|
||||
NetPacket::new_encrypt(vec![
|
||||
0u8;
|
||||
12 + len + ENCRYPTION_RESERVED
|
||||
])
|
||||
.unwrap();
|
||||
net_packet.set_version(Version::V1);
|
||||
net_packet.set_protocol(Protocol::IpTurn);
|
||||
net_packet.set_transport_protocol(crate::protocol::ip_turn_packet::Protocol::Ipv4.into());
|
||||
net_packet.first_set_ttl(MAX_TTL);
|
||||
net_packet.set_source(virtual_ip);
|
||||
net_packet.set_destination(dest_ip);
|
||||
net_packet.set_payload(ipv4_packet.buffer).unwrap();
|
||||
if let Err(e) = self.client_cipher.encrypt_ipv4(&mut net_packet) {
|
||||
log::warn!("加密失败:{}",e);
|
||||
net_packet
|
||||
.set_payload(ipv4_packet.buffer)
|
||||
.unwrap();
|
||||
if let Err(e) = self
|
||||
.client_cipher
|
||||
.encrypt_ipv4(&mut net_packet)
|
||||
{
|
||||
log::warn!("加密失败:{}", e);
|
||||
continue;
|
||||
}
|
||||
if self.sender.try_send_by_id(net_packet.buffer(), &dest_ip).is_err() {
|
||||
let _ = self.sender.try_send_main(net_packet.buffer(), connect_server);
|
||||
if self
|
||||
.sender
|
||||
.try_send_by_id(
|
||||
net_packet.buffer(),
|
||||
&dest_ip,
|
||||
)
|
||||
.is_err()
|
||||
{
|
||||
let _ = self.sender.try_send_main(
|
||||
net_packet.buffer(),
|
||||
connect_server,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -98,7 +131,7 @@ impl IcmpProxy {
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
log::warn!("icmp代理异常:{:?}",e);
|
||||
log::warn!("icmp代理异常:{:?}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -106,16 +139,12 @@ impl IcmpProxy {
|
||||
fn recv(&self, buf: &mut [MaybeUninit<u8>]) -> io::Result<(usize, IpAddr)> {
|
||||
let (size, addr) = self.icmp_socket.recv_from(buf)?;
|
||||
let addr = match addr.as_socket() {
|
||||
None => {
|
||||
IpAddr::V4(Ipv4Addr::UNSPECIFIED)
|
||||
}
|
||||
Some(add) => {
|
||||
add.ip()
|
||||
}
|
||||
None => IpAddr::V4(Ipv4Addr::UNSPECIFIED),
|
||||
Some(add) => add.ip(),
|
||||
};
|
||||
Ok((size, addr))
|
||||
}
|
||||
// fn send_to(&self, buf: &[u8], addr: SocketAddrV4) -> io::Result<usize> {
|
||||
// self.icmp_socket.send_to(buf, &SockAddr::from(addr))
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
+36
-22
@@ -1,16 +1,16 @@
|
||||
use std::{io, thread};
|
||||
use std::net::{Ipv4Addr, SocketAddrV4};
|
||||
use std::sync::Arc;
|
||||
use crossbeam_utils::atomic::AtomicCell;
|
||||
use dashmap::DashMap;
|
||||
use socket2::{SockAddr, Socket};
|
||||
use tokio::net::{TcpListener, UdpSocket};
|
||||
use crate::channel::sender::ChannelSender;
|
||||
use crate::cipher::Cipher;
|
||||
use crate::handle::CurrentDeviceInfo;
|
||||
use crate::ip_proxy::icmp_proxy::IcmpProxy;
|
||||
use crate::ip_proxy::tcp_proxy::TcpProxy;
|
||||
use crate::ip_proxy::udp_proxy::UdpProxy;
|
||||
use crossbeam_utils::atomic::AtomicCell;
|
||||
use dashmap::DashMap;
|
||||
use socket2::{SockAddr, Socket};
|
||||
use std::net::{Ipv4Addr, SocketAddrV4};
|
||||
use std::sync::Arc;
|
||||
use std::{io, thread};
|
||||
use tokio::net::{TcpListener, UdpSocket};
|
||||
|
||||
pub mod icmp_proxy;
|
||||
pub mod tcp_proxy;
|
||||
@@ -37,13 +37,18 @@ pub struct IpProxyMap {
|
||||
|
||||
impl IpProxyMap {
|
||||
pub fn send_icmp(&self, buf: &[u8], dest: &Ipv4Addr) -> io::Result<usize> {
|
||||
self.icmp_socket.send_to(buf, &SockAddr::from(SocketAddrV4::new(*dest, 0)))
|
||||
self.icmp_socket
|
||||
.send_to(buf, &SockAddr::from(SocketAddrV4::new(*dest, 0)))
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn init_proxy(sender: ChannelSender, current_device: Arc<AtomicCell<CurrentDeviceInfo>>, client_cipher: Cipher,) -> io::Result<(TcpProxy, UdpProxy, IpProxyMap)> {
|
||||
let tcp_proxy_map: Arc<DashMap<SocketAddrV4, SocketAddrV4>> = Arc::new(DashMap::new());
|
||||
let udp_proxy_map: Arc<DashMap<SocketAddrV4, SocketAddrV4>> = Arc::new(DashMap::new());
|
||||
pub async fn init_proxy(
|
||||
sender: ChannelSender,
|
||||
current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
|
||||
client_cipher: Cipher,
|
||||
) -> io::Result<(TcpProxy, UdpProxy, IpProxyMap)> {
|
||||
let tcp_proxy_map: Arc<DashMap<SocketAddrV4, SocketAddrV4>> = Arc::new(DashMap::new());
|
||||
let udp_proxy_map: Arc<DashMap<SocketAddrV4, SocketAddrV4>> = Arc::new(DashMap::new());
|
||||
let icmp_proxy_map: Arc<DashMap<(Ipv4Addr, u16, u16), Ipv4Addr>> = Arc::new(DashMap::new());
|
||||
let tcp_listener = TcpListener::bind("0.0.0.0:0").await?;
|
||||
let udp_socket = UdpSocket::bind("0.0.0.0:0").await?;
|
||||
@@ -52,19 +57,28 @@ pub async fn init_proxy(sender: ChannelSender, current_device: Arc<AtomicCell<Cu
|
||||
let tcp_proxy = TcpProxy::new(tcp_listener, tcp_proxy_map.clone());
|
||||
let udp_proxy = UdpProxy::new(udp_socket, udp_proxy_map.clone());
|
||||
let addr = SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 0);
|
||||
let icmp_proxy = IcmpProxy::new(addr, icmp_proxy_map.clone(),
|
||||
sender.clone(), current_device.clone(),client_cipher)?;
|
||||
let icmp_proxy = IcmpProxy::new(
|
||||
addr,
|
||||
icmp_proxy_map.clone(),
|
||||
sender.clone(),
|
||||
current_device.clone(),
|
||||
client_cipher,
|
||||
)?;
|
||||
let icmp_socket = icmp_proxy.icmp_socket();
|
||||
thread::spawn(move || {
|
||||
icmp_proxy.start();
|
||||
});
|
||||
|
||||
Ok((tcp_proxy, udp_proxy, IpProxyMap {
|
||||
tcp_proxy_port,
|
||||
udp_proxy_port,
|
||||
tcp_proxy_map,
|
||||
udp_proxy_map,
|
||||
icmp_proxy_map,
|
||||
icmp_socket,
|
||||
}))
|
||||
}
|
||||
Ok((
|
||||
tcp_proxy,
|
||||
udp_proxy,
|
||||
IpProxyMap {
|
||||
tcp_proxy_port,
|
||||
udp_proxy_port,
|
||||
tcp_proxy_map,
|
||||
udp_proxy_map,
|
||||
icmp_proxy_map,
|
||||
icmp_socket,
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use dashmap::DashMap;
|
||||
use std::io;
|
||||
use std::net::{SocketAddr, SocketAddrV4};
|
||||
use std::sync::Arc;
|
||||
use dashmap::DashMap;
|
||||
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
|
||||
@@ -11,7 +11,10 @@ pub struct TcpProxy {
|
||||
}
|
||||
|
||||
impl TcpProxy {
|
||||
pub fn new(tcp_listener: TcpListener, tcp_proxy_map: Arc<DashMap<SocketAddrV4, SocketAddrV4>>) -> Self {
|
||||
pub fn new(
|
||||
tcp_listener: TcpListener,
|
||||
tcp_proxy_map: Arc<DashMap<SocketAddrV4, SocketAddrV4>>,
|
||||
) -> Self {
|
||||
Self {
|
||||
tcp_listener,
|
||||
tcp_proxy_map,
|
||||
@@ -22,33 +25,36 @@ impl TcpProxy {
|
||||
let tcp_proxy_map = self.tcp_proxy_map;
|
||||
loop {
|
||||
match tcp_listener.accept().await {
|
||||
Ok((tcp_stream, sender_addr)) => {
|
||||
match sender_addr {
|
||||
SocketAddr::V4(sender_addr) => {
|
||||
if let Some(entry) = tcp_proxy_map.get(&sender_addr) {
|
||||
let dest_addr = *entry.value();
|
||||
drop(entry);
|
||||
let peer_tcp_stream = match TcpStream::connect(dest_addr).await {
|
||||
Ok(peer_tcp_stream) => { peer_tcp_stream }
|
||||
Err(e) => {
|
||||
log::warn!("tcp代理异常:{:?},来源:{},目标:{}",e,sender_addr,dest_addr);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = proxy(tcp_stream, peer_tcp_stream).await {
|
||||
log::warn!("{}->{},{}",sender_addr,dest_addr,e);
|
||||
}
|
||||
});
|
||||
}else {
|
||||
log::warn!("tcp代理异常: 来源:{},未找到目标",sender_addr);
|
||||
}
|
||||
Ok((tcp_stream, sender_addr)) => match sender_addr {
|
||||
SocketAddr::V4(sender_addr) => {
|
||||
if let Some(entry) = tcp_proxy_map.get(&sender_addr) {
|
||||
let dest_addr = *entry.value();
|
||||
drop(entry);
|
||||
let peer_tcp_stream = match TcpStream::connect(dest_addr).await {
|
||||
Ok(peer_tcp_stream) => peer_tcp_stream,
|
||||
Err(e) => {
|
||||
log::warn!(
|
||||
"tcp代理异常:{:?},来源:{},目标:{}",
|
||||
e,
|
||||
sender_addr,
|
||||
dest_addr
|
||||
);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = proxy(tcp_stream, peer_tcp_stream).await {
|
||||
log::warn!("{}->{},{}", sender_addr, dest_addr, e);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
log::warn!("tcp代理异常: 来源:{},未找到目标", sender_addr);
|
||||
}
|
||||
SocketAddr::V6(_) => {}
|
||||
}
|
||||
}
|
||||
SocketAddr::V6(_) => {}
|
||||
},
|
||||
Err(e) => {
|
||||
log::warn!("tcp代理监听:{:?}",e);
|
||||
log::warn!("tcp代理监听:{:?}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use dashmap::DashMap;
|
||||
use std::io;
|
||||
use std::net::{SocketAddr, SocketAddrV4};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use dashmap::DashMap;
|
||||
use tokio::net::UdpSocket;
|
||||
|
||||
/// 一个udp代理,作用是利用系统协议栈,将udp数据报解析出来再转发到目的地址
|
||||
@@ -14,10 +14,7 @@ pub struct UdpProxy {
|
||||
impl UdpProxy {
|
||||
pub fn new(udp_socket: UdpSocket, map: Arc<DashMap<SocketAddrV4, SocketAddrV4>>) -> Self {
|
||||
let udp_socket = Arc::new(udp_socket);
|
||||
Self {
|
||||
udp_socket,
|
||||
map,
|
||||
}
|
||||
Self { udp_socket, map }
|
||||
}
|
||||
pub async fn start(self) {
|
||||
let map = self.map;
|
||||
@@ -27,28 +24,33 @@ impl UdpProxy {
|
||||
|
||||
loop {
|
||||
match udp_socket.recv_from(&mut buf).await {
|
||||
Ok((len, sender_addr)) => {
|
||||
match sender_addr {
|
||||
SocketAddr::V4(sender_addr) => {
|
||||
match start0(&buf[..len], sender_addr, &inner_map, &map, &udp_socket).await {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
log::warn!("udp代理异常:{:?},来源:{}",e,sender_addr);
|
||||
}
|
||||
Ok((len, sender_addr)) => match sender_addr {
|
||||
SocketAddr::V4(sender_addr) => {
|
||||
match start0(&buf[..len], sender_addr, &inner_map, &map, &udp_socket).await
|
||||
{
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
log::warn!("udp代理异常:{:?},来源:{}", e, sender_addr);
|
||||
}
|
||||
}
|
||||
SocketAddr::V6(_) => {}
|
||||
}
|
||||
}
|
||||
SocketAddr::V6(_) => {}
|
||||
},
|
||||
Err(e) => {
|
||||
log::warn!("udp代理异常:{:?}",e);
|
||||
log::warn!("udp代理异常:{:?}", e);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn start0(buf: &[u8], sender_addr: SocketAddrV4, inner_map: &Arc<DashMap<SocketAddrV4, Arc<UdpSocket>>>, map: &Arc<DashMap<SocketAddrV4, SocketAddrV4>>, udp_socket: &Arc<UdpSocket>) -> io::Result<()> {
|
||||
async fn start0(
|
||||
buf: &[u8],
|
||||
sender_addr: SocketAddrV4,
|
||||
inner_map: &Arc<DashMap<SocketAddrV4, Arc<UdpSocket>>>,
|
||||
map: &Arc<DashMap<SocketAddrV4, SocketAddrV4>>,
|
||||
udp_socket: &Arc<UdpSocket>,
|
||||
) -> io::Result<()> {
|
||||
if let Some(entry) = inner_map.get(&sender_addr) {
|
||||
let udp = entry.value().clone();
|
||||
drop(entry);
|
||||
@@ -67,27 +69,35 @@ async fn start0(buf: &[u8], sender_addr: SocketAddrV4, inner_map: &Arc<DashMap<S
|
||||
tokio::spawn(async move {
|
||||
let mut buf = [0u8; 65536];
|
||||
loop {
|
||||
match tokio::time::timeout(Duration::from_secs(300), peer_udp_socket.recv(&mut buf)).await {
|
||||
Ok(rs) => {
|
||||
match rs {
|
||||
Ok(len) => {
|
||||
match udp_socket.send_to(&buf[..len], sender_addr).await {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
log::warn!("udp代理异常:{:?},来源:{},目标:{}",e,sender_addr,dest_addr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
match tokio::time::timeout(Duration::from_secs(300), peer_udp_socket.recv(&mut buf))
|
||||
.await
|
||||
{
|
||||
Ok(rs) => match rs {
|
||||
Ok(len) => match udp_socket.send_to(&buf[..len], sender_addr).await {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
log::warn!("udp代理异常:{:?},来源:{},目标:{}",e,sender_addr,dest_addr);
|
||||
log::warn!(
|
||||
"udp代理异常:{:?},来源:{},目标:{}",
|
||||
e,
|
||||
sender_addr,
|
||||
dest_addr
|
||||
);
|
||||
break;
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
log::warn!(
|
||||
"udp代理异常:{:?},来源:{},目标:{}",
|
||||
e,
|
||||
sender_addr,
|
||||
dest_addr
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(_) => {
|
||||
//超时关闭
|
||||
log::warn!("udp代理超时关闭,来源:{},目标:{}",sender_addr,dest_addr);
|
||||
log::warn!("udp代理超时关闭,来源:{},目标:{}", sender_addr, dest_addr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -97,4 +107,4 @@ async fn start0(buf: &[u8], sender_addr: SocketAddrV4, inner_map: &Arc<DashMap<S
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
+7
-7
@@ -1,17 +1,17 @@
|
||||
use crate::error::Error;
|
||||
pub const VNT_VERSION:&'static str = "1.2.2";
|
||||
pub const VNT_VERSION: &'static str = "1.2.2";
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
pub mod channel;
|
||||
pub mod cipher;
|
||||
pub mod core;
|
||||
pub mod error;
|
||||
pub mod external_route;
|
||||
pub mod handle;
|
||||
pub mod igmp_server;
|
||||
pub mod ip_proxy;
|
||||
pub mod nat;
|
||||
pub mod proto;
|
||||
pub mod protocol;
|
||||
pub mod ip_proxy;
|
||||
pub mod external_route;
|
||||
pub mod igmp_server;
|
||||
pub mod tun_tap_device;
|
||||
pub mod core;
|
||||
pub mod channel;
|
||||
pub mod util;
|
||||
pub mod cipher;
|
||||
|
||||
+15
-32
@@ -1,6 +1,6 @@
|
||||
use std::io;
|
||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6};
|
||||
use std::net::UdpSocket;
|
||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6};
|
||||
use std::sync::Arc;
|
||||
|
||||
use parking_lot::Mutex;
|
||||
@@ -15,12 +15,8 @@ pub fn local_ipv4() -> io::Result<Ipv4Addr> {
|
||||
socket.connect("8.8.8.8:80")?;
|
||||
let addr = socket.local_addr()?;
|
||||
match addr.ip() {
|
||||
IpAddr::V4(ip) => {
|
||||
Ok(ip)
|
||||
}
|
||||
IpAddr::V6(_) => {
|
||||
Ok(Ipv4Addr::UNSPECIFIED)
|
||||
}
|
||||
IpAddr::V4(ip) => Ok(ip),
|
||||
IpAddr::V6(_) => Ok(Ipv4Addr::UNSPECIFIED),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,22 +25,16 @@ pub fn local_ipv6() -> io::Result<Ipv6Addr> {
|
||||
socket.connect("[2001:4860:4860::8888]:80")?;
|
||||
let addr = socket.local_addr()?;
|
||||
match addr.ip() {
|
||||
IpAddr::V4(_) => {
|
||||
Ok(Ipv6Addr::UNSPECIFIED)
|
||||
}
|
||||
IpAddr::V6(ip) => {
|
||||
Ok(ip)
|
||||
}
|
||||
IpAddr::V4(_) => Ok(Ipv6Addr::UNSPECIFIED),
|
||||
IpAddr::V6(ip) => Ok(ip),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn local_ipv4_addr(port: u16) -> SocketAddrV4 {
|
||||
match local_ipv4() {
|
||||
Ok(ipv4) => {
|
||||
SocketAddrV4::new(ipv4, port)
|
||||
}
|
||||
Ok(ipv4) => SocketAddrV4::new(ipv4, port),
|
||||
Err(e) => {
|
||||
log::warn!("获取本地ipv4地址失败:{}",e);
|
||||
log::warn!("获取本地ipv4地址失败:{}", e);
|
||||
SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 0)
|
||||
}
|
||||
}
|
||||
@@ -52,11 +42,9 @@ pub fn local_ipv4_addr(port: u16) -> SocketAddrV4 {
|
||||
|
||||
pub fn local_ipv6_addr(port: u16) -> SocketAddrV6 {
|
||||
match local_ipv6() {
|
||||
Ok(ipv6) => {
|
||||
SocketAddrV6::new(ipv6, port, 0, 0)
|
||||
}
|
||||
Ok(ipv6) => SocketAddrV6::new(ipv6, port, 0, 0),
|
||||
Err(e) => {
|
||||
log::warn!("获取本地ipv6地址失败:{}",e);
|
||||
log::warn!("获取本地ipv6地址失败:{}", e);
|
||||
SocketAddrV6::new(Ipv6Addr::UNSPECIFIED, 0, 0, 0)
|
||||
}
|
||||
}
|
||||
@@ -105,19 +93,13 @@ impl NatTest {
|
||||
NatType::Cone,
|
||||
);
|
||||
let info = Arc::new(Mutex::new(nat_info));
|
||||
let nat_test = NatTest {
|
||||
stun_server,
|
||||
info,
|
||||
};
|
||||
let nat_test = NatTest { stun_server, info };
|
||||
{
|
||||
let nat_test = nat_test.clone();
|
||||
tokio::spawn(async move {
|
||||
let _ = nat_test.re_test(
|
||||
public_ip,
|
||||
public_port,
|
||||
local_ipv4_addr,
|
||||
ipv6_addr,
|
||||
).await;
|
||||
let _ = nat_test
|
||||
.re_test(public_ip, public_port, local_ipv4_addr, ipv6_addr)
|
||||
.await;
|
||||
});
|
||||
}
|
||||
nat_test
|
||||
@@ -145,7 +127,8 @@ impl NatTest {
|
||||
public_port,
|
||||
local_ipv4_addr,
|
||||
ipv6_addr,
|
||||
).await;
|
||||
)
|
||||
.await;
|
||||
*self.info.lock() = info.clone();
|
||||
info
|
||||
}
|
||||
|
||||
+18
-10
@@ -3,9 +3,9 @@ use std::io;
|
||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::channel::punch::NatType;
|
||||
use stun_format::Attr;
|
||||
use tokio::net::UdpSocket;
|
||||
use crate::channel::punch::NatType;
|
||||
|
||||
pub async fn stun_test_nat(stun_servers: Vec<String>) -> io::Result<(NatType, Vec<Ipv4Addr>, u16)> {
|
||||
let mut h = Vec::new();
|
||||
@@ -68,21 +68,30 @@ async fn test_nat(stun_server: String) -> io::Result<(NatType, Vec<Ipv4Addr>, u1
|
||||
Ok((nat_type, hash_set.into_iter().collect(), port_range))
|
||||
}
|
||||
|
||||
async fn test_nat_(udp: &UdpSocket, change_ip: bool, change_port: bool) -> io::Result<(SocketAddr, SocketAddr)> {
|
||||
async fn test_nat_(
|
||||
udp: &UdpSocket,
|
||||
change_ip: bool,
|
||||
change_port: bool,
|
||||
) -> io::Result<(SocketAddr, SocketAddr)> {
|
||||
for _ in 0..2 {
|
||||
let mut buf = [0u8; 28];
|
||||
let mut msg = stun_format::MsgBuilder::from(buf.as_mut_slice());
|
||||
msg.typ(stun_format::MsgType::BindingRequest).unwrap();
|
||||
msg.tid(1).unwrap();
|
||||
msg.add_attr(Attr::ChangeRequest { change_ip, change_port }).unwrap();
|
||||
msg.add_attr(Attr::ChangeRequest {
|
||||
change_ip,
|
||||
change_port,
|
||||
})
|
||||
.unwrap();
|
||||
udp.send(msg.as_bytes()).await?;
|
||||
let mut buf = [0; 10240];
|
||||
let (len, addr) = match tokio::time::timeout(Duration::from_millis(300), udp.recv_from(&mut buf)).await {
|
||||
Ok(rs) => { rs? }
|
||||
Err(_) => {
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let (len, addr) =
|
||||
match tokio::time::timeout(Duration::from_millis(300), udp.recv_from(&mut buf)).await {
|
||||
Ok(rs) => rs?,
|
||||
Err(_) => {
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let msg = stun_format::Msg::from(&buf[..len]);
|
||||
let mut mapped_addr = None;
|
||||
let mut changed_addr = None;
|
||||
@@ -126,4 +135,3 @@ fn stun_addr(addr: stun_format::SocketAddr) -> SocketAddr {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+84
-68
@@ -2,26 +2,26 @@ use std::{fmt, io};
|
||||
|
||||
pub const ENCRYPTION_RESERVED: usize = 32;
|
||||
/* aes_gcm加密数据体
|
||||
0 15 31
|
||||
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| 数据体 |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| random(32) |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| tag(32) |
|
||||
| tag(32) |
|
||||
| tag(32) |
|
||||
| tag(32) |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| finger(32) |
|
||||
| finger(32) |
|
||||
| finger(32) |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
0 15 31
|
||||
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| 数据体 |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| random(32) |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| tag(32) |
|
||||
| tag(32) |
|
||||
| tag(32) |
|
||||
| tag(32) |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| finger(32) |
|
||||
| finger(32) |
|
||||
| finger(32) |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
|
||||
注:finger用于快速校验数据是否被修改,上层可使用token、协议头参与计算finger,
|
||||
确保服务端和客户端都能感知修改(服务端不能解密也能校验指纹)
|
||||
*/
|
||||
注:finger用于快速校验数据是否被修改,上层可使用token、协议头参与计算finger,
|
||||
确保服务端和客户端都能感知修改(服务端不能解密也能校验指纹)
|
||||
*/
|
||||
pub struct SecretBody<B> {
|
||||
buffer: B,
|
||||
exist_finger: bool,
|
||||
@@ -30,11 +30,7 @@ pub struct SecretBody<B> {
|
||||
impl<B: AsRef<[u8]>> SecretBody<B> {
|
||||
pub fn new(buffer: B, exist_finger: bool) -> io::Result<SecretBody<B>> {
|
||||
let len = buffer.as_ref().len();
|
||||
let min_len = if exist_finger {
|
||||
32
|
||||
} else {
|
||||
32 - 12
|
||||
};
|
||||
let min_len = if exist_finger { 32 } else { 32 - 12 };
|
||||
// 不能大于udp最大载荷长度
|
||||
if len < min_len || len > 65535 - 20 - 8 - 12 {
|
||||
return Err(io::Error::new(
|
||||
@@ -42,7 +38,10 @@ impl<B: AsRef<[u8]>> SecretBody<B> {
|
||||
"SecretBody length overflow",
|
||||
));
|
||||
}
|
||||
Ok(SecretBody { buffer, exist_finger })
|
||||
Ok(SecretBody {
|
||||
buffer,
|
||||
exist_finger,
|
||||
})
|
||||
}
|
||||
pub fn random(&self) -> u32 {
|
||||
let mut end = self.buffer.as_ref().len() - 16;
|
||||
@@ -109,13 +108,19 @@ impl<B: AsRef<[u8]> + AsMut<[u8]>> SecretBody<B> {
|
||||
pub fn set_finger(&mut self, finger: &[u8]) -> io::Result<()> {
|
||||
if self.exist_finger {
|
||||
if finger.len() != 12 {
|
||||
return Err(io::Error::new(io::ErrorKind::InvalidData, "finger.len != 12"));
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
"finger.len != 12",
|
||||
));
|
||||
}
|
||||
let end = self.buffer.as_ref().len();
|
||||
self.buffer.as_mut()[end - 12..end].copy_from_slice(finger);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(io::Error::new(io::ErrorKind::InvalidData, "not exist finger"))
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
"not exist finger",
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,21 +163,21 @@ impl<B: AsRef<[u8]>> fmt::Debug for SecretBody<B> {
|
||||
}
|
||||
}
|
||||
/* aes_cbc加密数据体
|
||||
0 15 31
|
||||
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| 数据体 |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| random(32) |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| finger(32) |
|
||||
| finger(32) |
|
||||
| finger(32) |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
0 15 31
|
||||
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| 数据体 |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| random(32) |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| finger(32) |
|
||||
| finger(32) |
|
||||
| finger(32) |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
|
||||
注:finger用于快速校验数据是否被修改,上层可使用token、协议头参与计算finger,
|
||||
确保服务端和客户端都能感知修改(服务端不能解密也能校验指纹)
|
||||
*/
|
||||
注:finger用于快速校验数据是否被修改,上层可使用token、协议头参与计算finger,
|
||||
确保服务端和客户端都能感知修改(服务端不能解密也能校验指纹)
|
||||
*/
|
||||
pub struct AesCbcSecretBody<B> {
|
||||
buffer: B,
|
||||
exist_finger: bool,
|
||||
@@ -181,11 +186,7 @@ pub struct AesCbcSecretBody<B> {
|
||||
impl<B: AsRef<[u8]>> AesCbcSecretBody<B> {
|
||||
pub fn new(buffer: B, exist_finger: bool) -> io::Result<AesCbcSecretBody<B>> {
|
||||
let len = buffer.as_ref().len();
|
||||
let min_len = if exist_finger {
|
||||
16
|
||||
} else {
|
||||
16 - 12
|
||||
};
|
||||
let min_len = if exist_finger { 16 } else { 16 - 12 };
|
||||
// 不能大于udp最大载荷长度
|
||||
if len < min_len || len > 65535 - 20 - 8 - 12 {
|
||||
return Err(io::Error::new(
|
||||
@@ -193,7 +194,10 @@ impl<B: AsRef<[u8]>> AesCbcSecretBody<B> {
|
||||
"AesCbcSecretBody length overflow",
|
||||
));
|
||||
}
|
||||
Ok(AesCbcSecretBody { buffer, exist_finger })
|
||||
Ok(AesCbcSecretBody {
|
||||
buffer,
|
||||
exist_finger,
|
||||
})
|
||||
}
|
||||
pub fn en_body(&self) -> &[u8] {
|
||||
let mut end = self.buffer.as_ref().len();
|
||||
@@ -223,13 +227,19 @@ impl<B: AsRef<[u8]> + AsMut<[u8]>> AesCbcSecretBody<B> {
|
||||
pub fn set_finger(&mut self, finger: &[u8]) -> io::Result<()> {
|
||||
if self.exist_finger {
|
||||
if finger.len() != 12 {
|
||||
return Err(io::Error::new(io::ErrorKind::InvalidData, "finger.len != 12"));
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
"finger.len != 12",
|
||||
));
|
||||
}
|
||||
let end = self.buffer.as_ref().len();
|
||||
self.buffer.as_mut()[end - 12..end].copy_from_slice(finger);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(io::Error::new(io::ErrorKind::InvalidData, "cbc not exist finger"))
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
"cbc not exist finger",
|
||||
))
|
||||
}
|
||||
}
|
||||
pub fn en_body_mut(&mut self) -> &mut [u8] {
|
||||
@@ -242,22 +252,22 @@ impl<B: AsRef<[u8]> + AsMut<[u8]>> AesCbcSecretBody<B> {
|
||||
}
|
||||
|
||||
/* rsa加密数据体
|
||||
0 15 31
|
||||
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| 数据体(n) |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| random(32) |
|
||||
| random(32) |
|
||||
| random(32) |
|
||||
| random(32) |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| finger(32) |
|
||||
| finger(32) |
|
||||
| finger(32) |
|
||||
| finger(32) |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
*/
|
||||
0 15 31
|
||||
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| 数据体(n) |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| random(32) |
|
||||
| random(32) |
|
||||
| random(32) |
|
||||
| random(32) |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| finger(32) |
|
||||
| finger(32) |
|
||||
| finger(32) |
|
||||
| finger(32) |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
*/
|
||||
pub struct RsaSecretBody<B> {
|
||||
buffer: B,
|
||||
}
|
||||
@@ -298,7 +308,10 @@ impl<B: AsRef<[u8]>> RsaSecretBody<B> {
|
||||
impl<B: AsRef<[u8]> + AsMut<[u8]>> RsaSecretBody<B> {
|
||||
pub fn set_random(&mut self, random: &[u8]) -> io::Result<()> {
|
||||
if random.len() != 16 {
|
||||
return Err(io::Error::new(io::ErrorKind::InvalidData, "random.len != 16"));
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
"random.len != 16",
|
||||
));
|
||||
}
|
||||
let end = self.buffer.as_ref().len() - 16;
|
||||
self.buffer.as_mut()[end - 16..end].copy_from_slice(random);
|
||||
@@ -310,10 +323,13 @@ impl<B: AsRef<[u8]> + AsMut<[u8]>> RsaSecretBody<B> {
|
||||
}
|
||||
pub fn set_finger(&mut self, finger: &[u8]) -> io::Result<()> {
|
||||
if finger.len() != 16 {
|
||||
return Err(io::Error::new(io::ErrorKind::InvalidData, "finger.len != 16"));
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
"finger.len != 16",
|
||||
));
|
||||
}
|
||||
let end = self.buffer.as_ref().len();
|
||||
self.buffer.as_mut()[end - 16..end].copy_from_slice(finger);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use std::{fmt, io};
|
||||
use std::net::Ipv4Addr;
|
||||
use std::{fmt, io};
|
||||
|
||||
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
|
||||
pub enum Protocol {
|
||||
|
||||
@@ -40,7 +40,10 @@ impl<B: AsRef<[u8]>> BroadcastPacket<B> {
|
||||
let len = buffer.as_ref().len();
|
||||
let packet = Self::unchecked(buffer);
|
||||
if len < 2 + 4 || packet.addr_num() == 0 {
|
||||
Err(io::Error::new(io::ErrorKind::InvalidData, "BroadcastPacket InvalidData"))
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
"BroadcastPacket InvalidData",
|
||||
))
|
||||
} else {
|
||||
Ok(packet)
|
||||
}
|
||||
|
||||
+11
-5
@@ -1,6 +1,6 @@
|
||||
use std::{fmt, io};
|
||||
use std::net::Ipv4Addr;
|
||||
use crate::protocol::body::ENCRYPTION_RESERVED;
|
||||
use std::net::Ipv4Addr;
|
||||
use std::{fmt, io};
|
||||
|
||||
/*
|
||||
0 15 31
|
||||
@@ -21,9 +21,9 @@ pub const HEAD_LEN: usize = 12;
|
||||
pub mod body;
|
||||
pub mod control_packet;
|
||||
pub mod error_packet;
|
||||
pub mod service_packet;
|
||||
pub mod ip_turn_packet;
|
||||
pub mod other_turn_packet;
|
||||
pub mod service_packet;
|
||||
|
||||
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
|
||||
pub enum Version {
|
||||
@@ -230,7 +230,10 @@ impl<B: AsRef<[u8]> + AsMut<[u8]>> NetPacket<B> {
|
||||
}
|
||||
pub fn set_payload(&mut self, payload: &[u8]) -> io::Result<()> {
|
||||
if self.data_len - 12 != payload.len() {
|
||||
return Err(io::Error::new(io::ErrorKind::InvalidData, "data_len - 12 != payload.len"));
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
"data_len - 12 != payload.len",
|
||||
));
|
||||
}
|
||||
self.buffer.as_mut()[12..self.data_len].copy_from_slice(payload);
|
||||
Ok(())
|
||||
@@ -240,7 +243,10 @@ impl<B: AsRef<[u8]> + AsMut<[u8]>> NetPacket<B> {
|
||||
}
|
||||
pub fn set_data_len(&mut self, data_len: usize) -> io::Result<()> {
|
||||
if data_len > self.buffer.as_ref().len() || data_len < 12 {
|
||||
return Err(io::Error::new(io::ErrorKind::InvalidData, "data_len invalid"));
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
"data_len invalid",
|
||||
));
|
||||
}
|
||||
self.data_len = data_len;
|
||||
Ok(())
|
||||
|
||||
@@ -45,4 +45,4 @@ impl DeviceReader {
|
||||
|
||||
pub fn create(fd: i32) -> (DeviceWriter, DeviceReader) {
|
||||
(DeviceWriter(fd as _), DeviceReader(fd as _))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,24 @@
|
||||
use crate::tun_tap_device::linux_mac::DeviceW;
|
||||
use crate::tun_tap_device::{DeviceReader, DeviceType, DeviceWriter, DriverInfo};
|
||||
use parking_lot::Mutex;
|
||||
use std::io;
|
||||
use std::net::Ipv4Addr;
|
||||
use crate::tun_tap_device::{DeviceReader, DeviceType, DeviceWriter, DriverInfo};
|
||||
use tun::Device;
|
||||
use parking_lot::Mutex;
|
||||
use std::process::Command;
|
||||
use std::sync::Arc;
|
||||
use crate::tun_tap_device::linux_mac::DeviceW;
|
||||
use tun::Device;
|
||||
|
||||
impl DeviceWriter {
|
||||
pub fn change_ip(&self, address: Ipv4Addr, netmask: Ipv4Addr,
|
||||
gateway: Ipv4Addr, _old_netmask: Ipv4Addr, _old_gateway: Ipv4Addr) -> io::Result<()> {
|
||||
pub fn change_ip(
|
||||
&self,
|
||||
address: Ipv4Addr,
|
||||
netmask: Ipv4Addr,
|
||||
gateway: Ipv4Addr,
|
||||
_old_netmask: Ipv4Addr,
|
||||
_old_gateway: Ipv4Addr,
|
||||
) -> io::Result<()> {
|
||||
let mut config = tun::Configuration::default();
|
||||
let broadcast_address = (!u32::from_be_bytes(netmask.octets()))
|
||||
| u32::from_be_bytes(gateway.octets());
|
||||
let broadcast_address =
|
||||
(!u32::from_be_bytes(netmask.octets())) | u32::from_be_bytes(gateway.octets());
|
||||
let broadcast_address = Ipv4Addr::from(broadcast_address);
|
||||
config
|
||||
.destination(gateway)
|
||||
@@ -33,37 +39,45 @@ impl DeviceWriter {
|
||||
// add_route(name, address, netmask)?;
|
||||
// 广播和组播路由
|
||||
add_route(name, Ipv4Addr::BROADCAST, Ipv4Addr::BROADCAST)?;
|
||||
add_route(name, Ipv4Addr::from([224, 0, 0, 0]), Ipv4Addr::from([240, 0, 0, 0]))?;
|
||||
add_route(
|
||||
name,
|
||||
Ipv4Addr::from([224, 0, 0, 0]),
|
||||
Ipv4Addr::from([240, 0, 0, 0]),
|
||||
)?;
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_route(name: &str, address: Ipv4Addr, netmask: Ipv4Addr) -> io::Result<()> {
|
||||
let route_add_str: String = format!(
|
||||
"ip route add {:?}/{:?} dev {}",
|
||||
address, netmask, name
|
||||
);
|
||||
let route_add_str: String = format!("ip route add {:?}/{:?} dev {}", address, netmask, name);
|
||||
let route_add_out = Command::new("sh")
|
||||
.arg("-c")
|
||||
.arg(&route_add_str)
|
||||
.output()
|
||||
.expect("sh exec error!");
|
||||
if !route_add_out.status.success() {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, format!("添加路由失败: cmd:{},out:{:?}", route_add_str, route_add_out)));
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!(
|
||||
"添加路由失败: cmd:{},out:{:?}",
|
||||
route_add_str, route_add_out
|
||||
),
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn create_device(device_type: DeviceType,
|
||||
address: Ipv4Addr,
|
||||
netmask: Ipv4Addr,
|
||||
gateway: Ipv4Addr,
|
||||
in_ips: Vec<(Ipv4Addr, Ipv4Addr)>,
|
||||
mtu: u16,
|
||||
) -> io::Result<(DeviceWriter, DeviceReader,DriverInfo)> {
|
||||
pub fn create_device(
|
||||
device_type: DeviceType,
|
||||
address: Ipv4Addr,
|
||||
netmask: Ipv4Addr,
|
||||
gateway: Ipv4Addr,
|
||||
in_ips: Vec<(Ipv4Addr, Ipv4Addr)>,
|
||||
mtu: u16,
|
||||
) -> io::Result<(DeviceWriter, DeviceReader, DriverInfo)> {
|
||||
let mut config = tun::Configuration::default();
|
||||
let broadcast_address = (!u32::from_be_bytes(netmask.octets()))
|
||||
| u32::from_be_bytes(gateway.octets());
|
||||
let broadcast_address =
|
||||
(!u32::from_be_bytes(netmask.octets())) | u32::from_be_bytes(gateway.octets());
|
||||
let broadcast_address = Ipv4Addr::from(broadcast_address);
|
||||
config
|
||||
.destination(gateway)
|
||||
@@ -92,11 +106,13 @@ pub fn create_device(device_type: DeviceType,
|
||||
// add_route(name, address, netmask)?;
|
||||
// 广播和组播路由
|
||||
add_route(name, Ipv4Addr::BROADCAST, Ipv4Addr::BROADCAST)?;
|
||||
add_route(name, Ipv4Addr::from([224, 0, 0, 0]), Ipv4Addr::from([240, 0, 0, 0]))?;
|
||||
add_route(
|
||||
name,
|
||||
Ipv4Addr::from([224, 0, 0, 0]),
|
||||
Ipv4Addr::from([240, 0, 0, 0]),
|
||||
)?;
|
||||
let device_w = match device_type {
|
||||
DeviceType::Tun => {
|
||||
DeviceW::Tun(writer)
|
||||
}
|
||||
DeviceType::Tun => DeviceW::Tun(writer),
|
||||
DeviceType::Tap => {
|
||||
let get_mac_cmd = format!("cat /sys/class/net/{}/address", name);
|
||||
let mac_out = Command::new("sh")
|
||||
@@ -105,7 +121,10 @@ pub fn create_device(device_type: DeviceType,
|
||||
.output()
|
||||
.expect("sh exec error!");
|
||||
if !mac_out.status.success() {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, format!("获取mac地址错误: {:?}", mac_out)));
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("获取mac地址错误: {:?}", mac_out),
|
||||
));
|
||||
}
|
||||
let mac_str = String::from_utf8(mac_out.stdout).unwrap();
|
||||
let mut mac = [0; 6];
|
||||
@@ -118,15 +137,21 @@ pub fn create_device(device_type: DeviceType,
|
||||
};
|
||||
let driver_info = DriverInfo {
|
||||
device_type,
|
||||
name:name.to_string(),
|
||||
version:String::new(),
|
||||
name: name.to_string(),
|
||||
version: String::new(),
|
||||
mac: None,
|
||||
};
|
||||
Ok((
|
||||
DeviceWriter::new(device_w, Arc::new(Mutex::new(dev)), in_ips, address, packet_information),
|
||||
DeviceWriter::new(
|
||||
device_w,
|
||||
Arc::new(Mutex::new(dev)),
|
||||
in_ips,
|
||||
address,
|
||||
packet_information,
|
||||
),
|
||||
DeviceReader::new(reader),
|
||||
driver_info,
|
||||
))
|
||||
}
|
||||
|
||||
pub fn delete_device(_device_type: DeviceType) {}
|
||||
pub fn delete_device(_device_type: DeviceType) {}
|
||||
|
||||
@@ -2,15 +2,15 @@ use std::io;
|
||||
use std::sync::Arc;
|
||||
|
||||
use bytes::BufMut;
|
||||
use tun::platform::posix::{Reader, Writer};
|
||||
use packet::ethernet;
|
||||
use parking_lot::Mutex;
|
||||
use std::net::Ipv4Addr;
|
||||
use std::os::unix::io::AsRawFd;
|
||||
#[cfg(any(target_os = "linux"))]
|
||||
use tun::platform::linux::Device;
|
||||
#[cfg(any(target_os = "macos"))]
|
||||
use tun::platform::macos::Device;
|
||||
use parking_lot::Mutex;
|
||||
use packet::ethernet;
|
||||
use tun::platform::posix::{Reader, Writer};
|
||||
|
||||
use packet::ethernet::packet::EthernetPacket;
|
||||
#[derive(Clone)]
|
||||
@@ -22,12 +22,8 @@ pub enum DeviceW {
|
||||
impl DeviceW {
|
||||
pub fn is_tun(&self) -> bool {
|
||||
match self {
|
||||
DeviceW::Tun(_) => {
|
||||
true
|
||||
}
|
||||
DeviceW::Tap(_) => {
|
||||
false
|
||||
}
|
||||
DeviceW::Tun(_) => true,
|
||||
DeviceW::Tap(_) => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,7 +37,13 @@ pub struct DeviceWriter {
|
||||
}
|
||||
|
||||
impl DeviceWriter {
|
||||
pub fn new(writer: DeviceW,lock: Arc<Mutex<Device>>, in_ips: Vec<(Ipv4Addr, Ipv4Addr)>, _ip: Ipv4Addr, packet_information: bool) -> Self {
|
||||
pub fn new(
|
||||
writer: DeviceW,
|
||||
lock: Arc<Mutex<Device>>,
|
||||
in_ips: Vec<(Ipv4Addr, Ipv4Addr)>,
|
||||
_ip: Ipv4Addr,
|
||||
packet_information: bool,
|
||||
) -> Self {
|
||||
Self {
|
||||
writer,
|
||||
lock,
|
||||
@@ -69,33 +71,30 @@ impl DeviceWriter {
|
||||
///tun网卡写入ipv4数据
|
||||
pub fn write_ipv4_tun(&self, buf: &[u8]) -> io::Result<()> {
|
||||
match &self.writer {
|
||||
DeviceW::Tun(writer) => {
|
||||
Self::write(self.packet_information, writer, buf)
|
||||
}
|
||||
DeviceW::Tap(_) => {
|
||||
Err(io::Error::from(io::ErrorKind::Unsupported))
|
||||
}
|
||||
DeviceW::Tun(writer) => Self::write(self.packet_information, writer, buf),
|
||||
DeviceW::Tap(_) => Err(io::Error::from(io::ErrorKind::Unsupported)),
|
||||
}
|
||||
}
|
||||
/// tap网卡写入以太网帧
|
||||
pub fn write_ethernet_tap(&self, buf: &[u8]) -> io::Result<()> {
|
||||
match &self.writer {
|
||||
DeviceW::Tun(_) => {
|
||||
Err(io::Error::from(io::ErrorKind::Unsupported))
|
||||
}
|
||||
DeviceW::Tap((writer, _)) => {
|
||||
Self::write(self.packet_information, writer, buf)
|
||||
}
|
||||
DeviceW::Tun(_) => Err(io::Error::from(io::ErrorKind::Unsupported)),
|
||||
DeviceW::Tap((writer, _)) => Self::write(self.packet_information, writer, buf),
|
||||
}
|
||||
}
|
||||
///写入ipv4数据,头部必须留14字节,给tap写入以太网帧头
|
||||
pub fn write_ipv4(&self, buf: &mut [u8]) -> io::Result<()> {
|
||||
match &self.writer {
|
||||
DeviceW::Tun(writer) => {
|
||||
Self::write(self.packet_information, writer, &buf[14..])
|
||||
}
|
||||
DeviceW::Tun(writer) => Self::write(self.packet_information, writer, &buf[14..]),
|
||||
DeviceW::Tap((writer, mac)) => {
|
||||
let source_mac = [buf[14 + 12], buf[14 + 13], buf[14 + 14], buf[14 + 15], !mac[5], 234];
|
||||
let source_mac = [
|
||||
buf[14 + 12],
|
||||
buf[14 + 13],
|
||||
buf[14 + 14],
|
||||
buf[14 + 15],
|
||||
!mac[5],
|
||||
234,
|
||||
];
|
||||
let mut ethernet_packet = EthernetPacket::unchecked(buf);
|
||||
ethernet_packet.set_source(&source_mac);
|
||||
ethernet_packet.set_destination(mac);
|
||||
|
||||
@@ -1,15 +1,21 @@
|
||||
use crate::tun_tap_device::linux_mac::DeviceW;
|
||||
use crate::tun_tap_device::{DeviceReader, DeviceType, DeviceWriter, DriverInfo};
|
||||
use parking_lot::Mutex;
|
||||
use std::io;
|
||||
use std::net::Ipv4Addr;
|
||||
use crate::tun_tap_device::{DeviceReader, DeviceType, DeviceWriter, DriverInfo};
|
||||
use tun::Device;
|
||||
use parking_lot::Mutex;
|
||||
use std::process::Command;
|
||||
use std::sync::Arc;
|
||||
use crate::tun_tap_device::linux_mac::DeviceW;
|
||||
use tun::Device;
|
||||
|
||||
impl DeviceWriter {
|
||||
pub fn change_ip(&self, address: Ipv4Addr, netmask: Ipv4Addr,
|
||||
gateway: Ipv4Addr, _old_netmask: Ipv4Addr, _old_gateway: Ipv4Addr) -> io::Result<()> {
|
||||
pub fn change_ip(
|
||||
&self,
|
||||
address: Ipv4Addr,
|
||||
netmask: Ipv4Addr,
|
||||
gateway: Ipv4Addr,
|
||||
_old_netmask: Ipv4Addr,
|
||||
_old_gateway: Ipv4Addr,
|
||||
) -> io::Result<()> {
|
||||
let mut config = tun::Configuration::default();
|
||||
config
|
||||
.destination(gateway)
|
||||
@@ -21,7 +27,7 @@ impl DeviceWriter {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, format!("{:?}", e)));
|
||||
}
|
||||
if let Err(e) = config_ip(dev.name(), address, netmask, gateway) {
|
||||
log::error!("{}",e);
|
||||
log::error!("{}", e);
|
||||
}
|
||||
let name = dev.name();
|
||||
for (address, netmask) in &self.in_ips {
|
||||
@@ -31,17 +37,22 @@ impl DeviceWriter {
|
||||
add_route(name, address, netmask)?;
|
||||
// 广播和组播路由
|
||||
add_route(name, Ipv4Addr::BROADCAST, Ipv4Addr::BROADCAST)?;
|
||||
add_route(name, Ipv4Addr::from([224, 0, 0, 0]), Ipv4Addr::from([240, 0, 0, 0]))?;
|
||||
add_route(
|
||||
name,
|
||||
Ipv4Addr::from([224, 0, 0, 0]),
|
||||
Ipv4Addr::from([240, 0, 0, 0]),
|
||||
)?;
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_device(device_type: DeviceType,
|
||||
address: Ipv4Addr,
|
||||
netmask: Ipv4Addr,
|
||||
gateway: Ipv4Addr,
|
||||
in_ips: Vec<(Ipv4Addr, Ipv4Addr)>,
|
||||
mtu: u16,
|
||||
pub fn create_device(
|
||||
device_type: DeviceType,
|
||||
address: Ipv4Addr,
|
||||
netmask: Ipv4Addr,
|
||||
gateway: Ipv4Addr,
|
||||
in_ips: Vec<(Ipv4Addr, Ipv4Addr)>,
|
||||
mtu: u16,
|
||||
) -> io::Result<(DeviceWriter, DeviceReader, DriverInfo)> {
|
||||
match device_type {
|
||||
DeviceType::Tun => {}
|
||||
@@ -68,7 +79,11 @@ pub fn create_device(device_type: DeviceType,
|
||||
add_route(name, address, netmask)?;
|
||||
// 广播和组播路由
|
||||
add_route(name, Ipv4Addr::BROADCAST, Ipv4Addr::BROADCAST)?;
|
||||
add_route(name, Ipv4Addr::from([224, 0, 0, 0]), Ipv4Addr::from([240, 0, 0, 0]))?;
|
||||
add_route(
|
||||
name,
|
||||
Ipv4Addr::from([224, 0, 0, 0]),
|
||||
Ipv4Addr::from([240, 0, 0, 0]),
|
||||
)?;
|
||||
let packet_information = dev.has_packet_information();
|
||||
let queue = dev.queue(0).unwrap();
|
||||
let reader = queue.reader();
|
||||
@@ -80,9 +95,15 @@ pub fn create_device(device_type: DeviceType,
|
||||
mac: None,
|
||||
};
|
||||
Ok((
|
||||
DeviceWriter::new(DeviceW::Tun(writer), Arc::new(Mutex::new(dev)), in_ips, address, packet_information),
|
||||
DeviceWriter::new(
|
||||
DeviceW::Tun(writer),
|
||||
Arc::new(Mutex::new(dev)),
|
||||
in_ips,
|
||||
address,
|
||||
packet_information,
|
||||
),
|
||||
DeviceReader::new(reader),
|
||||
driver_info
|
||||
driver_info,
|
||||
))
|
||||
}
|
||||
|
||||
@@ -97,12 +118,23 @@ fn add_route(name: &str, address: Ipv4Addr, netmask: Ipv4Addr) -> io::Result<()>
|
||||
.output()
|
||||
.expect("sh exec error!");
|
||||
if !route_add_out.status.success() {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, format!("添加路由失败: cmd:{},out:{:?}", route_add_str, route_add_out)));
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!(
|
||||
"添加路由失败: cmd:{},out:{:?}",
|
||||
route_add_str, route_add_out
|
||||
),
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn config_ip(name: &str, address: Ipv4Addr, _netmask: Ipv4Addr, gateway: Ipv4Addr) -> io::Result<()> {
|
||||
fn config_ip(
|
||||
name: &str,
|
||||
address: Ipv4Addr,
|
||||
_netmask: Ipv4Addr,
|
||||
gateway: Ipv4Addr,
|
||||
) -> io::Result<()> {
|
||||
let up_eth_str: String = format!("ifconfig {} {:?} {:?} up ", name, address, gateway);
|
||||
let up_eth_out = Command::new("sh")
|
||||
.arg("-c")
|
||||
@@ -110,9 +142,12 @@ fn config_ip(name: &str, address: Ipv4Addr, _netmask: Ipv4Addr, gateway: Ipv4Add
|
||||
.output()
|
||||
.expect("sh exec error!");
|
||||
if !up_eth_out.status.success() {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, format!("设置网络地址失败: cmd:{},out:{:?}", up_eth_str, up_eth_out)));
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("设置网络地址失败: cmd:{},out:{:?}", up_eth_str, up_eth_out),
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn delete_device(_device_type: DeviceType) {}
|
||||
pub fn delete_device(_device_type: DeviceType) {}
|
||||
|
||||
@@ -1,25 +1,24 @@
|
||||
#[cfg(target_os = "windows")]
|
||||
mod windows;
|
||||
#[cfg(any(target_os = "linux"))]
|
||||
mod linux;
|
||||
#[cfg(target_os = "macos")]
|
||||
mod mac;
|
||||
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
||||
mod linux_mac;
|
||||
#[cfg(target_os = "android")]
|
||||
mod android;
|
||||
#[cfg(any(target_os = "linux"))]
|
||||
mod linux;
|
||||
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
||||
mod linux_mac;
|
||||
#[cfg(target_os = "macos")]
|
||||
mod mac;
|
||||
#[cfg(target_os = "windows")]
|
||||
mod windows;
|
||||
|
||||
|
||||
#[cfg(target_os = "android")]
|
||||
pub use android::create;
|
||||
#[cfg(target_os = "android")]
|
||||
pub use android::{DeviceReader, DeviceWriter};
|
||||
#[cfg(any(target_os = "linux"))]
|
||||
pub use linux::create_device;
|
||||
#[cfg(any(target_os = "linux"))]
|
||||
pub use linux::delete_device;
|
||||
#[cfg(target_os = "android")]
|
||||
pub use android::create;
|
||||
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
||||
pub use linux_mac::{DeviceWriter, DeviceReader};
|
||||
#[cfg(target_os = "android")]
|
||||
pub use android::{DeviceWriter, DeviceReader};
|
||||
pub use linux_mac::{DeviceReader, DeviceWriter};
|
||||
#[cfg(target_os = "macos")]
|
||||
pub use mac::create_device;
|
||||
#[cfg(target_os = "macos")]
|
||||
@@ -30,7 +29,7 @@ pub use windows::create_device;
|
||||
#[cfg(target_os = "windows")]
|
||||
pub use windows::delete_device;
|
||||
#[cfg(target_os = "windows")]
|
||||
pub use windows::{DeviceWriter, DeviceReader};
|
||||
pub use windows::{DeviceReader, DeviceWriter};
|
||||
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
pub enum DeviceType {
|
||||
@@ -50,4 +49,4 @@ pub struct DriverInfo {
|
||||
pub name: String,
|
||||
pub version: String,
|
||||
pub mac: Option<String>,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
use std::{io, thread};
|
||||
use crate::tun_tap_device::{DeviceType, DriverInfo};
|
||||
use libloading::Library;
|
||||
use packet::ethernet;
|
||||
use packet::ethernet::packet::EthernetPacket;
|
||||
use parking_lot::Mutex;
|
||||
use std::net::Ipv4Addr;
|
||||
use std::os::windows::process::CommandExt;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use libloading::Library;
|
||||
use parking_lot::Mutex;
|
||||
use packet::ethernet;
|
||||
use packet::ethernet::packet::EthernetPacket;
|
||||
use std::{io, thread};
|
||||
use win_tun_tap::{IFace, TapDevice, TunDevice};
|
||||
use crate::tun_tap_device::{DriverInfo, DeviceType};
|
||||
|
||||
pub const TUN_INTERFACE_NAME: &str = "Vnt-Tun-V1";
|
||||
pub const TUN_POOL_NAME: &str = "Vnt-Tun-V1";
|
||||
@@ -22,12 +22,8 @@ pub enum Device {
|
||||
impl Device {
|
||||
pub fn is_tun(&self) -> bool {
|
||||
match self {
|
||||
Device::Tun(_) => {
|
||||
true
|
||||
}
|
||||
Device::Tap(_) => {
|
||||
false
|
||||
}
|
||||
Device::Tun(_) => true,
|
||||
Device::Tap(_) => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -59,17 +55,13 @@ impl DeviceWriter {
|
||||
dev.send_packet(packet);
|
||||
Ok(())
|
||||
}
|
||||
Device::Tap(_) => {
|
||||
Err(io::Error::from(io::ErrorKind::Unsupported))
|
||||
}
|
||||
Device::Tap(_) => Err(io::Error::from(io::ErrorKind::Unsupported)),
|
||||
}
|
||||
}
|
||||
/// tap网卡写入以太网帧
|
||||
pub fn write_ethernet_tap(&self, buf: &[u8]) -> io::Result<()> {
|
||||
match self.device.as_ref() {
|
||||
Device::Tun(_) => {
|
||||
Err(io::Error::from(io::ErrorKind::Unsupported))
|
||||
}
|
||||
Device::Tun(_) => Err(io::Error::from(io::ErrorKind::Unsupported)),
|
||||
Device::Tap((dev, _)) => {
|
||||
dev.write(buf)?;
|
||||
Ok(())
|
||||
@@ -85,7 +77,14 @@ impl DeviceWriter {
|
||||
dev.send_packet(packet);
|
||||
}
|
||||
Device::Tap((dev, mac)) => {
|
||||
let source_mac = [buf[14 + 12], buf[14 + 13], buf[14 + 14], buf[14 + 15], !mac[5], 234];
|
||||
let source_mac = [
|
||||
buf[14 + 12],
|
||||
buf[14 + 13],
|
||||
buf[14 + 14],
|
||||
buf[14 + 15],
|
||||
!mac[5],
|
||||
234,
|
||||
];
|
||||
let mut ethernet_packet = EthernetPacket::unchecked(buf);
|
||||
ethernet_packet.set_source(&source_mac);
|
||||
ethernet_packet.set_destination(mac);
|
||||
@@ -105,16 +104,10 @@ impl DeviceWriter {
|
||||
) -> io::Result<()> {
|
||||
let _guard = self.lock.lock();
|
||||
let dev: &dyn IFace = match self.device.as_ref() {
|
||||
Device::Tun(dev) => {
|
||||
dev as &dyn IFace
|
||||
}
|
||||
Device::Tap((dev, _)) => {
|
||||
dev as &dyn IFace
|
||||
}
|
||||
Device::Tun(dev) => dev as &dyn IFace,
|
||||
Device::Tap((dev, _)) => dev as &dyn IFace,
|
||||
};
|
||||
if let Err(e) =
|
||||
dev.delete_route(dest(old_gateway, old_gateway), old_netmask, old_gateway)
|
||||
{
|
||||
if let Err(e) = dev.delete_route(dest(old_gateway, old_gateway), old_netmask, old_gateway) {
|
||||
log::warn!("{:?}", e);
|
||||
}
|
||||
dev.set_ip(address, netmask)?;
|
||||
@@ -125,18 +118,19 @@ impl DeviceWriter {
|
||||
dev.add_route(address, netmask, gateway, 1)?;
|
||||
// 广播和组播路由
|
||||
dev.add_route(Ipv4Addr::BROADCAST, Ipv4Addr::BROADCAST, gateway, 1)?;
|
||||
dev.add_route(Ipv4Addr::from([224, 0, 0, 0]), Ipv4Addr::from([240, 0, 0, 0]), gateway, 1)?;
|
||||
dev.add_route(
|
||||
Ipv4Addr::from([224, 0, 0, 0]),
|
||||
Ipv4Addr::from([240, 0, 0, 0]),
|
||||
gateway,
|
||||
1,
|
||||
)?;
|
||||
delete_cache();
|
||||
Ok(())
|
||||
}
|
||||
pub fn close(&self) -> io::Result<()> {
|
||||
match self.device.as_ref() {
|
||||
Device::Tun(dev) => {
|
||||
dev.shutdown()
|
||||
}
|
||||
Device::Tap((dev, _)) => {
|
||||
dev.shutdown()
|
||||
}
|
||||
Device::Tun(dev) => dev.shutdown(),
|
||||
Device::Tap((dev, _)) => dev.shutdown(),
|
||||
}
|
||||
}
|
||||
pub fn is_tun(&self) -> bool {
|
||||
@@ -161,9 +155,7 @@ pub struct DeviceReader {
|
||||
|
||||
impl DeviceReader {
|
||||
pub fn new(device: Arc<Device>) -> Self {
|
||||
Self {
|
||||
device,
|
||||
}
|
||||
Self { device }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,9 +172,7 @@ impl DeviceReader {
|
||||
buf[..len].copy_from_slice(packet);
|
||||
Ok(len)
|
||||
}
|
||||
Device::Tap((dev, _)) => {
|
||||
dev.read(buf)
|
||||
}
|
||||
Device::Tap((dev, _)) => dev.read(buf),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -224,10 +214,7 @@ fn create_tun(
|
||||
) {
|
||||
Ok(tun_device) => tun_device,
|
||||
Err(e) => {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("{:?}", e),
|
||||
));
|
||||
return Err(io::Error::new(io::ErrorKind::Other, format!("{:?}", e)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -245,7 +232,12 @@ fn create_tun(
|
||||
tun_device.add_route(address, netmask, gateway, 1)?;
|
||||
// 广播和组播路由
|
||||
tun_device.add_route(Ipv4Addr::BROADCAST, Ipv4Addr::BROADCAST, gateway, 1)?;
|
||||
tun_device.add_route(Ipv4Addr::from([224, 0, 0, 0]), Ipv4Addr::from([240, 0, 0, 0]), gateway, 1)?;
|
||||
tun_device.add_route(
|
||||
Ipv4Addr::from([224, 0, 0, 0]),
|
||||
Ipv4Addr::from([240, 0, 0, 0]),
|
||||
gateway,
|
||||
1,
|
||||
)?;
|
||||
delete_cache();
|
||||
let device = Arc::new(Device::Tun(tun_device));
|
||||
let driver_info = DriverInfo {
|
||||
@@ -257,7 +249,7 @@ fn create_tun(
|
||||
Ok((
|
||||
DeviceWriter::new(device.clone(), in_ips, address),
|
||||
DeviceReader::new(device),
|
||||
driver_info
|
||||
driver_info,
|
||||
))
|
||||
}
|
||||
}
|
||||
@@ -272,7 +264,7 @@ fn delete_cache() {
|
||||
.output()
|
||||
.unwrap();
|
||||
if !out.status.success() {
|
||||
log::warn!("删除缓存失败:{:?}",out);
|
||||
log::warn!("删除缓存失败:{:?}", out);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -318,7 +310,12 @@ fn create_tap(
|
||||
}
|
||||
// 广播和组播路由
|
||||
tap_device.add_route(Ipv4Addr::BROADCAST, Ipv4Addr::BROADCAST, gateway, 1)?;
|
||||
tap_device.add_route(Ipv4Addr::from([224, 0, 0, 0]), Ipv4Addr::from([240, 0, 0, 0]), gateway, 1)?;
|
||||
tap_device.add_route(
|
||||
Ipv4Addr::from([224, 0, 0, 0]),
|
||||
Ipv4Addr::from([240, 0, 0, 0]),
|
||||
gateway,
|
||||
1,
|
||||
)?;
|
||||
delete_cache();
|
||||
let tap = Arc::new(Device::Tap((tap_device, mac)));
|
||||
let driver_info = DriverInfo {
|
||||
@@ -330,7 +327,7 @@ fn create_tap(
|
||||
Ok((
|
||||
DeviceWriter::new(tap.clone(), in_ips, address),
|
||||
DeviceReader::new(tap),
|
||||
driver_info
|
||||
driver_info,
|
||||
))
|
||||
}
|
||||
|
||||
@@ -344,29 +341,23 @@ fn delete_tap() {
|
||||
let _ = tap_device.delete();
|
||||
}
|
||||
|
||||
pub fn create_device(device_type: DeviceType, address: Ipv4Addr,
|
||||
netmask: Ipv4Addr,
|
||||
gateway: Ipv4Addr,
|
||||
in_ips: Vec<(Ipv4Addr, Ipv4Addr)>,
|
||||
mtu: u16) -> io::Result<(DeviceWriter, DeviceReader, DriverInfo)> {
|
||||
pub fn create_device(
|
||||
device_type: DeviceType,
|
||||
address: Ipv4Addr,
|
||||
netmask: Ipv4Addr,
|
||||
gateway: Ipv4Addr,
|
||||
in_ips: Vec<(Ipv4Addr, Ipv4Addr)>,
|
||||
mtu: u16,
|
||||
) -> io::Result<(DeviceWriter, DeviceReader, DriverInfo)> {
|
||||
match device_type {
|
||||
DeviceType::Tun => {
|
||||
create_tun(address, netmask, gateway, in_ips, mtu)
|
||||
}
|
||||
DeviceType::Tap => {
|
||||
create_tap(address, netmask, gateway, in_ips, mtu)
|
||||
}
|
||||
DeviceType::Tun => create_tun(address, netmask, gateway, in_ips, mtu),
|
||||
DeviceType::Tap => create_tap(address, netmask, gateway, in_ips, mtu),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn delete_device(device_type: DeviceType) {
|
||||
match device_type {
|
||||
DeviceType::Tun => {
|
||||
delete_tun()
|
||||
}
|
||||
DeviceType::Tap => {
|
||||
delete_tap()
|
||||
}
|
||||
DeviceType::Tun => delete_tun(),
|
||||
DeviceType::Tap => delete_tap(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
pub mod wait;
|
||||
pub mod wait;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicIsize, Ordering};
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::watch::{channel, Receiver, Sender};
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -41,4 +41,4 @@ impl WaitGroup {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+17
-49
@@ -21,8 +21,8 @@ use winapi::um::winioctl::*;
|
||||
use winapi::um::winnt::*;
|
||||
use winapi::um::winreg::*;
|
||||
|
||||
use std::{io, mem, ptr};
|
||||
use std::error::Error;
|
||||
use std::{io, mem, ptr};
|
||||
use winapi::um::minwinbase::OVERLAPPED_u;
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
@@ -46,9 +46,7 @@ pub fn string_from_guid(guid: &GUID) -> io::Result<Vec<WCHAR>> {
|
||||
// GUID_STRING_CHARACTERS + 1
|
||||
let mut string = vec![0; 39];
|
||||
|
||||
match unsafe {
|
||||
StringFromGUID2(guid, string.as_mut_ptr(), string.len() as _)
|
||||
} {
|
||||
match unsafe { StringFromGUID2(guid, string.as_mut_ptr(), string.len() as _) } {
|
||||
0 => Err(io::Error::new(io::ErrorKind::Other, "Insufficent buffer")),
|
||||
_ => Ok(string),
|
||||
}
|
||||
@@ -85,12 +83,8 @@ pub fn luid_to_alias(luid: &NET_LUID) -> io::Result<Vec<WCHAR>> {
|
||||
// IF_MAX_STRING_SIZE + 1
|
||||
let mut alias = vec![0; 257];
|
||||
|
||||
match unsafe {
|
||||
ConvertInterfaceLuidToAlias(luid, alias.as_mut_ptr(), alias.len())
|
||||
} {
|
||||
0 => {
|
||||
Ok(alias)
|
||||
}
|
||||
match unsafe { ConvertInterfaceLuidToAlias(luid, alias.as_mut_ptr(), alias.len()) } {
|
||||
0 => Ok(alias),
|
||||
err => Err(io::Error::from_raw_os_error(err as _)),
|
||||
}
|
||||
}
|
||||
@@ -140,7 +134,8 @@ pub fn read_file(handle: HANDLE, buffer: &mut [u8]) -> io::Result<DWORD> {
|
||||
buffer.as_mut_ptr() as _,
|
||||
buffer.len() as _,
|
||||
&mut ret,
|
||||
&mut ip_overlapped, ) {
|
||||
&mut ip_overlapped,
|
||||
) {
|
||||
let e = io::Error::last_os_error();
|
||||
if e.raw_os_error().unwrap_or(0) == 997 {
|
||||
if 0 == GetOverlappedResult(handle, &mut ip_overlapped, &mut ret, 1) {
|
||||
@@ -191,9 +186,7 @@ pub fn create_device_info_list(guid: &GUID) -> io::Result<HDEVINFO> {
|
||||
}
|
||||
|
||||
pub fn get_class_devs(guid: &GUID, flags: DWORD) -> io::Result<HDEVINFO> {
|
||||
match unsafe {
|
||||
SetupDiGetClassDevsW(guid, ptr::null(), ptr::null_mut(), flags)
|
||||
} {
|
||||
match unsafe { SetupDiGetClassDevsW(guid, ptr::null(), ptr::null_mut(), flags) } {
|
||||
INVALID_HANDLE_VALUE => Err(io::Error::last_os_error()),
|
||||
devinfo => Ok(devinfo),
|
||||
}
|
||||
@@ -248,13 +241,8 @@ pub fn create_device_info(
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_selected_device(
|
||||
devinfo: HDEVINFO,
|
||||
devinfo_data: &SP_DEVINFO_DATA,
|
||||
) -> io::Result<()> {
|
||||
match unsafe {
|
||||
SetupDiSetSelectedDevice(devinfo, devinfo_data as *const _ as _)
|
||||
} {
|
||||
pub fn set_selected_device(devinfo: HDEVINFO, devinfo_data: &SP_DEVINFO_DATA) -> io::Result<()> {
|
||||
match unsafe { SetupDiSetSelectedDevice(devinfo, devinfo_data as *const _ as _) } {
|
||||
0 => Err(io::Error::last_os_error()),
|
||||
_ => Ok(()),
|
||||
}
|
||||
@@ -308,13 +296,8 @@ pub fn build_driver_info_list(
|
||||
devinfo_data: &SP_DEVINFO_DATA,
|
||||
driver_type: DWORD,
|
||||
) -> io::Result<()> {
|
||||
match unsafe {
|
||||
SetupDiBuildDriverInfoList(
|
||||
devinfo,
|
||||
devinfo_data as *const _ as _,
|
||||
driver_type,
|
||||
)
|
||||
} {
|
||||
match unsafe { SetupDiBuildDriverInfoList(devinfo, devinfo_data as *const _ as _, driver_type) }
|
||||
{
|
||||
0 => Err(io::Error::last_os_error()),
|
||||
_ => Ok(()),
|
||||
}
|
||||
@@ -326,11 +309,7 @@ pub fn destroy_driver_info_list(
|
||||
driver_type: DWORD,
|
||||
) -> io::Result<()> {
|
||||
match unsafe {
|
||||
SetupDiDestroyDriverInfoList(
|
||||
devinfo,
|
||||
devinfo_data as *const _ as _,
|
||||
driver_type,
|
||||
)
|
||||
SetupDiDestroyDriverInfoList(devinfo, devinfo_data as *const _ as _, driver_type)
|
||||
} {
|
||||
0 => Err(io::Error::last_os_error()),
|
||||
_ => Ok(()),
|
||||
@@ -342,8 +321,7 @@ pub fn get_driver_info_detail(
|
||||
devinfo_data: &SP_DEVINFO_DATA,
|
||||
drvinfo_data: &SP_DRVINFO_DATA_W,
|
||||
) -> io::Result<SP_DRVINFO_DETAIL_DATA_W2> {
|
||||
let mut drvinfo_detail: SP_DRVINFO_DETAIL_DATA_W2 =
|
||||
unsafe { mem::zeroed() };
|
||||
let mut drvinfo_detail: SP_DRVINFO_DETAIL_DATA_W2 = unsafe { mem::zeroed() };
|
||||
drvinfo_detail.cbSize = mem::size_of::<SP_DRVINFO_DETAIL_DATA_W>() as _;
|
||||
|
||||
match unsafe {
|
||||
@@ -402,11 +380,7 @@ pub fn call_class_installer(
|
||||
install_function: DI_FUNCTION,
|
||||
) -> io::Result<()> {
|
||||
match unsafe {
|
||||
SetupDiCallClassInstaller(
|
||||
install_function,
|
||||
devinfo,
|
||||
devinfo_data as *const _ as _,
|
||||
)
|
||||
SetupDiCallClassInstaller(install_function, devinfo, devinfo_data as *const _ as _)
|
||||
} {
|
||||
0 => Err(io::Error::last_os_error()),
|
||||
_ => Ok(()),
|
||||
@@ -444,16 +418,12 @@ pub fn notify_change_key_value(
|
||||
notify_filter: DWORD,
|
||||
milliseconds: DWORD,
|
||||
) -> io::Result<()> {
|
||||
let event = match unsafe {
|
||||
CreateEventW(ptr::null_mut(), FALSE, FALSE, ptr::null())
|
||||
} {
|
||||
let event = match unsafe { CreateEventW(ptr::null_mut(), FALSE, FALSE, ptr::null()) } {
|
||||
INVALID_HANDLE_VALUE => Err(io::Error::last_os_error()),
|
||||
event => Ok(event),
|
||||
}?;
|
||||
|
||||
match unsafe {
|
||||
RegNotifyChangeKeyValue(key, watch_subtree, notify_filter, event, TRUE)
|
||||
} {
|
||||
match unsafe { RegNotifyChangeKeyValue(key, watch_subtree, notify_filter, event, TRUE) } {
|
||||
0 => Ok(()),
|
||||
err => Err(io::Error::from_raw_os_error(err)),
|
||||
}?;
|
||||
@@ -499,9 +469,7 @@ pub fn enum_device_info(
|
||||
let mut devinfo_data: SP_DEVINFO_DATA = unsafe { mem::zeroed() };
|
||||
devinfo_data.cbSize = mem::size_of_val(&devinfo_data) as _;
|
||||
|
||||
match unsafe {
|
||||
SetupDiEnumDeviceInfo(devinfo, member_index, &mut devinfo_data)
|
||||
} {
|
||||
match unsafe { SetupDiEnumDeviceInfo(devinfo, member_index, &mut devinfo_data) } {
|
||||
0 if unsafe { GetLastError() == ERROR_NO_MORE_ITEMS } => None,
|
||||
0 => Some(Err(io::Error::last_os_error())),
|
||||
_ => Some(Ok(devinfo_data)),
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#![cfg(windows)]
|
||||
|
||||
mod tap;
|
||||
mod tun;
|
||||
mod ffi;
|
||||
mod netsh;
|
||||
mod route;
|
||||
use std::{io, net};
|
||||
mod tap;
|
||||
mod tun;
|
||||
use std::io;
|
||||
use std::net::Ipv4Addr;
|
||||
pub use tap::TapDevice;
|
||||
pub use tun::*;
|
||||
@@ -33,13 +33,15 @@ pub trait IFace {
|
||||
/// 设置ip
|
||||
fn set_ip(&self, address: Ipv4Addr, mask: Ipv4Addr) -> io::Result<()>;
|
||||
/// 设置路由
|
||||
fn add_route(&self, dest: Ipv4Addr,
|
||||
netmask: Ipv4Addr,
|
||||
gateway: Ipv4Addr, metric: u16) -> io::Result<()>;
|
||||
fn add_route(
|
||||
&self,
|
||||
dest: Ipv4Addr,
|
||||
netmask: Ipv4Addr,
|
||||
gateway: Ipv4Addr,
|
||||
metric: u16,
|
||||
) -> io::Result<()>;
|
||||
/// 删除路由
|
||||
fn delete_route(&self, dest: Ipv4Addr,
|
||||
netmask: Ipv4Addr,
|
||||
gateway: Ipv4Addr, ) -> io::Result<()>;
|
||||
fn delete_route(&self, dest: Ipv4Addr, netmask: Ipv4Addr, gateway: Ipv4Addr) -> io::Result<()>;
|
||||
/// 设置最大传输单元
|
||||
fn set_mtu(&self, mtu: u16) -> io::Result<()>;
|
||||
/// 设置跃点
|
||||
|
||||
@@ -4,14 +4,17 @@ use std::os::windows::process::CommandExt;
|
||||
|
||||
/// 设置网卡名称
|
||||
pub fn set_interface_name(old_name: &str, new_name: &str) -> io::Result<()> {
|
||||
let cmd = format!(" netsh interface set interface name={:?} newname={:?}", old_name, new_name);
|
||||
let cmd = format!(
|
||||
" netsh interface set interface name={:?} newname={:?}",
|
||||
old_name, new_name
|
||||
);
|
||||
let out = std::process::Command::new("cmd")
|
||||
.creation_flags(0x08000000) //winapi-0.3.9/src/um/winbase.rs:283
|
||||
.arg("/C")
|
||||
.arg(&cmd)
|
||||
.output()?;
|
||||
if !out.status.success() {
|
||||
log::warn!("修改网卡名称失败:cmd={:?},out={:?}",cmd,out);
|
||||
log::warn!("修改网卡名称失败:cmd={:?},out={:?}", cmd, out);
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "修改网卡名称失败"));
|
||||
}
|
||||
Ok(())
|
||||
@@ -28,8 +31,11 @@ pub fn set_interface_ip(index: u32, address: &Ipv4Addr, netmask: &Ipv4Addr) -> i
|
||||
.arg(&set_address)
|
||||
.output()?;
|
||||
if !out.status.success() {
|
||||
log::error!("cmd={:?},out={:?}",set_address,out);
|
||||
return Err(io::Error::new(io::ErrorKind::Other, format!("设置网络地址失败: {:?}", out)));
|
||||
log::error!("cmd={:?},out={:?}", set_address, out);
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("设置网络地址失败: {:?}", out),
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -45,21 +51,30 @@ pub fn set_interface_mtu(index: u32, mtu: u16) -> io::Result<()> {
|
||||
.arg(&set_mtu)
|
||||
.output()?;
|
||||
if !out.status.success() {
|
||||
log::error!("cmd={:?},out={:?}",set_mtu,out);
|
||||
return Err(io::Error::new(io::ErrorKind::Other, format!("设置mtu失败: {:?}", out)));
|
||||
log::error!("cmd={:?},out={:?}", set_mtu, out);
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("设置mtu失败: {:?}", out),
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
pub fn set_interface_metric(index: u32, metric: u16) -> io::Result<()> {
|
||||
let set_metric = format!("netsh interface ip set interface {} metric={}", index,metric);
|
||||
let set_metric = format!(
|
||||
"netsh interface ip set interface {} metric={}",
|
||||
index, metric
|
||||
);
|
||||
let out = std::process::Command::new("cmd")
|
||||
.creation_flags(0x08000000)
|
||||
.arg("/C")
|
||||
.arg(&set_metric)
|
||||
.output()?;
|
||||
if !out.status.success() {
|
||||
log::error!("cmd={:?},out={:?}",set_metric,out);
|
||||
return Err(io::Error::new(io::ErrorKind::Other, format!("设置metric失败: {:?}", out)));
|
||||
log::error!("cmd={:?},out={:?}", set_metric, out);
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("设置metric失败: {:?}", out),
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,9 +3,13 @@ use std::net::Ipv4Addr;
|
||||
use std::os::windows::process::CommandExt;
|
||||
|
||||
/// 添加路由
|
||||
pub fn add_route(index: u32, dest: Ipv4Addr,
|
||||
netmask: Ipv4Addr,
|
||||
gateway: Ipv4Addr, metric: u16) -> io::Result<()> {
|
||||
pub fn add_route(
|
||||
index: u32,
|
||||
dest: Ipv4Addr,
|
||||
netmask: Ipv4Addr,
|
||||
gateway: Ipv4Addr,
|
||||
metric: u16,
|
||||
) -> io::Result<()> {
|
||||
let set_route = format!(
|
||||
"route add {:?} mask {:?} {:?} metric {} if {}",
|
||||
dest, netmask, gateway, metric, index
|
||||
@@ -18,16 +22,27 @@ pub fn add_route(index: u32, dest: Ipv4Addr,
|
||||
.output()
|
||||
.unwrap();
|
||||
if !out.status.success() {
|
||||
log::error!("cmd={:?},out={:?}",set_route,out);
|
||||
return Err(io::Error::new(io::ErrorKind::Other, format!("添加路由失败: {:?}", out)));
|
||||
log::error!("cmd={:?},out={:?}", set_route, out);
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("添加路由失败: {:?}", out),
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 删除路由
|
||||
pub fn delete_route(index: u32, dest: Ipv4Addr, netmask: Ipv4Addr, gateway: Ipv4Addr) -> io::Result<()> {
|
||||
pub fn delete_route(
|
||||
index: u32,
|
||||
dest: Ipv4Addr,
|
||||
netmask: Ipv4Addr,
|
||||
gateway: Ipv4Addr,
|
||||
) -> io::Result<()> {
|
||||
if index == 0 {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, format!("网络接口索引错误: {:?}", index)));
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("网络接口索引错误: {:?}", index),
|
||||
));
|
||||
}
|
||||
let delete_route = format!(
|
||||
"route delete {:?} mask {:?} {:?} if {}",
|
||||
@@ -41,7 +56,10 @@ pub fn delete_route(index: u32, dest: Ipv4Addr, netmask: Ipv4Addr, gateway: Ipv4
|
||||
.output()
|
||||
.unwrap();
|
||||
if !out.status.success() {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, format!("删除路由失败: {:?}", out)));
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("删除路由失败: {:?}", out),
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,22 +51,15 @@ pub fn create_interface() -> io::Result<NET_LUID> {
|
||||
ffi::build_driver_info_list(devinfo, &devinfo_data, SPDIT_COMPATDRIVER)?;
|
||||
|
||||
let _guard = guard((), |_| {
|
||||
let _ = ffi::destroy_driver_info_list(
|
||||
devinfo,
|
||||
&devinfo_data,
|
||||
SPDIT_COMPATDRIVER,
|
||||
);
|
||||
let _ = ffi::destroy_driver_info_list(devinfo, &devinfo_data, SPDIT_COMPATDRIVER);
|
||||
});
|
||||
|
||||
let mut driver_version = 0;
|
||||
let mut member_index = 0;
|
||||
|
||||
while let Some(drvinfo_data) = ffi::enum_driver_info(
|
||||
devinfo,
|
||||
&devinfo_data,
|
||||
SPDIT_COMPATDRIVER,
|
||||
member_index,
|
||||
) {
|
||||
while let Some(drvinfo_data) =
|
||||
ffi::enum_driver_info(devinfo, &devinfo_data, SPDIT_COMPATDRIVER, member_index)
|
||||
{
|
||||
member_index += 1;
|
||||
|
||||
let drvinfo_data = match drvinfo_data {
|
||||
@@ -78,14 +71,11 @@ pub fn create_interface() -> io::Result<NET_LUID> {
|
||||
continue;
|
||||
}
|
||||
|
||||
let drvinfo_detail = match ffi::get_driver_info_detail(
|
||||
devinfo,
|
||||
&devinfo_data,
|
||||
&drvinfo_data,
|
||||
) {
|
||||
Ok(drvinfo_detail) => drvinfo_detail,
|
||||
_ => continue,
|
||||
};
|
||||
let drvinfo_detail =
|
||||
match ffi::get_driver_info_detail(devinfo, &devinfo_data, &drvinfo_data) {
|
||||
Ok(drvinfo_detail) => drvinfo_detail,
|
||||
_ => continue,
|
||||
};
|
||||
|
||||
let is_compatible = drvinfo_detail
|
||||
.HardwareID
|
||||
@@ -115,16 +105,8 @@ pub fn create_interface() -> io::Result<NET_LUID> {
|
||||
|
||||
ffi::call_class_installer(devinfo, &devinfo_data, DIF_REGISTERDEVICE)?;
|
||||
|
||||
let _ = ffi::call_class_installer(
|
||||
devinfo,
|
||||
&devinfo_data,
|
||||
DIF_REGISTER_COINSTALLERS,
|
||||
);
|
||||
let _ = ffi::call_class_installer(
|
||||
devinfo,
|
||||
&devinfo_data,
|
||||
DIF_INSTALLINTERFACES,
|
||||
);
|
||||
let _ = ffi::call_class_installer(devinfo, &devinfo_data, DIF_REGISTER_COINSTALLERS);
|
||||
let _ = ffi::call_class_installer(devinfo, &devinfo_data, DIF_INSTALLINTERFACES);
|
||||
|
||||
ffi::call_class_installer(devinfo, &devinfo_data, DIF_INSTALLDEVICE)?;
|
||||
|
||||
@@ -140,21 +122,11 @@ pub fn create_interface() -> io::Result<NET_LUID> {
|
||||
let key = RegKey::predef(key);
|
||||
|
||||
while let Err(_) = key.get_value::<DWORD, &str>("*IfType") {
|
||||
ffi::notify_change_key_value(
|
||||
key.raw_handle(),
|
||||
TRUE,
|
||||
REG_NOTIFY_CHANGE_NAME,
|
||||
2000,
|
||||
)?;
|
||||
ffi::notify_change_key_value(key.raw_handle(), TRUE, REG_NOTIFY_CHANGE_NAME, 2000)?;
|
||||
}
|
||||
|
||||
while let Err(_) = key.get_value::<DWORD, &str>("NetLuidIndex") {
|
||||
ffi::notify_change_key_value(
|
||||
key.raw_handle(),
|
||||
TRUE,
|
||||
REG_NOTIFY_CHANGE_NAME,
|
||||
2000,
|
||||
)?;
|
||||
ffi::notify_change_key_value(key.raw_handle(), TRUE, REG_NOTIFY_CHANGE_NAME, 2000)?;
|
||||
}
|
||||
|
||||
let if_type: DWORD = key.get_value("*IfType")?;
|
||||
@@ -181,8 +153,7 @@ pub fn check_interface(luid: &NET_LUID) -> io::Result<()> {
|
||||
|
||||
let mut member_index = 0;
|
||||
|
||||
while let Some(devinfo_data) = ffi::enum_device_info(devinfo, member_index)
|
||||
{
|
||||
while let Some(devinfo_data) = ffi::enum_device_info(devinfo, member_index) {
|
||||
member_index += 1;
|
||||
|
||||
let devinfo_data = match devinfo_data {
|
||||
@@ -190,14 +161,11 @@ pub fn check_interface(luid: &NET_LUID) -> io::Result<()> {
|
||||
Err(_) => continue,
|
||||
};
|
||||
|
||||
let hardware_id = match ffi::get_device_registry_property(
|
||||
devinfo,
|
||||
&devinfo_data,
|
||||
SPDRP_HARDWAREID,
|
||||
) {
|
||||
Ok(hardware_id) => hardware_id,
|
||||
Err(_) => continue,
|
||||
};
|
||||
let hardware_id =
|
||||
match ffi::get_device_registry_property(devinfo, &devinfo_data, SPDRP_HARDWAREID) {
|
||||
Ok(hardware_id) => hardware_id,
|
||||
Err(_) => continue,
|
||||
};
|
||||
|
||||
if !decode_utf16(&hardware_id).eq_ignore_ascii_case(HARDWARE_ID) {
|
||||
continue;
|
||||
@@ -238,7 +206,10 @@ pub fn check_interface(luid: &NET_LUID) -> io::Result<()> {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
Err(io::Error::new(io::ErrorKind::NotFound, "TAP Device not found"))
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::NotFound,
|
||||
"TAP Device not found",
|
||||
))
|
||||
}
|
||||
|
||||
/// Deletes an existing interface
|
||||
@@ -251,8 +222,7 @@ pub fn delete_interface(luid: &NET_LUID) -> io::Result<()> {
|
||||
|
||||
let mut member_index = 0;
|
||||
|
||||
while let Some(devinfo_data) = ffi::enum_device_info(devinfo, member_index)
|
||||
{
|
||||
while let Some(devinfo_data) = ffi::enum_device_info(devinfo, member_index) {
|
||||
member_index += 1;
|
||||
|
||||
let devinfo_data = match devinfo_data {
|
||||
@@ -260,14 +230,11 @@ pub fn delete_interface(luid: &NET_LUID) -> io::Result<()> {
|
||||
Err(_) => continue,
|
||||
};
|
||||
|
||||
let hardware_id = match ffi::get_device_registry_property(
|
||||
devinfo,
|
||||
&devinfo_data,
|
||||
SPDRP_HARDWAREID,
|
||||
) {
|
||||
Ok(hardware_id) => hardware_id,
|
||||
Err(_) => continue,
|
||||
};
|
||||
let hardware_id =
|
||||
match ffi::get_device_registry_property(devinfo, &devinfo_data, SPDRP_HARDWAREID) {
|
||||
Ok(hardware_id) => hardware_id,
|
||||
Err(_) => continue,
|
||||
};
|
||||
|
||||
if !decode_utf16(&hardware_id).eq_ignore_ascii_case(HARDWARE_ID) {
|
||||
continue;
|
||||
@@ -308,13 +275,15 @@ pub fn delete_interface(luid: &NET_LUID) -> io::Result<()> {
|
||||
return ffi::call_class_installer(devinfo, &devinfo_data, DIF_REMOVE);
|
||||
}
|
||||
|
||||
Err(io::Error::new(io::ErrorKind::NotFound, "TAP Device not found"))
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::NotFound,
|
||||
"TAP Device not found",
|
||||
))
|
||||
}
|
||||
|
||||
/// Open an handle to an interface
|
||||
pub fn open_interface(luid: &NET_LUID) -> io::Result<HANDLE> {
|
||||
let guid = ffi::luid_to_guid(luid)
|
||||
.and_then(|guid| ffi::string_from_guid(&guid))?;
|
||||
let guid = ffi::luid_to_guid(luid).and_then(|guid| ffi::string_from_guid(&guid))?;
|
||||
|
||||
let path = format!(r"\\.\Global\{}.tap", &decode_utf16(&guid));
|
||||
|
||||
@@ -323,6 +292,6 @@ pub fn open_interface(luid: &NET_LUID) -> io::Result<HANDLE> {
|
||||
GENERIC_READ | GENERIC_WRITE,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
||||
OPEN_EXISTING,
|
||||
FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED,//FILE_ATTRIBUTE_SYSTEM,
|
||||
FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, //FILE_ATTRIBUTE_SYSTEM,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use std::{io, time};
|
||||
use std::net::Ipv4Addr;
|
||||
use std::{io, time};
|
||||
|
||||
use winapi::shared::ifdef::NET_LUID;
|
||||
use winapi::um::winioctl::*;
|
||||
use winapi::um::winnt::HANDLE;
|
||||
|
||||
use crate::{decode_utf16, encode_utf16, ffi, IFace, netsh, route};
|
||||
use crate::{decode_utf16, encode_utf16, ffi, netsh, route, IFace};
|
||||
|
||||
mod iface;
|
||||
|
||||
@@ -13,7 +13,6 @@ pub struct TapDevice {
|
||||
index: u32,
|
||||
luid: NET_LUID,
|
||||
handle: HANDLE,
|
||||
|
||||
}
|
||||
|
||||
unsafe impl Send for TapDevice {}
|
||||
@@ -31,7 +30,7 @@ impl TapDevice {
|
||||
&(),
|
||||
&mut mac,
|
||||
)
|
||||
.map(|_| mac)
|
||||
.map(|_| mac)
|
||||
}
|
||||
|
||||
/// Retrieve the version of the driver
|
||||
@@ -44,7 +43,7 @@ impl TapDevice {
|
||||
&(),
|
||||
&mut version,
|
||||
)
|
||||
.map(|_| version)
|
||||
.map(|_| version)
|
||||
}
|
||||
|
||||
/// Retieve the mtu of the interface
|
||||
@@ -57,10 +56,9 @@ impl TapDevice {
|
||||
&(),
|
||||
&mut mtu,
|
||||
)
|
||||
.map(|_| mtu)
|
||||
.map(|_| mtu)
|
||||
}
|
||||
|
||||
|
||||
/// Set the status of the interface, true for connected,
|
||||
/// false for disconnected.
|
||||
pub fn set_status(&self, status: bool) -> io::Result<()> {
|
||||
@@ -98,7 +96,11 @@ impl TapDevice {
|
||||
};
|
||||
};
|
||||
let index = ffi::luid_to_index(&luid).map(|index| index as u32)?;
|
||||
Ok(Self { index, luid, handle })
|
||||
Ok(Self {
|
||||
index,
|
||||
luid,
|
||||
handle,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn open(name: &str) -> io::Result<Self> {
|
||||
@@ -109,7 +111,11 @@ impl TapDevice {
|
||||
|
||||
let handle = iface::open_interface(&luid)?;
|
||||
let index = ffi::luid_to_index(&luid).map(|index| index as u32)?;
|
||||
Ok(Self { index, luid, handle })
|
||||
Ok(Self {
|
||||
index,
|
||||
luid,
|
||||
handle,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn delete(self) -> io::Result<()> {
|
||||
@@ -140,12 +146,18 @@ impl IFace for TapDevice {
|
||||
netsh::set_interface_ip(index, &address, &mask)
|
||||
}
|
||||
|
||||
fn add_route(&self, dest: Ipv4Addr, netmask: Ipv4Addr, gateway: Ipv4Addr, metric: u16) -> io::Result<()> {
|
||||
fn add_route(
|
||||
&self,
|
||||
dest: Ipv4Addr,
|
||||
netmask: Ipv4Addr,
|
||||
gateway: Ipv4Addr,
|
||||
metric: u16,
|
||||
) -> io::Result<()> {
|
||||
let index = self.get_index()?;
|
||||
route::add_route(index, dest, netmask, gateway,metric)
|
||||
route::add_route(index, dest, netmask, gateway, metric)
|
||||
}
|
||||
|
||||
fn delete_route(&self, dest: Ipv4Addr, netmask: Ipv4Addr, gateway: Ipv4Addr) -> io::Result<()> {
|
||||
fn delete_route(&self, dest: Ipv4Addr, netmask: Ipv4Addr, gateway: Ipv4Addr) -> io::Result<()> {
|
||||
let index = self.get_index()?;
|
||||
route::delete_route(index, dest, netmask, gateway)
|
||||
}
|
||||
@@ -161,7 +173,6 @@ impl IFace for TapDevice {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl TapDevice {
|
||||
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
ffi::read_file(self.handle, buf).map(|res| res as _)
|
||||
@@ -177,6 +188,3 @@ impl Drop for TapDevice {
|
||||
let _ = iface::delete_interface(&self.luid);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use log::*;
|
||||
|
||||
use crate::tun::wintun_raw;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use widestring::U16CStr;
|
||||
use crate::tun::wintun_raw;
|
||||
|
||||
/// Sets the logger wintun will use when logging. Maps to the WintunSetLogger C function
|
||||
pub fn set_logger(win_tun: &wintun_raw::wintun, f: wintun_raw::WINTUN_LOGGER_CALLBACK) {
|
||||
|
||||
@@ -3,11 +3,11 @@ use std::net::Ipv4Addr;
|
||||
|
||||
use winapi::um::{handleapi, synchapi, winbase, winnt};
|
||||
|
||||
use crate::{decode_utf16, encode_utf16, ffi, IFace, netsh, route};
|
||||
use crate::{decode_utf16, encode_utf16, ffi, netsh, route, IFace};
|
||||
use rand::Rng;
|
||||
mod wintun_raw;
|
||||
mod log;
|
||||
pub mod packet;
|
||||
mod wintun_raw;
|
||||
|
||||
/// The maximum size of wintun's internal ring buffer (in bytes)
|
||||
pub const MAX_RING_CAPACITY: u32 = 0x400_0000;
|
||||
@@ -18,7 +18,6 @@ pub const MIN_RING_CAPACITY: u32 = 0x2_0000;
|
||||
/// Maximum pool name length including zero terminator
|
||||
pub const MAX_POOL: usize = 256;
|
||||
|
||||
|
||||
pub struct TunDevice {
|
||||
pub(crate) luid: u64,
|
||||
pub(crate) index: u32,
|
||||
@@ -38,7 +37,6 @@ pub struct TunDevice {
|
||||
|
||||
/// The adapter that owns this session
|
||||
pub(crate) adapter: wintun_raw::WINTUN_ADAPTER_HANDLE,
|
||||
|
||||
}
|
||||
|
||||
unsafe impl Send for TunDevice {}
|
||||
@@ -47,20 +45,31 @@ unsafe impl Sync for TunDevice {}
|
||||
|
||||
impl TunDevice {
|
||||
pub unsafe fn create<L>(library: L, pool: &str, name: &str) -> io::Result<Self>
|
||||
where L: Into<libloading::Library>, {
|
||||
where
|
||||
L: Into<libloading::Library>,
|
||||
{
|
||||
let win_tun = match wintun_raw::wintun::from_library(library) {
|
||||
Ok(win_tun) => { win_tun }
|
||||
Ok(win_tun) => win_tun,
|
||||
Err(e) => {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, format!("library error {:?} ", e)));
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("library error {:?} ", e),
|
||||
));
|
||||
}
|
||||
};
|
||||
let pool_utf16 = encode_utf16(pool);
|
||||
if pool_utf16.len() > MAX_POOL {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, format!("长度大于{}:{:?}", MAX_POOL, pool)));
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("长度大于{}:{:?}", MAX_POOL, pool),
|
||||
));
|
||||
}
|
||||
let name_utf16 = encode_utf16(name);
|
||||
if name_utf16.len() > MAX_POOL {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, format!("长度大于{}:{:?}", MAX_POOL, pool)));
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("长度大于{}:{:?}", MAX_POOL, pool),
|
||||
));
|
||||
}
|
||||
let mut guid_bytes: [u8; 16] = [0u8; 16];
|
||||
rand::thread_rng().fill(&mut guid_bytes);
|
||||
@@ -76,22 +85,32 @@ impl TunDevice {
|
||||
//SAFETY: the function is loaded from the wintun dll properly, we are providing valid
|
||||
//pointers, and all the strings are correct null terminated UTF-16. This safety rationale
|
||||
//applies for all Wintun* functions below
|
||||
let adapter = win_tun.WintunCreateAdapter(pool_utf16.as_ptr(), name_utf16.as_ptr(), guid_ptr);
|
||||
let adapter =
|
||||
win_tun.WintunCreateAdapter(pool_utf16.as_ptr(), name_utf16.as_ptr(), guid_ptr);
|
||||
if adapter.is_null() {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "Failed to crate adapter"));
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"Failed to crate adapter",
|
||||
));
|
||||
}
|
||||
Self::init(win_tun, adapter)
|
||||
}
|
||||
pub unsafe fn init(win_tun: wintun_raw::wintun, adapter: wintun_raw::WINTUN_ADAPTER_HANDLE) -> io::Result<Self> {
|
||||
pub unsafe fn init(
|
||||
win_tun: wintun_raw::wintun,
|
||||
adapter: wintun_raw::WINTUN_ADAPTER_HANDLE,
|
||||
) -> io::Result<Self> {
|
||||
// 开启session
|
||||
let session = win_tun.WintunStartSession(adapter, 128 * 1024);
|
||||
if session.is_null() {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "WintunStartSession failed"));
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"WintunStartSession failed",
|
||||
));
|
||||
}
|
||||
//SAFETY: We follow the contract required by CreateEventA. See MSDN
|
||||
//(the pointers are allowed to be null, and 0 is okay for the others)
|
||||
let shutdown_event = synchapi::CreateEventA(std::ptr::null_mut(),
|
||||
0, 0, std::ptr::null_mut());
|
||||
let shutdown_event =
|
||||
synchapi::CreateEventA(std::ptr::null_mut(), 0, 0, std::ptr::null_mut());
|
||||
let read_event = win_tun.WintunGetReadWaitEvent(session) as winnt::HANDLE;
|
||||
let mut luid: wintun_raw::NET_LUID = std::mem::zeroed();
|
||||
win_tun.WintunGetAdapterLUID(adapter, &mut luid as *mut wintun_raw::NET_LUID);
|
||||
@@ -107,18 +126,26 @@ impl TunDevice {
|
||||
})
|
||||
}
|
||||
pub unsafe fn delete_for_name<L>(library: L, name: &str) -> io::Result<()>
|
||||
where L: Into<libloading::Library>, {
|
||||
where
|
||||
L: Into<libloading::Library>,
|
||||
{
|
||||
let win_tun = match wintun_raw::wintun::from_library(library) {
|
||||
Ok(win_tun) => win_tun,
|
||||
Err(e) => {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, format!("library error {:?} ", e)));
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("library error {:?} ", e),
|
||||
));
|
||||
}
|
||||
};
|
||||
log::set_default_logger_if_unset(&win_tun);
|
||||
let name_utf16 = encode_utf16(name);
|
||||
let adapter = win_tun.WintunOpenAdapter(name_utf16.as_ptr());
|
||||
if adapter.is_null() {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "Failed to open adapter"));
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"Failed to open adapter",
|
||||
));
|
||||
}
|
||||
win_tun.WintunCloseAdapter(adapter);
|
||||
win_tun.WintunDeleteDriver();
|
||||
@@ -131,7 +158,10 @@ impl TunDevice {
|
||||
pub fn version(&self) -> io::Result<Version> {
|
||||
let version = unsafe { self.win_tun.WintunGetRunningDriverVersion() };
|
||||
if version == 0 {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "WintunGetRunningDriverVersion"));
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"WintunGetRunningDriverVersion",
|
||||
));
|
||||
} else {
|
||||
Ok(Version {
|
||||
major: ((version >> 16) & 0xFF) as u16,
|
||||
@@ -155,7 +185,6 @@ pub struct Version {
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
impl IFace for TunDevice {
|
||||
fn shutdown(&self) -> io::Result<()> {
|
||||
let _ = unsafe { synchapi::SetEvent(self.shutdown_event) };
|
||||
@@ -169,9 +198,7 @@ impl IFace for TunDevice {
|
||||
|
||||
fn get_name(&self) -> io::Result<String> {
|
||||
let luid = self.luid;
|
||||
ffi::luid_to_alias(&unsafe { std::mem::transmute(luid) }).map(|name| {
|
||||
decode_utf16(&name)
|
||||
})
|
||||
ffi::luid_to_alias(&unsafe { std::mem::transmute(luid) }).map(|name| decode_utf16(&name))
|
||||
}
|
||||
|
||||
fn set_name(&self, new_name: &str) -> io::Result<()> {
|
||||
@@ -179,15 +206,21 @@ impl IFace for TunDevice {
|
||||
netsh::set_interface_name(&name, new_name)
|
||||
}
|
||||
|
||||
fn set_ip(&self, address: Ipv4Addr, mask: Ipv4Addr) -> io::Result<()>{
|
||||
fn set_ip(&self, address: Ipv4Addr, mask: Ipv4Addr) -> io::Result<()> {
|
||||
netsh::set_interface_ip(self.get_index()?, &address, &mask)
|
||||
}
|
||||
|
||||
fn add_route(&self, dest: Ipv4Addr, netmask: Ipv4Addr, gateway: Ipv4Addr, metric: u16) -> io::Result<()> {
|
||||
fn add_route(
|
||||
&self,
|
||||
dest: Ipv4Addr,
|
||||
netmask: Ipv4Addr,
|
||||
gateway: Ipv4Addr,
|
||||
metric: u16,
|
||||
) -> io::Result<()> {
|
||||
route::add_route(self.get_index()?, dest, netmask, gateway, metric)
|
||||
}
|
||||
|
||||
fn delete_route(&self, dest: Ipv4Addr, netmask: Ipv4Addr, gateway: Ipv4Addr) -> io::Result<()> {
|
||||
fn delete_route(&self, dest: Ipv4Addr, netmask: Ipv4Addr, gateway: Ipv4Addr) -> io::Result<()> {
|
||||
route::delete_route(self.get_index()?, dest, netmask, gateway)
|
||||
}
|
||||
|
||||
@@ -257,14 +290,19 @@ impl TunDevice {
|
||||
)
|
||||
};
|
||||
match result {
|
||||
winbase::WAIT_FAILED => return Err(io::Error::new(io::ErrorKind::Other, "WAIT_FAILED")),
|
||||
winbase::WAIT_FAILED => {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "WAIT_FAILED"))
|
||||
}
|
||||
_ => {
|
||||
if result == winbase::WAIT_OBJECT_0 {
|
||||
//We have data!
|
||||
continue;
|
||||
} else if result == winbase::WAIT_OBJECT_0 + 1 {
|
||||
//Shutdown event triggered
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "Shutdown event triggered"));
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"Shutdown event triggered",
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -275,10 +313,14 @@ impl TunDevice {
|
||||
impl TunDevice {
|
||||
pub fn allocate_send_packet(&self, size: u16) -> io::Result<packet::TunPacket> {
|
||||
let bytes_ptr = unsafe {
|
||||
self.win_tun.WintunAllocateSendPacket(self.session, size as u32)
|
||||
self.win_tun
|
||||
.WintunAllocateSendPacket(self.session, size as u32)
|
||||
};
|
||||
if bytes_ptr.is_null() {
|
||||
Err(io::Error::new(io::ErrorKind::Other, "allocate_send_packet failed"))
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"allocate_send_packet failed",
|
||||
))
|
||||
} else {
|
||||
Ok(packet::TunPacket {
|
||||
kind: packet::Kind::SendPacketPending,
|
||||
@@ -302,7 +344,6 @@ impl TunDevice {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl Drop for TunDevice {
|
||||
fn drop(&mut self) {
|
||||
//Close adapter on drop
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
use crate::TunDevice;
|
||||
|
||||
pub(crate) enum Kind {
|
||||
@@ -12,7 +11,7 @@ pub(crate) enum Kind {
|
||||
/// Represents a wintun packet
|
||||
pub struct TunPacket<'a> {
|
||||
pub(crate) kind: Kind,
|
||||
pub(crate) size:usize,
|
||||
pub(crate) size: usize,
|
||||
pub(crate) bytes_ptr: *const u8,
|
||||
|
||||
//Share ownership of session to prevent the session from being dropped before packets that
|
||||
@@ -20,7 +19,7 @@ pub struct TunPacket<'a> {
|
||||
pub(crate) tun_device: Option<&'a TunDevice>,
|
||||
}
|
||||
|
||||
impl <'a>TunPacket<'a> {
|
||||
impl<'a> TunPacket<'a> {
|
||||
/// Returns the bytes this packet holds as &mut.
|
||||
/// The lifetime of the bytes is tied to the lifetime of this packet.
|
||||
pub fn bytes_mut(&mut self) -> &mut [u8] {
|
||||
@@ -30,11 +29,11 @@ impl <'a>TunPacket<'a> {
|
||||
/// Returns an immutable reference to the bytes this packet holds.
|
||||
/// The lifetime of the bytes is tied to the lifetime of this packet.
|
||||
pub fn bytes(&self) -> &[u8] {
|
||||
unsafe { std::slice::from_raw_parts(self.bytes_ptr,self.size) }
|
||||
unsafe { std::slice::from_raw_parts(self.bytes_ptr, self.size) }
|
||||
}
|
||||
}
|
||||
|
||||
impl <'a>Drop for TunPacket<'a> {
|
||||
impl<'a> Drop for TunPacket<'a> {
|
||||
fn drop(&mut self) {
|
||||
match self.kind {
|
||||
Kind::ReceivePacket => {
|
||||
@@ -46,7 +45,8 @@ impl <'a>Drop for TunPacket<'a> {
|
||||
// ring buffer that the wintun session owns. We return that region of
|
||||
// memory back to wintun here
|
||||
let tun_device = self.tun_device.unwrap();
|
||||
tun_device.win_tun
|
||||
tun_device
|
||||
.win_tun
|
||||
.WintunReleaseReceivePacket(tun_device.session, self.bytes_ptr)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -11,8 +11,8 @@ impl<Storage> __BindgenBitfieldUnit<Storage> {
|
||||
}
|
||||
}
|
||||
impl<Storage> __BindgenBitfieldUnit<Storage>
|
||||
where
|
||||
Storage: AsRef<[u8]> + AsMut<[u8]>,
|
||||
where
|
||||
Storage: AsRef<[u8]> + AsMut<[u8]>,
|
||||
{
|
||||
#[inline]
|
||||
pub fn get_bit(&self, index: usize) -> bool {
|
||||
@@ -112,40 +112,40 @@ fn bindgen_test_layout__GUID() {
|
||||
unsafe { &(*(::std::ptr::null::<_GUID>())).Data1 as *const _ as usize },
|
||||
0usize,
|
||||
concat!(
|
||||
"Offset of field: ",
|
||||
stringify!(_GUID),
|
||||
"::",
|
||||
stringify!(Data1)
|
||||
"Offset of field: ",
|
||||
stringify!(_GUID),
|
||||
"::",
|
||||
stringify!(Data1)
|
||||
)
|
||||
);
|
||||
assert_eq!(
|
||||
unsafe { &(*(::std::ptr::null::<_GUID>())).Data2 as *const _ as usize },
|
||||
4usize,
|
||||
concat!(
|
||||
"Offset of field: ",
|
||||
stringify!(_GUID),
|
||||
"::",
|
||||
stringify!(Data2)
|
||||
"Offset of field: ",
|
||||
stringify!(_GUID),
|
||||
"::",
|
||||
stringify!(Data2)
|
||||
)
|
||||
);
|
||||
assert_eq!(
|
||||
unsafe { &(*(::std::ptr::null::<_GUID>())).Data3 as *const _ as usize },
|
||||
6usize,
|
||||
concat!(
|
||||
"Offset of field: ",
|
||||
stringify!(_GUID),
|
||||
"::",
|
||||
stringify!(Data3)
|
||||
"Offset of field: ",
|
||||
stringify!(_GUID),
|
||||
"::",
|
||||
stringify!(Data3)
|
||||
)
|
||||
);
|
||||
assert_eq!(
|
||||
unsafe { &(*(::std::ptr::null::<_GUID>())).Data4 as *const _ as usize },
|
||||
8usize,
|
||||
concat!(
|
||||
"Offset of field: ",
|
||||
stringify!(_GUID),
|
||||
"::",
|
||||
stringify!(Data4)
|
||||
"Offset of field: ",
|
||||
stringify!(_GUID),
|
||||
"::",
|
||||
stringify!(Data4)
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -248,20 +248,20 @@ fn bindgen_test_layout__NET_LUID_LH() {
|
||||
unsafe { &(*(::std::ptr::null::<_NET_LUID_LH>())).Value as *const _ as usize },
|
||||
0usize,
|
||||
concat!(
|
||||
"Offset of field: ",
|
||||
stringify!(_NET_LUID_LH),
|
||||
"::",
|
||||
stringify!(Value)
|
||||
"Offset of field: ",
|
||||
stringify!(_NET_LUID_LH),
|
||||
"::",
|
||||
stringify!(Value)
|
||||
)
|
||||
);
|
||||
assert_eq!(
|
||||
unsafe { &(*(::std::ptr::null::<_NET_LUID_LH>())).Info as *const _ as usize },
|
||||
0usize,
|
||||
concat!(
|
||||
"Offset of field: ",
|
||||
stringify!(_NET_LUID_LH),
|
||||
"::",
|
||||
stringify!(Info)
|
||||
"Offset of field: ",
|
||||
stringify!(_NET_LUID_LH),
|
||||
"::",
|
||||
stringify!(Info)
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -310,33 +310,33 @@ pub struct wintun {
|
||||
pub WintunCloseAdapter: unsafe extern "C" fn(arg1: WINTUN_ADAPTER_HANDLE),
|
||||
pub WintunOpenAdapter: unsafe extern "C" fn(arg1: LPCWSTR) -> WINTUN_ADAPTER_HANDLE,
|
||||
pub WintunGetAdapterLUID:
|
||||
unsafe extern "C" fn(arg1: WINTUN_ADAPTER_HANDLE, arg2: *mut NET_LUID),
|
||||
unsafe extern "C" fn(arg1: WINTUN_ADAPTER_HANDLE, arg2: *mut NET_LUID),
|
||||
pub WintunGetRunningDriverVersion: unsafe extern "C" fn() -> DWORD,
|
||||
pub WintunDeleteDriver: unsafe extern "C" fn() -> BOOL,
|
||||
pub WintunSetLogger: unsafe extern "C" fn(arg1: WINTUN_LOGGER_CALLBACK),
|
||||
pub WintunStartSession:
|
||||
unsafe extern "C" fn(arg1: WINTUN_ADAPTER_HANDLE, arg2: DWORD) -> WINTUN_SESSION_HANDLE,
|
||||
unsafe extern "C" fn(arg1: WINTUN_ADAPTER_HANDLE, arg2: DWORD) -> WINTUN_SESSION_HANDLE,
|
||||
pub WintunEndSession: unsafe extern "C" fn(arg1: WINTUN_SESSION_HANDLE),
|
||||
pub WintunGetReadWaitEvent: unsafe extern "C" fn(arg1: WINTUN_SESSION_HANDLE) -> HANDLE,
|
||||
pub WintunReceivePacket:
|
||||
unsafe extern "C" fn(arg1: WINTUN_SESSION_HANDLE, arg2: *mut DWORD) -> *mut BYTE,
|
||||
unsafe extern "C" fn(arg1: WINTUN_SESSION_HANDLE, arg2: *mut DWORD) -> *mut BYTE,
|
||||
pub WintunReleaseReceivePacket:
|
||||
unsafe extern "C" fn(arg1: WINTUN_SESSION_HANDLE, arg2: *const BYTE),
|
||||
unsafe extern "C" fn(arg1: WINTUN_SESSION_HANDLE, arg2: *const BYTE),
|
||||
pub WintunAllocateSendPacket:
|
||||
unsafe extern "C" fn(arg1: WINTUN_SESSION_HANDLE, arg2: DWORD) -> *mut BYTE,
|
||||
unsafe extern "C" fn(arg1: WINTUN_SESSION_HANDLE, arg2: DWORD) -> *mut BYTE,
|
||||
pub WintunSendPacket: unsafe extern "C" fn(arg1: WINTUN_SESSION_HANDLE, arg2: *const BYTE),
|
||||
}
|
||||
impl wintun {
|
||||
pub unsafe fn new<P>(path: P) -> Result<Self, ::libloading::Error>
|
||||
where
|
||||
P: AsRef<::std::ffi::OsStr>,
|
||||
where
|
||||
P: AsRef<::std::ffi::OsStr>,
|
||||
{
|
||||
let library = ::libloading::Library::new(path)?;
|
||||
Self::from_library(library)
|
||||
}
|
||||
pub unsafe fn from_library<L>(library: L) -> Result<Self, ::libloading::Error>
|
||||
where
|
||||
L: Into<::libloading::Library>,
|
||||
where
|
||||
L: Into<::libloading::Library>,
|
||||
{
|
||||
let __library = library.into();
|
||||
let WintunCreateAdapter = __library.get(b"WintunCreateAdapter\0").map(|sym| *sym)?;
|
||||
|
||||
Reference in New Issue
Block a user