Put apresolve behind feature flag

This commit is contained in:
johannesd3 2021-02-10 22:50:08 +01:00 committed by Johannesd3
parent 9253be7bc9
commit 8cff10e983
6 changed files with 124 additions and 101 deletions

2
Cargo.lock generated
View file

@ -1450,6 +1450,7 @@ dependencies = [
"base64", "base64",
"byteorder", "byteorder",
"bytes", "bytes",
"cfg-if 1.0.0",
"env_logger", "env_logger",
"error-chain", "error-chain",
"futures", "futures",
@ -1473,7 +1474,6 @@ dependencies = [
"shannon", "shannon",
"tokio", "tokio",
"tokio-util", "tokio-util",
"tower-service",
"url 1.7.2", "url 1.7.2",
"vergen", "vergen",
] ]

View file

@ -61,6 +61,9 @@ sha-1 = "0.8"
hex = "0.4" hex = "0.4"
[features] [features]
apresolve = ["librespot-core/apresolve"]
apresolve-http2 = ["librespot-core/apresolve-http2"]
alsa-backend = ["librespot-playback/alsa-backend"] alsa-backend = ["librespot-playback/alsa-backend"]
portaudio-backend = ["librespot-playback/portaudio-backend"] portaudio-backend = ["librespot-playback/portaudio-backend"]
pulseaudio-backend = ["librespot-playback/pulseaudio-backend"] pulseaudio-backend = ["librespot-playback/pulseaudio-backend"]
@ -75,7 +78,7 @@ with-vorbis = ["librespot-audio/with-vorbis"]
# with-dns-sd = ["librespot-connect/with-dns-sd"] # with-dns-sd = ["librespot-connect/with-dns-sd"]
default = ["librespot-playback/rodio-backend"] default = ["rodio-backend", "apresolve"]
[package.metadata.deb] [package.metadata.deb]
maintainer = "librespot-org" maintainer = "librespot-org"

View file

@ -17,11 +17,12 @@ aes = "0.6"
base64 = "0.13" base64 = "0.13"
byteorder = "1.4" byteorder = "1.4"
bytes = "1.0" bytes = "1.0"
cfg-if = "1"
error-chain = { version = "0.12", default-features = false } error-chain = { version = "0.12", default-features = false }
futures = { version = "0.3", features = ["bilock", "unstable"] } futures = { version = "0.3", features = ["bilock", "unstable"] }
hmac = "0.10" hmac = "0.10"
httparse = "1.3" httparse = "1.3"
hyper = { version = "0.14", features = ["client", "tcp", "http1", "http2"] } hyper = { version = "0.14", optional = true, features = ["client", "tcp", "http1"] }
log = "0.4" log = "0.4"
num-bigint = "0.3" num-bigint = "0.3"
num-integer = "0.1" num-integer = "0.1"
@ -38,7 +39,6 @@ sha-1 = "0.9"
shannon = "0.2.0" shannon = "0.2.0"
tokio = { version = "1.0", features = ["io-util", "rt-multi-thread"] } tokio = { version = "1.0", features = ["io-util", "rt-multi-thread"] }
tokio-util = { version = "0.6", features = ["codec"] } tokio-util = { version = "0.6", features = ["codec"] }
tower-service = "0.3"
url = "1.7" url = "1.7"
[build-dependencies] [build-dependencies]
@ -48,3 +48,7 @@ vergen = "3.0.4"
[dev-dependencies] [dev-dependencies]
env_logger = "*" env_logger = "*"
tokio = {version = "1.0", features = ["macros"] } tokio = {version = "1.0", features = ["macros"] }
[features]
apresolve = ["hyper"]
apresolve-http2 = ["apresolve", "hyper/http2"]

View file

