-
Notifications
You must be signed in to change notification settings - Fork 4
/
ApiKey.java
104 lines (82 loc) · 3.07 KB
/
ApiKey.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package org.sleuthkit.autopsy.modules.virustotalonlinecheck;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.LinkedList;
import org.json.JSONObject;
public class ApiKey {
private static final String apiUrl = "https://www.virustotal.com/vtapi/v2/file/report";
private final static int allowedRequestsPerMinute = 4;
private final static long msOfAMinute = 60 * 1000;
private String apiKey;
private LinkedList<Date> lastRequests = new LinkedList<>();
public ApiKey(String apiKey) {
this.apiKey = apiKey;
}
public long getTimeUntilNextPossibleRequest() {
if (lastRequests.size() < allowedRequestsPerMinute) {
return 0;
}
Date relevantTimeStamp = lastRequests.get(lastRequests.size() - allowedRequestsPerMinute);
long timeToWait = msOfAMinute - (new Date().getTime() - relevantTimeStamp.getTime());
if (timeToWait < 0) {
timeToWait = 0;
}
return timeToWait;
}
public VirusTotalReport getReport(String resource) throws InterruptedException {
String urlString = apiUrl + "?apikey=" + apiKey + "&resource=" + resource;
URL url = null;
try {
url = new URL(urlString);
} catch (MalformedURLException ex) {
return null;
}
boolean successful = false;
while (!successful) {
long time = getTimeUntilNextPossibleRequest();
Thread.sleep(time);
VirusTotalReport result = getResult(url);
if (result != null) {
lastRequests.add(new Date());
return result;
}
}
return null;
}
private VirusTotalReport getResult(URL url) {
JSONObject response = null;
try {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream is = connection.getInputStream();
int ret = connection.getResponseCode();
if (ret == 204) {
return null;
}
BufferedReader streamReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuilder responseStrBuilder = new StringBuilder();
String inputStr;
while ((inputStr = streamReader.readLine()) != null) {
responseStrBuilder.append(inputStr);
}
String responseString = responseStrBuilder.toString();
response = new JSONObject(responseString);
} catch (IOException ex) {
return new VirusTotalReport();
}
try {
int positives = response.getInt("positives");
if (positives > 0) {
String permaLink = response.getString("permalink");
return new VirusTotalReport(permaLink);
}
} catch (Exception e) {
}
return new VirusTotalReport();
}
}