mio::event::Source

This commit is contained in:
ssrlive 2023-09-19 12:55:02 +08:00
parent 8aeaf238f2
commit 3e7a4dc8b2
4 changed files with 32 additions and 2 deletions

View file

@ -108,6 +108,7 @@ pub struct Options {
dns_over_tcp: bool, dns_over_tcp: bool,
dns_addr: Option<std::net::IpAddr>, dns_addr: Option<std::net::IpAddr>,
ipv6_enabled: bool, ipv6_enabled: bool,
bypass_ip: Option<std::net::IpAddr>,
} }
impl Options { impl Options {
@ -141,6 +142,11 @@ impl Options {
self.mtu = Some(mtu); self.mtu = Some(mtu);
self self
} }
pub fn with_bypass_ip(mut self, ip: Option<std::net::IpAddr>) -> Self {
self.bypass_ip = ip;
self
}
} }
pub fn tun_to_proxy<'a>( pub fn tun_to_proxy<'a>(

View file

@ -110,12 +110,18 @@ fn main() -> ExitCode {
Some(_fd) => { Some(_fd) => {
options = options.with_mtu(args.tun_mtu); options = options.with_mtu(args.tun_mtu);
#[cfg(not(target_family = "unix"))] #[cfg(not(target_family = "unix"))]
panic!("Not supported"); panic!("Not supported file descriptor");
#[cfg(target_family = "unix")] #[cfg(target_family = "unix")]
NetworkInterface::Fd(_fd) NetworkInterface::Fd(_fd)
} }
}; };
let bypass_tun_ip = match args.bypass_ip {
Some(addr) => addr,
None => args.proxy.addr.ip(),
};
options = options.with_bypass_ip(Some(bypass_tun_ip));
let block = || -> Result<(), Error> { let block = || -> Result<(), Error> {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{ {

View file

@ -250,7 +250,7 @@ impl<'a> TunToProxy<'a> {
}; };
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
let tun = match _interface { let mut tun = match _interface {
NetworkInterface::Named(name) => WinTunInterface::new(name.as_str(), Medium::Ip)?, NetworkInterface::Named(name) => WinTunInterface::new(name.as_str(), Medium::Ip)?,
}; };
@ -260,6 +260,9 @@ impl<'a> TunToProxy<'a> {
poll.registry() poll.registry()
.register(&mut SourceFd(&tun.as_raw_fd()), TUN_TOKEN, Interest::READABLE)?; .register(&mut SourceFd(&tun.as_raw_fd()), TUN_TOKEN, Interest::READABLE)?;
#[cfg(target_os = "windows")]
poll.registry().register(&mut tun, TUN_TOKEN, Interest::READABLE)?;
#[cfg(target_family = "unix")] #[cfg(target_family = "unix")]
let (exit_sender, mut exit_receiver) = mio::unix::pipe::new()?; let (exit_sender, mut exit_receiver) = mio::unix::pipe::new()?;
#[cfg(target_family = "unix")] #[cfg(target_family = "unix")]

View file

@ -1,3 +1,4 @@
use mio::{event, Interest, Registry, Token};
use smoltcp::{ use smoltcp::{
phy::{self, Device, DeviceCapabilities, Medium}, phy::{self, Device, DeviceCapabilities, Medium},
time::Instant, time::Instant,
@ -22,6 +23,20 @@ pub struct WinTunInterface {
// } // }
// } // }
impl event::Source for WinTunInterface {
fn register(&mut self, _registry: &Registry, _token: Token, _interests: Interest) -> io::Result<()> {
Err(io::Error::new(io::ErrorKind::Other, "register"))
}
fn reregister(&mut self, _registry: &Registry, _token: Token, _interests: Interest) -> io::Result<()> {
Err(io::Error::new(io::ErrorKind::Other, "reregister"))
}
fn deregister(&mut self, _registry: &Registry) -> io::Result<()> {
Err(io::Error::new(io::ErrorKind::Other, "deregister"))
}
}
impl WinTunInterface { impl WinTunInterface {
pub fn new(name: &str, medium: Medium) -> io::Result<WinTunInterface> { pub fn new(name: &str, medium: Medium) -> io::Result<WinTunInterface> {
let wintun = unsafe { wintun::load() }.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; let wintun = unsafe { wintun::load() }.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;