From 21153dd0825f88a4b55223a9476da1d93df8c46e Mon Sep 17 00:00:00 2001 From: Dom Eori <4j6dq2zi8@relay.firefox.com> Date: Mon, 15 Aug 2022 04:44:47 +0900 Subject: [PATCH] [general] Make AutoChecker to display version info --- pom.xml | 2 + .../samnyan/aqua/api/config/WebConfig.java | 2 +- .../samnyan/aqua/spring/util/AutoChecker.java | 105 +++++++++++------- src/main/resources/application.properties | 3 + 4 files changed, 68 insertions(+), 44 deletions(-) diff --git a/pom.xml b/pom.xml index 362f57af..93614f44 100644 --- a/pom.xml +++ b/pom.xml @@ -16,6 +16,8 @@ 11 + ${maven.build.timestamp} + yyyy-MM-dd HH:mm:ss z diff --git a/src/main/java/icu/samnyan/aqua/api/config/WebConfig.java b/src/main/java/icu/samnyan/aqua/api/config/WebConfig.java index 5d0a001f..0159bfcf 100644 --- a/src/main/java/icu/samnyan/aqua/api/config/WebConfig.java +++ b/src/main/java/icu/samnyan/aqua/api/config/WebConfig.java @@ -15,7 +15,7 @@ public class WebConfig implements WebMvcConfigurer { private final boolean AQUAVIEWER_ENABLED; - public WebConfig(@Value("${aquaviewer.server.enable:true}") boolean AQUAVIEWER_ENABLED) { + public WebConfig(@Value("${aquaviewer.server.enable}") boolean AQUAVIEWER_ENABLED) { this.AQUAVIEWER_ENABLED = AQUAVIEWER_ENABLED; } diff --git a/src/main/java/icu/samnyan/aqua/spring/util/AutoChecker.java b/src/main/java/icu/samnyan/aqua/spring/util/AutoChecker.java index 03a3bc5e..320d770c 100644 --- a/src/main/java/icu/samnyan/aqua/spring/util/AutoChecker.java +++ b/src/main/java/icu/samnyan/aqua/spring/util/AutoChecker.java @@ -5,11 +5,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; import java.net.Socket; -import java.net.UnknownHostException; import java.util.Objects; /** @@ -30,9 +26,11 @@ public class AutoChecker { private final boolean BILLING_ENABLED; private final int BILLING_PORT; private final boolean AQUAVIEWER_ENABLED; + private final String VERSION_TAG; + private final String BUILD_TIMESTAMP; public AutoChecker( - @Value("${server.host:}") String SERVER_PORT, + @Value("${server.port:}") String SERVER_PORT, @Value("${allnet.server.host:}") String ALLNET_HOST, @Value("${allnet.server.port:}") String ALLNET_PORT, @Value("${aimedb.server.address}") String AIMEDB_BIND, @@ -40,7 +38,9 @@ public class AutoChecker { @Value("${aimedb.server.enable}") boolean AIMEDB_ENABLED, @Value("${billing.server.port}") int BILLING_PORT, @Value("${billing.server.enable}") boolean BILLING_ENABLED, - @Value("${aquaviewer.server.enable:true}") boolean AQUAVIEWER_ENABLED) { + @Value("${aquaviewer.server.enable}") boolean AQUAVIEWER_ENABLED, + @Value("${build.version:N/A}") String VERSION_TAG, + @Value("${build.timestamp:N/A}") String BUILD_TIMESTAMP) { this.SERVER_PORT = SERVER_PORT; this.ALLNET_HOST_OVERRIDE = ALLNET_HOST; this.ALLNET_PORT_OVERRIDE = ALLNET_PORT; @@ -50,27 +50,44 @@ public class AutoChecker { this.BILLING_PORT = BILLING_PORT; this.BILLING_ENABLED = BILLING_ENABLED; this.AQUAVIEWER_ENABLED = AQUAVIEWER_ENABLED; + this.VERSION_TAG = VERSION_TAG; + this.BUILD_TIMESTAMP = BUILD_TIMESTAMP; } public void check() { + String host = ALLNET_HOST_OVERRIDE.equals("") ? "127.0.0.1" : ALLNET_HOST_OVERRIDE; + String port = ALLNET_PORT_OVERRIDE.equals("") ? SERVER_PORT : ALLNET_PORT_OVERRIDE; + // Boot message - System.out.println(" █████╗ ██████╗ ██╗ ██╗ █████╗ \n" + - "██╔══██╗██╔═══██╗██║ ██║██╔══██╗\n" + - "███████║██║ ██║██║ ██║███████║\n" + - "██╔══██║██║▄▄ ██║██║ ██║██╔══██║\n" + - "██║ ██║╚██████╔╝╚██████╔╝██║ ██║\n" + - "╚═╝ ╚═╝ ╚══▀▀═╝ ╚═════╝ ╚═╝ ╚═╝\n" + - " "); + System.out.println( + " _____ _____ _____ _____ " + LINEBREAK + + "| _ | | | | _ |" + LINEBREAK + + "| | | | | | |" + LINEBREAK + + "|__|__|__ _|_____|__|__|" + LINEBREAK + + " |__| " + LINEBREAK); + System.out.println("Aqua local server"); + System.out.println("Version: " + VERSION_TAG + " (Built on " + BUILD_TIMESTAMP + ")\n"); + + System.out.println("This is a free open-source software. If you paid for it, you were scammed.\n"); + + System.out.println("[Web Interface]"); if (AQUAVIEWER_ENABLED) { - System.out.println("Aqua viewer at http://localhost/web/\n"); - } + System.out.println("Enabled : http://" + host + ":" + port + "/web/" + LINEBREAK); + } else { + System.out.println("Disabled" + LINEBREAK); + } - System.out.println("======= Self test running ======="); - // Check aimedb - System.out.print(" AimeDB : "); + System.out.println("[Self testing]"); + StringBuilder failDetail = new StringBuilder(); + + /* + * Aime DB: try open socket to Aime DB port (default 22345) + * TODO: Sending hello request would be more reliable than testing if port is open + */ + System.out.print("Aime DB : "); if(!AIMEDB_ENABLED) { - System.out.println("DISABLED, SKIP"); + System.out.println("SKIP (DISABLED)"); } else { String address = "127.0.0.1"; if(!AIMEDB_BIND.equals("0.0.0.0")) { @@ -79,55 +96,57 @@ public class AutoChecker { try (Socket test = new Socket(address, AIMEDB_PORT)){ System.out.println("OK"); } catch (Exception e) { - System.out.println("ERROR!!"); - System.out.println(e.getMessage()); + System.out.println("ERROR"); + failDetail.append("Aime DB self-test raised an exception during testing").append(LINEBREAK); + failDetail.append("Exception: ").append(e.getCause().getMessage()).append(LINEBREAK); } } - // Check billing - System.out.print(" Billing : "); + /* + * Billing: try open socket to Billing port (default 8443) + * TODO: Try access /sys/test endpoint for more reliable testing (without cert verification) + */ + System.out.print("Billing : "); if(!BILLING_ENABLED) { - System.out.println("DISABLED, SKIP"); + System.out.println("SKIP (DISABLED)"); } else { - String host = ALLNET_HOST_OVERRIDE.equals("") ? "127.0.0.1" : ALLNET_HOST_OVERRIDE; try (Socket test = new Socket(host, BILLING_PORT)){ System.out.println("OK"); } catch (Exception e) { - System.out.println("ERROR!!"); - System.out.println(e.getMessage()); + System.out.println("ERROR"); + failDetail.append("Billing self-test raised an exception during testing").append(LINEBREAK); + failDetail.append("Exception: ").append(e.getCause().getMessage()).append(LINEBREAK); } } - // Check http part - System.out.print(" AllNet : "); - StringBuilder allNetSb = new StringBuilder(); - if(ALLNET_HOST_OVERRIDE.equals("localhost")||ALLNET_HOST_OVERRIDE.startsWith("127.0.0.")) { - System.out.print("WARNING!! "); - allNetSb.append("You are using loopback address.").append(LINEBREAK); - allNetSb.append("Some game won't connect with loopback address,").append(LINEBREAK); - allNetSb.append("please change setting in `application.properties`.").append(LINEBREAK); + // ALL.Net: try access /sys/test endpoint (default 80) + System.out.print("ALL.Net : "); + + if(ALLNET_HOST_OVERRIDE.equals("localhost")||ALLNET_HOST_OVERRIDE.startsWith("127.")) { + System.out.print("WARN, "); + failDetail.append("ALL.Net host is currently using loopback address.").append(LINEBREAK); + failDetail.append("Game might not connect to server with this. If it was not intentional, please edit configuration file.").append(LINEBREAK); } RestTemplate restTemplate = new RestTemplate(); - String host = ALLNET_HOST_OVERRIDE.equals("") ? "127.0.0.1" : ALLNET_HOST_OVERRIDE; - String port = ALLNET_PORT_OVERRIDE.equals("") ? SERVER_PORT : ALLNET_PORT_OVERRIDE; String url = "http://" + host + ":" + port + "/sys/test"; try{ ResponseEntity resp = restTemplate.getForEntity(url, String.class); if(resp.getStatusCode().is2xxSuccessful() && Objects.equals(resp.getBody(), "Server running")) { System.out.println("OK"); } else { - System.out.println("FAIL!"); - allNetSb.append("Could not connect to ").append(url).append(LINEBREAK); - allNetSb.append("Status code: ").append(resp.getStatusCodeValue()).append(LINEBREAK); + System.out.println("ERROR"); + failDetail.append("ALL.Net self-test could not connect to ").append(url).append(LINEBREAK); + failDetail.append("Status code: ").append(resp.getStatusCodeValue()).append(LINEBREAK); } } catch (Exception e) { - System.out.println("ERROR!"); - System.out.println(e.getCause().getMessage()); + System.out.println("ERROR"); + failDetail.append("ALL.Net self-test raised an exception during testing").append(url).append(LINEBREAK); + failDetail.append("Exception: ").append(e.getCause().getMessage()).append(LINEBREAK); } System.out.println(); - System.out.println(allNetSb.toString()); + System.out.println(failDetail.toString()); } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index be459bd0..9e4773b9 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -11,6 +11,9 @@ aquaviewer.server.enable=true server.port=80 +build.version=@project.version@ +build.timestamp=@timestamp@ + spring.servlet.multipart.max-file-size=10MB spring.servlet.multipart.max-request-size=20MB