Browse Source

rkn fix

master
gsd 6 months ago
parent
commit
31d12647a0
  1. 69
      src/main/java/app/configurations/RestTemplateConfig.java
  2. 5
      src/main/java/app/controllers/auth/AuthDiscordController.java
  3. 16
      src/main/java/app/services/db/ReportService.java
  4. 14
      src/main/java/app/services/db/VIPService.java

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

5
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";

16
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<String, Object> 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();
}

14
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<String, Boolean> 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);
}
}
}

Loading…
Cancel
Save