88import com .xiaomitool .v2 .gui .visual .VisiblePane ;
99import com .xiaomitool .v2 .language .LRes ;
1010import com .xiaomitool .v2 .logging .Log ;
11- import com .xiaomitool .v2 .utility .Pointer ;
1211import com .xiaomitool .v2 .utility .WaitSemaphore ;
13- import com .xiaomitool .v2 .utility .utils .CookieUtils ;
1412import com .xiaomitool .v2 .xiaomi .XiaomiKeystore ;
1513import javafx .application .Platform ;
1614import javafx .concurrent .Worker ;
3129import javafx .stage .Stage ;
3230import javafx .stage .WindowEvent ;
3331
34- import java .net .HttpCookie ;
32+ import java .net .CookieHandler ;
3533import java .net .URI ;
34+ import java .util .Collections ;
35+ import java .util .List ;
3636import java .util .Locale ;
37+ import java .util .Map ;
3738
3839public 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 );
0 commit comments