Skip to content

Lightweight native Discord client for Windows, Linux and macOS, written in Rust. No Electron, no browser engine.

Start every login page signed out; log why Discord ends a session

VRonGitHuba0e4940d2558
diff --git a/apps/rustcord/src/login-handoff.js b/apps/rustcord/src/login-handoff.js
index 849e07d..d8ce32d 100644
--- a/apps/rustcord/src/login-handoff.js
+++ b/apps/rustcord/src/login-handoff.js
@@ -8,6 +8,20 @@
if (window !== window.top || location.origin !== "https://discord.com") return;
const capability = "__RUSTCORD_LOGIN_CAPABILITY__";
let delivered = false;
+ // Set when Discord answers the login with a suspended account's token: that
+ // one only opens Discord's appeal pages, so it is never handed over.
+ let suspended = false;
+
+ // Every login page starts signed out. The pages one RustCord run opens share
+ // the engine's private storage, so a page would otherwise sign in by itself
+ // with the login an earlier page left there, even one Discord has ended.
+ // (A new page has empty sessionStorage; the page's own reloads keep it.)
+ try {
+ if (sessionStorage.getItem("rustcord-login") === null) {
+ localStorage.clear();
+ sessionStorage.setItem("rustcord-login", "1");
+ }
+ } catch {}
const apiPath = value => {
try {
const url = new URL(value, location.href);
@@ -43,10 +57,11 @@
const diag = post("diag:");
const note = post("note:");
- const deliver = value => {
- if (delivered || typeof value !== "string" || value.length < 16 || value.length > 2048 || /\s/.test(value)) return;
+ // `how`: from the login answer, or from a request the page made with it.
+ const deliver = (value, how) => {
+ if (delivered || suspended || typeof value !== "string" || value.length < 16 || value.length > 2048 || /\s/.test(value)) return;
delivered = true;
- diag("signed in");
+ diag("signed in (" + how + ")");
window.ipc.postMessage(capability + value);
};
// Look at a login response: token → done; otherwise say what Discord wants.
@@ -54,7 +69,17 @@
let data = null;
try { data = JSON.parse(text); } catch { return; }
if (!data || typeof data !== "object") return;
- if (typeof data.token === "string") return deliver(data.token);
+ if (typeof data.token === "string") {
+ // What Discord wants done before the account is used normally (such as
+ // UPDATE_PASSWORD, which web asks for once connected): names only.
+ const actions = Array.isArray(data.required_actions) ? data.required_actions.filter(a => /^[A-Z_]{1,48}$/.test(a)) : [];
+ if (actions.length) diag("Discord's login answer lists required actions: " + actions.join(", "));
+ return deliver(data.token, "login answer");
+ }
+ if (typeof data.suspended_user_token === "string") {
+ suspended = true;
+ return note("Discord has suspended this account. Log in on discord.com to see why and to appeal.");
+ }
if (data.mfa === true) return diag("Discord asked for a 2FA code");
if (data.captcha_key) return diag("Discord asked for a captcha");
if (status >= 400) diag("Discord answered " + status + " " + (data.message || ""));
@@ -70,7 +95,7 @@
return open.call(this, method, url, ...rest);
};
XMLHttpRequest.prototype.setRequestHeader = function(name, value) {
- if (allowed(urls.get(this)) && String(name).toLowerCase() === "authorization") deliver(value);
+ if (allowed(urls.get(this)) && String(name).toLowerCase() === "authorization") deliver(value, "page request");
return setHeader.call(this, name, value);
};
XMLHttpRequest.prototype.send = function(...args) {
@@ -90,7 +115,7 @@
if (isTracking(url)) return Promise.reject(new TypeError("Failed to fetch"));
if (allowed(url)) {
const headers = new Headers(init?.headers ?? (input instanceof Request ? input.headers : undefined));
- deliver(headers.get("authorization"));
+ deliver(headers.get("authorization"), "page request");
}
const result = originalFetch.apply(this, arguments);
if (isAuth(url)) {
diff --git a/crates/discord/src/gateway.rs b/crates/discord/src/gateway.rs
index c69ee14..7c1f24e 100644
--- a/crates/discord/src/gateway.rs
+++ b/crates/discord/src/gateway.rs
@@ -609,9 +609,11 @@ where
Some(Ok(WsMessage::Close(frame))) => {
let reason = frame.as_ref().map(|f| f.reason.to_string()).unwrap_or_default();
let code = frame.map(|f| u16::from(f.code)).unwrap_or(1000);
+ // Discord's reason says which check failed (as for 4004).
+ log::info!("gateway: Discord closed the connection: {code} {reason}");
if code == 4002 {
// Discord couldn't take something we sent: say what.
- log::warn!("gateway: Discord closed with 4002 ({reason}); last payloads sent:");
+ log::warn!("gateway: last payloads sent before the 4002:");
// (The payloads themselves stay out of the log file:
// they can hold a custom status or a search.)
for (op, len, preview) in &st.recent {