11package com .fox2code .mmm .androidacy ;
22
3- import android .annotation . SuppressLint ;
3+ import android .content . Context ;
44import android .content .SharedPreferences ;
5+ import android .os .Looper ;
56import android .util .Log ;
67import android .widget .Toast ;
78
89import androidx .annotation .NonNull ;
910
11+ import com .fingerprintjs .android .fingerprint .Fingerprinter ;
12+ import com .fingerprintjs .android .fingerprint .FingerprinterFactory ;
13+ import com .fingerprintjs .android .fpjs_pro .Configuration ;
14+ import com .fingerprintjs .android .fpjs_pro .FingerprintJS ;
15+ import com .fingerprintjs .android .fpjs_pro .FingerprintJSFactory ;
1016import com .fox2code .mmm .BuildConfig ;
1117import com .fox2code .mmm .MainApplication ;
1218import com .fox2code .mmm .R ;
1723import com .fox2code .mmm .utils .Http ;
1824import com .fox2code .mmm .utils .HttpException ;
1925import com .fox2code .mmm .utils .PropUtils ;
26+ import com .google .android .material .dialog .MaterialAlertDialogBuilder ;
2027
2128import org .json .JSONArray ;
2229import org .json .JSONException ;
@@ -84,44 +91,31 @@ public static String generateDeviceId() {
8491 if (deviceIdPref != null ) {
8592 return deviceIdPref ;
8693 } else {
87- // Collect device information
88- String device = android .os .Build .DEVICE ;
89- String model = android .os .Build .MODEL ;
90- String product = android .os .Build .PRODUCT ;
91- String manufacturer = android .os .Build .MANUFACTURER ;
92- String brand = android .os .Build .BRAND ;
93- String androidVersion = android .os .Build .VERSION .RELEASE ;
94- String androidSdk = String .valueOf (android .os .Build .VERSION .SDK_INT );
95- @ SuppressLint ("HardwareIds" ) String androidId = android .provider .Settings .Secure .getString (MainApplication .getINSTANCE ().getContentResolver (), android .provider .Settings .Secure .ANDROID_ID );
96- // Generate a unique ID for this device. For privacy reasons, we don't want to send this
97- // info directly to the server, so we hash it.
98- String deviceId = androidId + device + model + product + manufacturer + brand + androidVersion + androidSdk ;
99- // Now, we need to hash the device ID. We use SHA-256, which is a secure hash function.
100- // This means that it's impossible to reverse the hash and get the original device ID.
101- // We use the SHA-256 hash because it's the same hash function used by the API to hash
102- // the device ID.
103- try {
104- java .security .MessageDigest digest = java .security .MessageDigest .getInstance ("SHA-256" );
105- byte [] hash = digest .digest (deviceId .getBytes (java .nio .charset .StandardCharsets .UTF_8 ));
106- // Convert the hash to a normal string
107- StringBuilder hexString = new StringBuilder ();
108- for (byte b : hash ) {
109- String hex = Integer .toHexString (0xff & b );
110- if (hex .length () == 1 ) {
111- hexString .append ('0' );
112- }
113- hexString .append (hex );
114- }
115- // Save the device ID to the shared preferences
94+ Context context = MainApplication .getINSTANCE ().getApplicationContext ();
95+ FingerprintJSFactory factory = new FingerprintJSFactory (context );
96+ Configuration .Region region = Configuration .Region .US ;
97+ Configuration configuration = new Configuration (
98+ "NiZiHi266YaTLreOIOzc" ,
99+ region ,
100+ region .getEndpointUrl (),
101+ true
102+ );
103+
104+ FingerprintJS fpjsClient = factory .createInstance (
105+ configuration
106+ );
107+
108+ fpjsClient .getVisitorId (visitorIdResponse -> {
109+ // Use the ID
110+ String visitorId = visitorIdResponse .getVisitorId ();
111+ // Save the ID in the shared preferences
116112 SharedPreferences .Editor editor = sharedPreferences .edit ();
117- editor .putString ("device_id" , hexString . toString () );
113+ editor .putString ("device_id" , visitorId );
118114 editor .apply ();
119- return hexString .toString ();
120- } catch (java .security .NoSuchAlgorithmException e ) {
121- // This should never happen, but if it does, we'll just return the device ID without
122- // hashing it.
123- return deviceId ;
124- }
115+ return null ;
116+ });
117+ // return the id
118+ return sharedPreferences .getString ("device_id" , null );
125119 }
126120 }
127121
@@ -147,17 +141,37 @@ public boolean isValidToken(String token) throws IOException {
147141 @ Override
148142 protected boolean prepare () {
149143 if (Http .needCaptchaAndroidacy ()) return false ;
144+ // Check if we have a device ID yet
145+ SharedPreferences sharedPreferences = MainApplication .getINSTANCE ().getSharedPreferences ("androidacy" , 0 );
146+ String deviceIdPref = sharedPreferences .getString ("device_id" , null );
147+ if (deviceIdPref == null ) {
148+ // Generate a device ID
149+ generateDeviceId ();
150+ // Loop until we have a device ID
151+ while (sharedPreferences .getString ("device_id" , null ) == null ) {
152+ try {
153+ Thread .sleep (100 );
154+ } catch (InterruptedException e ) {
155+ e .printStackTrace ();
156+ }
157+ }
158+ }
150159 // Implementation details discussed on telegram
151160 // First, ping the server to check if it's alive
152161 try {
153162 Http .doHttpGet ("https://" + this .host + "/ping" , false );
154163 } catch (Exception e ) {
155164 Log .e (TAG , "Failed to ping server" , e );
156165 // Inform user
157- /*if (!HttpException.shouldTimeout(e)) {
158- UiThreadHandler.run(() -> Toast.makeText(MainApplication.getINSTANCE(),
159- R.string.androidacy_server_down, Toast.LENGTH_SHORT).show());
160- }*/
166+ Looper .prepare ();
167+ MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder (MainApplication .getINSTANCE ().getBaseContext ());
168+ builder .setTitle ("Androidacy Server Down" );
169+ builder .setMessage ("The Androidacy server is down. Unfortunately, this means that you" +
170+ " will not be able to download or view modules from the Androidacy repository" +
171+ ". Please try again later." );
172+ builder .setPositiveButton ("OK" , (dialog , which ) -> dialog .dismiss ());
173+ builder .show ();
174+ Looper .loop ();
161175 return false ;
162176 }
163177 String deviceId = generateDeviceId ();
@@ -383,10 +397,6 @@ public String getName() {
383397 return this .testMode ? super .getName () + " (Test Mode)" : super .getName ();
384398 }
385399
386- String getToken () {
387- return this .token ;
388- }
389-
390400 public void setToken (String token ) {
391401 if (Http .hasWebView ()) {
392402 this .token = token ;
0 commit comments