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

30
.gitea/workflows/ci.yml Normal file

@ -0,0 +1,30 @@
name: Deploy to Dokku
on:
push:
branches: ["master"]
paths-ignore:
- .gitignore
- README.md
- LICENSE
- servers.json
jobs:
docker:
strategy:
matrix:
arch: ["ubuntu-latest"]
runs-on: ${{ matrix.arch }}
# Steps to run
steps:
# Checkout the repo
- name: Checkout
uses: actions/checkout@v4
# Deploy to Dokku
- name: Push to dokku
uses: dokku/github-action@master
with:
git_remote_url: "ssh://dokku@10.0.50.65:22/pia-servers"
ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }}

@ -1,53 +0,0 @@
name: Fetch new Pia Servers
on:
schedule:
# - cron: "@hourly"
- cron: "*/30 * * * *"
push:
branches: ["master"]
paths-ignore:
- .gitignore
- README.md
- LICENSE
- servers.json
jobs:
docker:
strategy:
matrix:
arch: ["ubuntu-latest"]
git-version: ["2.44.0"]
java-version: ["17"]
maven-version: ["3.8.5"]
runs-on: ${{ matrix.arch }}
# Steps to run
steps:
# Checkout the repo
- name: Checkout
uses: actions/checkout@v4
# Setup Java and Maven
- name: Set up JDK and Maven
uses: s4u/setup-maven-action@v1.12.0
with:
java-version: ${{ matrix.java-version }}
distribution: "zulu"
maven-version: ${{ matrix.maven-version }}
- name: Build PIA Servers
run: mvn clean package -T 2C -q
- name: Fetch new PIA servers
run: java -jar target/PIA-Servers.jar
- name: Commit and push changes
run: |
git config --global user.email "liam+pia-servers-ci@fascinated.cc"
git config --global user.name "PIA Servers CI"
git add servers.json
git add README.md
git commit -m "Scheduled update"
git push https://pia-servers-ci:${{ secrets.AUTH_TOKEN }}@git.fascinated.cc/Fascinated/PIA-Servers

30
Dockerfile Normal file

@ -0,0 +1,30 @@
# Stage 1: Build the application
FROM maven:3.9.6-eclipse-temurin-17-alpine AS builder
# Set the working directory
WORKDIR /home/container
# Copy the current directory contents into the container at /home/container
COPY . .
# Build the jar
RUN mvn package -q -Dmaven.test.skip -DskipTests -T2C
# Stage 2: Create the final lightweight image
FROM eclipse-temurin:17.0.11_9-jre-focal
# Set the app to be in production mode
ENV ENVIRONMENT=production
# Set the working directory
WORKDIR /home/container
# Copy the built jar file from the builder stage
COPY --from=builder /home/container/target/PIA-Servers.jar .
# Make port 80 available to the world outside this container
EXPOSE 80
ENV PORT=80
# Run the jar file
CMD java -jar PIA-Servers.jar -Djava.awt.headless=true

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

@ -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();
}
}
}

@ -1,13 +1,15 @@
package cc.fascinated.piaservers.pia; 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.model.PiaServer; import cc.fascinated.piaservers.model.PiaServer;
import cc.fascinated.piaservers.model.PiaServerToken; import cc.fascinated.piaservers.model.PiaServerToken;
import cc.fascinated.piaservers.readme.ReadMeManager;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import org.codehaus.plexus.archiver.zip.ZipUnArchiver; import org.codehaus.plexus.archiver.zip.ZipUnArchiver;
import org.xbill.DNS.*;
import org.xbill.DNS.Record; import org.xbill.DNS.Record;
import org.xbill.DNS.*;
import java.io.File; import java.io.File;
import java.net.InetAddress; import java.net.InetAddress;
@ -17,9 +19,7 @@ import java.net.http.HttpRequest;
import java.net.http.HttpResponse; import java.net.http.HttpResponse;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList; import java.util.*;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class PiaManager { public class PiaManager {
@ -29,21 +29,35 @@ public class PiaManager {
public static List<PiaServer> SERVERS = new ArrayList<>(); public static List<PiaServer> SERVERS = new ArrayList<>();
@SneakyThrows @SneakyThrows
public static void updateServers() { public PiaManager() {
File serversFile = new File("servers.json"); File serversFile = new File("servers.json");
if (!serversFile.exists()) { if (!serversFile.exists()) {
System.out.println("serversFile.json does not exist, creating..."); System.out.println("serversFile.json does not exist, creating...");
serversFile.createNewFile(); serversFile.createNewFile();
} }
List<PiaServerToken> piaDomain = getPiaDomains();
System.out.println("Found " + piaDomain.size() + " pia domains");
// Load the serversFile from the file // Load the serversFile from the file
SERVERS = Main.GSON.fromJson(Files.readString(serversFile.toPath()), new TypeToken<List<PiaServer>>() {}.getType()); SERVERS = Main.GSON.fromJson(Files.readString(serversFile.toPath()), new TypeToken<List<PiaServer>>() {}.getType());
if (SERVERS == null) { if (SERVERS == null) {
SERVERS = new ArrayList<>(); 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<>(); List<PiaServer> toRemove = new ArrayList<>();
System.out.println("Removing old servers..."); System.out.println("Removing old servers...");

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