2021-03-17 20:24:28 +00:00
|
|
|
use std::error::Error;
|
2016-03-13 20:03:09 +00:00
|
|
|
|
2021-03-17 20:24:28 +00:00
|
|
|
use hyper::client::HttpConnector;
|
|
|
|
use hyper::{Body, Client, Method, Request, Uri};
|
|
|
|
use hyper_proxy::{Intercept, Proxy, ProxyConnector};
|
|
|
|
use serde::Deserialize;
|
2018-03-24 08:00:38 +00:00
|
|
|
use url::Url;
|
2016-03-13 20:03:09 +00:00
|
|
|
|
2021-03-17 20:24:28 +00:00
|
|
|
use super::AP_FALLBACK;
|
2021-01-30 13:45:31 +00:00
|
|
|
|
2021-03-17 20:24:28 +00:00
|
|
|
const APRESOLVE_ENDPOINT: &str = "http://apresolve.spotify.com:80";
|
2016-03-13 20:03:09 +00:00
|
|
|
|
2021-03-17 20:24:28 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
2021-03-31 18:05:32 +00:00
|
|
|
struct ApResolveData {
|
2021-03-17 20:24:28 +00:00
|
|
|
ap_list: Vec<String>,
|
|
|
|
}
|
2017-01-18 15:17:10 +00:00
|
|
|
|
2021-03-17 20:24:28 +00:00
|
|
|
async fn try_apresolve(
|
|
|
|
proxy: Option<&Url>,
|
|
|
|
ap_port: Option<u16>,
|
|
|
|
) -> Result<String, Box<dyn Error>> {
|
|
|
|
let port = ap_port.unwrap_or(443);
|
2018-07-03 11:09:22 +00:00
|
|
|
|
2021-03-17 20:24:28 +00:00
|
|
|
let mut req = Request::new(Body::empty());
|
|
|
|
*req.method_mut() = Method::GET;
|
|
|
|
// panic safety: APRESOLVE_ENDPOINT above is valid url.
|
|
|
|
*req.uri_mut() = APRESOLVE_ENDPOINT.parse().expect("invalid AP resolve URL");
|
2021-02-10 21:50:08 +00:00
|
|
|
|
2021-03-17 20:24:28 +00:00
|
|
|
let response = if let Some(url) = proxy {
|
|
|
|
// Panic safety: all URLs are valid URIs
|
|
|
|
let uri = url.to_string().parse().unwrap();
|
|
|
|
let proxy = Proxy::new(Intercept::All, uri);
|
|
|
|
let connector = HttpConnector::new();
|
|
|
|
let proxy_connector = ProxyConnector::from_proxy_unsecured(connector, proxy);
|
|
|
|
Client::builder()
|
|
|
|
.build(proxy_connector)
|
|
|
|
.request(req)
|
|
|
|
.await?
|
|
|
|
} else {
|
|
|
|
Client::new().request(req).await?
|
|
|
|
};
|
2021-02-10 21:50:08 +00:00
|
|
|
|
2021-03-17 20:24:28 +00:00
|
|
|
let body = hyper::body::to_bytes(response.into_body()).await?;
|
2021-03-31 18:05:32 +00:00
|
|
|
let data: ApResolveData = serde_json::from_slice(body.as_ref())?;
|
2021-02-10 21:50:08 +00:00
|
|
|
|
2021-03-17 20:24:28 +00:00
|
|
|
let ap = if ap_port.is_some() || proxy.is_some() {
|
|
|
|
data.ap_list.into_iter().find_map(|ap| {
|
|
|
|
if ap.parse::<Uri>().ok()?.port()? == port {
|
|
|
|
Some(ap)
|
2021-02-10 21:50:08 +00:00
|
|
|
} else {
|
2021-03-17 20:24:28 +00:00
|
|
|
None
|
2018-03-23 15:52:24 +00:00
|
|
|
}
|
2021-03-17 20:24:28 +00:00
|
|
|
})
|
2021-02-10 21:50:08 +00:00
|
|
|
} else {
|
2021-03-17 20:24:28 +00:00
|
|
|
data.ap_list.into_iter().next()
|
2021-02-10 21:50:08 +00:00
|
|
|
}
|
2021-03-17 20:24:28 +00:00
|
|
|
.ok_or("empty AP List")?;
|
|
|
|
|
|
|
|
Ok(ap)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn apresolve(proxy: Option<&Url>, ap_port: Option<u16>) -> String {
|
|
|
|
try_apresolve(proxy, ap_port).await.unwrap_or_else(|e| {
|
|
|
|
warn!("Failed to resolve Access Point: {}", e);
|
|
|
|
warn!("Using fallback \"{}\"", AP_FALLBACK);
|
|
|
|
AP_FALLBACK.into()
|
|
|
|
})
|
2016-03-13 20:03:09 +00:00
|
|
|
}
|