This commit is contained in:
Lee 2024-04-29 07:24:47 +01:00
parent 0a56bef53d
commit 6b7bca3b90
2 changed files with 14 additions and 33 deletions

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

@ -3,7 +3,6 @@ package cc.fascinated.piaservers.pia;
import cc.fascinated.piaservers.Main; import cc.fascinated.piaservers.Main;
import cc.fascinated.piaservers.common.GitUtils; import cc.fascinated.piaservers.common.GitUtils;
import cc.fascinated.piaservers.model.PiaServer; import cc.fascinated.piaservers.model.PiaServer;
import cc.fascinated.piaservers.model.PiaServerToken;
import cc.fascinated.piaservers.readme.ReadMeManager; import cc.fascinated.piaservers.readme.ReadMeManager;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import lombok.SneakyThrows; import lombok.SneakyThrows;
@ -42,6 +41,9 @@ public class PiaManager {
} }
System.out.println("Loaded " + SERVERS.size() + " servers from the file"); System.out.println("Loaded " + SERVERS.size() + " servers from the file");
// Set the DNS resolver to Cloudflare
Lookup.setDefaultResolver(new SimpleResolver("1.1.1.1"));
GitUtils.cloneRepo(); // Clone the repository GitUtils.cloneRepo(); // Clone the repository
// Update the servers every 2 minutes // Update the servers every 2 minutes
@ -51,7 +53,7 @@ public class PiaManager {
updateServers(serversFile); // Update the servers updateServers(serversFile); // Update the servers
README_PATH = ReadMeManager.updateReadme(); // Update the README.md README_PATH = ReadMeManager.updateReadme(); // Update the README.md
} }
}, 0, TimeUnit.MINUTES.toMillis(2)); }, 0, TimeUnit.MINUTES.toMillis(5));
// Commit the files every hour // Commit the files every hour
new Timer().scheduleAtFixedRate(new TimerTask() { new Timer().scheduleAtFixedRate(new TimerTask() {
@ -64,8 +66,8 @@ public class PiaManager {
@SneakyThrows @SneakyThrows
public static void updateServers(File serversFile) { public static void updateServers(File serversFile) {
List<PiaServerToken> piaDomain = getPiaDomains(); List<PiaServer> servers = getPiaServers();
System.out.println("Found " + piaDomain.size() + " pia domains"); System.out.println("Found " + servers.size() + " pia server tokens");
List<PiaServer> toRemove = new ArrayList<>(); List<PiaServer> toRemove = new ArrayList<>();
@ -82,14 +84,14 @@ public class PiaManager {
int newServers = 0; int newServers = 0;
// Add the new servers to the list // Add the new servers to the list
for (PiaServerToken serverToken : piaDomain) { for (PiaServer piaServer : servers) {
boolean newServer = SERVERS.stream().noneMatch(server -> server.getIp().equals(serverToken.getIp())); boolean newServer = SERVERS.stream().noneMatch(server -> server.getIp().equals(piaServer.getIp()));
if (newServer) { if (newServer) {
newServers++; newServers++;
} }
// Add the server to the list // Add the server to the list
SERVERS.add(new PiaServer(serverToken.getIp(), serverToken.getRegion(), new Date())); SERVERS.add(piaServer);
} }
// Save the servers to the file // Save the servers to the file
@ -98,7 +100,7 @@ public class PiaManager {
} }
@SneakyThrows @SneakyThrows
private static List<PiaServerToken> getPiaDomains() { private static List<PiaServer> getPiaServers() {
HttpRequest request = HttpRequest.newBuilder() HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(PIA_OPENVPN_CONFIGS_URL)) .uri(URI.create(PIA_OPENVPN_CONFIGS_URL))
.GET() .GET()
@ -125,11 +127,8 @@ public class PiaManager {
System.exit(1); System.exit(1);
} }
// Set the DNS resolver to Cloudflare // Search for the servers
Lookup.setDefaultResolver(new SimpleResolver("1.1.1.1")); List<PiaServer> servers = new ArrayList<>();
// Search for the server domains
List<PiaServerToken> domains = new ArrayList<>();
for (File file : files) { for (File file : files) {
if (file.isDirectory()) { if (file.isDirectory()) {
continue; continue;
@ -151,13 +150,12 @@ public class PiaManager {
} }
for (Record record : records) { for (Record record : records) {
ARecord aRecord = (ARecord) record; ARecord aRecord = (ARecord) record;
domains.add(new PiaServerToken(aRecord.getAddress().getHostAddress(), region)); servers.add(new PiaServer(aRecord.getAddress().getHostAddress(), region, new Date()));
} }
break; break;
} }
} }
} }
return servers;
return domains;
} }
} }