mirror of
https://github.com/tun2proxy/tun2proxy.git
synced 2025-06-07 23:27:46 +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 {
|
impl From<&str> for Error {
|
||||||
fn from(err: &str) -> Self {
|
fn from(err: &str) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
|
|
@ -76,8 +76,8 @@ impl std::fmt::Display for ProxyType {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main_entry(tun: &str, proxy: Proxy, options: Options) {
|
pub fn main_entry(tun: &str, proxy: Proxy, options: Options) -> Result<(), Error> {
|
||||||
let mut ttp = TunToProxy::new(tun, options);
|
let mut ttp = TunToProxy::new(tun, options)?;
|
||||||
match proxy.proxy_type {
|
match proxy.proxy_type {
|
||||||
ProxyType::Socks5 => {
|
ProxyType::Socks5 => {
|
||||||
ttp.add_connection_manager(Socks5Manager::new(proxy.addr, proxy.credentials));
|
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));
|
ttp.add_connection_manager(HttpManager::new(proxy.addr, proxy.credentials));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Err(e) = ttp.run() {
|
ttp.run()
|
||||||
log::error!("{e}");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,5 +47,7 @@ fn main() {
|
||||||
options = options.with_virtual_dns();
|
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::fmt::{Display, Formatter};
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
use std::net::Shutdown::Both;
|
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::os::unix::io::AsRawFd;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
@ -286,45 +286,29 @@ pub(crate) struct TunToProxy<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> TunToProxy<'a> {
|
impl<'a> TunToProxy<'a> {
|
||||||
pub(crate) fn new(interface: &str, options: Options) -> Self {
|
pub(crate) fn new(interface: &str, options: Options) -> Result<Self, Error> {
|
||||||
let tun = TunTapInterface::new(interface, Medium::Ip).unwrap();
|
let tun = TunTapInterface::new(interface, Medium::Ip)?;
|
||||||
let poll = Poll::new().unwrap();
|
let poll = Poll::new()?;
|
||||||
poll.registry()
|
poll.registry().register(
|
||||||
.register(
|
&mut SourceFd(&tun.as_raw_fd()),
|
||||||
&mut SourceFd(&tun.as_raw_fd()),
|
TCP_TOKEN,
|
||||||
TCP_TOKEN,
|
Interest::READABLE,
|
||||||
Interest::READABLE,
|
)?;
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let config = Config::new();
|
let config = Config::new();
|
||||||
let mut virt = VirtualTunDevice::new(tun.capabilities());
|
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);
|
let mut iface = Interface::new(config, &mut virt);
|
||||||
iface.update_ip_addrs(|ip_addrs| {
|
iface.update_ip_addrs(|ip_addrs| {
|
||||||
ip_addrs
|
ip_addrs.push(IpCidr::new(gateway4.into(), 0)).unwrap();
|
||||||
.push(IpCidr::new(
|
ip_addrs.push(IpCidr::new(gateway6.into(), 0)).unwrap()
|
||||||
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()
|
|
||||||
});
|
});
|
||||||
iface
|
iface.routes_mut().add_default_ipv4_route(gateway4.into())?;
|
||||||
.routes_mut()
|
iface.routes_mut().add_default_ipv6_route(gateway6.into())?;
|
||||||
.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.set_any_ip(true);
|
iface.set_any_ip(true);
|
||||||
|
|
||||||
Self {
|
let tun = Self {
|
||||||
tun,
|
tun,
|
||||||
poll,
|
poll,
|
||||||
iface,
|
iface,
|
||||||
|
@ -336,7 +320,8 @@ impl<'a> TunToProxy<'a> {
|
||||||
device: virt,
|
device: virt,
|
||||||
options,
|
options,
|
||||||
write_sockets: Default::default(),
|
write_sockets: Default::default(),
|
||||||
}
|
};
|
||||||
|
Ok(tun)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn add_connection_manager(&mut self, manager: Rc<dyn ConnectionManager>) {
|
pub(crate) fn add_connection_manager(&mut self, manager: Rc<dyn ConnectionManager>) {
|
||||||
|
|
|
@ -146,7 +146,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
Ok(Fork::Child) => {
|
Ok(Fork::Child) => {
|
||||||
prctl::set_death_signal(signal::SIGKILL as isize).unwrap(); // 9 == SIGKILL
|
prctl::set_death_signal(signal::SIGKILL as isize).unwrap(); // 9 == SIGKILL
|
||||||
main_entry(
|
let _ = main_entry(
|
||||||
TUN_TEST_DEVICE,
|
TUN_TEST_DEVICE,
|
||||||
test.proxy,
|
test.proxy,
|
||||||
Options::new().with_virtual_dns(),
|
Options::new().with_virtual_dns(),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue