2023-03-24 16:32:47 +08:00
|
|
|
use crate::error::Error;
|
2023-04-15 12:10:30 +02:00
|
|
|
use crate::socks::SocksVersion;
|
|
|
|
use crate::{http::HttpManager, socks::SocksManager, tun2proxy::TunToProxy};
|
2023-03-22 13:18:07 +01:00
|
|
|
use std::net::{SocketAddr, ToSocketAddrs};
|
2023-03-21 00:11:51 +08:00
|
|
|
|
2023-04-14 18:44:41 +08:00
|
|
|
mod android;
|
2023-03-25 21:41:40 +01:00
|
|
|
pub mod error;
|
2023-03-25 13:07:39 +01:00
|
|
|
mod http;
|
2023-03-25 21:12:41 +01:00
|
|
|
pub mod setup;
|
2023-04-15 12:10:30 +02:00
|
|
|
mod socks;
|
2023-03-25 13:07:39 +01:00
|
|
|
mod tun2proxy;
|
|
|
|
mod virtdevice;
|
|
|
|
mod virtdns;
|
2023-03-21 00:11:51 +08:00
|
|
|
|
2023-03-22 13:18:07 +01:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct Proxy {
|
|
|
|
pub proxy_type: ProxyType,
|
|
|
|
pub addr: SocketAddr,
|
|
|
|
pub credentials: Option<Credentials>,
|
|
|
|
}
|
|
|
|
|
2023-04-10 23:24:53 +02:00
|
|
|
pub enum NetworkInterface {
|
|
|
|
Named(String),
|
|
|
|
Fd(std::os::fd::RawFd),
|
|
|
|
}
|
|
|
|
|
2023-03-22 13:18:07 +01:00
|
|
|
impl Proxy {
|
2023-03-22 22:19:00 +08:00
|
|
|
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))?;
|
2023-03-22 22:19:00 +08:00
|
|
|
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))?;
|
2023-03-22 13:18:07 +01:00
|
|
|
|
|
|
|
let mut url_host = String::from(host);
|
2023-03-22 22:19:00 +08:00
|
|
|
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))?;
|
2023-03-22 13:18:07 +01:00
|
|
|
url_host.push(':');
|
|
|
|
url_host.push_str(port.to_string().as_str());
|
|
|
|
|
2023-03-22 22:19:00 +08:00
|
|
|
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))?;
|
2023-03-22 13:18:07 +01:00
|
|
|
|
2023-03-22 22:19:00 +08:00
|
|
|
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))?;
|
2023-03-22 13:18:07 +01:00
|
|
|
|
|
|
|
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(""));
|
|
|
|
Some(Credentials::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),
|
2023-03-22 13:18:07 +01:00
|
|
|
"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")))?;
|
2023-03-22 13:18:07 +01:00
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2023-03-22 12:18:41 +01:00
|
|
|
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"),
|
2023-03-22 12:18:41 +01:00
|
|
|
ProxyType::Socks5 => write!(f, "socks5"),
|
|
|
|
ProxyType::Http => write!(f, "http"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-25 13:07:39 +01:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Options {
|
|
|
|
virtdns: Option<virtdns::VirtualDns>,
|
2023-04-10 23:24:53 +02:00
|
|
|
mtu: Option<usize>,
|
2023-03-25 13:07:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Options {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Default::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_virtual_dns(mut self) -> Self {
|
|
|
|
self.virtdns = Some(virtdns::VirtualDns::new());
|
|
|
|
self
|
|
|
|
}
|
2023-04-10 23:24:53 +02:00
|
|
|
|
|
|
|
pub fn with_mtu(mut self, mtu: usize) -> Self {
|
|
|
|
self.mtu = Some(mtu);
|
|
|
|
self
|
|
|
|
}
|
2023-03-25 13:07:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default, Clone, Debug)]
|
|
|
|
pub struct Credentials {
|
2023-06-22 13:09:36 -04:00
|
|
|
pub(crate) username: String,
|
|
|
|
pub(crate) password: String,
|
2023-03-25 13:07:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Credentials {
|
|
|
|
pub fn new(username: &str, password: &str) -> Self {
|
|
|
|
Self {
|
2023-06-22 13:09:36 -04:00
|
|
|
username: String::from(username),
|
|
|
|
password: String::from(password),
|
2023-03-25 13:07:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-03-22 13:18:07 +01:00
|
|
|
match proxy.proxy_type {
|
2023-03-25 01:39:46 +01:00
|
|
|
ProxyType::Socks4 => {
|
|
|
|
ttp.add_connection_manager(SocksManager::new(
|
|
|
|
proxy.addr,
|
|
|
|
SocksVersion::V4,
|
2023-04-01 02:19:20 +02:00
|
|
|
proxy.credentials.clone(),
|
2023-03-25 01:39:46 +01:00
|
|
|
));
|
|
|
|
}
|
2023-03-21 00:11:51 +08:00
|
|
|
ProxyType::Socks5 => {
|
2023-03-25 01:39:46 +01:00
|
|
|
ttp.add_connection_manager(SocksManager::new(
|
|
|
|
proxy.addr,
|
|
|
|
SocksVersion::V5,
|
2023-04-01 02:19:20 +02:00
|
|
|
proxy.credentials.clone(),
|
2023-03-25 01:39:46 +01:00
|
|
|
));
|
2023-03-21 00:11:51 +08:00
|
|
|
}
|
|
|
|
ProxyType::Http => {
|
2023-04-01 02:19:20 +02:00
|
|
|
ttp.add_connection_manager(HttpManager::new(proxy.addr, proxy.credentials.clone()));
|
2023-03-21 00:11:51 +08:00
|
|
|
}
|
|
|
|
}
|
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-04-27 22:41:54 +02:00
|
|
|
pub fn main_entry(
|
|
|
|
interface: &NetworkInterface,
|
|
|
|
proxy: &Proxy,
|
|
|
|
options: Options,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
let ttp = tun_to_proxy(interface, proxy, options);
|
|
|
|
ttp?.run()
|
2023-04-14 17:27:37 +08:00
|
|
|
}
|