mirror of
https://github.com/librespot-org/librespot.git
synced 2025-10-04 02:09:26 +02:00
Add back hyper-proxy
This commit is contained in:
parent
963d50e725
commit
95fedf5357
8 changed files with 178 additions and 197 deletions
|
@ -2,16 +2,16 @@ use std::io;
|
|||
|
||||
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
|
||||
|
||||
pub async fn connect<T: AsyncRead + AsyncWrite + Unpin>(
|
||||
pub async fn proxy_connect<T: AsyncRead + AsyncWrite + Unpin>(
|
||||
mut proxy_connection: T,
|
||||
connect_host: &str,
|
||||
connect_port: u16,
|
||||
connect_port: &str,
|
||||
) -> io::Result<T> {
|
||||
let mut buffer = Vec::new();
|
||||
buffer.extend_from_slice(b"CONNECT ");
|
||||
buffer.extend_from_slice(connect_host.as_bytes());
|
||||
buffer.push(b':');
|
||||
buffer.extend_from_slice(connect_port.to_string().as_bytes());
|
||||
buffer.extend_from_slice(connect_port.as_bytes());
|
||||
buffer.extend_from_slice(b" HTTP/1.1\r\n\r\n");
|
||||
|
||||
proxy_connection.write_all(buffer.as_ref()).await?;
|
||||
|
@ -49,61 +49,7 @@ pub async fn connect<T: AsyncRead + AsyncWrite + Unpin>(
|
|||
}
|
||||
|
||||
if offset >= buffer.len() {
|
||||
buffer.resize(buffer.len() * 2, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "apresolve")] {
|
||||
use std::future::Future;
|
||||
use std::net::{SocketAddr, ToSocketAddrs};
|
||||
use std::pin::Pin;
|
||||
use std::task::Poll;
|
||||
|
||||
use hyper::service::Service;
|
||||
use hyper::Uri;
|
||||
use tokio::net::TcpStream;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ProxyTunnel {
|
||||
proxy_addr: SocketAddr,
|
||||
}
|
||||
|
||||
impl ProxyTunnel {
|
||||
pub fn new<T: ToSocketAddrs>(addr: T) -> io::Result<Self> {
|
||||
let addr = addr.to_socket_addrs()?.next().ok_or_else(|| {
|
||||
io::Error::new(io::ErrorKind::InvalidInput, "No socket address given")
|
||||
})?;
|
||||
Ok(Self { proxy_addr: addr })
|
||||
}
|
||||
}
|
||||
|
||||
impl Service<Uri> for ProxyTunnel {
|
||||
type Response = TcpStream;
|
||||
type Error = io::Error;
|
||||
type Future = Pin<Box<dyn Future<Output = io::Result<TcpStream>> + Send>>;
|
||||
|
||||
fn poll_ready(&mut self, _: &mut std::task::Context<'_>) -> Poll<io::Result<()>> {
|
||||
Poll::Ready(Ok(()))
|
||||
}
|
||||
|
||||
fn call(&mut self, url: Uri) -> Self::Future {
|
||||
let proxy_addr = self.proxy_addr;
|
||||
let fut = async move {
|
||||
let host = url
|
||||
.host()
|
||||
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "Host is missing"))?;
|
||||
let port = url
|
||||
.port()
|
||||
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "Port is missing"))?;
|
||||
|
||||
let conn = TcpStream::connect(proxy_addr).await?;
|
||||
connect(conn, host, port.as_u16()).await
|
||||
};
|
||||
|
||||
Box::pin(fut)
|
||||
}
|
||||
buffer.resize(buffer.len() + 100, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue