Compare commits
No commits in common. "43c2b01fca0964b7500e67086bf37e64af2f8d79" and "2096c3faac43f884c1a6534c1a248082b064b325" have entirely different histories.
43c2b01fca
...
2096c3faac
4
.vscode/settings.json
vendored
4
.vscode/settings.json
vendored
@ -1,4 +1,4 @@
|
|||||||
{
|
{
|
||||||
// "rust-analyzer.cargo.target": "x86_64-pc-windows-gnu"
|
"rust-analyzer.cargo.target": null,
|
||||||
// "rust-analyzer.cargo.features": ["tun"]
|
"rust-analyzer.cargo.features": ["tun"]
|
||||||
}
|
}
|
||||||
@ -15,6 +15,8 @@ use tokio::sync::{
|
|||||||
oneshot,
|
oneshot,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::network::tun::DNS_IP;
|
||||||
|
|
||||||
use super::{get_edge, get_route_table, init_arp_wait_list, init_route};
|
use super::{get_edge, get_route_table, init_arp_wait_list, init_route};
|
||||||
|
|
||||||
static GLOBAL_ARP: OnceCell<ArpActor> = OnceCell::new();
|
static GLOBAL_ARP: OnceCell<ArpActor> = OnceCell::new();
|
||||||
|
|||||||
@ -18,8 +18,6 @@ pub use route::*;
|
|||||||
mod tuntap;
|
mod tuntap;
|
||||||
pub use tuntap::*;
|
pub use tuntap::*;
|
||||||
|
|
||||||
pub const DNS_IP: u32 = (100<<24) + (100<<16) + (100<<8) + 100;
|
|
||||||
|
|
||||||
#[cfg_attr(target_os = "linux", path = "tun_linux.rs")]
|
#[cfg_attr(target_os = "linux", path = "tun_linux.rs")]
|
||||||
#[cfg_attr(target_os = "windows", path = "tun_win.rs")]
|
#[cfg_attr(target_os = "windows", path = "tun_win.rs")]
|
||||||
mod tun;
|
mod tun;
|
||||||
|
|||||||
@ -27,9 +27,10 @@ use crate::tcp::PacketType;
|
|||||||
use super::device::{DeviceConfig, Mode};
|
use super::device::{DeviceConfig, Mode};
|
||||||
use super::TunTapPacketHandler;
|
use super::TunTapPacketHandler;
|
||||||
|
|
||||||
|
pub const DNS_IP: u32 = (100<<24) + (100<<16) + (100<<8) + 100;
|
||||||
|
|
||||||
const RESOLV_FILE: &'static str = "/etc/resolv.conf";
|
const RESOLV_FILE: &'static str = "/etc/resolv.conf";
|
||||||
const RESOLV_FILE_BACKUP: &'static str = "/etc/resolv.conf.punchnet.bak";
|
const RESOLV_FILE_BACKUP: &'static str = "/etc/resolv.conf.punchnet.bak";
|
||||||
use crate::network::DNS_IP;
|
|
||||||
|
|
||||||
// #[link(name = "tuntap", kind="static")]
|
// #[link(name = "tuntap", kind="static")]
|
||||||
#[link(name = "tuntap")]
|
#[link(name = "tuntap")]
|
||||||
@ -119,10 +120,7 @@ impl Iface {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let mask = net_bit_len_to_mask(netbit);
|
let mask = net_bit_len_to_mask(netbit);
|
||||||
let mut default_gw = (ip & mask) + 1;
|
let default_gw = (ip & mask) + 1;
|
||||||
if default_gw == ip {
|
|
||||||
default_gw += 1;
|
|
||||||
}
|
|
||||||
let ip = ip_to_string(&ip);
|
let ip = ip_to_string(&ip);
|
||||||
let netbit = ip_to_string(&net_bit_len_to_mask(netbit));
|
let netbit = ip_to_string(&net_bit_len_to_mask(netbit));
|
||||||
|
|
||||||
@ -224,7 +222,6 @@ impl TunTapPacketHandler for Iface {
|
|||||||
if let Some(ip) = headers.net {
|
if let Some(ip) = headers.net {
|
||||||
match ip {
|
match ip {
|
||||||
etherparse::NetHeaders::Ipv4(ipv4, _) => {
|
etherparse::NetHeaders::Ipv4(ipv4, _) => {
|
||||||
|
|
||||||
if u32::from_be_bytes(ipv4.destination) == DNS_IP {
|
if u32::from_be_bytes(ipv4.destination) == DNS_IP {
|
||||||
// should send to dns
|
// should send to dns
|
||||||
if let Err(e) = edge.udp_sock_for_dns.send_to(&data[14..], format!("{}:15353", edge.server_ip)).await {
|
if let Err(e) = edge.udp_sock_for_dns.send_to(&data[14..], format!("{}:15353", edge.server_ip)).await {
|
||||||
@ -278,6 +275,9 @@ impl TunTapPacketHandler for Iface {
|
|||||||
async fn handle_packet_from_net(&self, data: &[u8], key: &[u8]) -> std::io::Result<()> {
|
async fn handle_packet_from_net(&self, data: &[u8], key: &[u8]) -> std::io::Result<()> {
|
||||||
debug!("in tun mode");
|
debug!("in tun mode");
|
||||||
|
|
||||||
|
if key.len() == 0 {
|
||||||
|
// got layer2 packet, just write to interface;
|
||||||
|
}
|
||||||
// got layer 2 frame
|
// got layer 2 frame
|
||||||
match Ethernet2Header::from_slice(&data) {
|
match Ethernet2Header::from_slice(&data) {
|
||||||
Ok((hdr, rest)) => {
|
Ok((hdr, rest)) => {
|
||||||
@ -478,9 +478,9 @@ impl TunTapPacketHandler for Iface {
|
|||||||
}
|
}
|
||||||
if dstip == DNS_IP {
|
if dstip == DNS_IP {
|
||||||
// should do the dns request
|
// should do the dns request
|
||||||
// println!("request for dns");
|
println!("request for dns");
|
||||||
let addr = format!("{}:15353", eee.server_ip);
|
let addr = format!("{}:15353", eee.server_ip);
|
||||||
// println!("send dns to {}", addr);
|
println!("send dns to {}", addr);
|
||||||
if let Err(e) = eee.udp_sock_for_dns.send_to(&data, &addr).await {
|
if let Err(e) = eee.udp_sock_for_dns.send_to(&data, &addr).await {
|
||||||
error!("failed to send request to 15353: {}", e);
|
error!("failed to send request to 15353: {}", e);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -14,7 +14,9 @@ use wintun;
|
|||||||
|
|
||||||
use crate::get_edge;
|
use crate::get_edge;
|
||||||
use crate::network::{
|
use crate::network::{
|
||||||
ARP_REPLY, ARP_REQUEST, ArpHdr, ArpRequestInfo, ArpResponse, DNS_IP, add_to_arp_wait_list, arp_arrived, form_ethernet_packet, generate_arp_request, send_arp_request, send_packet_to_net
|
add_to_arp_wait_list, arp_arrived, form_ethernet_packet, generate_arp_request,
|
||||||
|
send_arp_request, send_packet_to_net, ArpHdr, ArpRequestInfo, ArpResponse, ARP_REPLY,
|
||||||
|
ARP_REQUEST,
|
||||||
};
|
};
|
||||||
use crate::pb::{encode_to_udp_message, SdlData};
|
use crate::pb::{encode_to_udp_message, SdlData};
|
||||||
use crate::tcp::PacketType;
|
use crate::tcp::PacketType;
|
||||||
@ -24,7 +26,6 @@ use super::device::{DeviceConfig, Mode};
|
|||||||
use super::TunTapPacketHandler;
|
use super::TunTapPacketHandler;
|
||||||
|
|
||||||
pub struct Iface {
|
pub struct Iface {
|
||||||
if_idx: u32,
|
|
||||||
name: String,
|
name: String,
|
||||||
_adapter: Arc<wintun::Adapter>,
|
_adapter: Arc<wintun::Adapter>,
|
||||||
session: Arc<wintun::Session>,
|
session: Arc<wintun::Session>,
|
||||||
@ -64,14 +65,7 @@ impl Iface {
|
|||||||
error!("reload config's ip is 0");
|
error!("reload config's ip is 0");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mask = net_bit_len_to_mask(netbit);
|
|
||||||
let mut default_gw = (ip & mask) + 1;
|
|
||||||
if default_gw == ip {
|
|
||||||
default_gw += 1;
|
|
||||||
}
|
|
||||||
let ip = ip_to_string(&ip);
|
let ip = ip_to_string(&ip);
|
||||||
|
|
||||||
let netbit = ip_to_string(&net_bit_len_to_mask(netbit));
|
let netbit = ip_to_string(&net_bit_len_to_mask(netbit));
|
||||||
|
|
||||||
let mut cmd = Command::new("netsh");
|
let mut cmd = Command::new("netsh");
|
||||||
@ -120,13 +114,11 @@ impl Iface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let gw = ip_to_string(&default_gw);
|
/*
|
||||||
println!("gw = {}", gw);
|
if let Err(e) = set_dns(&self.name, network_domain) {
|
||||||
if let Err(e) = set_dns(&self.name, network_domain, &gw, self.if_idx) {
|
|
||||||
println!("failed to set dns: {:?}", e);
|
println!("failed to set dns: {:?}", e);
|
||||||
} else {
|
|
||||||
println!("set dns ok");
|
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -153,13 +145,15 @@ impl TunTapPacketHandler for Iface {
|
|||||||
let edge = get_edge();
|
let edge = get_edge();
|
||||||
let self_mac = edge.device_config.get_mac();
|
let self_mac = edge.device_config.get_mac();
|
||||||
|
|
||||||
|
/*
|
||||||
if hdr.destination != self_mac && hdr.destination != BROADCAST_MAC {
|
if hdr.destination != self_mac && hdr.destination != BROADCAST_MAC {
|
||||||
error!(
|
error!(
|
||||||
"packet to [{:?}] not direct to us",
|
"packet to [{:?}] not direct to us",
|
||||||
mac_to_string(&hdr.destination)
|
mac_to_string(&hdr.destination)
|
||||||
);
|
);
|
||||||
return Ok(());
|
return;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
if hdr.ether_type == ARP {
|
if hdr.ether_type == ARP {
|
||||||
let mut arp = ArpHdr::from_slice(&data);
|
let mut arp = ArpHdr::from_slice(&data);
|
||||||
@ -308,15 +302,6 @@ impl TunTapPacketHandler for Iface {
|
|||||||
debug!("drop tun packet due to not authed");
|
debug!("drop tun packet due to not authed");
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
if dstip == DNS_IP {
|
|
||||||
// println!("request for dns");
|
|
||||||
let addr = format!("{}:15353", eee.server_ip);
|
|
||||||
// println!("send dns to {}", addr);
|
|
||||||
if let Err(e) = eee.udp_sock_for_dns.send_to(&data, &addr).await {
|
|
||||||
error!("failed to send request to 15353: {}", e);
|
|
||||||
}
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
match send_arp_request(ArpRequestInfo::Lookup { ip: dstip }).await {
|
match send_arp_request(ArpRequestInfo::Lookup { ip: dstip }).await {
|
||||||
ArpResponse::LookupResp {
|
ArpResponse::LookupResp {
|
||||||
mac,
|
mac,
|
||||||
@ -409,13 +394,10 @@ fn create_wintun(path: &str, name: &str) -> Iface {
|
|||||||
let adapter = match wintun::Adapter::open(&wt, name) {
|
let adapter = match wintun::Adapter::open(&wt, name) {
|
||||||
Ok(a) => a,
|
Ok(a) => a,
|
||||||
Err(_e) => wintun::Adapter::create(&wt, name, "Example", None)
|
Err(_e) => wintun::Adapter::create(&wt, name, "Example", None)
|
||||||
.expect("failed to create tun adapter"),
|
.expect("failed to create wintun adapter"),
|
||||||
};
|
};
|
||||||
let idx = adapter.get_adapter_index().expect("failed to get adapter index");
|
|
||||||
println!("idx = {}", idx);
|
|
||||||
let session = Arc::new(adapter.start_session(wintun::MAX_RING_CAPACITY).unwrap());
|
let session = Arc::new(adapter.start_session(wintun::MAX_RING_CAPACITY).unwrap());
|
||||||
Iface {
|
Iface {
|
||||||
if_idx: idx,
|
|
||||||
_adapter: adapter,
|
_adapter: adapter,
|
||||||
session,
|
session,
|
||||||
name: name.to_owned(),
|
name: name.to_owned(),
|
||||||
@ -431,35 +413,25 @@ pub fn get_install_channel() -> String {
|
|||||||
"windows".to_owned()
|
"windows".to_owned()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_dns(name: &str, _network_domain: &str, gw: &str, ifidx: u32) -> std::io::Result<()>{
|
pub fn set_dns(name: &str, network_domain: &str) -> std::io::Result<()>{
|
||||||
let res = Command::new("ROUTE")
|
Command::new("netsh")
|
||||||
.arg("ADD")
|
|
||||||
.arg("100.100.100.100")
|
|
||||||
.arg("MASK")
|
|
||||||
.arg("255.255.255.255")
|
|
||||||
.arg(gw)
|
|
||||||
.arg("IF")
|
|
||||||
.arg(ifidx.to_string())
|
|
||||||
.creation_flags(0x08000000)
|
|
||||||
.output()?;
|
|
||||||
|
|
||||||
println!("res1: {}", res.status.success());
|
|
||||||
|
|
||||||
println!("route set ok");
|
|
||||||
let res = Command::new("netsh")
|
|
||||||
.arg("dnsclient")
|
.arg("dnsclient")
|
||||||
.arg("set")
|
.arg("set")
|
||||||
.arg("dnsserver")
|
.arg("dnsserver")
|
||||||
.arg(&format!("name={}", name))
|
.arg(&format!("name=\"{}\"", name))
|
||||||
.arg("source=static")
|
.arg("source=static")
|
||||||
.arg("address=100.100.100.100")
|
.arg("address=100.100.100.100")
|
||||||
.arg("validate=no")
|
.arg("validate=yes").output()?;
|
||||||
.creation_flags(0x08000000)
|
|
||||||
|
Command::new("netsh")
|
||||||
|
.arg("interface")
|
||||||
|
.arg("ipv4")
|
||||||
|
.arg("set")
|
||||||
|
.arg("dns")
|
||||||
|
.arg("suffix")
|
||||||
|
.arg(&format!("name=\"{}\"", name))
|
||||||
|
.arg(&format!("suffix=\"{}\"", network_domain))
|
||||||
.output()?;
|
.output()?;
|
||||||
println!("res2: {}", res.status.success());
|
|
||||||
|
|
||||||
println!("netsh set ok");
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user