From be8d9a3c8245a828a185b30037df256b042520ed Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Sun, 19 Mar 2023 16:34:53 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D1=81=D1=82=D1=80=D0=BE=D0=B9?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=B2=D0=B5=D0=B1=D1=85=D1=83=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../quarkus/core/TelegramWebhookConnect.java | 22 +++++--- .../simple/core/TelegramWebhookConnect.java | 14 +++-- .../telegram/domain/config/ProxyConfig.java | 55 ++----------------- .../domain/config/TelegramBotConfig.java | 2 +- .../telegram/domain/config/WebhookConfig.java | 15 +++++ .../telegram/webhook/WebhookController.java | 27 ++++++--- 6 files changed, 63 insertions(+), 72 deletions(-) create mode 100644 telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/config/WebhookConfig.java diff --git a/telegram-core/telegram-core-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/core/TelegramWebhookConnect.java b/telegram-core/telegram-core-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/core/TelegramWebhookConnect.java index 08ee929..a764cc7 100644 --- a/telegram-core/telegram-core-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/core/TelegramWebhookConnect.java +++ b/telegram-core/telegram-core-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/core/TelegramWebhookConnect.java @@ -1,6 +1,7 @@ package dev.struchkov.godfather.telegram.quarkus.core; import dev.struchkov.godfather.telegram.domain.config.TelegramBotConfig; +import dev.struchkov.godfather.telegram.domain.config.WebhookConfig; import dev.struchkov.godfather.telegram.quarkus.context.service.EventDistributor; import dev.struchkov.godfather.telegram.quarkus.context.service.TelegramConnect; import lombok.extern.slf4j.Slf4j; @@ -18,16 +19,19 @@ public class TelegramWebhookConnect implements TelegramConnect { } private void initWebHook(TelegramBotConfig telegramBotConfig) { -// try { + try { final TelegramWebhookBot bot = new TelegramWebhookBot(telegramBotConfig); - final SetWebhook setWebhook = SetWebhook.builder() - .url(telegramBotConfig.getWebHookUrl()) - .build(); -// bot.setWebhook(setWebhook); - webhookBot = bot; -// } catch (TelegramApiException e) { -// log.error(e.getMessage()); -// } + final WebhookConfig webhookConfig = telegramBotConfig.getWebhookConfig(); + if (webhookConfig.isEnable()) { + final SetWebhook setWebhook = SetWebhook.builder() + .url(webhookConfig.getRootUrl() + "/" + webhookConfig.getRootUrl() + "?webhookAccessKey=" + webhookConfig.getAccessKey()) + .build(); + bot.setWebhook(setWebhook); + webhookBot = bot; + } + } catch (TelegramApiException e) { + log.error(e.getMessage()); + } } @Override diff --git a/telegram-core/telegram-core-simple/src/main/java/dev/struchkov/godfather/telegram/simple/core/TelegramWebhookConnect.java b/telegram-core/telegram-core-simple/src/main/java/dev/struchkov/godfather/telegram/simple/core/TelegramWebhookConnect.java index c94302f..106972e 100644 --- a/telegram-core/telegram-core-simple/src/main/java/dev/struchkov/godfather/telegram/simple/core/TelegramWebhookConnect.java +++ b/telegram-core/telegram-core-simple/src/main/java/dev/struchkov/godfather/telegram/simple/core/TelegramWebhookConnect.java @@ -1,6 +1,7 @@ package dev.struchkov.godfather.telegram.simple.core; import dev.struchkov.godfather.telegram.domain.config.TelegramBotConfig; +import dev.struchkov.godfather.telegram.domain.config.WebhookConfig; import dev.struchkov.godfather.telegram.simple.context.service.EventDistributor; import dev.struchkov.godfather.telegram.simple.context.service.TelegramConnect; import lombok.extern.slf4j.Slf4j; @@ -20,11 +21,14 @@ public class TelegramWebhookConnect implements TelegramConnect { private void initWebHook(TelegramBotConfig telegramBotConfig) { try { final TelegramWebhookBot bot = new TelegramWebhookBot(telegramBotConfig); - final SetWebhook setWebhook = SetWebhook.builder() - .url(telegramBotConfig.getWebHookUrl()) - .build(); - bot.setWebhook(setWebhook); - webhookBot = bot; + final WebhookConfig webhookConfig = telegramBotConfig.getWebhookConfig(); + if (webhookConfig.isEnable()) { + final SetWebhook setWebhook = SetWebhook.builder() + .url(webhookConfig.getRootUrl() + "/" + webhookConfig.getRootUrl() + "?webhookAccessKey=" + webhookConfig.getAccessKey()) + .build(); + bot.setWebhook(setWebhook); + webhookBot = bot; + } } catch (TelegramApiException e) { log.error(e.getMessage()); } diff --git a/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/config/ProxyConfig.java b/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/config/ProxyConfig.java index d2c7d6a..d121017 100644 --- a/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/config/ProxyConfig.java +++ b/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/config/ProxyConfig.java @@ -1,67 +1,24 @@ package dev.struchkov.godfather.telegram.domain.config; +import lombok.Getter; +import lombok.Setter; + /** * TODO: Добавить описание класса. * * @author upagge [30.01.2020] */ +@Getter +@Setter public class ProxyConfig { - private boolean enable = true; + private boolean enable = false; private String host; private Integer port; private String user; private String password; private Type type; - public String getHost() { - return host; - } - - public void setHost(String host) { - this.host = host; - } - - public Integer getPort() { - return port; - } - - public void setPort(Integer port) { - this.port = port; - } - - public String getUser() { - return user; - } - - public void setUser(String user) { - this.user = user; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - - public Type getType() { - return type; - } - - public void setType(Type type) { - this.type = type; - } - - public boolean isEnable() { - return enable; - } - - public void setEnable(boolean enable) { - this.enable = enable; - } - public enum Type { SOCKS5, SOCKS4, HTTP } diff --git a/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/config/TelegramBotConfig.java b/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/config/TelegramBotConfig.java index 610290c..1037ebb 100644 --- a/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/config/TelegramBotConfig.java +++ b/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/config/TelegramBotConfig.java @@ -16,9 +16,9 @@ public class TelegramBotConfig { private String username; private String token; - private String webHookUrl; private ProxyConfig proxyConfig; + private WebhookConfig webhookConfig; public TelegramBotConfig(String username, String token) { this.username = username; diff --git a/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/config/WebhookConfig.java b/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/config/WebhookConfig.java new file mode 100644 index 0000000..3ef431b --- /dev/null +++ b/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/config/WebhookConfig.java @@ -0,0 +1,15 @@ +package dev.struchkov.godfather.telegram.domain.config; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class WebhookConfig { + + private boolean enable = false; + private String rootUrl; + private String path; + private String accessKey; + +} diff --git a/telegram-webhook/telegram-webhook-quarkus/src/main/java/dev/struchkov/godfather/telegram/webhook/WebhookController.java b/telegram-webhook/telegram-webhook-quarkus/src/main/java/dev/struchkov/godfather/telegram/webhook/WebhookController.java index b88fa7b..59a1dd5 100644 --- a/telegram-webhook/telegram-webhook-quarkus/src/main/java/dev/struchkov/godfather/telegram/webhook/WebhookController.java +++ b/telegram-webhook/telegram-webhook-quarkus/src/main/java/dev/struchkov/godfather/telegram/webhook/WebhookController.java @@ -12,38 +12,49 @@ import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import static dev.struchkov.haiti.context.exception.AccessException.accessException; +import static dev.struchkov.haiti.utils.Inspector.isTrue; + @Slf4j -@Path("callback") public class WebhookController { private final String pathKey; + private final String accessKey; private final EventDistributor eventDistributor; public WebhookController(TelegramBotConfig telegramBotConfig, EventDistributor eventDistributor) { + this.accessKey = telegramBotConfig.getWebhookConfig().getAccessKey(); this.eventDistributor = eventDistributor; - this.pathKey = telegramBotConfig.getWebHookUrl().split("callback")[1]; + this.pathKey = telegramBotConfig.getWebhookConfig().getPath(); } @POST - @Path("/{botPath}") + @Path("{webhookPath}") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) - public Uni updateReceived(@PathParam("botPath") String botPath, Update update) { + public Uni updateReceived(@PathParam("webhookPath") String botPath, @QueryParam("webhookAccessKey") String webhookAccessKey, Update update) { return Uni.createFrom().voidItem() -// .onItem().invoke(() -> isTrue(pathKey.equals(botPath), accessException("В доступе отказано!"))) + .onItem().invoke(() -> { + isTrue(pathKey.equals(botPath), accessException("В доступе отказано!")); + isTrue(accessKey.equals(webhookAccessKey), accessException("В доступе отказано!")); + }) .onItem().ignore().andSwitchTo(() -> eventDistributor.processing(update)) .onItem().transform(ignore -> Response.ok().build()); } @GET - @Path("/{botPath}") + @Path("{webhookPath}") @Produces(MediaType.APPLICATION_JSON) - public Uni testReceived(@PathParam("botPath") String botPath) { + public Uni testReceived(@PathParam("webhookPath") String botPath, @QueryParam("webhookAccessKey") String webhookAccessKey) { return Uni.createFrom().voidItem() -// .onItem().invoke(() -> isTrue(pathKey.equals(botPath), accessException("В доступе отказано!"))) + .onItem().invoke(() -> { + isTrue(pathKey.equals(botPath), accessException("В доступе отказано!")); + isTrue(accessKey.equals(webhookAccessKey), accessException("В доступе отказано!")); + }) .onItem().transform(ignore -> "Hi there " + botPath + "!"); }