diff --git a/Havana-Web/src/main/java/org/alexdev/http/controllers/api/ImagerController.java b/Havana-Web/src/main/java/org/alexdev/http/controllers/api/ImagerController.java index b2b46d5..b2812cb 100644 --- a/Havana-Web/src/main/java/org/alexdev/http/controllers/api/ImagerController.java +++ b/Havana-Web/src/main/java/org/alexdev/http/controllers/api/ImagerController.java @@ -6,6 +6,7 @@ import org.alexdev.duckhttpd.response.ResponseBuilder; import org.alexdev.duckhttpd.server.connection.WebConnection; import org.alexdev.duckhttpd.util.MimeType; import org.alexdev.havana.util.config.GameConfiguration; +import org.alexdev.http.server.Watchdog; import org.apache.http.HttpEntity; import org.apache.http.HttpHeaders; import org.apache.http.client.HttpClient; @@ -27,40 +28,43 @@ public class ImagerController { public static void imager_redirect(WebConnection webConnection) { boolean sentFurniResponse = false; - var reqConfig = RequestConfig.custom() + if (Watchdog.IS_IMAGER_ONLINE) { + var reqConfig = RequestConfig.custom() .setConnectTimeout(GameConfiguration.getInstance().getInteger("site.imaging.timeout")) .build(); - try (final var httpClient = HttpClientBuilder.create() - .setDefaultRequestConfig(reqConfig) - .build()) { + try (final var httpClient = HttpClientBuilder.create() + .setDefaultRequestConfig(reqConfig) + .build()) { - HttpGet request = new HttpGet(GameConfiguration.getInstance().getString("site.imaging.path") + webConnection.request().uri()); - request.addHeader(HttpHeaders.USER_AGENT, "Imager"); + HttpGet request = new HttpGet(GameConfiguration.getInstance().getString("site.imaging.path") + webConnection.request().uri()); + request.addHeader(HttpHeaders.USER_AGENT, "Imager"); - try (var r = httpClient.execute(request)) { - HttpEntity entity = r.getEntity(); + try (var r = httpClient.execute(request)) { + HttpEntity entity = r.getEntity(); - if (entity != null) { - if (r.getStatusLine().getStatusCode() == HttpResponseStatus.OK.code()) { - FullHttpResponse response = ResponseBuilder.create( - HttpResponseStatus.OK, entity.getContentType().getValue(), EntityUtils.toByteArray(entity) - ); + if (entity != null) { + if (r.getStatusLine().getStatusCode() == HttpResponseStatus.OK.code()) { + FullHttpResponse response = ResponseBuilder.create( + HttpResponseStatus.OK, entity.getContentType().getValue(), EntityUtils.toByteArray(entity) + ); - webConnection.send(response); - sentFurniResponse = true; + webConnection.send(response); + sentFurniResponse = true; + } } } + } catch (Exception ignored) { + ignored.printStackTrace(); } - } catch (Exception ignored) { - ignored.printStackTrace(); - } finally { - if (!sentFurniResponse) { - FullHttpResponse response = ResponseBuilder.create( - HttpResponseStatus.NO_CONTENT, MimeType.getContentType("png"), new byte[0] - ); - webConnection.send(response); - } + + } + + if (!sentFurniResponse) { + FullHttpResponse response = ResponseBuilder.create( + HttpResponseStatus.NO_CONTENT, MimeType.getContentType("png"), new byte[0] + ); + webConnection.send(response); } } } diff --git a/Havana-Web/src/main/java/org/alexdev/http/server/Watchdog.java b/Havana-Web/src/main/java/org/alexdev/http/server/Watchdog.java index af8c938..b57df5d 100644 --- a/Havana-Web/src/main/java/org/alexdev/http/server/Watchdog.java +++ b/Havana-Web/src/main/java/org/alexdev/http/server/Watchdog.java @@ -15,7 +15,9 @@ import org.alexdev.http.game.news.NewsDateKey; import org.alexdev.http.util.config.WebSettingsConfigWriter; import org.apache.commons.lang3.tuple.Pair; +import java.net.MalformedURLException; import java.net.Socket; +import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; @@ -35,6 +37,7 @@ public class Watchdog implements Runnable { public static List NEWS_STAFF = new ArrayList<>(); public static int USERS_ONLNE; + public static boolean IS_IMAGER_ONLINE; public static boolean IS_SERVER_ONLINE; public static int LAST_VISITS; @@ -73,6 +76,21 @@ public class Watchdog implements Runnable { if (this.counter.get() % 30 == 0) { try { + String imagerPath = ServerConfiguration.getString("site.imager.path"); + + if (!imagerPath.isBlank()) { + try { + URL url = new URL(imagerPath); + String hostname = url.getHost(); + int port = url.getPort(); + + IS_IMAGER_ONLINE = isServerOnline(hostname, port); + } catch (MalformedURLException e) { } + + } + + IS_SERVER_ONLINE = isServerOnline(ServerConfiguration.getString("rcon.ip"), ServerConfiguration.getInteger("rcon.port")); + IS_SERVER_ONLINE = isServerOnline(ServerConfiguration.getString("rcon.ip"), ServerConfiguration.getInteger("rcon.port")); USERS_ONLNE = Integer.parseInt(SettingsDao.getSetting("players.online")); LAST_VISITS = SiteDao.getLastVisits();