Skip to content
This repository was archived by the owner on May 4, 2023. It is now read-only.

Commit 35b00cf

Browse files
Update androidacy api integration with new reqs
Signed-off-by: androidacy-user <opensource@androidacy.com>
1 parent 29e3d7e commit 35b00cf

File tree

3 files changed

+79
-19
lines changed

3 files changed

+79
-19
lines changed

app/src/main/java/com/fox2code/mmm/NotificationType.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ public boolean shouldRemove() {
5353
return InstallerInitializer.getErrorNotification() != this;
5454
}
5555
},
56-
MAGISK_OUTDATED(R.string.magisk_outdated, R.drawable.ic_baseline_update_24, v -> {
57-
IntentHelper.openUrl(v.getContext(), "https://github.com/topjohnwu/Magisk/releases");
58-
}) {
56+
MAGISK_OUTDATED(R.string.magisk_outdated, R.drawable.ic_baseline_update_24, v -> IntentHelper.openUrl(v.getContext(), "https://github.com/topjohnwu/Magisk/releases")) {
5957
@Override
6058
public boolean shouldRemove() {
6159
return InstallerInitializer.peekMagiskPath() == null ||
@@ -86,10 +84,8 @@ public boolean shouldRemove() {
8684
}
8785
},
8886
UPDATE_AVAILABLE(R.string.app_update_available, R.drawable.ic_baseline_system_update_24,
89-
R.attr.colorPrimary, R.attr.colorOnPrimary, v -> {
90-
IntentHelper.openUrl(v.getContext(),
91-
"https://github.com/Fox2Code/FoxMagiskModuleManager/releases");
92-
}, false) {
87+
R.attr.colorPrimary, R.attr.colorOnPrimary, v -> IntentHelper.openUrl(v.getContext(),
88+
"https://github.com/Fox2Code/FoxMagiskModuleManager/releases"), false) {
9389
@Override
9490
public boolean shouldRemove() {
9591
return !AppUpdateManager.getAppUpdateManager().peekShouldUpdate();

app/src/main/java/com/fox2code/mmm/androidacy/AndroidacyActivity.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@
3737
import com.fox2code.mmm.utils.IntentHelper;
3838
import com.google.android.material.progressindicator.LinearProgressIndicator;
3939

40-
import org.json.JSONException;
41-
import org.json.JSONObject;
42-
4340
import java.io.ByteArrayInputStream;
4441
import java.io.File;
4542
import java.io.FileOutputStream;
@@ -105,6 +102,13 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
105102
token = MainApplication.getSharedPreferences().getString("pref_androidacy_api_token", null);
106103
url = url + "&token=" + token;
107104
}
105+
// Add device_id to url if not present
106+
String device_id = uri.getQueryParameter("device_id");
107+
if (device_id == null) {
108+
// get from shared preferences
109+
device_id = AndroidacyRepoData.generateDeviceId();
110+
url = url + "&device_id=" + device_id;
111+
}
108112
boolean allowInstall = intent.getBooleanExtra(Constants.EXTRA_ANDROIDACY_ALLOW_INSTALL, false);
109113
String title = intent.getStringExtra(Constants.EXTRA_ANDROIDACY_ACTIONBAR_TITLE);
110114
String config = intent.getStringExtra(Constants.EXTRA_ANDROIDACY_ACTIONBAR_CONFIG);

app/src/main/java/com/fox2code/mmm/androidacy/AndroidacyRepoData.java

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.fox2code.mmm.androidacy;
22

3+
import android.annotation.SuppressLint;
34
import android.content.SharedPreferences;
45
import android.util.Log;
56
import android.widget.Toast;
@@ -16,15 +17,13 @@
1617
import com.fox2code.mmm.utils.Http;
1718
import com.fox2code.mmm.utils.HttpException;
1819
import com.fox2code.mmm.utils.PropUtils;
19-
import com.topjohnwu.superuser.internal.UiThreadHandler;
2020

2121
import org.json.JSONArray;
2222
import org.json.JSONException;
2323
import org.json.JSONObject;
2424

2525
import java.io.File;
2626
import java.io.IOException;
27-
import java.nio.charset.StandardCharsets;
2827
import java.util.ArrayList;
2928
import java.util.Iterator;
3029
import java.util.List;
@@ -44,9 +43,9 @@ public final class AndroidacyRepoData extends RepoData {
4443

4544
private final boolean testMode;
4645
private final String host;
46+
public String token = MainApplication.getINSTANCE().getSharedPreferences("androidacy", 0).getString("pref_androidacy_api_token", null);
4747
// Avoid spamming requests to Androidacy
4848
private long androidacyBlockade = 0;
49-
public String token = this.cachedPreferences.getString("pref_androidacy_api_token", null);
5049

5150
public AndroidacyRepoData(File cacheRoot, SharedPreferences cachedPreferences, boolean testMode) {
5251
super(testMode ? RepoManager.ANDROIDACY_TEST_MAGISK_REPO_ENDPOINT : RepoManager.ANDROIDACY_MAGISK_REPO_ENDPOINT, cacheRoot, cachedPreferences);
@@ -76,14 +75,65 @@ private static String filterURL(String url) {
7675
return url;
7776
}
7877

78+
// Generates a unique device ID. This is used to identify the device in the API for rate
79+
// limiting and fraud detection.
80+
public static String generateDeviceId() {
81+
// Try to get the device ID from the shared preferences
82+
SharedPreferences sharedPreferences = MainApplication.getINSTANCE().getSharedPreferences("androidacy", 0);
83+
String deviceIdPref = sharedPreferences.getString("device_id", null);
84+
if (deviceIdPref != null) {
85+
return deviceIdPref;
86+
} 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
116+
SharedPreferences.Editor editor = sharedPreferences.edit();
117+
editor.putString("device_id", hexString.toString());
118+
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+
}
125+
}
126+
}
127+
79128
public boolean isValidToken(String token) throws IOException {
129+
String deviceId = generateDeviceId();
80130
try {
81-
Http.doHttpGet("https://" + this.host + "/auth/me?token=" + token, false);
131+
Http.doHttpGet("https://" + this.host + "/auth/me?token=" + token + "&device_id=" + deviceId, false);
82132
} catch (HttpException e) {
83133
if (e.getErrorCode() == 401) {
84134
Log.w(TAG, "Invalid token, resetting...");
85135
// Remove saved preference
86-
SharedPreferences.Editor editor = this.cachedPreferences.edit();
136+
SharedPreferences.Editor editor = MainApplication.getINSTANCE().getSharedPreferences("androidacy", 0).edit();
87137
editor.remove("pref_androidacy_api_token");
88138
editor.apply();
89139
return false;
@@ -110,6 +160,7 @@ protected boolean prepare() {
110160
}*/
111161
return false;
112162
}
163+
String deviceId = generateDeviceId();
113164
long time = System.currentTimeMillis();
114165
if (this.androidacyBlockade > time) return false;
115166
this.androidacyBlockade = time + 30_000L;
@@ -137,8 +188,8 @@ protected boolean prepare() {
137188
if (token == null) {
138189
try {
139190
Log.i(TAG, "Requesting new token...");
140-
// POST request to https://production-api.androidacy.com/auth/register
141-
token = new String(Http.doHttpPost("https://" + this.host + "/auth/register", "{\"foxmmm\": \"true\"}", false), StandardCharsets.UTF_8);
191+
// POST json request to https://production-api.androidacy.com/auth/register
192+
token = new String(Http.doHttpPost("https://" + this.host + "/auth/register", "{\"device_id\":\"" + deviceId + "\"}", false));
142193
// Parse token
143194
try {
144195
JSONObject jsonObject = new JSONObject(token);
@@ -157,7 +208,7 @@ protected boolean prepare() {
157208
return false;
158209
}
159210
// Save token to shared preference
160-
SharedPreferences.Editor editor = this.cachedPreferences.edit();
211+
SharedPreferences.Editor editor = MainApplication.getINSTANCE().getSharedPreferences("androidacy", 0).edit();
161212
editor.putString("pref_androidacy_api_token", token);
162213
editor.apply();
163214
} catch (Exception e) {
@@ -289,7 +340,8 @@ public boolean tryLoadMetadata(RepoModule repoModule) {
289340

290341
@Override
291342
public String getUrl() {
292-
return this.token == null ? this.url : this.url + "?token=" + this.token;
343+
return this.token == null ? this.url :
344+
this.url + "?token=" + this.token + "&v=" + BuildConfig.VERSION_CODE + "&c=" + BuildConfig.VERSION_NAME + "&device_id=" + generateDeviceId();
293345
}
294346

295347
private String injectToken(String url) {
@@ -307,13 +359,21 @@ private String injectToken(String url) {
307359
}
308360
}
309361
String token = "token=" + this.token;
362+
String deviceId = "device_id=" + generateDeviceId();
310363
if (!url.contains(token)) {
311364
if (url.lastIndexOf('/') < url.lastIndexOf('?')) {
312365
return url + '&' + token;
313366
} else {
314367
return url + '?' + token;
315368
}
316369
}
370+
if (!url.contains(deviceId)) {
371+
if (url.lastIndexOf('/') < url.lastIndexOf('?')) {
372+
return url + '&' + deviceId;
373+
} else {
374+
return url + '?' + deviceId;
375+
}
376+
}
317377
return url;
318378
}
319379

0 commit comments

Comments
 (0)