tun2proxy/src/lib.rs

175 lines
4.8 KiB
Rust
Raw Normal View History

2023-08-06 13:48:56 +08:00
use crate::{
error::Error,
http::HttpManager,
socks::SocksProxyManager,
tun2proxy::{ConnectionManager, TunToProxy},
};
2023-09-03 10:40:40 +08:00
use socks5_impl::protocol::UserKey;
2023-08-05 15:52:32 +08:00
use std::{
net::{SocketAddr, ToSocketAddrs},
rc::Rc,
2023-07-23 02:03:15 +08:00
};
2023-03-21 00:11:51 +08:00
2023-04-14 18:44:41 +08:00
mod android;
2023-08-06 11:42:19 +08:00
mod dns;
2023-03-25 21:41:40 +01:00
pub mod error;
mod http;
2023-03-25 21:12:41 +01:00
pub mod setup;
2023-04-15 12:10:30 +02:00
mod socks;
mod tun2proxy;
2023-09-03 12:34:01 +08:00
#[cfg(any(target_os = "linux", target_os = "android"))]
mod tuntapinterface;
#[cfg(any(target_os = "linux", target_os = "android"))]
mod tuntapinterfacedesc;
mod virtdevice;
mod virtdns;
2023-09-18 21:40:56 +08:00
#[cfg(target_os = "windows")]
mod wintuninterface;
2023-03-21 00:11:51 +08:00
#[derive(Clone, Debug)]
pub struct Proxy {
pub proxy_type: ProxyType,
pub addr: SocketAddr,
2023-07-23 02:03:15 +08:00
pub credentials: Option<UserKey>,
}
2023-04-10 23:24:53 +02:00
pub enum NetworkInterface {
Named(String),
2023-08-31 14:31:02 +08:00
#[cfg(target_family = "unix")]
2023-04-10 23:24:53 +02:00
Fd(std::os::fd::RawFd),
}
impl Proxy {
pub fn from_url(s: &str) -> Result<Proxy, Error> {
let e = format!("`{s}` is not a valid proxy URL");
2023-03-24 16:32:47 +08:00
let url = url::Url::parse(s).map_err(|_| Error::from(&e))?;
let e = format!("`{s}` does not contain a host");
2023-03-24 16:32:47 +08:00
let host = url.host_str().ok_or(Error::from(e))?;
let mut url_host = String::from(host);
let e = format!("`{s}` does not contain a port");
2023-03-24 16:32:47 +08:00
let port = url.port().ok_or(Error::from(&e))?;
url_host.push(':');
url_host.push_str(port.to_string().as_str());
let e = format!("`{host}` could not be resolved");
2023-03-24 16:32:47 +08:00
let mut addr_iter = url_host.to_socket_addrs().map_err(|_| Error::from(&e))?;
let e = format!("`{host}` does not resolve to a usable IP address");
2023-03-24 16:32:47 +08:00
let addr = addr_iter.next().ok_or(Error::from(&e))?;
let credentials = if url.username() == "" && url.password().is_none() {
None
} else {
let username = String::from(url.username());
let password = String::from(url.password().unwrap_or(""));
2023-07-23 02:03:15 +08:00
Some(UserKey::new(username, password))
};
let scheme = url.scheme();
let proxy_type = match url.scheme().to_ascii_lowercase().as_str() {
2023-03-25 01:39:46 +01:00
"socks4" => Some(ProxyType::Socks4),
"socks5" => Some(ProxyType::Socks5),
"http" => Some(ProxyType::Http),
_ => None,
}
2023-03-24 16:32:47 +08:00
.ok_or(Error::from(&format!("`{scheme}` is an invalid proxy type")))?;
Ok(Proxy {
proxy_type,
addr,
credentials,
})
}
}
2023-03-22 11:17:28 +01:00
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
2023-03-21 00:11:51 +08:00
pub enum ProxyType {
2023-03-25 01:39:46 +01:00
Socks4,
2023-03-21 00:11:51 +08:00
Socks5,
Http,
}
impl std::fmt::Display for ProxyType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
2023-03-25 01:39:46 +01:00
ProxyType::Socks4 => write!(f, "socks4"),
ProxyType::Socks5 => write!(f, "socks5"),
ProxyType::Http => write!(f, "http"),
}
}
}
#[derive(Default)]
pub struct Options {
2023-08-05 15:52:32 +08:00
virtual_dns: Option<virtdns::VirtualDns>,
2023-04-10 23:24:53 +02:00
mtu: Option<usize>,
2023-08-21 19:58:13 +08:00
dns_over_tcp: bool,
2023-08-31 15:59:07 +08:00
dns_addr: Option<std::net::IpAddr>,
2023-08-23 10:35:21 +08:00
ipv6_enabled: bool,
2023-09-19 12:55:02 +08:00
bypass_ip: Option<std::net::IpAddr>,
}
impl Options {
pub fn new() -> Self {
2023-07-23 02:03:15 +08:00
Options::default()
}
pub fn with_virtual_dns(mut self) -> Self {
2023-08-05 15:52:32 +08:00
self.virtual_dns = Some(virtdns::VirtualDns::new());
2023-08-21 19:58:13 +08:00
self.dns_over_tcp = false;
self
}
pub fn with_dns_over_tcp(mut self) -> Self {
self.dns_over_tcp = true;
self.virtual_dns = None;
self
}
2023-04-10 23:24:53 +02:00
2023-08-31 15:59:07 +08:00
pub fn with_dns_addr(mut self, addr: Option<std::net::IpAddr>) -> Self {
self.dns_addr = addr;
self
}
2023-08-23 10:45:37 +08:00
pub fn with_ipv6_enabled(mut self) -> Self {
2023-08-23 10:35:21 +08:00
self.ipv6_enabled = true;
self
}
2023-04-10 23:24:53 +02:00
pub fn with_mtu(mut self, mtu: usize) -> Self {
self.mtu = Some(mtu);
self
}
2023-09-19 12:55:02 +08:00
pub fn with_bypass_ip(mut self, ip: Option<std::net::IpAddr>) -> Self {
self.bypass_ip = ip;
self
}
}
2023-04-27 22:41:54 +02:00
pub fn tun_to_proxy<'a>(
2023-04-10 23:24:53 +02:00
interface: &NetworkInterface,
proxy: &Proxy,
options: Options,
2023-04-27 22:41:54 +02:00
) -> Result<TunToProxy<'a>, Error> {
2023-04-10 23:24:53 +02:00
let mut ttp = TunToProxy::new(interface, options)?;
2023-08-05 15:52:32 +08:00
let credentials = proxy.credentials.clone();
let server = proxy.addr;
2023-09-03 10:40:40 +08:00
use socks5_impl::protocol::Version::{V4, V5};
2023-08-05 15:52:32 +08:00
let mgr = match proxy.proxy_type {
2023-09-03 10:40:40 +08:00
ProxyType::Socks4 => Rc::new(SocksProxyManager::new(server, V4, credentials)) as Rc<dyn ConnectionManager>,
ProxyType::Socks5 => Rc::new(SocksProxyManager::new(server, V5, credentials)) as Rc<dyn ConnectionManager>,
2023-08-08 23:45:16 +08:00
ProxyType::Http => Rc::new(HttpManager::new(server, credentials)) as Rc<dyn ConnectionManager>,
2023-08-05 15:52:32 +08:00
};
ttp.set_connection_manager(Some(mgr));
2023-04-27 22:41:54 +02:00
Ok(ttp)
2023-03-21 00:11:51 +08:00
}
2023-04-14 17:27:37 +08:00
2023-08-08 23:45:16 +08:00
pub fn main_entry(interface: &NetworkInterface, proxy: &Proxy, options: Options) -> Result<(), Error> {
2023-08-05 15:52:32 +08:00
let mut ttp = tun_to_proxy(interface, proxy, options)?;
ttp.run()?;
Ok(())
2023-04-14 17:27:37 +08:00
}