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

46
Cargo.lock generated
View file

@ -962,6 +962,31 @@ version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04" checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04"
[[package]]
name = "headers"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62689dc57c7456e69712607ffcbd0aa1dfcccf9af73727e9b25bc1825375cac3"
dependencies = [
"base64 0.13.0",
"bitflags 1.2.1",
"bytes",
"headers-core",
"http",
"mime",
"sha-1",
"time 0.1.43",
]
[[package]]
name = "headers-core"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7f66481bfee273957b1f20485a4ff3362987f85b2c236580d81b4eb7a326429"
dependencies = [
"http",
]
[[package]] [[package]]
name = "heck" name = "heck"
version = "0.3.2" version = "0.3.2"
@ -1047,6 +1072,20 @@ dependencies = [
"want", "want",
] ]
[[package]]
name = "hyper-proxy"
version = "0.8.0"
source = "git+https://github.com/e00E/hyper-proxy.git?branch=upgrade-tokio#4be706f2f0297bd3d14f301b6ea0be8f3078bb17"
dependencies = [
"bytes",
"futures",
"headers",
"http",
"hyper",
"tokio",
"tower-service",
]
[[package]] [[package]]
name = "ident_case" name = "ident_case"
version = "1.0.1" version = "1.0.1"
@ -1323,6 +1362,7 @@ dependencies = [
"hmac", "hmac",
"httparse", "httparse",
"hyper", "hyper",
"hyper-proxy",
"librespot-protocol", "librespot-protocol",
"log", "log",
"num-bigint", "num-bigint",
@ -1452,6 +1492,12 @@ version = "2.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525"
[[package]]
name = "mime"
version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d"
[[package]] [[package]]
name = "miniz_oxide" name = "miniz_oxide"
version = "0.4.3" version = "0.4.3"

View file

@ -21,6 +21,7 @@ futures = { version = "0.3", features = ["bilock", "unstable"] }
hmac = "0.7" hmac = "0.7"
httparse = "1.3" httparse = "1.3"
hyper = { version = "0.14", features = ["client", "tcp", "http1", "http2"] } hyper = { version = "0.14", features = ["client", "tcp", "http1", "http2"] }
hyper-proxy = { git = "https://github.com/e00E/hyper-proxy.git", branch="upgrade-tokio", default_features = false }
log = "0.4" log = "0.4"
num-bigint = "0.3" num-bigint = "0.3"
num-integer = "0.1" num-integer = "0.1"

View file

@ -1,7 +1,8 @@
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/"; 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 std::error::Error;
use url::Url; 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>> { 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 mut req = Request::builder()
.method(Method::GET) .method(Method::GET)
.uri( .uri(
APRESOLVE_ENDPOINT APRESOLVE_ENDPOINT
@ -22,25 +23,22 @@ async fn apresolve(proxy: &Option<Url>, ap_port: &Option<u16>) -> Result<String,
) )
.body(Body::empty())?; .body(Body::empty())?;
let client = if proxy.is_some() { let response = if let Some(url) = proxy {
todo!("proxies not yet supported") let proxy = {
/*let proxy = { let proxy_url = url.as_str().parse().expect("invalid http proxy");
let proxy_url = val.as_str().parse().expect("invalid http proxy"); let proxy = Proxy::new(Intercept::All, proxy_url);
let mut proxy = Proxy::new(Intercept::All, proxy_url);
let connector = HttpConnector::new(); let connector = HttpConnector::new();
let proxy_connector = ProxyConnector::from_proxy_unsecured(connector, proxy); ProxyConnector::from_proxy_unsecured(connector, proxy)
proxy_connector
}; };
if let Some(headers) = proxy.http_headers(&APRESOLVE_ENDPOINT.parse().unwrap()) { if let Some(headers) = proxy.http_headers(&APRESOLVE_ENDPOINT.parse().unwrap()) {
req.headers_mut().extend(headers.clone()); 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 body = hyper::body::to_bytes(response.into_body()).await?;
let data: APResolveData = serde_json::from_slice(body.as_ref())?; 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() data.ap_list.into_iter().next()
} }
.ok_or("empty AP List")?; .ok_or("empty AP List")?;
Ok(ap) Ok(ap)
} }

View file

@ -14,6 +14,7 @@ extern crate futures;
extern crate hmac; extern crate hmac;
extern crate httparse; extern crate httparse;
extern crate hyper; extern crate hyper;
extern crate hyper_proxy;
extern crate num_bigint; extern crate num_bigint;
extern crate num_integer; extern crate num_integer;
extern crate num_traits; extern crate num_traits;