Skip to content

Commit

Permalink
fix: lint issue
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Nov 24, 2022
1 parent 1f88c47 commit 0275c79
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 129 deletions.
208 changes: 103 additions & 105 deletions android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,11 @@
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.json.JSONException;
import org.json.JSONObject;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import org.json.JSONException;
import org.json.JSONObject;

interface Callback {
void callback(JSONObject jsonObject);
Expand Down Expand Up @@ -95,23 +94,23 @@ private boolean isProd() {

private boolean isEmulator() {
return (
(Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic")) ||
Build.FINGERPRINT.startsWith("generic") ||
Build.FINGERPRINT.startsWith("unknown") ||
Build.HARDWARE.contains("goldfish") ||
Build.HARDWARE.contains("ranchu") ||
Build.MODEL.contains("google_sdk") ||
Build.MODEL.contains("Emulator") ||
Build.MODEL.contains("Android SDK built for x86") ||
Build.MANUFACTURER.contains("Genymotion") ||
Build.PRODUCT.contains("sdk_google") ||
Build.PRODUCT.contains("google_sdk") ||
Build.PRODUCT.contains("sdk") ||
Build.PRODUCT.contains("sdk_x86") ||
Build.PRODUCT.contains("sdk_gphone64_arm64") ||
Build.PRODUCT.contains("vbox86p") ||
Build.PRODUCT.contains("emulator") ||
Build.PRODUCT.contains("simulator")
(Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic")) ||
Build.FINGERPRINT.startsWith("generic") ||
Build.FINGERPRINT.startsWith("unknown") ||
Build.HARDWARE.contains("goldfish") ||
Build.HARDWARE.contains("ranchu") ||
Build.MODEL.contains("google_sdk") ||
Build.MODEL.contains("Emulator") ||
Build.MODEL.contains("Android SDK built for x86") ||
Build.MANUFACTURER.contains("Genymotion") ||
Build.PRODUCT.contains("sdk_google") ||
Build.PRODUCT.contains("google_sdk") ||
Build.PRODUCT.contains("sdk") ||
Build.PRODUCT.contains("sdk_x86") ||
Build.PRODUCT.contains("sdk_gphone64_arm64") ||
Build.PRODUCT.contains("vbox86p") ||
Build.PRODUCT.contains("emulator") ||
Build.PRODUCT.contains("simulator")
);
}

Expand Down Expand Up @@ -150,7 +149,7 @@ private File unzip(final String id, final File zipFile, final String dest) throw

if (!canonicalPath.startsWith(canonicalDir)) {
throw new FileNotFoundException(
"SecurityException, Failed to ensure directory is the start path : " + canonicalDir + " of " + canonicalPath
"SecurityException, Failed to ensure directory is the start path : " + canonicalDir + " of " + canonicalPath
);
}

Expand Down Expand Up @@ -266,7 +265,7 @@ private String getChecksum(File file) throws IOException {
}

private void decodeFile(File file) throws IOException {
if(this.privateKey.equal("")) {
if (this.privateKey.equal("")) {
return;
}
try {
Expand All @@ -282,25 +281,24 @@ private void decodeFile(File file) throws IOException {
String content = sb.toString();
String decrypted = RSACipher.decryptRSA(content, pKey);
// write the decrypted string to the file
FileOutputStream fos = new FileOutputStream(file
.getAbsolutePath());
FileOutputStream fos = new FileOutputStream(file.getAbsolutePath());
fos.write(decrypted.getBytes());
fos.close();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
throw new IOException("NoSuchPaddingException");
throw new IOException("NoSuchPaddingException");
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
throw new IOException("IllegalBlockSizeException");
throw new IOException("IllegalBlockSizeException");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
throw new IOException("NoSuchAlgorithmException");
throw new IOException("NoSuchAlgorithmException");
} catch (BadPaddingException e) {
e.printStackTrace();
throw new IOException("BadPaddingException");
throw new IOException("BadPaddingException");
} catch (InvalidKeyException e) {
e.printStackTrace();
throw new IOException("InvalidKeyException");
throw new IOException("InvalidKeyException");
}
}

Expand Down Expand Up @@ -459,21 +457,21 @@ public void getLatest(final String updateUrl, final Callback callback) {
Log.i(CapacitorUpdater.TAG, "Auto-update parameters: " + json);
// Building a request
JsonObjectRequest request = new JsonObjectRequest(
Request.Method.POST,
updateUrl,
json,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
callback.callback(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error getting Latest" + error.toString());
}
Request.Method.POST,
updateUrl,
json,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
callback.callback(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error getting Latest" + error.toString());
}
}
);
this.requestQueue.add(request);
} catch (JSONException ex) {
Expand All @@ -493,34 +491,34 @@ public void setChannel(final String channel, final CallbackChannel callback) {

// Building a request
JsonObjectRequest request = new JsonObjectRequest(
Request.Method.POST,
channelUrl,
json,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject res) {
final JSObject ret = new JSObject();
Iterator<String> keys = res.keys();
while (keys.hasNext()) {
String key = keys.next();
if (res.has(key)) {
try {
ret.put(key, res.get(key));
} catch (JSONException e) {
e.printStackTrace();
}
Request.Method.POST,
channelUrl,
json,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject res) {
final JSObject ret = new JSObject();
Iterator<String> keys = res.keys();
while (keys.hasNext()) {
String key = keys.next();
if (res.has(key)) {
try {
ret.put(key, res.get(key));
} catch (JSONException e) {
e.printStackTrace();
}
}
Log.i(TAG, "Channel set to \"" + channel);
callback.callback(ret);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error set channel: " + error.toString());
}
Log.i(TAG, "Channel set to \"" + channel);
callback.callback(ret);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error set channel: " + error.toString());
}
}
);
this.requestQueue.add(request);
} catch (JSONException ex) {
Expand All @@ -539,34 +537,34 @@ public void getChannel(final CallbackChannel callback) {

// Building a request
JsonObjectRequest request = new JsonObjectRequest(
Request.Method.PUT,
channelUrl,
json,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject res) {
final JSObject ret = new JSObject();
Iterator<String> keys = res.keys();
while (keys.hasNext()) {
String key = keys.next();
if (res.has(key)) {
try {
ret.put(key, res.get(key));
} catch (JSONException e) {
e.printStackTrace();
}
Request.Method.PUT,
channelUrl,
json,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject res) {
final JSObject ret = new JSObject();
Iterator<String> keys = res.keys();
while (keys.hasNext()) {
String key = keys.next();
if (res.has(key)) {
try {
ret.put(key, res.get(key));
} catch (JSONException e) {
e.printStackTrace();
}
}
Log.i(TAG, "Channel get to \"" + ret);
callback.callback(ret);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error get channel: " + error.toString());
}
Log.i(TAG, "Channel get to \"" + ret);
callback.callback(ret);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error get channel: " + error.toString());
}
}
);
this.requestQueue.add(request);
} catch (JSONException ex) {
Expand All @@ -586,21 +584,21 @@ public void sendStats(final String action, final String versionName) {

// Building a request
JsonObjectRequest request = new JsonObjectRequest(
Request.Method.POST,
statsUrl,
json,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i(TAG, "Stats send for \"" + action + "\", version " + versionName);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error sending stats: " + error.toString());
}
Request.Method.POST,
statsUrl,
json,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i(TAG, "Stats send for \"" + action + "\", version " + versionName);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error sending stats: " + error.toString());
}
}
);
this.requestQueue.add(request);
} catch (JSONException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -773,9 +773,9 @@ public void run() {
"Error checksum " + next.getChecksum() + " " + checksum
);
CapacitorUpdaterPlugin.this.implementation.sendStats(
"checksum_fail",
current.getVersionName()
);
"checksum_fail",
current.getVersionName()
);
final Boolean res =
CapacitorUpdaterPlugin.this.implementation.delete(next.getId());
if (res) {
Expand Down
19 changes: 3 additions & 16 deletions android/src/main/java/ee/forgr/capacitor_updater/RSACipher.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
* references: http://stackoverflow.com/questions/12471999/rsa-encryption-decryption-in-android
*/
import android.util.Base64;

import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
Expand All @@ -22,12 +20,7 @@
public class RSACipher {

public static String decryptRSA(String source, PrivateKey privateKey)
throws NoSuchAlgorithmException,
NoSuchPaddingException,
InvalidKeyException,
IllegalBlockSizeException,
BadPaddingException {

throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedBytes = cipher.doFinal(Base64.decode(source, Base64.DEFAULT));
Expand All @@ -37,12 +30,7 @@ public static String decryptRSA(String source, PrivateKey privateKey)
}

public static PrivateKey stringToPrivateKey(String private_key)
throws NoSuchAlgorithmException,
NoSuchPaddingException,
InvalidKeyException,
IllegalBlockSizeException,
BadPaddingException {

throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
try {
// Remove the "BEGIN" and "END" lines, as well as any whitespace
String pkcs8Pem = private_key.toString();
Expand All @@ -60,11 +48,10 @@ public static PrivateKey stringToPrivateKey(String private_key)
KeyFactory kf = KeyFactory.getInstance("RSA");
PrivateKey privKey = kf.generatePrivate(keySpec);
return privKey;

} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
e.printStackTrace();

return null;
}
}
}
}
6 changes: 3 additions & 3 deletions ios/Plugin/CapacitorUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,9 @@ extension CustomError: LocalizedError {
return ""
}
}
private func decodeFile(filePath: URL) throws {
if (self.privateKey == "") {

private func decodeFile(filePath: URL) throws {
if self.privateKey == "" {
return
}
do {
Expand Down
3 changes: 1 addition & 2 deletions ios/Plugin/CapacitorUpdaterPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
if percent == 100 {
self.notifyListeners("downloadComplete", data: ["bundle": bundle.toJSON()])
self.implementation.sendStats(action: "download_complete", versionName: bundle.getVersionName())
}
else if percent.isMultiple(of: 10) {
} else if percent.isMultiple(of: 10) {
self.implementation.sendStats(action: "download_\(percent)", versionName: bundle.getVersionName())
}
}
Expand Down

0 comments on commit 0275c79

Please sign in to comment.