2023-03-24 16:32:47 +08:00
|
|
|
use crate::error::Error;
|
2023-03-25 01:39:46 +01:00
|
|
|
use crate::socks5::SocksVersion;
|
|
|
|
use crate::{http::HttpManager, socks5::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-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-03-25 13:07:39 +01:00
|
|
|
mod socks5;
|
|
|
|
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>,
|
|
|
|
}
|
|
|
|
|
|
|
|
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>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Options {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Default::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_virtual_dns(mut self) -> Self {
|
|
|
|
self.virtdns = Some(virtdns::VirtualDns::new());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default, Clone, Debug)]
|
|
|
|
pub struct Credentials {
|
|
|
|
pub(crate) username: Vec<u8>,
|
|
|
|
pub(crate) password: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Credentials {
|
|
|
|
pub fn new(username: &str, password: &str) -> Self {
|
|
|
|
Self {
|
|
|
|
username: username.as_bytes().to_vec(),
|
|
|
|
password: password.as_bytes().to_vec(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-24 09:27:31 +08:00
|
|
|
pub fn main_entry(tun: &str, proxy: Proxy, options: Options) -> Result<(), Error> {
|
|
|
|
let mut ttp = TunToProxy::new(tun, 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,
|
|
|
|
proxy.credentials,
|
|
|
|
));
|
|
|
|
}
|
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,
|
|
|
|
proxy.credentials,
|
|
|
|
));
|
2023-03-21 00:11:51 +08:00
|
|
|
}
|
|
|
|
ProxyType::Http => {
|
2023-03-22 13:18:07 +01:00
|
|
|
ttp.add_connection_manager(HttpManager::new(proxy.addr, proxy.credentials));
|
2023-03-21 00:11:51 +08:00
|
|
|
}
|
|
|
|
}
|
2023-03-24 09:27:31 +08:00
|
|
|
ttp.run()
|
2023-03-21 00:11:51 +08:00
|
|
|
}
|