1
0
Fork 0
mirror of https://github.com/librespot-org/librespot.git synced 2025-10-04 10:19:27 +02:00

Add proxy support to apresolve

This commit is contained in:
johannesd3 2021-01-25 20:55:49 +01:00
parent 9546fb6e61
commit 07514c9dcc
4 changed files with 61 additions and 14 deletions

View file

@ -1,7 +1,8 @@
const AP_FALLBACK: &'static str = "ap.spotify.com:443";
const APRESOLVE_ENDPOINT: &'static str = "http://apresolve.spotify.com/";
use hyper::{Body, Client, Method, Request, Uri};
use hyper::{client::HttpConnector, Body, Client, Method, Request, Uri};
use hyper_proxy::{Intercept, Proxy, ProxyConnector};
use std::error::Error;
use url::Url;
@ -13,7 +14,7 @@ pub struct APResolveData {
async fn apresolve(proxy: &Option<Url>, ap_port: &Option<u16>) -> Result<String, Box<dyn Error>> {
let port = ap_port.unwrap_or(443);
let req = Request::builder()
let mut req = Request::builder()
.method(Method::GET)
.uri(
APRESOLVE_ENDPOINT
@ -22,25 +23,22 @@ async fn apresolve(proxy: &Option<Url>, ap_port: &Option<u16>) -> Result<String,
)
.body(Body::empty())?;
let client = if proxy.is_some() {
todo!("proxies not yet supported")
/*let proxy = {
let proxy_url = val.as_str().parse().expect("invalid http proxy");
let mut proxy = Proxy::new(Intercept::All, proxy_url);
let response = if let Some(url) = proxy {
let proxy = {
let proxy_url = url.as_str().parse().expect("invalid http proxy");
let proxy = Proxy::new(Intercept::All, proxy_url);
let connector = HttpConnector::new();
let proxy_connector = ProxyConnector::from_proxy_unsecured(connector, proxy);
proxy_connector
ProxyConnector::from_proxy_unsecured(connector, proxy)
};
if let Some(headers) = proxy.http_headers(&APRESOLVE_ENDPOINT.parse().unwrap()) {
req.headers_mut().extend(headers.clone());
};
Client::builder().build(proxy)*/
} else {
Client::new()
};
let response = client.request(req).await?;
Client::builder().build(proxy).request(req).await?
} else {
Client::new().request(req).await?
};
let body = hyper::body::to_bytes(response.into_body()).await?;
let data: APResolveData = serde_json::from_slice(body.as_ref())?;
@ -57,6 +55,7 @@ async fn apresolve(proxy: &Option<Url>, ap_port: &Option<u16>) -> Result<String,
data.ap_list.into_iter().next()
}
.ok_or("empty AP List")?;
Ok(ap)
}