Skip to main content

instapi360_cloud/
config.rs

1//! Client configuration: which cloud environment and how the client identifies
2//! itself. Values that are still being reverse-engineered are marked below and
3//! centralized here so a single edit wires them in once capture confirms them.
4
5use std::fmt;
6
7/// Cloud environment / region. The base host is region-selected; this user's
8/// desktop `startup.ini` reported `current_area=OverSea` →
9/// `HOST_OPENAPI_INSTA360=openapi-g.insta360.com`.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
11pub enum Region {
12    /// Global / OverSea: `openapi-g.insta360.com`.
13    #[default]
14    Global,
15    /// Mainland China: `openapi.insta360.com`.
16    Cn,
17    /// Test environment: `openapi-test.insta360.com`.
18    Test,
19}
20
21impl Region {
22    /// Base origin for the **account/app** API (`/account/*`, `/app/*`).
23    pub fn openapi_base(self) -> &'static str {
24        match self {
25            Region::Global => "https://openapi-g.insta360.com",
26            Region::Cn => "https://openapi.insta360.com",
27            Region::Test => "https://openapi-test.insta360.com",
28        }
29    }
30
31    /// Base origin for the **cloud media** API (`/cloud/service/*`). This is a
32    /// different host from the account API — verified: list/download 404 on
33    /// openapi-g but serve from the regional `service-*` host.
34    pub fn cloud_base(self) -> &'static str {
35        match self {
36            // "fra" = Frankfurt (OverSea/EU). Other regions' hosts differ; this
37            // is the confirmed Global one.
38            Region::Global => "https://service-fra.insta360.com",
39            Region::Cn => "https://service.insta360.com",
40            Region::Test => "https://service-test.insta360.com",
41        }
42    }
43
44    /// Value sent in the `X-MainLand` header (`1` for CN, `0` otherwise).
45    pub fn mainland_flag(self) -> &'static str {
46        match self {
47            Region::Cn => "1",
48            _ => "0",
49        }
50    }
51}
52
53/// The platform value sent in `x-insta360-platform` and used to pick the
54/// client-identity constants. The desktop app sends `windows`; a future Android
55/// build sends `android`.
56#[derive(Debug, Clone, Copy, PartialEq, Eq)]
57pub enum Platform {
58    Windows,
59    Android,
60}
61
62impl Platform {
63    /// Value for `x-insta360-platform`. The desktop app sends `pc/windows`
64    /// (verbatim from captured requests), not just `windows`.
65    pub fn header_value(self) -> &'static str {
66        match self {
67            Platform::Windows => "pc/windows",
68            Platform::Android => "android",
69        }
70    }
71}
72
73impl fmt::Display for Platform {
74    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75        f.write_str(self.header_value())
76    }
77}
78
79/// Static client identity + request-signing material.
80///
81/// NOTE (reverse-engineering pending): `app_key`, `client_id` and `sign_key`
82/// are not hardcoded literals in the desktop binary — they are recovered from
83/// mitmproxy capture / APK analysis (see the project plan, Steps 2–3). Until
84/// then they default to empty and signing will return [`crate::Error::Signing`].
85#[derive(Debug, Clone)]
86pub struct ClientConfig {
87    pub region: Region,
88    pub platform: Platform,
89    /// `X-APP-VERSION` — e.g. the Studio build `6.0.2` or the app version.
90    pub app_version: String,
91    /// `X-APP` — short app identifier.
92    pub app_id: String,
93    /// `app_traffic` header — release channel (e.g. `release`).
94    pub app_traffic: String,
95    /// `X-Client-Id`.
96    pub client_id: String,
97    /// `X-MCS-AppKey` — the app key sent with signed requests.
98    pub app_key: String,
99    /// Per-install `X-Equipment-Code`. Persisted by the caller across runs.
100    pub equipment_code: String,
101    /// UI language sent as `X-Language` (e.g. `en`).
102    pub language: String,
103    /// HMAC key for request signing (`client_key`/secret). Pending capture.
104    pub sign_key: String,
105    /// `User-Agent` header.
106    pub user_agent: String,
107}
108
109impl ClientConfig {
110    /// A skeleton config for the desktop (`windows`) client. Identity/signing
111    /// fields are left blank and must be filled from capture before signed
112    /// endpoints will work; token-import + download of already-signed OSS URLs
113    /// works without them.
114    pub fn windows(region: Region) -> Self {
115        ClientConfig {
116            region,
117            platform: Platform::Windows,
118            app_version: "6.0.2".to_string(),
119            app_id: "studio_win".to_string(),
120            app_traffic: "release".to_string(),
121            client_id: String::new(),
122            app_key: String::new(),
123            equipment_code: String::new(),
124            language: "en".to_string(),
125            sign_key: String::new(),
126            user_agent: "insta360/studio".to_string(),
127        }
128    }
129
130    /// True once the fields required to sign an API request are populated.
131    pub fn can_sign(&self) -> bool {
132        !self.app_key.is_empty() && !self.sign_key.is_empty()
133    }
134}