@ -1,18 +1,23 @@
const AP_FALLBACK: &'static str = "ap.spotify.com:443"; const AP_FALLBACK: &'static str = "ap.spotify.com:443";
const APRESOLVE_ENDPOINT: &'static str = "http://apresolve.spotify.com:80";
use hyper::{Body, Client, Method, Request, Uri};
use std::error::Error;
use url::Url; use url::Url;
use crate::proxytunnel::ProxyTunnel; cfg_if! {
if #[cfg(feature = "apresolve")] {
const APRESOLVE_ENDPOINT: &'static str = "http://apresolve.spotify.com:80";
#[derive(Clone, Debug, Serialize, Deserialize)] use std::error::Error;
pub struct APResolveData {
use hyper::{Body, Client, Method, Request, Uri};
use crate::proxytunnel::ProxyTunnel;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct APResolveData {
ap_list: Vec<String>, ap_list: Vec<String>,
} }
async fn apresolve(proxy: &Option<Url>, ap_port: &Option<u16>) -> Result<String, Box<dyn Error>> { async fn apresolve(proxy: &Option<Url>, ap_port: &Option<u16>) -> Result<String, Box<dyn Error>> {
let port = ap_port.unwrap_or(443); let port = ap_port.unwrap_or(443);
let req = Request::builder() let req = Request::builder()
@ -50,12 +55,18 @@ async fn apresolve(proxy: &Option<Url>, ap_port: &Option<u16>) -> Result<String,
.ok_or("empty AP List")?; .ok_or("empty AP List")?;
Ok(ap) Ok(ap)
} }
pub async fn apresolve_or_fallback(proxy: &Option<Url>, ap_port: &Option<u16>) -> String { pub async fn apresolve_or_fallback(proxy: &Option<Url>, ap_port: &Option<u16>) -> String {
apresolve(proxy, ap_port).await.unwrap_or_else(|e| { apresolve(proxy, ap_port).await.unwrap_or_else(|e| {
warn!("Failed to resolve Access Point: {}", e); warn!("Failed to resolve Access Point: {}", e);
warn!("Using fallback \"{}\"", AP_FALLBACK); warn!("Using fallback \"{}\"", AP_FALLBACK);
AP_FALLBACK.into() AP_FALLBACK.into()
}) })
}
} else {
pub async fn apresolve_or_fallback(_: &Option<Url>, _: &Option<u16>) -> String {
AP_FALLBACK.to_string()
}
}
} }

View file

@ -3,6 +3,8 @@
#[macro_use] #[macro_use]
extern crate log; extern crate log;
#[macro_use] #[macro_use]
extern crate cfg_if;
#[macro_use]
extern crate serde_derive; extern crate serde_derive;
#[macro_use] #[macro_use]
extern crate pin_project_lite; extern crate pin_project_lite;

View file

@ -1,16 +1,6 @@
use futures::Future; use std::io;
use hyper::Uri;
use std::{ use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
io,
net::{SocketAddr, ToSocketAddrs},
pin::Pin,
task::Poll,
};
use tokio::{
io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt},
net::TcpStream,
};
use tower_service::Service;
pub async fn connect<T: AsyncRead + AsyncWrite + Unpin>( pub async fn connect<T: AsyncRead + AsyncWrite + Unpin>(
mut proxy_connection: T, mut proxy_connection: T,
@ -64,21 +54,32 @@ pub async fn connect<T: AsyncRead + AsyncWrite + Unpin>(
} }
} }
#[derive(Clone)] cfg_if! {
pub struct ProxyTunnel { if #[cfg(feature = "apresolve")] {
proxy_addr: SocketAddr, use std::future::Future;
} use std::net::{SocketAddr, ToSocketAddrs};
use std::pin::Pin;
use std::task::Poll;
impl ProxyTunnel { 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> { pub fn new<T: ToSocketAddrs>(addr: T) -> io::Result<Self> {
let addr = addr.to_socket_addrs()?.next().ok_or_else(|| { let addr = addr.to_socket_addrs()?.next().ok_or_else(|| {
io::Error::new(io::ErrorKind::InvalidInput, "No socket address given") io::Error::new(io::ErrorKind::InvalidInput, "No socket address given")
})?; })?;
Ok(Self { proxy_addr: addr }) Ok(Self { proxy_addr: addr })
} }
} }
impl Service<Uri> for ProxyTunnel { impl Service<Uri> for ProxyTunnel {
type Response = TcpStream; type Response = TcpStream;
type Error = io::Error; type Error = io::Error;
type Future = Pin<Box<dyn Future<Output = io::Result<TcpStream>> + Send>>; type Future = Pin<Box<dyn Future<Output = io::Result<TcpStream>> + Send>>;
@ -103,4 +104,6 @@ impl Service<Uri> for ProxyTunnel {
Box::pin(fut) Box::pin(fut)
} }
}
}
} }