mirror of
https://github.com/tun2proxy/tun2proxy.git
synced 2025-04-20 05:49:09 +00:00
refine TunToProxy struct
This commit is contained in:
parent
7dec7f59f1
commit
ad388f897a
5 changed files with 41 additions and 40 deletions
16
src/error.rs
16
src/error.rs
|
@ -15,6 +15,22 @@ impl From<std::io::Error> for Error {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<std::net::AddrParseError> for Error {
|
||||
fn from(err: std::net::AddrParseError) -> Self {
|
||||
Self {
|
||||
message: err.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<smoltcp::iface::RouteTableFull> for Error {
|
||||
fn from(err: smoltcp::iface::RouteTableFull) -> Self {
|
||||
Self {
|
||||
message: format!("{err:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for Error {
|
||||
fn from(err: &str) -> Self {
|
||||
Self {
|
||||
|
|
|
@ -76,8 +76,8 @@ impl std::fmt::Display for ProxyType {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn main_entry(tun: &str, proxy: Proxy, options: Options) {
|
||||
let mut ttp = TunToProxy::new(tun, options);
|
||||
pub fn main_entry(tun: &str, proxy: Proxy, options: Options) -> Result<(), Error> {
|
||||
let mut ttp = TunToProxy::new(tun, options)?;
|
||||
match proxy.proxy_type {
|
||||
ProxyType::Socks5 => {
|
||||
ttp.add_connection_manager(Socks5Manager::new(proxy.addr, proxy.credentials));
|
||||
|
@ -86,7 +86,5 @@ pub fn main_entry(tun: &str, proxy: Proxy, options: Options) {
|
|||
ttp.add_connection_manager(HttpManager::new(proxy.addr, proxy.credentials));
|
||||
}
|
||||
}
|
||||
if let Err(e) = ttp.run() {
|
||||
log::error!("{e}");
|
||||
}
|
||||
ttp.run()
|
||||
}
|
||||
|
|
|
@ -47,5 +47,7 @@ fn main() {
|
|||
options = options.with_virtual_dns();
|
||||
}
|
||||
|
||||
main_entry(&args.tun, args.proxy, options);
|
||||
if let Err(e) = main_entry(&args.tun, args.proxy, options) {
|
||||
log::error!("{e}");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ use std::convert::{From, TryFrom};
|
|||
use std::fmt::{Display, Formatter};
|
||||
use std::io::{Read, Write};
|
||||
use std::net::Shutdown::Both;
|
||||
use std::net::{IpAddr, SocketAddr};
|
||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
|
||||
use std::os::unix::io::AsRawFd;
|
||||
use std::rc::Rc;
|
||||
use std::str::FromStr;
|
||||
|
@ -286,45 +286,29 @@ pub(crate) struct TunToProxy<'a> {
|
|||
}
|
||||
|
||||
impl<'a> TunToProxy<'a> {
|
||||
pub(crate) fn new(interface: &str, options: Options) -> Self {
|
||||
let tun = TunTapInterface::new(interface, Medium::Ip).unwrap();
|
||||
let poll = Poll::new().unwrap();
|
||||
poll.registry()
|
||||
.register(
|
||||
&mut SourceFd(&tun.as_raw_fd()),
|
||||
TCP_TOKEN,
|
||||
Interest::READABLE,
|
||||
)
|
||||
.unwrap();
|
||||
pub(crate) fn new(interface: &str, options: Options) -> Result<Self, Error> {
|
||||
let tun = TunTapInterface::new(interface, Medium::Ip)?;
|
||||
let poll = Poll::new()?;
|
||||
poll.registry().register(
|
||||
&mut SourceFd(&tun.as_raw_fd()),
|
||||
TCP_TOKEN,
|
||||
Interest::READABLE,
|
||||
)?;
|
||||
|
||||
let config = Config::new();
|
||||
let mut virt = VirtualTunDevice::new(tun.capabilities());
|
||||
let gateway4: Ipv4Addr = std::net::Ipv4Addr::from_str("0.0.0.1")?;
|
||||
let gateway6: Ipv6Addr = std::net::Ipv6Addr::from_str("::1")?;
|
||||
let mut iface = Interface::new(config, &mut virt);
|
||||
iface.update_ip_addrs(|ip_addrs| {
|
||||
ip_addrs
|
||||
.push(IpCidr::new(
|
||||
std::net::Ipv4Addr::from_str("0.0.0.1").unwrap().into(),
|
||||
0,
|
||||
))
|
||||
.unwrap();
|
||||
ip_addrs
|
||||
.push(IpCidr::new(
|
||||
std::net::Ipv6Addr::from_str("::1").unwrap().into(),
|
||||
0,
|
||||
))
|
||||
.unwrap()
|
||||
ip_addrs.push(IpCidr::new(gateway4.into(), 0)).unwrap();
|
||||
ip_addrs.push(IpCidr::new(gateway6.into(), 0)).unwrap()
|
||||
});
|
||||
iface
|
||||
.routes_mut()
|
||||
.add_default_ipv4_route(std::net::Ipv4Addr::from_str("0.0.0.1").unwrap().into())
|
||||
.unwrap();
|
||||
iface
|
||||
.routes_mut()
|
||||
.add_default_ipv6_route(std::net::Ipv6Addr::from_str("::1").unwrap().into())
|
||||
.unwrap();
|
||||
iface.routes_mut().add_default_ipv4_route(gateway4.into())?;
|
||||
iface.routes_mut().add_default_ipv6_route(gateway6.into())?;
|
||||
iface.set_any_ip(true);
|
||||
|
||||
Self {
|
||||
let tun = Self {
|
||||
tun,
|
||||
poll,
|
||||
iface,
|
||||
|
@ -336,7 +320,8 @@ impl<'a> TunToProxy<'a> {
|
|||
device: virt,
|
||||
options,
|
||||
write_sockets: Default::default(),
|
||||
}
|
||||
};
|
||||
Ok(tun)
|
||||
}
|
||||
|
||||
pub(crate) fn add_connection_manager(&mut self, manager: Rc<dyn ConnectionManager>) {
|
||||
|
|
|
@ -146,7 +146,7 @@ mod tests {
|
|||
}
|
||||
Ok(Fork::Child) => {
|
||||
prctl::set_death_signal(signal::SIGKILL as isize).unwrap(); // 9 == SIGKILL
|
||||
main_entry(
|
||||
let _ = main_entry(
|
||||
TUN_TEST_DEVICE,
|
||||
test.proxy,
|
||||
Options::new().with_virtual_dns(),
|
||||
|
|
Loading…
Add table
Reference in a new issue