Advertisement
NLinker

Minimal rust socks5 server

Dec 26th, 2018
818
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.47 KB | None | 0 0
  1. use hyper::client::Client;
  2. use std::net::SocketAddr;
  3.  
  4. const DEFAULT_HTTPS_DNS_WORKER_THREADS: usize = 1;
  5.  
  6. pub trait Executor {
  7. }
  8.  
  9. struct HyperExecutor<C> {
  10.     inner: Client<C>
  11. }
  12.  
  13. impl<C> HyperExecutor<C> {
  14.     fn new(inner: Client<C>) -> Self {
  15.         HyperExecutor { inner }
  16.     }
  17. }
  18.  
  19. impl<C> Executor for HyperExecutor<C> {
  20. }
  21.  
  22. fn default_executor() -> impl Executor {
  23.     let connector = hyper_tls::HttpsConnector::new(DEFAULT_HTTPS_DNS_WORKER_THREADS).unwrap();
  24.     let client = Client::builder().build(connector);
  25.     HyperExecutor::new(client)
  26. }
  27.  
  28. fn socks_proxy_executor(proxy: &str) -> impl Executor {
  29.     let client = Client::builder().build(hyper_socks2::Connector::new(parse_socks_proxy(proxy)));
  30.     HyperExecutor::new(client)
  31. }
  32.  
  33. fn http_proxy_executor() -> impl Executor {
  34.     let uri = "http://my-proxy:8080".parse().unwrap();
  35.     let mut proxy = hyper_proxy::Proxy::new(hyper_proxy::Intercept::All, uri);
  36.     proxy.set_authorization(typed_headers::Credentials::basic("John Doe", "Agent1234").unwrap());
  37.     let connector = hyper_tls::HttpsConnector::new(DEFAULT_HTTPS_DNS_WORKER_THREADS).unwrap();
  38.     let proxy_connector = hyper_proxy::ProxyConnector::from_proxy(connector, proxy).unwrap();
  39.     let client = Client::builder().build(proxy_connector);
  40.     HyperExecutor::new(client)
  41. }
  42.  
  43. fn parse_socks_proxy(dsn: &str) -> hyper_socks2::Proxy<SocketAddr> {
  44.     hyper_socks2::Proxy::Socks5 {
  45.         addrs: "host:port",
  46.         auth: None
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement