Skip to content

Commit 5c351d8

Browse files
committed
2023-02-28 aggregate fixes, but Xiaomi Server denies unlock anyway
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
1 parent 256679c commit 5c351d8

File tree

3 files changed

+48
-119
lines changed

3 files changed

+48
-119
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ java {
3232
}
3333

3434
javafx {
35-
version = "11.0.2"
35+
version = "19.0.2.1"
3636
modules = listOf("javafx.controls", "javafx.fxml", "javafx.web", "javafx.swing")
3737
}
3838

src/main/java/com/xiaomitool/v2/gui/controller/LoginController.java

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
import com.xiaomitool.v2.gui.visual.VisiblePane;
99
import com.xiaomitool.v2.language.LRes;
1010
import com.xiaomitool.v2.logging.Log;
11-
import com.xiaomitool.v2.utility.Pointer;
1211
import com.xiaomitool.v2.utility.WaitSemaphore;
13-
import com.xiaomitool.v2.utility.utils.CookieUtils;
1412
import com.xiaomitool.v2.xiaomi.XiaomiKeystore;
1513
import javafx.application.Platform;
1614
import javafx.concurrent.Worker;
@@ -31,12 +29,15 @@
3129
import javafx.stage.Stage;
3230
import javafx.stage.WindowEvent;
3331

34-
import java.net.HttpCookie;
32+
import java.net.CookieHandler;
3533
import java.net.URI;
34+
import java.util.Collections;
35+
import java.util.List;
3636
import java.util.Locale;
37+
import java.util.Map;
3738

3839
public class LoginController extends DefaultController {
39-
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();
40+
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();
4041
private static boolean loggedIn = false;
4142
private static Thread loginThread = null;
4243
@FXML
@@ -90,7 +91,7 @@ public void run() {
9091
}
9192

9293
public static void logout() {
93-
CookieUtils.clear();
94+
CookieHandler.setDefault(null);
9495
XiaomiKeystore.clear();
9596
setLoginNumber(null);
9697
}
@@ -203,26 +204,6 @@ private void initBrowser() {
203204
BROWSER_AREA.add(LOADING_NODE);
204205
ENGINE = BROWSER.getEngine();
205206
ENGINE.load(LOGIN_URL);
206-
Pointer pointer = new Pointer();
207-
pointer.pointed = new CookieUtils.EventCookieAdd() {
208-
@Override
209-
public boolean run(URI url, HttpCookie cookie) {
210-
String name = cookie.getName();
211-
if ("passToken".equals(name)) {
212-
passToken = cookie.getValue();
213-
} else if ("deviceId".equals(name)) {
214-
deviceId = cookie.getValue();
215-
} else if ("userId".equals(name)) {
216-
userId = cookie.getValue();
217-
}
218-
if (passToken != null && userId != null && deviceId != null && !passToken.isEmpty() && !userId.isEmpty() && !deviceId.isEmpty()) {
219-
loginDone();
220-
return false;
221-
}
222-
return true;
223-
}
224-
};
225-
CookieUtils.addCookieListener((CookieUtils.EventCookieAdd) pointer.pointed);
226207
ENGINE.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> {
227208
if (loadingLocalContent) {
228209
return;
@@ -236,10 +217,51 @@ public boolean run(URI url, HttpCookie cookie) {
236217
setErrorPage();
237218
} else if (Worker.State.SUCCEEDED.equals(newValue)) {
238219
setBrowserPage();
220+
221+
scanCookies();
222+
if (passToken != null && !passToken.isEmpty()
223+
&& userId != null && !userId.isEmpty()
224+
&& deviceId != null && !deviceId.isEmpty()) {
225+
loginDone();
226+
}
239227
}
240228
});
241229
}
242230

231+
private void scanCookies() {
232+
CookieHandler cookies = CookieHandler.getDefault();
233+
if (cookies == null) {
234+
Log.error("disabled cookie handler");
235+
return;
236+
}
237+
238+
Map<String, List<String>> headers = Collections.emptyMap();
239+
try {
240+
headers = cookies.get(new URI(LOGIN_URL), headers);
241+
} catch (java.net.URISyntaxException e) {
242+
assert false; // unreachable
243+
} catch (java.io.IOException e) {
244+
assert false; // unreachable
245+
}
246+
247+
for (String hdr : headers.getOrDefault("Cookie", Collections.emptyList())) {
248+
for (String pair : hdr.split(";")) {
249+
String[] p = pair.split("=", 2);
250+
if (p.length < 2) continue;
251+
String name = p[0].trim();
252+
String value = p[1].trim();
253+
254+
if ("passToken".equals(name)) {
255+
passToken = value;
256+
} else if ("userId".equals(name)) {
257+
userId = value;
258+
} else if ("deviceId".equals(name)) {
259+
deviceId = value;
260+
}
261+
}
262+
}
263+
}
264+
243265
private void loginDone() {
244266
Log.info("Logged in succesfulyl: " + userId);
245267
XiaomiKeystore.getInstance().setCredentials(userId, passToken, deviceId);

src/main/java/com/xiaomitool/v2/utility/utils/CookieUtils.java

Lines changed: 0 additions & 93 deletions
This file was deleted.

0 commit comments

Comments
 (0)