Skip to content

Commit a245a3c

Browse files
authored
Merge pull request #1230 from dsheets/cargo-replace-hex-with-data-encoding
Cargo replace hex with data-encoding
2 parents f7ffa9a + 0fbd19b commit a245a3c

6 files changed

Lines changed: 45 additions & 28 deletions

File tree

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ path = "protocol"
5050
version = "0.5.0-dev"
5151

5252
[dependencies]
53+
data-encoding = "2.5"
5354
env_logger = { version = "0.10", default-features = false, features = ["color", "humantime", "auto-color"] }
5455
futures-util = { version = "0.3", default_features = false }
5556
getopts = "0.2"
56-
hex = "0.4"
5757
log = "0.4"
5858
rpassword = "7.0"
5959
sha1 = "0.10"

core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ form_urlencoded = "1.0"
2323
futures-core = "0.3"
2424
futures-util = { version = "0.3", features = ["alloc", "bilock", "sink", "unstable"] }
2525
governor = { version = "0.6", default-features = false, features = ["std", "jitter"] }
26-
hex = "0.4"
2726
hmac = "0.12"
2827
httparse = "1.7"
2928
http = "0.2"
@@ -57,6 +56,7 @@ tokio-tungstenite = { version = "*", default-features = false, features = ["rust
5756
tokio-util = { version = "0.7", features = ["codec"] }
5857
url = "2"
5958
uuid = { version = "1", default-features = false, features = ["fast-rng", "v4"] }
59+
data-encoding = "2.5"
6060

6161
[build-dependencies]
6262
rand = "0.8"

core/src/config.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ pub struct SessionConfig {
1616
pub autoplay: Option<bool>,
1717
}
1818

19-
impl Default for SessionConfig {
20-
fn default() -> SessionConfig {
19+
impl SessionConfig {
20+
pub(crate) fn default_for_os(os: &str) -> Self {
2121
let device_id = uuid::Uuid::new_v4().as_hyphenated().to_string();
22-
let client_id = match std::env::consts::OS {
22+
let client_id = match os {
2323
"android" => ANDROID_CLIENT_ID,
2424
"ios" => IOS_CLIENT_ID,
2525
_ => KEYMASTER_CLIENT_ID,
2626
}
2727
.to_owned();
2828

29-
SessionConfig {
29+
Self {
3030
client_id,
3131
device_id,
3232
proxy: None,
@@ -37,6 +37,12 @@ impl Default for SessionConfig {
3737
}
3838
}
3939

40+
impl Default for SessionConfig {
41+
fn default() -> Self {
42+
Self::default_for_os(std::env::consts::OS)
43+
}
44+
}
45+
4046
#[derive(Clone, Copy, Debug, Hash, PartialOrd, Ord, PartialEq, Eq)]
4147
pub enum DeviceType {
4248
Unknown = 0,

core/src/spclient.rs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::{
66

77
use byteorder::{BigEndian, ByteOrder};
88
use bytes::Bytes;
9+
use data_encoding::HEXUPPER_PERMISSIVE;
910
use futures_util::future::IntoStream;
1011
use http::header::HeaderValue;
1112
use hyper::{
@@ -189,9 +190,10 @@ impl SpClient {
189190
// on macOS and Windows. On Android and iOS we can send a platform-specific client ID and are
190191
// then presented with a hash cash challenge. On Linux, we have to pass the old keymaster ID.
191192
// We delegate most of this logic to `SessionConfig`.
192-
let client_id = match OS {
193+
let os = OS;
194+
let client_id = match os {
193195
"macos" | "windows" => self.session().client_id(),
194-
_ => SessionConfig::default().client_id,
196+
os => SessionConfig::default_for_os(os).client_id,
195197
};
196198
client_data.client_id = client_id;
197199

@@ -206,7 +208,7 @@ impl SpClient {
206208
let os_version = sys.os_version().unwrap_or_else(|| String::from("0"));
207209
let kernel_version = sys.kernel_version().unwrap_or_else(|| String::from("0"));
208210

209-
match OS {
211+
match os {
210212
"windows" => {
211213
let os_version = os_version.parse::<f32>().unwrap_or(10.) as i32;
212214
let kernel_version = kernel_version.parse::<i32>().unwrap_or(21370);
@@ -269,7 +271,10 @@ impl SpClient {
269271
match ClientTokenResponseType::from_i32(message.response_type.value()) {
270272
// depending on the platform, you're either given a token immediately
271273
// or are presented a hash cash challenge to solve first
272-
Some(ClientTokenResponseType::RESPONSE_GRANTED_TOKEN_RESPONSE) => break message,
274+
Some(ClientTokenResponseType::RESPONSE_GRANTED_TOKEN_RESPONSE) => {
275+
debug!("Received a granted token");
276+
break message;
277+
}
273278
Some(ClientTokenResponseType::RESPONSE_CHALLENGES_RESPONSE) => {
274279
debug!("Received a hash cash challenge, solving...");
275280

@@ -279,20 +284,22 @@ impl SpClient {
279284
let hash_cash_challenge = challenge.evaluate_hashcash_parameters();
280285

281286
let ctx = vec![];
282-
let prefix = hex::decode(&hash_cash_challenge.prefix).map_err(|e| {
283-
Error::failed_precondition(format!(
284-
"Unable to decode hash cash challenge: {e}"
285-
))
286-
})?;
287+
let prefix = HEXUPPER_PERMISSIVE
288+
.decode(hash_cash_challenge.prefix.as_bytes())
289+
.map_err(|e| {
290+
Error::failed_precondition(format!(
291+
"Unable to decode hash cash challenge: {e}"
292+
))
293+
})?;
287294
let length = hash_cash_challenge.length;
288295

289-
let mut suffix = vec![0; 0x10];
296+
let mut suffix = [0u8; 0x10];
290297
let answer = Self::solve_hash_cash(&ctx, &prefix, length, &mut suffix);
291298

292299
match answer {
293300
Ok(_) => {
294301
// the suffix must be in uppercase
295-
let suffix = hex::encode(suffix).to_uppercase();
302+
let suffix = HEXUPPER_PERMISSIVE.encode(&suffix);
296303

297304
let mut answer_message = ClientTokenRequest::new();
298305
answer_message.request_type =
@@ -302,7 +309,7 @@ impl SpClient {
302309
let challenge_answers = answer_message.mut_challenge_answers();
303310

304311
let mut challenge_answer = ChallengeAnswer::new();
305-
challenge_answer.mut_hash_cash().suffix = suffix.to_string();
312+
challenge_answer.mut_hash_cash().suffix = suffix;
306313
challenge_answer.ChallengeType =
307314
ChallengeType::CHALLENGE_HASH_CASH.into();
308315

@@ -477,11 +484,14 @@ impl SpClient {
477484
HeaderValue::from_str(&format!("{} {}", token.token_type, token.access_token,))?,
478485
);
479486

480-
if let Ok(client_token) = self.client_token().await {
481-
headers_mut.insert(CLIENT_TOKEN, HeaderValue::from_str(&client_token)?);
482-
} else {
483-
// currently these endpoints seem to work fine without it
484-
warn!("Unable to get client token. Trying to continue without...");
487+
match self.client_token().await {
488+
Ok(client_token) => {
489+
let _ = headers_mut.insert(CLIENT_TOKEN, HeaderValue::from_str(&client_token)?);
490+
}
491+
Err(e) => {
492+
// currently these endpoints seem to work fine without it
493+
warn!("Unable to get client token: {e} Trying to continue without...")
494+
}
485495
}
486496

487497
last_response = self.session().http_client().request_body(request).await;

src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use data_encoding::HEXLOWER;
12
use futures_util::StreamExt;
23
use log::{debug, error, info, trace, warn};
34
use sha1::{Digest, Sha1};
@@ -39,7 +40,7 @@ mod player_event_handler;
3940
use player_event_handler::{run_program_on_sink_events, EventHandler};
4041

4142
fn device_id(name: &str) -> String {
42-
hex::encode(Sha1::digest(name.as_bytes()))
43+
HEXLOWER.encode(&Sha1::digest(name.as_bytes()))
4344
}
4445

4546
fn usage(program: &str, opts: &getopts::Options) -> String {

0 commit comments

Comments
 (0)