2023-03-22 01:02:27 +01:00
|
|
|
use crate::tun2proxy::Credentials;
|
2023-03-21 00:11:51 +08:00
|
|
|
use crate::{http::HttpManager, socks5::Socks5Manager, tun2proxy::TunToProxy};
|
2023-03-22 13:18:07 +01:00
|
|
|
use std::net::{SocketAddr, ToSocketAddrs};
|
2023-03-21 00:11:51 +08:00
|
|
|
|
|
|
|
pub mod http;
|
|
|
|
pub mod socks5;
|
|
|
|
pub mod tun2proxy;
|
|
|
|
pub mod virtdevice;
|
|
|
|
|
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 {
|
|
|
|
pub fn from_url(s: &str) -> Result<Proxy, String> {
|
|
|
|
let url = url::Url::parse(s).map_err(|_| format!("`{s}` is not a valid proxy URL"))?;
|
|
|
|
let host = url
|
|
|
|
.host_str()
|
|
|
|
.ok_or(format!("`{s}` does not contain a host"))?;
|
|
|
|
|
|
|
|
let mut url_host = String::from(host);
|
|
|
|
let port = url.port().ok_or(format!("`{s}` does not contain a port"))?;
|
|
|
|
url_host.push(':');
|
|
|
|
url_host.push_str(port.to_string().as_str());
|
|
|
|
|
|
|
|
let mut addr_iter = url_host
|
|
|
|
.to_socket_addrs()
|
|
|
|
.map_err(|_| format!("`{host}` could not be resolved"))?;
|
|
|
|
|
|
|
|
let addr = addr_iter
|
|
|
|
.next()
|
|
|
|
.ok_or(format!("`{host}` does not resolve to a usable IP address"))?;
|
|
|
|
|
|
|
|
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() {
|
|
|
|
"socks5" => Some(ProxyType::Socks5),
|
|
|
|
"http" => Some(ProxyType::Http),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
.ok_or(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 {
|
|
|
|
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 {
|
|
|
|
ProxyType::Socks5 => write!(f, "socks5"),
|
|
|
|
ProxyType::Http => write!(f, "http"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-22 13:18:07 +01:00
|
|
|
pub fn main_entry(tun: &str, proxy: Proxy) {
|
2023-03-21 00:11:51 +08:00
|
|
|
let mut ttp = TunToProxy::new(tun);
|
2023-03-22 13:18:07 +01:00
|
|
|
match proxy.proxy_type {
|
2023-03-21 00:11:51 +08:00
|
|
|
ProxyType::Socks5 => {
|
2023-03-22 13:18:07 +01:00
|
|
|
ttp.add_connection_manager(Socks5Manager::new(proxy.addr, 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
|
|
|
}
|
|
|
|
}
|
|
|
|
ttp.run();
|
|
|
|
}
|