Compare commits

..

No commits in common. "43c2b01fca0964b7500e67086bf37e64af2f8d79" and "2096c3faac43f884c1a6534c1a248082b064b325" have entirely different histories.

5 changed files with 35 additions and 63 deletions

View File

@ -1,4 +1,4 @@
{
// "rust-analyzer.cargo.target": "x86_64-pc-windows-gnu"
// "rust-analyzer.cargo.features": ["tun"]
"rust-analyzer.cargo.target": null,
"rust-analyzer.cargo.features": ["tun"]
}

View File

@ -15,6 +15,8 @@ use tokio::sync::{
oneshot,
};
use crate::network::tun::DNS_IP;
use super::{get_edge, get_route_table, init_arp_wait_list, init_route};
static GLOBAL_ARP: OnceCell<ArpActor> = OnceCell::new();

View File

@ -18,8 +18,6 @@ pub use route::*;
mod 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 = "windows", path = "tun_win.rs")]
mod tun;

View File

@ -27,9 +27,10 @@ use crate::tcp::PacketType;
use super::device::{DeviceConfig, Mode};
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_BACKUP: &'static str = "/etc/resolv.conf.punchnet.bak";
use crate::network::DNS_IP;
// #[link(name = "tuntap", kind="static")]
#[link(name = "tuntap")]
@ -119,10 +120,7 @@ impl Iface {
return;
}
let mask = net_bit_len_to_mask(netbit);
let mut default_gw = (ip & mask) + 1;
if default_gw == ip {
default_gw += 1;
}
let default_gw = (ip & mask) + 1;
let ip = ip_to_string(&ip);
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 {
match ip {
etherparse::NetHeaders::Ipv4(ipv4, _) => {
if u32::from_be_bytes(ipv4.destination) == DNS_IP {
// should send to dns
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<()> {
debug!("in tun mode");
if key.len() == 0 {
// got layer2 packet, just write to interface;
}
// got layer 2 frame
match Ethernet2Header::from_slice(&data) {
Ok((hdr, rest)) => {
@ -478,9 +478,9 @@ impl TunTapPacketHandler for Iface {
}
if dstip == DNS_IP {
// should do the dns request
// println!("request for dns");
println!("request for dns");
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 {
error!("failed to send request to 15353: {}", e);
}

View File

@ -14,7 +14,9 @@ use wintun;
use crate::get_edge;
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::tcp::PacketType;
@ -24,7 +26,6 @@ use super::device::{DeviceConfig, Mode};
use super::TunTapPacketHandler;
pub struct Iface {
if_idx: u32,
name: String,
_adapter: Arc<wintun::Adapter>,
session: Arc<wintun::Session>,
@ -64,14 +65,7 @@ impl Iface {
error!("reload config's ip is 0");
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 netbit = ip_to_string(&net_bit_len_to_mask(netbit));
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, &gw, self.if_idx) {
/*
if let Err(e) = set_dns(&self.name, network_domain) {
println!("failed to set dns: {:?}", e);
} else {
println!("set dns ok");
}
*/
}
}
@ -153,13 +145,15 @@ impl TunTapPacketHandler for Iface {
let edge = get_edge();
let self_mac = edge.device_config.get_mac();
/*
if hdr.destination != self_mac && hdr.destination != BROADCAST_MAC {
error!(
"packet to [{:?}] not direct to us",
mac_to_string(&hdr.destination)
);
return Ok(());
return;
}
*/
if hdr.ether_type == ARP {
let mut arp = ArpHdr::from_slice(&data);
@ -308,15 +302,6 @@ impl TunTapPacketHandler for Iface {
debug!("drop tun packet due to not authed");
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 {
ArpResponse::LookupResp {
mac,
@ -409,13 +394,10 @@ fn create_wintun(path: &str, name: &str) -> Iface {
let adapter = match wintun::Adapter::open(&wt, name) {
Ok(a) => a,
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());
Iface {
if_idx: idx,
_adapter: adapter,
session,
name: name.to_owned(),
@ -431,35 +413,25 @@ pub fn get_install_channel() -> String {
"windows".to_owned()
}
pub fn set_dns(name: &str, _network_domain: &str, gw: &str, ifidx: u32) -> std::io::Result<()>{
let res = Command::new("ROUTE")
.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")
pub fn set_dns(name: &str, network_domain: &str) -> std::io::Result<()>{
Command::new("netsh")
.arg("dnsclient")
.arg("set")
.arg("dnsserver")
.arg(&format!("name={}", name))
.arg(&format!("name=\"{}\"", name))
.arg("source=static")
.arg("address=100.100.100.100")
.arg("validate=no")
.creation_flags(0x08000000)
.arg("validate=yes").output()?;
Command::new("netsh")
.arg("interface")
.arg("ipv4")
.arg("set")
.arg("dns")
.arg("suffix")
.arg(&format!("name=\"{}\"", name))
.arg(&format!("suffix=\"{}\"", network_domain))
.output()?;
println!("res2: {}", res.status.success());
println!("netsh set ok");
Ok(())
}