tun2proxy/src/lib.rs

106 lines
3.2 KiB
Rust
Raw Normal View History

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::tun2proxy::{Credentials, Options};
2023-03-25 01:39:46 +01:00
use crate::{http::HttpManager, socks5::SocksManager, tun2proxy::TunToProxy};
use std::net::{SocketAddr, ToSocketAddrs};
2023-03-21 00:11:51 +08:00
pub mod error;
2023-03-21 00:11:51 +08:00
pub mod http;
pub mod socks5;
pub mod tun2proxy;
pub mod virtdevice;
pub mod virtdns;
2023-03-21 00:11:51 +08: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, 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(""));
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),
"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"),
}
}
}
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)?;
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 => {
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
}