Completed new antivpn plugin

This commit is contained in:
Dawson Hessler
2021-06-15 13:48:39 -04:00
parent 9039a97894
commit e1abdb7bf6
23 changed files with 792 additions and 3 deletions
@@ -1,11 +1,39 @@
package dev.brighten.antivpn;
import dev.brighten.antivpn.api.VPNConfig;
import dev.brighten.antivpn.api.VPNExecutor;
import dev.brighten.antivpn.utils.VPNResponse;
import dev.brighten.antivpn.utils.json.JSONException;
import dev.brighten.antivpn.utils.json.JSONObject;
import dev.brighten.antivpn.utils.json.JsonReader;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import java.io.IOException;
@Getter
@Setter(AccessLevel.PRIVATE)
public class AntiVPN {
private static AntiVPN INSTANCE;
private VPNConfig config;
private VPNExecutor executor;
public static void start(VPNConfig config, VPNExecutor executor) {
//Initializing
public static void start() {
INSTANCE = new AntiVPN();
INSTANCE.setConfig(config);
INSTANCE.setExecutor(executor);
getInstance().getExecutor().registerListeners();
getInstance().getConfig().update();
}
public void stop() {
executor.shutdown();
}
public static AntiVPN getInstance() {
@@ -14,5 +42,13 @@ public class AntiVPN {
return INSTANCE;
}
public void getAPI()
public static VPNResponse getVPNResponse(String ip, String license, boolean cachedResults /* faster if set to true*/)
throws JSONException, IOException {
JSONObject result = JsonReader.readJsonFromUrl(String
.format("https://funkemunky.cc/vpn?ip=%s&license=%s&cache=%s",
ip, license.length() == 0 ? "none" : license, cachedResults));
return VPNResponse.fromJson(result);
}
}
@@ -0,0 +1,13 @@
package dev.brighten.antivpn.api;
public interface VPNConfig {
String getLicense();
boolean cachedResults();
String getKickString();
void update();
}
@@ -0,0 +1,56 @@
package dev.brighten.antivpn.api;
import dev.brighten.antivpn.AntiVPN;
import dev.brighten.antivpn.utils.VPNResponse;
import dev.brighten.antivpn.utils.json.JSONException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
public abstract class VPNExecutor {
public static ExecutorService threadExecutor = Executors.newSingleThreadExecutor();
private static final Map<String, VPNResponse> responseCache = new HashMap<>();
public abstract void registerListeners();
public abstract void runCacheReset();
public void resetCache() {
responseCache.clear();
}
public abstract void shutdown();
public void checkIp(String ip, boolean cachedResults, Consumer<VPNResponse> result) {
threadExecutor.execute(() -> result.accept(responseCache.compute(ip, (key, val) -> {
if(val == null) {
try {
return AntiVPN.getVPNResponse(ip, AntiVPN.getInstance().getConfig().getLicense(), cachedResults);
} catch (JSONException | IOException e) {
e.printStackTrace();
}
}
return val;
})));
}
public VPNResponse checkIp(String ip, boolean cachedResults) {
return responseCache.compute(ip, (key, val) -> {
if(val == null) {
try {
return AntiVPN.getVPNResponse(ip, AntiVPN.getInstance().getConfig().getLicense(), cachedResults);
} catch (JSONException | IOException e) {
e.printStackTrace();
}
}
return val;
});
}
}
@@ -4,14 +4,17 @@ import dev.brighten.antivpn.utils.json.JSONException;
import dev.brighten.antivpn.utils.json.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
@RequiredArgsConstructor
public class VPNResponse {
private String asn, ip, countryName, countryCode, city, timeZone, method, isp;
private boolean proxy, cached, success;
private boolean proxy, cached;
private final boolean success;
private double latitude, longitude;
private long lastAccess;
private long queriesLeft;
@@ -47,4 +50,19 @@ public class VPNResponse {
jsonObject.getDouble("latitude"), jsonObject.getDouble("longitude"),
jsonObject.getLong("lastAccess"), jsonObject.getInt("queriesLeft"));
}
public static VPNResponse fromJson(JSONObject jsonObject) throws JSONException {
if(jsonObject.getBoolean("success")) {
return new VPNResponse(jsonObject.getString("asn"), jsonObject.getString("ip"),
jsonObject.getString("countryName"), jsonObject.getString("countryCode"),
jsonObject.getString("city"), jsonObject.getString("timeZone"),
jsonObject.has("method") ? jsonObject.getString("method") : "N/A",
jsonObject.getString("isp"), jsonObject.getBoolean("proxy"),
jsonObject.getBoolean("cached"), jsonObject.getBoolean("success"),
jsonObject.getDouble("latitude"), jsonObject.getDouble("longitude"),
jsonObject.getLong("lastAccess"), jsonObject.getInt("queriesLeft"));
}
return new VPNResponse(false);
}
}