Easier mocking of platforms (#1378)

* core: move OS info into config.rs
This commit is contained in:
Felix Prillwitz 2024-10-21 22:11:38 +02:00 committed by GitHub
parent 4580dab73f
commit 2e655e7f80
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 25 additions and 15 deletions

View file

@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ### Added
- [core] Add `login` (mobile) and `auth_token` retrieval via login5 - [core] Add `login` (mobile) and `auth_token` retrieval via login5
- [core] Add `OS` and `os_version` to `config.rs`
### Removed ### Removed

View file

@ -6,6 +6,20 @@ pub(crate) const KEYMASTER_CLIENT_ID: &str = "65b708073fc0480ea92a077233ca87bd";
pub(crate) const ANDROID_CLIENT_ID: &str = "9a8d2f0ce77a4e248bb71fefcb557637"; pub(crate) const ANDROID_CLIENT_ID: &str = "9a8d2f0ce77a4e248bb71fefcb557637";
pub(crate) const IOS_CLIENT_ID: &str = "58bd3c95768941ea9eb4350aaa033eb3"; pub(crate) const IOS_CLIENT_ID: &str = "58bd3c95768941ea9eb4350aaa033eb3";
// Easily adjust the current platform to mock the behavior on it. If for example
// android or ios needs to be mocked, the `os_version` has to be set to a valid version.
// Otherwise, client-token or login5 requests will fail with a generic invalid-credential error.
/// See [std::env::consts::OS]
pub const OS: &str = std::env::consts::OS;
// valid versions for some os:
// 'android': 30
// 'ios': 17
/// See [sysinfo::System::os_version]
pub fn os_version() -> String {
sysinfo::System::os_version().unwrap_or("0".into())
}
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct SessionConfig { pub struct SessionConfig {
pub client_id: String, pub client_id: String,
@ -39,7 +53,7 @@ impl SessionConfig {
impl Default for SessionConfig { impl Default for SessionConfig {
fn default() -> Self { fn default() -> Self {
Self::default_for_os(std::env::consts::OS) Self::default_for_os(OS)
} }
} }

View file

@ -110,7 +110,7 @@ where
let mut client_nonce = vec![0; 0x10]; let mut client_nonce = vec![0; 0x10];
thread_rng().fill_bytes(&mut client_nonce); thread_rng().fill_bytes(&mut client_nonce);
let platform = match std::env::consts::OS { let platform = match crate::config::OS {
"android" => Platform::PLATFORM_ANDROID_ARM, "android" => Platform::PLATFORM_ANDROID_ARM,
"freebsd" | "netbsd" | "openbsd" => match ARCH { "freebsd" | "netbsd" | "openbsd" => match ARCH {
"x86_64" => Platform::PLATFORM_FREEBSD_X86_64, "x86_64" => Platform::PLATFORM_FREEBSD_X86_64,

View file

@ -112,7 +112,7 @@ pub async fn authenticate(
_ => CpuFamily::CPU_UNKNOWN, _ => CpuFamily::CPU_UNKNOWN,
}; };
let os = match std::env::consts::OS { let os = match crate::config::OS {
"android" => Os::OS_ANDROID, "android" => Os::OS_ANDROID,
"freebsd" | "netbsd" | "openbsd" => Os::OS_FREEBSD, "freebsd" | "netbsd" | "openbsd" => Os::OS_FREEBSD,
"ios" => Os::OS_IPHONE, "ios" => Os::OS_IPHONE,

View file

@ -1,6 +1,5 @@
use std::{ use std::{
collections::HashMap, collections::HashMap,
env::consts::OS,
time::{Duration, Instant}, time::{Duration, Instant},
}; };
@ -21,11 +20,11 @@ use hyper_util::{
use nonzero_ext::nonzero; use nonzero_ext::nonzero;
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
use parking_lot::Mutex; use parking_lot::Mutex;
use sysinfo::System;
use thiserror::Error; use thiserror::Error;
use url::Url; use url::Url;
use crate::{ use crate::{
config::{os_version, OS},
date::Date, date::Date,
version::{spotify_version, FALLBACK_USER_AGENT, VERSION_STRING}, version::{spotify_version, FALLBACK_USER_AGENT, VERSION_STRING},
Error, Error,
@ -106,12 +105,10 @@ pub struct HttpClient {
impl HttpClient { impl HttpClient {
pub fn new(proxy_url: Option<&Url>) -> Self { pub fn new(proxy_url: Option<&Url>) -> Self {
let zero_str = String::from("0"); let zero_str = String::from("0");
let os_version = System::os_version().unwrap_or_else(|| zero_str.clone()); let os_version = os_version();
let (spotify_platform, os_version) = match OS { let (spotify_platform, os_version) = match OS {
// example os_version: 30
"android" => ("Android", os_version), "android" => ("Android", os_version),
// example os_version: 17
"ios" => ("iOS", os_version), "ios" => ("iOS", os_version),
"macos" => ("OSX", zero_str), "macos" => ("OSX", zero_str),
"windows" => ("Win32", zero_str), "windows" => ("Win32", zero_str),

View file

@ -1,3 +1,4 @@
use crate::config::OS;
use crate::spclient::CLIENT_TOKEN; use crate::spclient::CLIENT_TOKEN;
use crate::token::Token; use crate::token::Token;
use crate::{util, Error, SessionConfig}; use crate::{util, Error, SessionConfig};
@ -15,7 +16,6 @@ use librespot_protocol::{
}; };
use protobuf::well_known_types::duration::Duration as ProtoDuration; use protobuf::well_known_types::duration::Duration as ProtoDuration;
use protobuf::{Message, MessageField}; use protobuf::{Message, MessageField};
use std::env::consts::OS;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use thiserror::Error; use thiserror::Error;
use tokio::time::sleep; use tokio::time::sleep;

View file

@ -1,5 +1,4 @@
use std::{ use std::{
env::consts::OS,
fmt::Write, fmt::Write,
time::{Duration, Instant}, time::{Duration, Instant},
}; };
@ -18,6 +17,7 @@ use rand::RngCore;
use sysinfo::System; use sysinfo::System;
use thiserror::Error; use thiserror::Error;
use crate::config::{os_version, OS};
use crate::{ use crate::{
apresolve::SocketAddress, apresolve::SocketAddress,
cdn_url::CdnUrl, cdn_url::CdnUrl,
@ -162,7 +162,7 @@ impl SpClient {
.platform_specific_data .platform_specific_data
.mut_or_insert_default(); .mut_or_insert_default();
let os_version = System::os_version().unwrap_or_else(|| String::from("0")); let os_version = os_version();
let kernel_version = System::kernel_version().unwrap_or_else(|| String::from("0")); let kernel_version = System::kernel_version().unwrap_or_else(|| String::from("0"));
match os { match os {
@ -191,12 +191,10 @@ impl SpClient {
ios_data.user_interface_idiom = 0; ios_data.user_interface_idiom = 0;
ios_data.target_iphone_simulator = false; ios_data.target_iphone_simulator = false;
ios_data.hw_machine = "iPhone14,5".to_string(); ios_data.hw_machine = "iPhone14,5".to_string();
// example system_version: 17
ios_data.system_version = os_version; ios_data.system_version = os_version;
} }
"android" => { "android" => {
let android_data = platform_data.mut_android(); let android_data = platform_data.mut_android();
// example android_version: 30
android_data.android_version = os_version; android_data.android_version = os_version;
android_data.api_version = 31; android_data.api_version = 31;
"Pixel".clone_into(&mut android_data.device_name); "Pixel".clone_into(&mut android_data.device_name);

View file

@ -29,14 +29,14 @@ pub const SPOTIFY_MOBILE_VERSION: &str = "8.6.84";
pub const FALLBACK_USER_AGENT: &str = "Spotify/117300517 Linux/0 (librespot)"; pub const FALLBACK_USER_AGENT: &str = "Spotify/117300517 Linux/0 (librespot)";
pub fn spotify_version() -> String { pub fn spotify_version() -> String {
match std::env::consts::OS { match crate::config::OS {
"android" | "ios" => SPOTIFY_MOBILE_VERSION.to_owned(), "android" | "ios" => SPOTIFY_MOBILE_VERSION.to_owned(),
_ => SPOTIFY_VERSION.to_string(), _ => SPOTIFY_VERSION.to_string(),
} }
} }
pub fn spotify_semantic_version() -> String { pub fn spotify_semantic_version() -> String {
match std::env::consts::OS { match crate::config::OS {
"android" | "ios" => SPOTIFY_MOBILE_VERSION.to_owned(), "android" | "ios" => SPOTIFY_MOBILE_VERSION.to_owned(),
_ => SPOTIFY_SEMANTIC_VERSION.to_string(), _ => SPOTIFY_SEMANTIC_VERSION.to_string(),
} }