Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CookieManager from javafx #94

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 47 additions & 24 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,9 +29,12 @@
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=unlockApi&json=false&passive=true&hidden=false&_snsDefault=facebook&checkSafePhone=true&_locale=" + Locale.getDefault().getLanguage().toLowerCase();
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 @@ -202,27 +203,8 @@ private void initBrowser() {
BROWSER_AREA = new VisiblePane(CONTENT);
BROWSER_AREA.add(LOADING_NODE);
ENGINE = BROWSER.getEngine();
ENGINE.setUserAgent("miNative/1.0");
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 +218,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.