mirror of
https://github.com/VallierDev/uu-remote-linux.git
synced 2026-11-16 16:50:41 +01:00
Like the official client: share a device ID + verification code; the other person connects from 开始协助. Guest identity before sign-in, account identity after (same share ID). One GTK window: 本设备 (allow switch, ID/code with copy, verification modes incl. custom code, rotation schedule, copy & share) and 账号 (QR before sign-in, masked account + sign out after). Logged-in hosts keep the window hidden; `uuremote-server show` / the desktop launcher opens it. App icon and .desktop entry in contrib/desktop. Protocol from the official Windows lite host; guest flow, wrong-code rejection, rotation, switching off, sign-in/sign-out verified with the official Mac viewer. Signed-in assistance from another account is not yet verified. Co-Authored-By: Claude Opus 5.5 <[email protected]>
314 lines
15 KiB
Rust
314 lines
15 KiB
Rust
//! UU Remote Official Authentication & REST Client
|
|
|
|
use hmac::{Hmac, Mac};
|
|
use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
|
|
use serde::{Deserialize, Serialize};
|
|
use sha2::Sha256;
|
|
use std::collections::BTreeMap;
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
|
|
|
type HmacSha256 = Hmac<Sha256>;
|
|
|
|
/// Request-signing key built into the official UU PC client (x-param-plat=1); the same for
|
|
/// every user. Config "sign_key" / UU_SIGN_KEY override it if NetEase rotates the key.
|
|
pub const DEFAULT_SIGN_KEY: &str = "alWiSzXZTLu3WfFnw13uBru3";
|
|
/// API code for an invalid or expired login token ("Token无效或已过期", HTTP 422).
|
|
pub const TOKEN_INVALID: i64 = 1120;
|
|
|
|
#[derive(Debug)]
|
|
pub enum RoomError { LoginExpired, Other(String) }
|
|
impl std::fmt::Display for RoomError {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
match self { RoomError::LoginExpired => f.write_str("login token invalid or expired"), RoomError::Other(e) => f.write_str(e) }
|
|
}
|
|
}
|
|
|
|
pub const BASE_URL: &str = "https://api.nrd.nie.163.com";
|
|
|
|
/// Account binding and request-signing key. Never committed: loaded at startup from
|
|
/// a private config file or environment variables (see config.example.json).
|
|
#[derive(Clone, Serialize, Deserialize, Default)]
|
|
pub struct Credentials {
|
|
pub user_id: String,
|
|
pub device_id: String,
|
|
pub client_id: String,
|
|
pub system_id: String,
|
|
pub token: String,
|
|
pub csrf: String,
|
|
/// HMAC-SHA256 key for x-param-sign.
|
|
pub sign_key: String,
|
|
}
|
|
|
|
impl std::fmt::Debug for Credentials {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
f.write_str("Credentials { .. }")
|
|
}
|
|
}
|
|
|
|
impl Credentials {
|
|
/// Environment variables win over the file (UU_USER_ID, UU_DEVICE_ID, UU_CLIENT_ID,
|
|
/// UU_SYSTEM_ID, UU_TOKEN, UU_CSRF, UU_SIGN_KEY).
|
|
pub fn load(file: Option<&serde_json::Value>) -> Result<Self, String> {
|
|
let get = |key: &str| -> String {
|
|
std::env::var(format!("UU_{}", key.to_uppercase())).ok().filter(|v| !v.is_empty())
|
|
.or_else(|| file.and_then(|f| f.get(key)).and_then(|v| v.as_str()).map(str::to_owned))
|
|
.unwrap_or_default()
|
|
};
|
|
let creds = Self {
|
|
user_id: get("user_id"), device_id: get("device_id"), client_id: get("client_id"),
|
|
system_id: get("system_id"), token: get("token"), csrf: get("csrf"),
|
|
sign_key: Some(get("sign_key")).filter(|k| !k.is_empty()).unwrap_or_else(|| DEFAULT_SIGN_KEY.into()),
|
|
};
|
|
let missing: Vec<&str> = [("user_id", &creds.user_id), ("device_id", &creds.device_id), ("client_id", &creds.client_id),
|
|
("system_id", &creds.system_id), ("token", &creds.token), ("csrf", &creds.csrf), ("sign_key", &creds.sign_key)]
|
|
.iter().filter(|(_, v)| v.is_empty()).map(|(k, _)| *k).collect();
|
|
if missing.is_empty() { Ok(creds) } else { Err(format!("missing credential settings: {}", missing.join(", "))) }
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
pub struct ApiResponse<T> {
|
|
pub code: i32,
|
|
pub msg: String,
|
|
pub data: Option<T>,
|
|
}
|
|
|
|
#[derive(Clone, Deserialize)]
|
|
pub struct RoomData {
|
|
pub signaling_server: String,
|
|
pub token: String,
|
|
pub signaling_list: Option<Vec<String>>,
|
|
pub ws_connect_timeout_ms: Option<u64>,
|
|
}
|
|
|
|
impl std::fmt::Debug for RoomData {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str("RoomData { .. }") }
|
|
}
|
|
|
|
pub struct UUAuthClient {
|
|
pub creds: Credentials,
|
|
http: reqwest::Client,
|
|
guest_id: Option<String>,
|
|
}
|
|
|
|
impl UUAuthClient {
|
|
pub fn new(creds: Credentials) -> Self {
|
|
let http = reqwest::Client::builder()
|
|
.timeout(std::time::Duration::from_secs(12))
|
|
.build()
|
|
.unwrap();
|
|
Self { creds, http, guest_id: None }
|
|
}
|
|
|
|
/// Explicit guest identity: never inherits account Authorization/cookies.
|
|
/// Official ApiDefaultHeaders 0x1003853ac/0x1003853f8 (see assist evidence).
|
|
pub fn new_guest(mut device: Credentials, guest_id: String, token: String) -> Self {
|
|
device.user_id.clear(); device.csrf.clear(); device.token = token;
|
|
let mut client = Self::new(device);
|
|
client.guest_id = Some(guest_id);
|
|
client
|
|
}
|
|
|
|
/// 计算网易 NRD HMAC-SHA256 签名
|
|
pub fn sign_request(
|
|
&self,
|
|
method: &str,
|
|
path: &str,
|
|
headers: &BTreeMap<String, String>,
|
|
body_str: &str,
|
|
) -> String {
|
|
let mut filtered: BTreeMap<String, String> = BTreeMap::new();
|
|
for (k, v) in headers {
|
|
let kl = k.to_lowercase();
|
|
if kl.starts_with("x-param-") && kl != "x-param-sign" {
|
|
filtered.insert(kl, v.clone());
|
|
}
|
|
}
|
|
let params_str = filtered
|
|
.iter()
|
|
.map(|(k, v)| format!("{}={}", k, v))
|
|
.collect::<Vec<_>>()
|
|
.join("&");
|
|
|
|
let string_to_sign = format!("{}{}{}{}", method.to_uppercase(), path, params_str, body_str);
|
|
let mut mac = HmacSha256::new_from_slice(self.creds.sign_key.as_bytes()).expect("HMAC can take key");
|
|
mac.update(string_to_sign.as_bytes());
|
|
hex::encode(mac.finalize().into_bytes())
|
|
}
|
|
|
|
pub fn build_headers(
|
|
&self,
|
|
method: &str,
|
|
path: &str,
|
|
body_str: &str,
|
|
) -> HeaderMap {
|
|
let now = SystemTime::now()
|
|
.duration_since(UNIX_EPOCH)
|
|
.unwrap()
|
|
.as_secs();
|
|
let ts = now.to_string();
|
|
|
|
let mut param_map = BTreeMap::new();
|
|
param_map.insert("x-param-plat".into(), "1".into());
|
|
param_map.insert("x-param-vn".into(), "4.42.0.2770".into());
|
|
param_map.insert("x-param-vc".into(), "2770".into());
|
|
param_map.insert("x-param-pkgn".into(), "com.netease.gameviewer".into());
|
|
param_map.insert("x-param-chn".into(), "gwqd".into());
|
|
param_map.insert("x-param-lang".into(), "zh-CN".into());
|
|
param_map.insert("x-param-cnt".into(), "CN".into());
|
|
// Before login some identifiers do not exist yet; absent ones are not sent or signed.
|
|
for (key, value) in [("x-param-client-id", &self.creds.client_id), ("x-param-device-id", &self.creds.device_id),
|
|
("x-param-system-id", &self.creds.system_id), ("x-param-user-id", &self.creds.user_id)] {
|
|
if !value.is_empty() { param_map.insert(key.into(), value.clone()); }
|
|
}
|
|
if let Some(id) = &self.guest_id { param_map.insert("x-param-guest-id".into(), id.clone()); }
|
|
param_map.insert("x-param-ts".into(), ts);
|
|
|
|
let sign = self.sign_request(method, path, ¶m_map, body_str);
|
|
|
|
let mut headers = HeaderMap::new();
|
|
headers.insert("Content-Type", HeaderValue::from_static("application/json"));
|
|
headers.insert("Accept", HeaderValue::from_static("application/json"));
|
|
headers.insert(
|
|
"User-Agent",
|
|
HeaderValue::from_static("UURemote/4.42.0 (Windows NT 10.0; Win64; x64)"),
|
|
);
|
|
if !self.creds.token.is_empty() {
|
|
if let Ok(v) = HeaderValue::from_str(&format!("Bearer {}", self.creds.token)) { headers.insert("Authorization", v); }
|
|
if self.guest_id.is_none() {
|
|
if let Ok(v) = HeaderValue::from_str(&format!("nrd_access_token={}; nrd_csrf_access_token={}", self.creds.token, self.creds.csrf)) {
|
|
headers.insert("Cookie", v);
|
|
}
|
|
if let Ok(v) = HeaderValue::from_str(&self.creds.csrf) { headers.insert("X-CSRF-TOKEN", v); }
|
|
}
|
|
}
|
|
|
|
for (k, v) in param_map {
|
|
if let Ok(name) = HeaderName::from_bytes(k.as_bytes()) {
|
|
if let Ok(val) = HeaderValue::from_str(&v) {
|
|
headers.insert(name, val);
|
|
}
|
|
}
|
|
}
|
|
headers.insert("X-Param-SIGN", HeaderValue::from_str(&sign).unwrap());
|
|
|
|
headers
|
|
}
|
|
|
|
/// Signed JSON POST; returns (HTTP status, decoded body).
|
|
pub async fn post_json(&self, path: &str, body: &serde_json::Value) -> Result<(u16, serde_json::Value), String> {
|
|
let body_str = serde_json::to_string(body).map_err(|e| e.to_string())?;
|
|
let resp = self.http.post(format!("{}{}", BASE_URL, path))
|
|
.headers(self.build_headers("POST", path, &body_str)).body(body_str)
|
|
.send().await.map_err(|e| format!("HTTP post {path} failed: {e}"))?;
|
|
let status = resp.status().as_u16();
|
|
let value = resp.json().await.unwrap_or(serde_json::Value::Null);
|
|
Ok((status, value))
|
|
}
|
|
|
|
/// Guest APIs use requestPlain for empty requests; sign the actual empty body.
|
|
/// Errors intentionally exclude response text and URL (both can contain secrets).
|
|
pub async fn assist_post(&self, path: &str, body: Option<&serde_json::Value>) -> Result<(u16, serde_json::Value), String> {
|
|
self.assist_send(self.assist_request(path, body)?).await
|
|
}
|
|
/// Account share info is a GET (POST answers 405, verified live).
|
|
pub async fn assist_get(&self, path: &str) -> Result<(u16, serde_json::Value), String> {
|
|
let request = self.http.get(format!("{BASE_URL}{path}")).headers(self.build_headers("GET", path, "")).build()
|
|
.map_err(|_| "assist request construction failed")?;
|
|
self.assist_send(request).await
|
|
}
|
|
async fn assist_send(&self, request: reqwest::Request) -> Result<(u16, serde_json::Value), String> {
|
|
let response = self.http.execute(request)
|
|
.await.map_err(|_| "assist request transport failed")?;
|
|
let status = response.status().as_u16();
|
|
let bytes = response.bytes().await.map_err(|_| "assist response read failed")?;
|
|
if bytes.len() > 65536 { return Err("assist response too large".into()); }
|
|
let value = serde_json::from_slice(&bytes).map_err(|_| "assist response malformed")?;
|
|
Ok((status, value))
|
|
}
|
|
|
|
fn assist_request(&self, path: &str, body: Option<&serde_json::Value>) -> Result<reqwest::Request, String> {
|
|
let body = body.map(serde_json::to_string).transpose().map_err(|_| "assist request encoding failed")?.unwrap_or_default();
|
|
self.http.post(format!("{BASE_URL}{path}"))
|
|
.headers(self.build_headers("POST", path, &body)).body(body).build()
|
|
.map_err(|_| "assist request construction failed".into())
|
|
}
|
|
|
|
/// 向网易服务器请求创建房间,获取最新的信令服务器与鉴权 Token
|
|
pub async fn create_room(&self) -> Result<RoomData, RoomError> {
|
|
let (status, body) = self.post_json("/api/v1/room/create", &serde_json::json!({})).await.map_err(RoomError::Other)?;
|
|
let code = body.get("code").and_then(|c| c.as_i64()).unwrap_or(-1);
|
|
if code == TOKEN_INVALID { return Err(RoomError::LoginExpired); }
|
|
if status == 200 && code == 0 {
|
|
if let Some(data) = body.get("data") {
|
|
let room: RoomData = serde_json::from_value(data.clone()).map_err(|e| RoomError::Other(format!("room data decode failed: {e}")))?;
|
|
let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs();
|
|
log::info!("Room token lifetime: remaining_s={:?}", room_token_lifetime(&room.token, now));
|
|
return Ok(room);
|
|
}
|
|
}
|
|
Err(RoomError::Other(format!("create_room error http {status} code {code}: {}",
|
|
body.get("msg").and_then(|m| m.as_str()).unwrap_or(""))))
|
|
}
|
|
}
|
|
|
|
// Report only the remaining lifetime; never return or log token/claim contents.
|
|
fn room_token_lifetime(token: &str, now: u64) -> Option<i64> {
|
|
use base64::Engine as _;
|
|
let payload = token.split('.').nth(1)?;
|
|
if payload.len() > 16384 { return None; }
|
|
let bytes = base64::engine::general_purpose::URL_SAFE_NO_PAD.decode(payload)
|
|
.or_else(|_| base64::engine::general_purpose::URL_SAFE.decode(payload)).ok()?;
|
|
let claims: serde_json::Value = serde_json::from_slice(&bytes).ok()?;
|
|
let remaining = claims.get("exp")?.as_i64()?.saturating_sub(i64::try_from(now).ok()?);
|
|
(remaining.unsigned_abs() <= 7 * 24 * 3600).then_some(remaining)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod room_lifetime_tests {
|
|
use super::room_token_lifetime;
|
|
use base64::Engine as _;
|
|
#[test]
|
|
fn exposes_only_remaining_seconds_and_handles_opaque_tokens() {
|
|
let payload = base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(br#"{"exp":2200,"ignored":"test-only"}"#);
|
|
assert_eq!(room_token_lifetime(&format!("h.{payload}.s"),1000),Some(1200));
|
|
assert_eq!(room_token_lifetime("opaque",1000),None);
|
|
assert_eq!(room_token_lifetime("h.invalid.s",1000),None);
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod guest_tests {
|
|
use super::*;
|
|
#[test]
|
|
fn official_request_plain_body_and_sanitized_room_debug() {
|
|
let client = UUAuthClient::new_guest(Credentials { sign_key: "fixture-key".into(), ..Default::default() },
|
|
"fixture-guest".into(), "fixture-token".into());
|
|
let request = client.assist_request("/api/v1/guest/create", None).unwrap();
|
|
assert_eq!(request.method(), reqwest::Method::POST);
|
|
assert_eq!(request.body().unwrap().as_bytes(), Some(&b""[..]));
|
|
let other = client.assist_request("/api/v1/guest/create", Some(&serde_json::json!({}))).unwrap();
|
|
assert_eq!(other.body().unwrap().as_bytes(), Some(&b"{}"[..]));
|
|
assert_ne!(request.headers()["x-param-sign"], other.headers()["x-param-sign"]);
|
|
let room: RoomData = serde_json::from_value(serde_json::json!({"signaling_server":"wss://fixture.invalid", "token":"synthetic-room-secret"})).unwrap();
|
|
assert!(!format!("{room:?}").contains("synthetic-room-secret"));
|
|
assert!(serde_json::from_value::<RoomData>(serde_json::json!({"token":42})).is_err());
|
|
}
|
|
#[test]
|
|
fn guest_signs_guest_id_and_never_inherits_account_identity() {
|
|
let account = Credentials { user_id: "fixture-account".into(), token: "fixture-account-token".into(),
|
|
csrf: "fixture-account-csrf".into(), client_id: "fixture-client".into(),
|
|
device_id: "fixture-device".into(), sign_key: "fixture-sign-key".into(), ..Default::default() };
|
|
let client = UUAuthClient::new_guest(account, "fixture-guest".into(), "fixture-guest-token".into());
|
|
let headers = client.build_headers("POST", "/api/v1/guest/room/create", "");
|
|
assert_eq!(headers["authorization"], "Bearer fixture-guest-token");
|
|
assert_eq!(headers["x-param-guest-id"], "fixture-guest");
|
|
for key in ["x-param-user-id", "cookie", "x-csrf-token"] { assert!(!headers.contains_key(key)); }
|
|
let params: BTreeMap<String,String> = headers.iter().filter_map(|(key, value)|
|
|
key.as_str().starts_with("x-param-").then(|| (key.to_string(), value.to_str().unwrap().to_owned()))).collect();
|
|
assert_eq!(headers["x-param-sign"], client.sign_request("POST", "/api/v1/guest/room/create", ¶ms, ""));
|
|
let mut without_guest = params; without_guest.remove("x-param-guest-id");
|
|
assert_ne!(headers["x-param-sign"], client.sign_request("POST", "/api/v1/guest/room/create", &without_guest, ""));
|
|
}
|
|
}
|