63 lines
2.4 KiB
Java
63 lines
2.4 KiB
Java
import java.net.URI;
|
|
import java.net.http.HttpClient;
|
|
import java.net.http.HttpRequest;
|
|
import java.net.http.HttpResponse;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
public class AttackData {
|
|
|
|
public static List<Map<String, String>> getAttackData(String host, String attackDataUrl) throws Exception {
|
|
HttpClient client = HttpClient.newHttpClient();
|
|
HttpRequest request = HttpRequest.newBuilder()
|
|
.uri(URI.create(attackDataUrl))
|
|
.GET()
|
|
.build();
|
|
|
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
|
String responseBody = response.body();
|
|
|
|
return parseAttackData(responseBody, host, attackDataUrl);
|
|
}
|
|
|
|
private static List<Map<String, String>> parseAttackData(String jsonResponse, String host, String attackDataUrl) {
|
|
List<Map<String, String>> userData = new ArrayList<>();
|
|
|
|
String hostPattern = "\"" + Pattern.quote(host) + "\"\\s*:\\s*\\[([^\\]]+)\\]";
|
|
Pattern pattern = Pattern.compile(hostPattern, Pattern.DOTALL);
|
|
Matcher matcher = pattern.matcher(jsonResponse);
|
|
|
|
if (matcher.find()) {
|
|
String arrayContent = matcher.group(1);
|
|
|
|
Pattern userPattern = Pattern.compile("\\{\\\\\"userId\\\\\":\\s*\\\\\"(\\d+)\\\\\",\\s*\\\\\"username\\\\\":\\s*\\\\\"([^\\\\\"]+)\\\\\"\\}");
|
|
Matcher userMatcher = userPattern.matcher(arrayContent);
|
|
|
|
while (userMatcher.find()) {
|
|
Map<String, String> user = new HashMap<>();
|
|
user.put("userId", userMatcher.group(1));
|
|
user.put("username", userMatcher.group(2));
|
|
userData.add(user);
|
|
}
|
|
}
|
|
|
|
return userData;
|
|
}
|
|
|
|
public static Map<String, String> getFirstUser(String host, String attackDataUrl) throws Exception {
|
|
List<Map<String, String>> users = getAttackData(host, attackDataUrl);
|
|
if (users.isEmpty()) {
|
|
throw new Exception("No attack data found for host: " + host);
|
|
}
|
|
return users.get(0);
|
|
}
|
|
|
|
public static List<Map<String, String>> getAllUsers(String host, String attackDataUrl) throws Exception {
|
|
return getAttackData(host, attackDataUrl);
|
|
}
|
|
}
|