cleanup
This commit is contained in:
parent
317a6be545
commit
7c6d9ddc59
@ -11,6 +11,7 @@ import org.xbill.DNS.Record;
|
|||||||
import org.xbill.DNS.*;
|
import org.xbill.DNS.*;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.http.HttpClient;
|
import java.net.http.HttpClient;
|
||||||
import java.net.http.HttpRequest;
|
import java.net.http.HttpRequest;
|
||||||
@ -46,7 +47,7 @@ public class PiaManager {
|
|||||||
|
|
||||||
GitUtils.cloneRepo(); // Clone the repository
|
GitUtils.cloneRepo(); // Clone the repository
|
||||||
|
|
||||||
// Update the servers every 2 minutes
|
// Update the servers every 5 minutes
|
||||||
new Timer().scheduleAtFixedRate(new TimerTask() {
|
new Timer().scheduleAtFixedRate(new TimerTask() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
@ -67,20 +68,14 @@ public class PiaManager {
|
|||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public static void updateServers(File serversFile) {
|
public static void updateServers(File serversFile) {
|
||||||
List<PiaServer> servers = getPiaServers();
|
List<PiaServer> servers = getPiaServers();
|
||||||
List<PiaServer> toRemove = new ArrayList<>();
|
|
||||||
|
|
||||||
// Get the servers that need to be removed
|
// Remove the servers that haven't been active in 2 weeks
|
||||||
for (PiaServer server : SERVERS) {
|
int before = SERVERS.size();
|
||||||
if (server.getLastSeen().getTime() < System.currentTimeMillis() - REMOVAL_THRESHOLD) {
|
SERVERS.removeIf(server -> System.currentTimeMillis() - server.getLastSeen().getTime() > REMOVAL_THRESHOLD);
|
||||||
toRemove.add(server);
|
System.out.printf("Removed %s servers that haven't been active in 2 weeks%n", before - SERVERS.size());
|
||||||
}
|
|
||||||
}
|
|
||||||
toRemove.forEach(SERVERS::remove); // Remove the servers
|
|
||||||
System.out.printf("Removed %s servers that haven't been active in 2 weeks\n", toRemove.size());
|
|
||||||
|
|
||||||
int newServers = 0;
|
|
||||||
|
|
||||||
// Add the new servers to the list
|
// Add the new servers to the list
|
||||||
|
int newServers = 0;
|
||||||
for (PiaServer piaServer : servers) {
|
for (PiaServer piaServer : servers) {
|
||||||
boolean newServer = SERVERS.stream().noneMatch(server -> server.getIp().equals(piaServer.getIp()));
|
boolean newServer = SERVERS.stream().noneMatch(server -> server.getIp().equals(piaServer.getIp()));
|
||||||
if (newServer) {
|
if (newServer) {
|
||||||
@ -103,55 +98,40 @@ public class PiaManager {
|
|||||||
.uri(URI.create(PIA_OPENVPN_CONFIGS_URL))
|
.uri(URI.create(PIA_OPENVPN_CONFIGS_URL))
|
||||||
.GET()
|
.GET()
|
||||||
.build();
|
.build();
|
||||||
// Send the request and get the response
|
|
||||||
HttpResponse<Path> response = HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofFile(Files.createTempFile("openvpn", ".zip")));
|
HttpResponse<Path> response = HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofFile(Files.createTempFile("openvpn", ".zip")));
|
||||||
if (response.statusCode() != 200) {
|
if (response.statusCode() != 200) {
|
||||||
System.out.println("Failed to get the PIA OpenVPN configs, status code: " + response.statusCode());
|
throw new IOException("Failed to get the PIA OpenVPN configs, status code: " + response.statusCode());
|
||||||
System.exit(1);
|
|
||||||
}
|
}
|
||||||
System.out.printf("Downloaded the OpenVPN configs in %sms%n", System.currentTimeMillis() - start);
|
System.out.printf("Downloaded the OpenVPN configs in %sms%n", System.currentTimeMillis() - start);
|
||||||
Path downloadedFile = response.body();
|
Path downloadedFile = response.body();
|
||||||
File tempDir = Files.createTempDirectory("openvpn").toFile();
|
File tempDir = Files.createTempDirectory("openvpn").toFile();
|
||||||
ZipUnArchiver unArchiver = new ZipUnArchiver();
|
ZipUnArchiver unArchiver = new ZipUnArchiver();
|
||||||
|
|
||||||
// Extract the downloaded file
|
|
||||||
unArchiver.setSourceFile(downloadedFile.toFile());
|
unArchiver.setSourceFile(downloadedFile.toFile());
|
||||||
unArchiver.setDestDirectory(tempDir);
|
unArchiver.setDestDirectory(tempDir);
|
||||||
unArchiver.extract();
|
unArchiver.extract();
|
||||||
|
|
||||||
// Get the extracted files
|
|
||||||
File[] files = tempDir.listFiles();
|
File[] files = tempDir.listFiles();
|
||||||
if (files == null || files.length == 0) {
|
if (files == null || files.length == 0) {
|
||||||
System.out.println("Failed to extract the OpenVPN configs");
|
throw new IOException("Failed to extract the OpenVPN configs");
|
||||||
System.exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.printf("Found %s regions%n", files.length - 1);
|
|
||||||
|
|
||||||
// Search for the servers
|
|
||||||
List<PiaServer> servers = new ArrayList<>();
|
List<PiaServer> servers = new ArrayList<>();
|
||||||
for (File file : files) {
|
for (File file : files) {
|
||||||
if (file.isDirectory()) {
|
if (file.isDirectory() || !file.getName().endsWith(".ovpn")) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!file.getName().endsWith(".ovpn")) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// Read the file and get the server domain
|
|
||||||
List<String> lines = Files.readAllLines(file.toPath());
|
List<String> lines = Files.readAllLines(file.toPath());
|
||||||
for (String line : lines) {
|
for (String line : lines) {
|
||||||
if (line.startsWith("remote ")) {
|
if (line.startsWith("remote ")) {
|
||||||
String[] parts = line.split(" ");
|
String[] parts = line.split(" ");
|
||||||
String hostname = parts[1];
|
String hostname = parts[1];
|
||||||
String region = file.getName().split("\\.")[0];
|
String region = file.getName().split("\\.")[0];
|
||||||
|
|
||||||
Record[] records = new Lookup(hostname, Type.A).run();
|
Record[] records = new Lookup(hostname, Type.A).run();
|
||||||
if (records == null) {
|
if (records != null) {
|
||||||
continue;
|
for (Record record : records) {
|
||||||
}
|
ARecord aRecord = (ARecord) record;
|
||||||
for (Record record : records) {
|
servers.add(new PiaServer(aRecord.getAddress().getHostAddress(), region, new Date()));
|
||||||
ARecord aRecord = (ARecord) record;
|
}
|
||||||
servers.add(new PiaServer(aRecord.getAddress().getHostAddress(), region, new Date()));
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user