untested, might work, might not

This commit is contained in:
Lee
2024-04-29 06:39:32 +01:00
parent 33353845df
commit 9eaca049b7
7 changed files with 130 additions and 72 deletions

View File

@ -1,13 +1,10 @@
package cc.fascinated.piaservers;
import cc.fascinated.piaservers.pia.PiaManager;
import cc.fascinated.piaservers.readme.ReadMeManager;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.SneakyThrows;
import java.util.concurrent.TimeUnit;
public class Main {
public static final Gson GSON = new GsonBuilder()
.setPrettyPrinting()
@ -15,9 +12,6 @@ public class Main {
@SneakyThrows
public static void main(String[] args) {
PiaManager.updateServers();
Thread.sleep(TimeUnit.MINUTES.toMillis(3));
PiaManager.updateServers();
new ReadMeManager();
new PiaManager();
}
}

View File

@ -0,0 +1,41 @@
package cc.fascinated.piaservers.common;
import java.nio.file.Path;
public class GitUtils {
/**
* Commit files to git
*
* @param message The commit message
* @param files The files to commit
*/
public static void commitFiles(String message, Path... files) {
if (System.getenv("ENVIRONMENT").equals("production")) {
runCommand("git", "config", "--global", "user.email", "liam+pia-servers-ci@fascinated.cc");
runCommand("git", "config", "--global", "user.name", "PIA Servers CI");
}
for (Path file : files) {
runCommand("git", "add", file.toAbsolutePath().toString());
}
runCommand("git", "commit", "-m", message);
runCommand("git", "push", "https://pia-servers-ci:%s@git.fascinated.cc/Fascinated/PIA-Servers".formatted(System.getenv("AUTH_TOKEN")));
}
/**
* Run a system command
*
* @param args The command to run (with arguments)
*/
private static void runCommand(String... args) {
ProcessBuilder processBuilder = new ProcessBuilder(args);
processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
try {
Process process = processBuilder.start();
process.waitFor();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

View File

@ -1,13 +1,15 @@
package cc.fascinated.piaservers.pia;
import cc.fascinated.piaservers.Main;
import cc.fascinated.piaservers.common.GitUtils;
import cc.fascinated.piaservers.model.PiaServer;
import cc.fascinated.piaservers.model.PiaServerToken;
import cc.fascinated.piaservers.readme.ReadMeManager;
import com.google.gson.reflect.TypeToken;
import lombok.SneakyThrows;
import org.codehaus.plexus.archiver.zip.ZipUnArchiver;
import org.xbill.DNS.*;
import org.xbill.DNS.Record;
import org.xbill.DNS.*;
import java.io.File;
import java.net.InetAddress;
@ -17,9 +19,7 @@ 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.*;
import java.util.concurrent.TimeUnit;
public class PiaManager {
@ -29,21 +29,35 @@ public class PiaManager {
public static List<PiaServer> SERVERS = new ArrayList<>();
@SneakyThrows
public static void updateServers() {
public PiaManager() {
File serversFile = new File("servers.json");
if (!serversFile.exists()) {
System.out.println("serversFile.json does not exist, creating...");
serversFile.createNewFile();
}
List<PiaServerToken> piaDomain = getPiaDomains();
System.out.println("Found " + piaDomain.size() + " pia domains");
// Load the serversFile from the file
SERVERS = Main.GSON.fromJson(Files.readString(serversFile.toPath()), new TypeToken<List<PiaServer>>() {}.getType());
if (SERVERS == null) {
SERVERS = new ArrayList<>();
}
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
updateServers(serversFile); // Update the servers
Path readmePath = ReadMeManager.updateReadme(); // Update the README.md
// Commit the changes to the git repository
GitUtils.commitFiles("Scheduled update", serversFile.toPath(), readmePath);
}
}, 0, TimeUnit.MINUTES.toMillis(5));
}
@SneakyThrows
public static void updateServers(File serversFile) {
List<PiaServerToken> piaDomain = getPiaDomains();
System.out.println("Found " + piaDomain.size() + " pia domains");
List<PiaServer> toRemove = new ArrayList<>();
System.out.println("Removing old servers...");

View File

@ -8,20 +8,21 @@ import lombok.SneakyThrows;
import java.io.File;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class ReadMeManager {
private final DecimalFormat decimalFormat = new DecimalFormat("#,###");
private static final DecimalFormat decimalFormat = new DecimalFormat("#,###");
@SneakyThrows
public ReadMeManager() {
public static Path updateReadme() {
InputStream readmeStream = Main.class.getResourceAsStream("/README.md");
if (readmeStream == null) {
System.out.println("Failed to find README.md");
return;
return null;
}
File readmeFile = new File("README.md");
if (!readmeFile.exists()) { // Create the file if it doesn't exist
@ -48,5 +49,6 @@ public class ReadMeManager {
.reduce((a, b) -> a + "\n" + b).orElse("")); // Reduce the entries to a single string
Files.write(readmeFile.toPath(), contents.getBytes());
return readmeFile.toPath();
}
}