Skip to content

Commit

Permalink
2023-02-28 aggregate fixes, but Xiaomi Server denies unlock anyway
Browse files Browse the repository at this point in the history
francescotescari#41 - login URL fix
francescotescari#94 - Use CookieManager from javafx
francescotescari#67 - update JavaFX to show captcha properly

finishes with:
Failed to unlock your device, Xiaomi server returned error 20045:
Error descripiton: Unknown error: 20045
Server description: Please use common user tool on the official website
  • Loading branch information
nazarewk committed Feb 28, 2023
1 parent 256679c commit 5c351d8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 119 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ java {
}

javafx {
version = "11.0.2"
version = "19.0.2.1"
modules = listOf("javafx.controls", "javafx.fxml", "javafx.web", "javafx.swing")
}

Expand Down
72 changes: 47 additions & 25 deletions src/main/java/com/xiaomitool/v2/gui/controller/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import com.xiaomitool.v2.gui.visual.VisiblePane;
import com.xiaomitool.v2.language.LRes;
import com.xiaomitool.v2.logging.Log;
import com.xiaomitool.v2.utility.Pointer;
import com.xiaomitool.v2.utility.WaitSemaphore;
import com.xiaomitool.v2.utility.utils.CookieUtils;
import com.xiaomitool.v2.xiaomi.XiaomiKeystore;
import javafx.application.Platform;
import javafx.concurrent.Worker;
Expand All @@ -31,12 +29,15 @@
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

import java.net.HttpCookie;
import java.net.CookieHandler;
import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class LoginController extends DefaultController {
private static final String LOGIN_URL = "https://account.xiaomi.com/pass/serviceLogin?sid=passport&json=false&passive=true&hidden=false&_snsDefault=facebook&_locale=" + Locale.getDefault().getLanguage().toLowerCase();
private static final String LOGIN_URL = "https://account.xiaomi.com/pass/serviceLogin?sid=unlockApi&json=false&passive=true&hidden=false&_snsDefault=facebook&checkSafePhone=true&_locale=" + Locale.getDefault().getLanguage().toLowerCase();
private static boolean loggedIn = false;
private static Thread loginThread = null;
@FXML
Expand Down Expand Up @@ -90,7 +91,7 @@ public void run() {
}

public static void logout() {
CookieUtils.clear();
CookieHandler.setDefault(null);
XiaomiKeystore.clear();
setLoginNumber(null);
}
Expand Down Expand Up @@ -203,26 +204,6 @@ private void initBrowser() {
BROWSER_AREA.add(LOADING_NODE);
ENGINE = BROWSER.getEngine();
ENGINE.load(LOGIN_URL);
Pointer pointer = new Pointer();
pointer.pointed = new CookieUtils.EventCookieAdd() {
@Override
public boolean run(URI url, HttpCookie cookie) {
String name = cookie.getName();
if ("passToken".equals(name)) {
passToken = cookie.getValue();
} else if ("deviceId".equals(name)) {
deviceId = cookie.getValue();
} else if ("userId".equals(name)) {
userId = cookie.getValue();
}
if (passToken != null && userId != null && deviceId != null && !passToken.isEmpty() && !userId.isEmpty() && !deviceId.isEmpty()) {
loginDone();
return false;
}
return true;
}
};
CookieUtils.addCookieListener((CookieUtils.EventCookieAdd) pointer.pointed);
ENGINE.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> {
if (loadingLocalContent) {
return;
Expand All @@ -236,10 +217,51 @@ public boolean run(URI url, HttpCookie cookie) {
setErrorPage();
} else if (Worker.State.SUCCEEDED.equals(newValue)) {
setBrowserPage();

scanCookies();
if (passToken != null && !passToken.isEmpty()
&& userId != null && !userId.isEmpty()
&& deviceId != null && !deviceId.isEmpty()) {
loginDone();
}
}
});
}

private void scanCookies() {
CookieHandler cookies = CookieHandler.getDefault();
if (cookies == null) {
Log.error("disabled cookie handler");
return;
}

Map<String, List<String>> headers = Collections.emptyMap();
try {
headers = cookies.get(new URI(LOGIN_URL), headers);
} catch (java.net.URISyntaxException e) {
assert false; // unreachable
} catch (java.io.IOException e) {
assert false; // unreachable
}

for (String hdr : headers.getOrDefault("Cookie", Collections.emptyList())) {
for (String pair : hdr.split(";")) {
String[] p = pair.split("=", 2);
if (p.length < 2) continue;
String name = p[0].trim();
String value = p[1].trim();

if ("passToken".equals(name)) {
passToken = value;
} else if ("userId".equals(name)) {
userId = value;
} else if ("deviceId".equals(name)) {
deviceId = value;
}
}
}
}

private void loginDone() {
Log.info("Logged in succesfulyl: " + userId);
XiaomiKeystore.getInstance().setCredentials(userId, passToken, deviceId);
Expand Down
93 changes: 0 additions & 93 deletions src/main/java/com/xiaomitool/v2/utility/utils/CookieUtils.java

This file was deleted.

0 comments on commit 5c351d8

Please sign in to comment.