initial commit

This commit is contained in:
Lee 2024-04-27 04:41:08 +01:00
commit 70fd95f242
9 changed files with 267 additions and 0 deletions

29
.gitignore vendored Normal file

@ -0,0 +1,29 @@
### ME template
*.class
*.log
*.ctxt
.mtj.tmp/
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
hs_err_pid*
replay_pid*
.idea
cmake-build-*/
.idea/**/mongoSettings.xml
*.iws
out/
build/
work/
.idea_modules/
atlassian-ide-plugin.xml
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
git.properties
pom.xml.versionsBackup

3
.idea/.gitignore generated vendored Normal file

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

7
.idea/encodings.xml generated Normal file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>

14
.idea/misc.xml generated Normal file

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="azul-17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

6
.idea/vcs.xml generated Normal file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

40
pom.xml Normal file

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cc.fascinated.piaservers</groupId>
<artifactId>PIA-Servers</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- Libraries -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.32</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.1</version>
<scope>compile</scope>
</dependency>
<!-- Archive Utilities -->
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-archiver</artifactId>
<version>4.9.2</version>
</dependency>
</dependencies>
</project>

@ -0,0 +1,127 @@
package cc.fascinated.piaservers;
import cc.fascinated.piaservers.pia.PiaServer;
import cc.fascinated.piaservers.pia.PiaServerToken;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import org.codehaus.plexus.archiver.zip.ZipUnArchiver;
import lombok.SneakyThrows;
import java.io.File;
import java.net.InetAddress;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class Main {
private static final Gson GSON = new GsonBuilder()
.setPrettyPrinting()
.create();
private static final HttpClient HTTP_CLIENT = HttpClient.newHttpClient();
private static final String PIA_OPENVPN_CONFIGS_URL = "https://www.privateinternetaccess.com/openvpn/openvpn.zip";
private static final long REMOVAL_THRESHOLD = TimeUnit.DAYS.toMicros(14); // 2 weeks
@SneakyThrows
public static void main(String[] args) {
File serversFile = new File("servers.json");
if (!serversFile.exists()) {
System.out.println("serversFile.json does not exist, creating...");
serversFile.createNewFile();
}
List<PiaServerToken> serverDomains = getServerDomains();
System.out.println("Found " + serverDomains.size() + " server domains");
// Load the serversFile from the file
List<PiaServer> servers = GSON.fromJson(Files.readString(serversFile.toPath()), new TypeToken<PiaServer>() {}.getType());
if (servers == null) {
servers = new ArrayList<>();
}
List<PiaServer> toRemove = new ArrayList<>();
System.out.println("Removing old servers...");
// Get the servers that need to be removed
for (PiaServer server : servers) {
if (server.getLastSeen().getTime() < System.currentTimeMillis() - REMOVAL_THRESHOLD) {
toRemove.add(server);
}
}
servers.removeAll(toRemove); // Remove the servers
System.out.printf("Removed %s old servers\n", toRemove.size());
// Add the new servers to the list
for (PiaServerToken serverToken : serverDomains) {
InetAddress address = InetAddress.getByName(serverToken.getHostname());
// Add the server to the list
servers.add(new PiaServer(address.getHostAddress(), serverToken.getRegion(), new Date()));
}
// Save the servers to the file
Files.writeString(serversFile.toPath(), GSON.toJson(servers));
System.out.printf("Wrote %s servers to the file\n", servers.size());
}
@SneakyThrows
private static List<PiaServerToken> getServerDomains() {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(PIA_OPENVPN_CONFIGS_URL))
.GET()
.build();
// Send the request and get the response
HttpResponse<Path> response = HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofFile(Files.createTempFile("openvpn", ".zip")));
if (response.statusCode() != 200) {
System.out.println("Failed to get the PIA OpenVPN configs, status code: " + response.statusCode());
System.exit(1);
}
Path downloadedFile = response.body();
File tempDir = Files.createTempDirectory("openvpn").toFile();
ZipUnArchiver unArchiver = new ZipUnArchiver();
// Extract the downloaded file
unArchiver.setSourceFile(downloadedFile.toFile());
unArchiver.setDestDirectory(tempDir);
unArchiver.extract();
// Get the extracted files
File[] files = tempDir.listFiles();
if (files == null || files.length == 0) {
System.out.println("Failed to extract the OpenVPN configs");
System.exit(1);
}
// Search for the server domains
List<PiaServerToken> domains = new ArrayList<>();
for (File file : files) {
if (file.isDirectory()) {
continue;
}
if (!file.getName().endsWith(".ovpn")) {
continue;
}
// Read the file and get the server domain
List<String> lines = Files.readAllLines(file.toPath());
for (String line : lines) {
if (line.startsWith("remote ")) {
String[] parts = line.split(" ");
String domain = parts[1];
String region = file.getName().split("\\.")[0];
domains.add(new PiaServerToken(domain, region));
break;
}
}
}
return domains;
}
}

@ -0,0 +1,24 @@
package cc.fascinated.piaservers.pia;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.Date;
@AllArgsConstructor @Getter
public class PiaServer {
/**
* The IP of this server.
*/
private final String ip;
/**
* The region this server is in.
*/
private final String region;
/**
* The last time this IP was seen.
*/
private Date lastSeen;
}

@ -0,0 +1,17 @@
package cc.fascinated.piaservers.pia;
import lombok.AllArgsConstructor;
import lombok.Getter;
@AllArgsConstructor @Getter
public class PiaServerToken {
/**
* The hostname for this server.
*/
private final String hostname;
/**
* The region this server is in.
*/
private final String region;
}