mirror of
https://github.com/librespot-org/librespot.git
synced 2025-10-04 18:29:45 +02:00
Put apresolve behind feature flag
This commit is contained in:
parent
9253be7bc9
commit
8cff10e983
6 changed files with 124 additions and 101 deletions
|
@ -1,16 +1,6 @@
|
|||
use futures::Future;
|
||||
use hyper::Uri;
|
||||
use std::{
|
||||
io,
|
||||
net::{SocketAddr, ToSocketAddrs},
|
||||
pin::Pin,
|
||||
task::Poll,
|
||||
};
|
||||
use tokio::{
|
||||
io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt},
|
||||
net::TcpStream,
|
||||
};
|
||||
use tower_service::Service;
|
||||
use std::io;
|
||||
|
||||
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
|
||||
|
||||
pub async fn connect<T: AsyncRead + AsyncWrite + Unpin>(
|
||||
mut proxy_connection: T,
|
||||
|
@ -64,43 +54,56 @@ pub async fn connect<T: AsyncRead + AsyncWrite + Unpin>(
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ProxyTunnel {
|
||||
proxy_addr: SocketAddr,
|
||||
}
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "apresolve")] {
|
||||
use std::future::Future;
|
||||
use std::net::{SocketAddr, ToSocketAddrs};
|
||||
use std::pin::Pin;
|
||||
use std::task::Poll;
|
||||
|
||||
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)
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue