Added VirusTotal upload

This commit is contained in:
M66B
2022-07-24 21:17:10 +02:00
parent 057c4f7b8e
commit 3606401af8
7 changed files with 225 additions and 87 deletions

View File

@@ -30,6 +30,7 @@ import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
@@ -319,95 +320,165 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String apiKey = prefs.getString("vt_apikey", null);
Bundle args = new Bundle();
args.putSerializable("file", attachment.getFile(context));
args.putString("apiKey", apiKey);
View view = LayoutInflater.from(context).inflate(R.layout.dialog_virus_total, null);
final TextView tvName = view.findViewById(R.id.tvName);
final ProgressBar pbAnalysis = view.findViewById(R.id.pbAnalysis);
final TextView tvCount = view.findViewById(R.id.tvCount);
final TextView tvLabel = view.findViewById(R.id.tvLabel);
final TextView tvUnknown = view.findViewById(R.id.tvUnknown);
final Button btnUpload = view.findViewById(R.id.btnUpload);
final ProgressBar pbUpload = view.findViewById(R.id.pbUpload);
final ProgressBar pbWait = view.findViewById(R.id.pbWait);
final TextView tvAnalyzing = view.findViewById(R.id.tvAnalyzing);
final Group grpAnalysis = view.findViewById(R.id.grpAnalysis);
tvName.setText(attachment.name);
tvLabel.setVisibility(View.GONE);
tvUnknown.setVisibility(View.GONE);
btnUpload.setVisibility(View.GONE);
pbUpload.setVisibility(View.GONE);
tvAnalyzing.setVisibility(View.GONE);
grpAnalysis.setVisibility(View.GONE);
Bundle args = new Bundle();
args.putString("apiKey", apiKey);
args.putSerializable("file", attachment.getFile(context));
final SimpleTask<Bundle> taskLookup = new SimpleTask<Bundle>() {
@Override
protected void onPreExecute(Bundle args) {
pbWait.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(Bundle args) {
pbWait.setVisibility(View.GONE);
}
new SimpleTask<Bundle>() {
@Override
protected Bundle onExecute(Context context, Bundle args) throws Throwable {
File file = (File) args.getSerializable("file");
String apiKey = args.getString("apiKey");
File file = (File) args.getSerializable("file");
return VirusTotal.lookup(context, file, apiKey);
}
@Override
protected void onExecuted(Bundle args, Bundle result) {
String uri = result.getString("uri");
int count = result.getInt("count", -1);
int malicious = result.getInt("malicious", -1);
String label = result.getString("label");
if (count < 0) {
Helper.view(context, Uri.parse(uri), true);
return;
}
View view = LayoutInflater.from(context).inflate(R.layout.dialog_virus_total, null);
final TextView tvName = view.findViewById(R.id.tvName);
final ProgressBar pbAnalysis = view.findViewById(R.id.pbAnalysis);
final TextView tvCount = view.findViewById(R.id.tvCount);
final TextView tvLabel = view.findViewById(R.id.tvLabel);
final TextView tvUnknown = view.findViewById(R.id.tvUnknown);
final Group grpAnalysis = view.findViewById(R.id.grpAnalysis);
tvName.setText(attachment.name);
pbAnalysis.setMax(count);
pbAnalysis.setProgress(malicious);
tvCount.setText(malicious + "/" + count);
tvLabel.setText(label);
tvLabel.setVisibility(TextUtils.isEmpty(label) ? View.GONE : View.VISIBLE);
tvUnknown.setVisibility(count == 0 ? View.VISIBLE : View.GONE);
btnUpload.setVisibility(count == 0 && !TextUtils.isEmpty(apiKey) ? View.VISIBLE : View.GONE);
grpAnalysis.setVisibility(count == 0 ? View.GONE : View.VISIBLE);
AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setView(view)
.setPositiveButton(R.string.title_info, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Helper.view(context, Uri.parse(uri), true);
}
})
.setNegativeButton(android.R.string.cancel, null);
if (!TextUtils.isEmpty(apiKey) && BuildConfig.DEBUG)
builder.setNeutralButton("Upload", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) throws Throwable {
File file = (File) args.getSerializable("file");
String apiKey = args.getString("apiKey");
VirusTotal.upload(context, file, apiKey);
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
}.execute(context, owner, args, "attachment:upload");
}
});
AlertDialog dialog = builder.create();
dialog.show();
powner.getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroy() {
dialog.dismiss();
powner.getLifecycle().removeObserver(this);
}
});
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
}.execute(context, owner, args, "attachment:scan");
};
final SimpleTask<Void> taskUpload = new SimpleTask<Void>() {
@Override
protected void onPreExecute(Bundle args) {
btnUpload.setEnabled(false);
pbUpload.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(Bundle args) {
btnUpload.setEnabled(true);
tvAnalyzing.setVisibility(View.GONE);
pbUpload.setVisibility(View.GONE);
}
@Override
protected Void onExecute(Context context, Bundle args) throws Throwable {
String apiKey = args.getString("apiKey");
File file = (File) args.getSerializable("file");
VirusTotal.upload(context, file, apiKey, new Runnable() {
private int step = 0;
@Override
public void run() {
postProgress(Integer.toString(++step));
}
});
return null;
}
@Override
protected void onProgress(CharSequence status, Bundle data) {
tvAnalyzing.setVisibility(View.VISIBLE);
}
@Override
protected void onExecuted(Bundle args, Void data) {
taskLookup.execute(context, owner, args, "attachment:lookup");
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
};
final SimpleTask<String> taskUrl = new SimpleTask<String>() {
@Override
protected String onExecute(Context context, Bundle args) throws Throwable {
File file = (File) args.getSerializable("file");
return VirusTotal.getUrl(file);
}
@Override
protected void onExecuted(Bundle args, String uri) {
Helper.view(context, Uri.parse(uri), true);
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
};
btnUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
taskUpload.execute(context, owner, args, "attachment:upload");
}
});
if (!TextUtils.isEmpty(apiKey))
taskLookup.execute(context, owner, args, "attachment:lookup");
else
pbWait.setVisibility(View.GONE);
AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setView(view)
.setPositiveButton(R.string.title_info, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
taskUrl.execute(context, owner, args, "attachment:virustotal");
}
})
.setNegativeButton(android.R.string.cancel, null);
AlertDialog dialog = builder.create();
dialog.show();
powner.getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroy() {
dialog.dismiss();
powner.getLifecycle().removeObserver(this);
}
});
}
private void onShare(EntityAttachment attachment) {