Refactoring

pull/209/head
M66B 3 years ago
parent f5fff57f88
commit c1b89c8655

@ -23,6 +23,7 @@ import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Pair;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
@ -58,45 +59,22 @@ public class VirusTotal {
Bundle result = new Bundle(); Bundle result = new Bundle();
result.putString("uri", uri); result.putString("uri", uri);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); Pair<Integer, String> response = call(context, "api/v3/files/" + hash);
String apikey = prefs.getString("vt_apikey", null);
if (TextUtils.isEmpty(apikey)) if (response == null)
return result; return result;
URL url = new URL(URI_ENDPOINT + "api/v3/files/" + hash); if (response.first != HttpsURLConnection.HTTP_OK &&
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); response.first != HttpsURLConnection.HTTP_NOT_FOUND)
connection.setRequestMethod("GET"); throw new FileNotFoundException(response.second);
connection.setReadTimeout(VT_TIMEOUT * 1000);
connection.setConnectTimeout(VT_TIMEOUT * 1000);
ConnectionHelper.setUserAgent(context, connection);
connection.setRequestProperty("x-apikey", apikey);
connection.setRequestProperty("Accept", "application/json");
connection.connect();
try { if (response.first == HttpsURLConnection.HTTP_NOT_FOUND) {
int status = connection.getResponseCode();
if (status != HttpsURLConnection.HTTP_OK && status != HttpsURLConnection.HTTP_NOT_FOUND) {
String error = "Error " + status + ": " + connection.getResponseMessage();
try {
InputStream is = connection.getErrorStream();
if (is != null)
error += "\n" + Helper.readStream(is);
} catch (Throwable ex) {
Log.w(ex);
}
throw new FileNotFoundException(error);
}
if (status == HttpsURLConnection.HTTP_NOT_FOUND) {
result.putInt("count", 0); result.putInt("count", 0);
result.putInt("malicious", 0); result.putInt("malicious", 0);
} else { } else {
String response = Helper.readStream(connection.getInputStream());
Log.i("VT response=" + response);
// https://developers.virustotal.com/reference/files // https://developers.virustotal.com/reference/files
// Example: https://gist.github.com/M66B/4ea95fdb93fb10bf4047761fcc9ec21a // Example: https://gist.github.com/M66B/4ea95fdb93fb10bf4047761fcc9ec21a
JSONObject jroot = new JSONObject(response); JSONObject jroot = new JSONObject(response.second);
JSONObject jdata = jroot.getJSONObject("data"); JSONObject jdata = jroot.getJSONObject("data");
JSONObject jattributes = jdata.getJSONObject("attributes"); JSONObject jattributes = jdata.getJSONObject("attributes");
@ -124,10 +102,46 @@ public class VirusTotal {
result.putInt("malicious", malicious); result.putInt("malicious", malicious);
result.putString("label", label); result.putString("label", label);
} }
return result;
}
static Pair<Integer, String> call(Context context, String api) throws IOException {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String apikey = prefs.getString("vt_apikey", null);
if (TextUtils.isEmpty(apikey))
return null;
URL url = new URL(URI_ENDPOINT + api);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(VT_TIMEOUT * 1000);
connection.setConnectTimeout(VT_TIMEOUT * 1000);
ConnectionHelper.setUserAgent(context, connection);
connection.setRequestProperty("x-apikey", apikey);
connection.setRequestProperty("Accept", "application/json");
connection.connect();
try {
int status = connection.getResponseCode();
if (status != HttpsURLConnection.HTTP_OK) {
String error = "Error " + status + ": " + connection.getResponseMessage();
try {
InputStream is = connection.getErrorStream();
if (is != null)
error += "\n" + Helper.readStream(is);
} catch (Throwable ex) {
Log.w(ex);
}
return new Pair<>(status, error);
}
String response = Helper.readStream(connection.getInputStream());
Log.i("VT response=" + response);
return new Pair<>(status, response);
} finally { } finally {
connection.disconnect(); connection.disconnect();
} }
return result;
} }
} }

Loading…
Cancel
Save