Merge Android support branch

This commit is contained in:
B. Blechschmidt 2023-04-17 22:37:55 +02:00
commit 0c45714a45
7 changed files with 164 additions and 14 deletions

View file

@ -3,6 +3,7 @@ use crate::socks::SocksVersion;
use crate::{http::HttpManager, socks::SocksManager, tun2proxy::TunToProxy};
use std::net::{SocketAddr, ToSocketAddrs};
mod android;
pub mod error;
mod http;
pub mod setup;
@ -18,6 +19,11 @@ pub struct Proxy {
pub credentials: Option<Credentials>,
}
pub enum NetworkInterface {
Named(String),
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");
@ -83,6 +89,7 @@ impl std::fmt::Display for ProxyType {
#[derive(Default)]
pub struct Options {
virtdns: Option<virtdns::VirtualDns>,
mtu: Option<usize>,
}
impl Options {
@ -94,6 +101,11 @@ impl Options {
self.virtdns = Some(virtdns::VirtualDns::new());
self
}
pub fn with_mtu(mut self, mtu: usize) -> Self {
self.mtu = Some(mtu);
self
}
}
#[derive(Default, Clone, Debug)]
@ -111,8 +123,12 @@ impl Credentials {
}
}
pub fn main_entry(tun: &str, proxy: &Proxy, options: Options) -> Result<(), Error> {
let mut ttp = TunToProxy::new(tun, options)?;
pub fn main_entry(
interface: &NetworkInterface,
proxy: &Proxy,
options: Options,
) -> Result<(), Error> {
let mut ttp = TunToProxy::new(interface, options)?;
match proxy.proxy_type {
ProxyType::Socks4 => {
ttp.add_connection_manager(SocksManager::new(
@ -134,3 +150,7 @@ pub fn main_entry(tun: &str, proxy: &Proxy, options: Options) -> Result<(), Erro
}
ttp.run()
}
pub fn shutdown() -> Result<(), Error> {
TunToProxy::shutdown()
}