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

Use proxytunnel in apresolve

Implementing the tower_service::Service trait for a newly created
ProxyTunnel struct, so it can be used as connector in hyper.
This commit is contained in:
johannesd3 2021-01-30 14:45:31 +01:00
parent c1d62d72a7
commit bb44b99c92
6 changed files with 95 additions and 81 deletions

View file

@ -1,11 +1,12 @@
const AP_FALLBACK: &'static str = "ap.spotify.com:443";
const APRESOLVE_ENDPOINT: &'static str = "http://apresolve.spotify.com/";
const APRESOLVE_ENDPOINT: &'static str = "http://apresolve.spotify.com:80";
use hyper::{client::HttpConnector, Body, Client, Method, Request, Uri};
use hyper_proxy::{Intercept, Proxy, ProxyConnector};
use hyper::{Body, Client, Method, Request, Uri};
use std::error::Error;
use url::Url;
use crate::proxytunnel::ProxyTunnel;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct APResolveData {
ap_list: Vec<String>,
@ -14,7 +15,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 mut req = Request::builder()
let req = Request::builder()
.method(Method::GET)
.uri(
APRESOLVE_ENDPOINT
@ -24,18 +25,10 @@ async fn apresolve(proxy: &Option<Url>, ap_port: &Option<u16>) -> Result<String,
.body(Body::empty())?;
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();
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).request(req).await?
Client::builder()
.build(ProxyTunnel::new(url)?)
.request(req)
.await?
} else {
Client::new().request(req).await?
};