Mac下tun的测试
cargo.toml
tokio={version="1.0",features=["full"]}
tun = { version = "0.5", features = ["async"] }
futures="0.3"
tokio-util="0.6.9"
packet="0.1.4"
main.rs
use std::io::Error;
use futures::{SinkExt};
use packet::{builder::Builder, icmp, ip, Packet};
use tun::{self, Configuration, TunPacket};
use std::process::Command;
use futures::StreamExt;
#[tokio::main]
async fn main() {
let mut config = tun::Configuration::default();
config
.address((10, 0, 0, 1))
.netmask((255, 255, 255, 0))
.up();
#[cfg(target_os = "linux")]
config.platform(|config| {
config.packet_information(true);
});
let dev = tun::create_as_async(&config).unwrap();
let upeth_str: String = "ifconfig utun3 10.0.0.13 10.0.0.1 up ".to_string();
let routeadd_str: String = "sudo route -n add -net 18.10.49.3 -netmask 255.255.255.0 10.0.0.1".to_string();
let upeth = Command::new("sh").arg("-c").arg(upeth_str).output().expect("sh exec error!");
let ifconfigcmd = Command::new("sh").arg("-c").arg(routeadd_str).output().expect("sh exec error!");
let cmd_str: String = " ifconfig|grep flags=8051|awk -F ':' '{print $1}'|tail -1".to_string();
let ifconfigcmd = Command::new("sh").arg("-c").arg(cmd_str).output().expect("sh exec error!");
println!("{:?}", ifconfigcmd);
let mut stream = dev.into_framed();
while let Some(packet) = stream.next().await {
match packet {
Ok(pkt) => match ip::Packet::new(pkt.get_bytes()) {
Ok(ip::Packet::V4(pkt)) => match icmp::Packet::new(pkt.payload()) {
Ok(icmp) => match icmp.echo() {
Ok(icmp) => {
let reply = ip::v4::Builder::default()
.id(0x42)
.unwrap()
.ttl(64)
.unwrap()
.source(pkt.destination())
.unwrap()
.destination(pkt.source())
.unwrap()
.icmp()
.unwrap()
.echo()
.unwrap()
.reply()
.unwrap()
.identifier(icmp.identifier())
.unwrap()
.sequence(icmp.sequence())
.unwrap()
.payload(icmp.payload())
.unwrap()
.build()
.unwrap();
stream.send(TunPacket::new(reply)).await.unwrap();
}
_ => println!("in pkt:{:?} ", pkt),
}
_ => println!("out pkt: {:#?}", pkt),
}
Err(err) => panic!("Error: {:?}", err),
_ => {}
},
_ => {}
}
}
}
测试 tcp nc -t 18.10.49.3 2222
warning: `vmudp` (bin "vmudp") generated 4 warnings
Finished dev [unoptimized + debuginfo] target(s) in 0.03s
Running `target/debug/vmudp`
Output { status: ExitStatus(unix_wait_status(0)), stdout: "utun3\n", stderr: "" }
in pkt:ip::v4::Packet { version: 4, header: 5, dscp: 0, ecn: 0, length: 64, id: 0, flags: DONT_FRAGMENT, offset: 0, ttl: 64, protocol: Tcp, checksum: 60830, source: 10.0.0.13, destination: 18.10.49.3, payload: [193, 96, 8, 174, 73, 109, 50, 54, 0, 0, 0, 0, 176, 194, 255, 255, 67, 148, 0, 0, 2, 4, 5, 180, 1, 3, 3, 6, 1, 1, 8, 10, 83, 254, 11, 222, 0, 0, 0, 0, 4, 2, 0, 0] }
in pkt:ip::v4::Packet { version: 4, header: 5, dscp: 0, ecn: 0, length: 64, id: 0, flags: DONT_FRAGMENT, offset: 0, ttl: 64, protocol: Tcp, checksum: 60830, source: 10.0.0.13, destination: 18.10.49.3, payload: [193, 96, 8, 174, 73, 109, 50, 54, 0, 0, 0, 0, 176, 2, 255, 255, 64, 108, 0, 0, 2, 4, 5, 180, 1, 3, 3, 6, 1, 1, 8, 10, 83, 254, 15, 198, 0, 0, 0, 0, 4, 2, 0, 0] }
测试 udp nc -u 18.10.49.3 88 8888 sdsss
in pkt:ip::v4::Packet { version: 4, header: 5, dscp: 0, ecn: 0, length: 34, id: 17256, flags: (empty), offset: 0, ttl: 64, protocol: Udp, checksum: 59977, source: 10.0.0.13, destination: 18.10.49.3, payload: [242, 76, 0, 88, 0, 14, 102, 49, 115, 100, 115, 115, 115, 10] }
测试 ping
(base) ? star@localhost ~/Desktop/rusts/vmudp master ping 18.10.49.3
PING 18.10.49.3 (18.10.49.3): 56 data bytes
64 bytes from 18.10.49.3: icmp_seq=0 ttl=64 time=0.297 ms
64 bytes from 18.10.49.3: icmp_seq=1 ttl=64 time=0.512 ms
64 bytes from 18.10.49.3: icmp_seq=2 ttl=64 time=0.526 ms
64 bytes from 18.10.49.3: icmp_seq=3 ttl=64 time=0.507 ms
64 bytes from 18.10.49.3: icmp_seq=4 ttl=64 time=0.590 ms
64 bytes from 18.10.49.3: icmp_seq=5 ttl=64 time=0.475 ms
64 bytes from 18.10.49.3: icmp_seq=6 ttl=64 time=0.527 ms
Mac路由命令
|