Добавил настройку прокси для telegram

This commit is contained in:
Struchkov Mark 2022-12-13 18:34:42 +03:00
parent 4b2175b0df
commit 7fda16b63c
23 changed files with 76 additions and 74 deletions

View File

@ -5,7 +5,7 @@
<groupId>dev.struchkov.godfather.telegram</groupId> <groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-bot</artifactId> <artifactId>telegram-bot</artifactId>
<version>0.0.41</version> <version>0.0.42</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<modules> <modules>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>telegram-bot</artifactId> <artifactId>telegram-bot</artifactId>
<groupId>dev.struchkov.godfather.telegram</groupId> <groupId>dev.struchkov.godfather.telegram</groupId>
<version>0.0.41</version> <version>0.0.42</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>dev.struchkov.godfather.telegram</groupId> <groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-consumer</artifactId> <artifactId>telegram-consumer</artifactId>
<version>0.0.41</version> <version>0.0.42</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>dev.struchkov.godfather.telegram</groupId> <groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-consumer</artifactId> <artifactId>telegram-consumer</artifactId>
<version>0.0.41</version> <version>0.0.42</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>dev.struchkov.godfather.telegram</groupId> <groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-consumer</artifactId> <artifactId>telegram-consumer</artifactId>
<version>0.0.41</version> <version>0.0.42</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>telegram-bot</artifactId> <artifactId>telegram-bot</artifactId>
<groupId>dev.struchkov.godfather.telegram</groupId> <groupId>dev.struchkov.godfather.telegram</groupId>
<version>0.0.41</version> <version>0.0.42</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>dev.struchkov.godfather.telegram</groupId> <groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-context</artifactId> <artifactId>telegram-context</artifactId>
<version>0.0.41</version> <version>0.0.42</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>dev.struchkov.godfather.telegram</groupId> <groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-context</artifactId> <artifactId>telegram-context</artifactId>
<version>0.0.41</version> <version>0.0.42</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>telegram-context</artifactId> <artifactId>telegram-context</artifactId>
<groupId>dev.struchkov.godfather.telegram</groupId> <groupId>dev.struchkov.godfather.telegram</groupId>
<version>0.0.41</version> <version>0.0.42</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>dev.struchkov.godfather.telegram</groupId> <groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-bot</artifactId> <artifactId>telegram-bot</artifactId>
<version>0.0.41</version> <version>0.0.42</version>
</parent> </parent>
<modules> <modules>
<module>telegram-core-main</module> <module>telegram-core-main</module>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>dev.struchkov.godfather.telegram</groupId> <groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-core</artifactId> <artifactId>telegram-core</artifactId>
<version>0.0.41</version> <version>0.0.42</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -11,6 +11,8 @@ import org.telegram.telegrambots.meta.bots.AbsSender;
import java.net.Authenticator; import java.net.Authenticator;
import java.net.PasswordAuthentication; import java.net.PasswordAuthentication;
import static dev.struchkov.haiti.utils.Checker.checkNotNull;
public class TelegramDefaultConnect implements TelegramConnect { public class TelegramDefaultConnect implements TelegramConnect {
private final String botToken; private final String botToken;
@ -26,28 +28,30 @@ public class TelegramDefaultConnect implements TelegramConnect {
final DefaultBotOptions botOptions = new DefaultBotOptions(); final DefaultBotOptions botOptions = new DefaultBotOptions();
final ProxyConfig proxyConfig = connectConfig.getProxyConfig(); final ProxyConfig proxyConfig = connectConfig.getProxyConfig();
if (proxyConfig != null && proxyConfig.getPassword() != null) { if (checkNotNull(proxyConfig) && proxyConfig.isEnable()) {
try { if (checkNotNull(proxyConfig.getPassword())) {
Authenticator.setDefault(new Authenticator() { try {
@Override Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() { @Override
return new PasswordAuthentication( protected PasswordAuthentication getPasswordAuthentication() {
proxyConfig.getUser(), return new PasswordAuthentication(
proxyConfig.getPassword().toCharArray() proxyConfig.getUser(),
); proxyConfig.getPassword().toCharArray()
} );
}); }
} catch (Exception e) { });
e.printStackTrace(); } catch (Exception e) {
e.printStackTrace();
}
} }
}
if (proxyConfig != null && proxyConfig.getHost() != null) { if (checkNotNull(proxyConfig.getHost())) {
System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2"); System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
System.setProperty("javax.net.debug", "all"); System.setProperty("javax.net.debug", "all");
botOptions.setProxyHost(proxyConfig.getHost()); botOptions.setProxyHost(proxyConfig.getHost());
botOptions.setProxyPort(proxyConfig.getPort()); botOptions.setProxyPort(proxyConfig.getPort());
botOptions.setProxyType(convertProxyType(proxyConfig.getType())); botOptions.setProxyType(convertProxyType(proxyConfig.getType()));
}
} }
return new DefaultAbsSender(botOptions) { return new DefaultAbsSender(botOptions) {
@ -59,16 +63,11 @@ public class TelegramDefaultConnect implements TelegramConnect {
} }
private DefaultBotOptions.ProxyType convertProxyType(ProxyConfig.Type type) { private DefaultBotOptions.ProxyType convertProxyType(ProxyConfig.Type type) {
switch (type) { return switch (type) {
case SOCKS5: case SOCKS5 -> DefaultBotOptions.ProxyType.SOCKS5;
return DefaultBotOptions.ProxyType.SOCKS5; case SOCKS4 -> DefaultBotOptions.ProxyType.SOCKS4;
case SOCKS4: case HTTP -> DefaultBotOptions.ProxyType.HTTP;
return DefaultBotOptions.ProxyType.SOCKS4; };
case HTTP:
return DefaultBotOptions.ProxyType.HTTP;
default:
return DefaultBotOptions.ProxyType.NO_PROXY;
}
} }
@Override @Override

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>telegram-core</artifactId> <artifactId>telegram-core</artifactId>
<groupId>dev.struchkov.godfather.telegram</groupId> <groupId>dev.struchkov.godfather.telegram</groupId>
<version>0.0.41</version> <version>0.0.42</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -17,6 +17,8 @@ import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
import java.net.Authenticator; import java.net.Authenticator;
import java.net.PasswordAuthentication; import java.net.PasswordAuthentication;
import static dev.struchkov.haiti.utils.Checker.checkNotNull;
/** /**
* TODO: Добавить описание класса. * TODO: Добавить описание класса.
* *
@ -52,7 +54,7 @@ public class TelegramConnectBot implements TelegramConnect {
private void initLongPolling(TelegramConnectConfig telegramConnectConfig) { private void initLongPolling(TelegramConnectConfig telegramConnectConfig) {
final ProxyConfig proxyConfig = telegramConnectConfig.getProxyConfig(); final ProxyConfig proxyConfig = telegramConnectConfig.getProxyConfig();
if (proxyConfig != null && proxyConfig.getPassword() != null) { if (checkNotNull(proxyConfig) && proxyConfig.isEnable() && checkNotNull(proxyConfig.getPassword()) && !"".equals(proxyConfig.getPassword())) {
try { try {
Authenticator.setDefault(new Authenticator() { Authenticator.setDefault(new Authenticator() {
@Override @Override
@ -70,7 +72,7 @@ public class TelegramConnectBot implements TelegramConnect {
final TelegramBotsApi botapi; final TelegramBotsApi botapi;
try { try {
if (proxyConfig != null && proxyConfig.getHost() != null) { if (checkNotNull(proxyConfig) && proxyConfig.isEnable() && checkNotNull(proxyConfig.getHost()) && !"".equals(proxyConfig.getHost())) {
System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2"); System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
System.setProperty("javax.net.debug", "all"); System.setProperty("javax.net.debug", "all");
log.info(System.getProperty("https.protocols")); log.info(System.getProperty("https.protocols"));
@ -97,16 +99,11 @@ public class TelegramConnectBot implements TelegramConnect {
} }
private DefaultBotOptions.ProxyType convertProxyType(Type type) { private DefaultBotOptions.ProxyType convertProxyType(Type type) {
switch (type) { return switch (type) {
case SOCKS5: case SOCKS5 -> DefaultBotOptions.ProxyType.SOCKS5;
return DefaultBotOptions.ProxyType.SOCKS5; case SOCKS4 -> DefaultBotOptions.ProxyType.SOCKS4;
case SOCKS4: case HTTP -> DefaultBotOptions.ProxyType.HTTP;
return DefaultBotOptions.ProxyType.SOCKS4; };
case HTTP:
return DefaultBotOptions.ProxyType.HTTP;
default:
return DefaultBotOptions.ProxyType.NO_PROXY;
}
} }
public void initEventDistributor(EventDistributor eventDistributor) { public void initEventDistributor(EventDistributor eventDistributor) {

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>dev.struchkov.godfather.telegram</groupId> <groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-core</artifactId> <artifactId>telegram-core</artifactId>
<version>0.0.41</version> <version>0.0.42</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -17,6 +17,8 @@ import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
import java.net.Authenticator; import java.net.Authenticator;
import java.net.PasswordAuthentication; import java.net.PasswordAuthentication;
import static dev.struchkov.haiti.utils.Checker.checkNotNull;
/** /**
* TODO: Добавить описание класса. * TODO: Добавить описание класса.
* *
@ -35,9 +37,8 @@ public class TelegramConnectBot implements TelegramConnect {
} }
private void initLongPolling(TelegramConnectConfig telegramConnectConfig) { private void initLongPolling(TelegramConnectConfig telegramConnectConfig) {
final ProxyConfig proxyConfig = telegramConnectConfig.getProxyConfig(); final ProxyConfig proxyConfig = telegramConnectConfig.getProxyConfig();
if (proxyConfig != null && proxyConfig.getPassword() != null) { if (checkNotNull(proxyConfig) && checkNotNull(proxyConfig.getPassword()) && !"".equals(proxyConfig.getPassword())) {
try { try {
Authenticator.setDefault(new Authenticator() { Authenticator.setDefault(new Authenticator() {
@Override @Override
@ -55,7 +56,7 @@ public class TelegramConnectBot implements TelegramConnect {
final TelegramBotsApi botapi; final TelegramBotsApi botapi;
try { try {
if (proxyConfig != null && proxyConfig.getHost() != null) { if (checkNotNull(proxyConfig) && proxyConfig.isEnable() && checkNotNull(proxyConfig.getHost()) && !"".equals(proxyConfig.getHost())) {
System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2"); System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
System.setProperty("javax.net.debug", "all"); System.setProperty("javax.net.debug", "all");
log.info(System.getProperty("https.protocols")); log.info(System.getProperty("https.protocols"));
@ -81,16 +82,11 @@ public class TelegramConnectBot implements TelegramConnect {
} }
private DefaultBotOptions.ProxyType convertProxyType(Type type) { private DefaultBotOptions.ProxyType convertProxyType(Type type) {
switch (type) { return switch (type) {
case SOCKS5: case SOCKS5 -> DefaultBotOptions.ProxyType.SOCKS5;
return DefaultBotOptions.ProxyType.SOCKS5; case SOCKS4 -> DefaultBotOptions.ProxyType.SOCKS4;
case SOCKS4: case HTTP -> DefaultBotOptions.ProxyType.HTTP;
return DefaultBotOptions.ProxyType.SOCKS4; };
case HTTP:
return DefaultBotOptions.ProxyType.HTTP;
default:
return DefaultBotOptions.ProxyType.NO_PROXY;
}
} }
public void initEventDistributor(EventDistributor eventDistributor) { public void initEventDistributor(EventDistributor eventDistributor) {

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>dev.struchkov.godfather.telegram</groupId> <groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-bot</artifactId> <artifactId>telegram-bot</artifactId>
<version>0.0.41</version> <version>0.0.42</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -7,6 +7,7 @@ package dev.struchkov.godfather.telegram.domain.config;
*/ */
public class ProxyConfig { public class ProxyConfig {
private boolean enable = true;
private String host; private String host;
private Integer port; private Integer port;
private String user; private String user;
@ -53,7 +54,16 @@ public class ProxyConfig {
this.type = type; this.type = type;
} }
public boolean isEnable() {
return enable;
}
public void setEnable(boolean enable) {
this.enable = enable;
}
public enum Type { public enum Type {
SOCKS5, SOCKS4, HTTP SOCKS5, SOCKS4, HTTP
} }
} }

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>dev.struchkov.godfather.telegram</groupId> <groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-bot</artifactId> <artifactId>telegram-bot</artifactId>
<version>0.0.41</version> <version>0.0.42</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>dev.struchkov.godfather.telegram</groupId> <groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-sender</artifactId> <artifactId>telegram-sender</artifactId>
<version>0.0.41</version> <version>0.0.42</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>dev.struchkov.godfather.telegram</groupId> <groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-sender</artifactId> <artifactId>telegram-sender</artifactId>
<version>0.0.41</version> <version>0.0.42</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>dev.struchkov.godfather.telegram</groupId> <groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-sender</artifactId> <artifactId>telegram-sender</artifactId>
<version>0.0.41</version> <version>0.0.42</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>dev.struchkov.godfather.telegram</groupId> <groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-bot</artifactId> <artifactId>telegram-bot</artifactId>
<version>0.0.41</version> <version>0.0.42</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>