From 31d12647a06dcc68fb7e852245400f06a86930cb Mon Sep 17 00:00:00 2001 From: gsd Date: Sun, 13 Oct 2024 20:52:35 +0300 Subject: [PATCH] rkn fix --- .../configurations/RestTemplateConfig.java | 69 +++++++++++++++++++ .../auth/AuthDiscordController.java | 5 +- .../java/app/services/db/ReportService.java | 16 ++++- src/main/java/app/services/db/VIPService.java | 14 +++- 4 files changed, 97 insertions(+), 7 deletions(-) create mode 100644 src/main/java/app/configurations/RestTemplateConfig.java diff --git a/src/main/java/app/configurations/RestTemplateConfig.java b/src/main/java/app/configurations/RestTemplateConfig.java new file mode 100644 index 0000000..b6bbe9a --- /dev/null +++ b/src/main/java/app/configurations/RestTemplateConfig.java @@ -0,0 +1,69 @@ +package app.configurations; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; +import org.springframework.context.annotation.Scope; +import org.springframework.core.env.Environment; +import org.springframework.http.client.SimpleClientHttpRequestFactory; +import org.springframework.web.client.RestTemplate; + +import java.net.InetSocketAddress; +import java.net.Proxy; + +@Configuration +public class RestTemplateConfig { + + private final String proxy = System.getenv("PROXY_URL"); + private final Logger logger = LoggerFactory.getLogger(getClass()); + + @Bean(name = "RestTemplate") + @Scope(value = "prototype") + @Primary + public RestTemplate getRestTemplate() { + return new RestTemplate(); + } + + @Bean(name = "RestTemplateProxy") + @Scope(value = "prototype") + public RestTemplate getRestTemplateProxy() { + if (proxy == null || proxy.isEmpty()) { + logger.warn("Called proxied restTemplate but PROXY_URL is not setted"); + return getRestTemplate(); + } + + try { + String[] proto_address = proxy.split("://"); + String[] address_port = proto_address[0].split(":"); + int port = Integer.parseInt(address_port[1]); + + Proxy.Type type; + switch (proto_address[0]) { + case "http" -> { + type = Proxy.Type.HTTP; + } + case "socks5" -> { + type = Proxy.Type.SOCKS; + } + default -> { + logger.error("PROXY_URL is setted but can't be parse proxy type"); + return getRestTemplate(); + } + } + + SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); + InetSocketAddress address = new InetSocketAddress(address_port[0], port); + Proxy proxy_inst = new Proxy(type, address); + factory.setProxy(proxy_inst); + + RestTemplate restTemplate = getRestTemplate(); + restTemplate.setRequestFactory(factory); + return restTemplate; + } catch (IndexOutOfBoundsException | NumberFormatException e) { + logger.error("PROXY_URL is setted but can't be splitted or parsed"); + return getRestTemplate(); + } + } +} diff --git a/src/main/java/app/controllers/auth/AuthDiscordController.java b/src/main/java/app/controllers/auth/AuthDiscordController.java index a2db2b0..dfae9d0 100644 --- a/src/main/java/app/controllers/auth/AuthDiscordController.java +++ b/src/main/java/app/controllers/auth/AuthDiscordController.java @@ -17,6 +17,7 @@ import jakarta.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.http.*; import org.springframework.web.bind.annotation.*; import org.springframework.web.client.RestTemplate; @@ -34,7 +35,9 @@ import java.util.Map; public class AuthDiscordController { private CryptedCookie cryptedCookie; - private RestTemplate restTemplate = new RestTemplate(); + @Autowired + @Qualifier(value = "RestTemplateProxy") + private RestTemplate restTemplate; private final String processing = "https://discord.com/api/oauth2/authorize?client_id=684685147144060948&redirect_uri=https%3A%2F%2Ftf2.pblr-nyk.pro%2Fapi%2Fauth%2Fdiscord%2Fprocesslogin&response_type=token&scope=identify"; diff --git a/src/main/java/app/services/db/ReportService.java b/src/main/java/app/services/db/ReportService.java index 0cbc5b4..9028a5c 100644 --- a/src/main/java/app/services/db/ReportService.java +++ b/src/main/java/app/services/db/ReportService.java @@ -12,6 +12,8 @@ import app.entities.server.request.PlayerOnServer; import app.services.ProfileService; import app.services.StatsService; import com.fasterxml.jackson.databind.ObjectMapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; @@ -37,7 +39,10 @@ public class ReportService { Stats stats; StatsService statsService; String tf2_icon = "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/apps/440/e3f595a92552da3d664ad00277fad2107345f743.jpg"; - RestTemplate restTemplate; + + @Autowired + @Qualifier(value = "RestTemplateProxy") + private RestTemplate restTemplate; @Autowired @Qualifier("jt_rw") @@ -47,13 +52,14 @@ public class ReportService { @Qualifier("jt_ro") private JdbcTemplate jdbcTemplate_ro; + private final Logger logger = LoggerFactory.getLogger(getClass()); + @Autowired public ReportService(Stats stats, StatsService statsService) { this.stats = stats; this.statsService = statsService; userKD = new HashMap<>(); - restTemplate = new RestTemplate(); } private long addReport(PlayerProfile current_user, PlayerProfile reported_user, String text, ReportType reportType) { @@ -185,7 +191,11 @@ public class ReportService { HashMap payload = new HashMap<>(); payload.put("embeds", List.of(embed)); - restTemplate.postForEntity(webhook_url, (Object) payload, String.class); + try { + restTemplate.postForEntity(webhook_url, (Object) payload, String.class); + } catch (Exception e) { + logger.error("Cannot push webhook", e); + } return Instant.now().getEpochSecond(); } diff --git a/src/main/java/app/services/db/VIPService.java b/src/main/java/app/services/db/VIPService.java index 24973f1..9c8ad35 100644 --- a/src/main/java/app/services/db/VIPService.java +++ b/src/main/java/app/services/db/VIPService.java @@ -5,6 +5,8 @@ import app.entities.db.Permition; import app.entities.other.SteamID; import app.services.ServerService; import app.utils.SteamIDConverter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; @@ -29,7 +31,9 @@ public class VIPService { private HashMap services; - RestTemplate restTemplate; + @Autowired + @Qualifier(value = "RestTemplateProxy") + private RestTemplate restTemplate; @Value("${backend.vip.discord}") String webhook_url; @@ -37,11 +41,11 @@ public class VIPService { PermitionService permitionService; ServerService serverService; DonateService donateService; + private final Logger logger = LoggerFactory.getLogger(getClass()); @Autowired public VIPService(PermitionService permitionService, ServerService serverService, DonateService donateService) { - this.restTemplate = new RestTemplate(); this.permitionService = permitionService; this.serverService = serverService; this.donateService = donateService; @@ -162,6 +166,10 @@ public class VIPService { Map.of("name", "Количество дней", "value", "%d".formatted(amount/60/60/24), "inline", true), Map.of("name", "Оплата", "value", status, "inline", true) )))); - restTemplate.postForEntity(webhook_url, payload, String.class); + try { + restTemplate.postForEntity(webhook_url, payload, String.class); + } catch (Exception e) { + logger.error("Cannot push webhook", e); + } } }