Добавил настройку прокси для 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>
<artifactId>telegram-bot</artifactId>
<version>0.0.41</version>
<version>0.0.42</version>
<packaging>pom</packaging>
<modules>

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -5,7 +5,7 @@
<parent>
<groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-core</artifactId>
<version>0.0.41</version>
<version>0.0.42</version>
</parent>
<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.PasswordAuthentication;
import static dev.struchkov.haiti.utils.Checker.checkNotNull;
public class TelegramDefaultConnect implements TelegramConnect {
private final String botToken;
@ -26,28 +28,30 @@ public class TelegramDefaultConnect implements TelegramConnect {
final DefaultBotOptions botOptions = new DefaultBotOptions();
final ProxyConfig proxyConfig = connectConfig.getProxyConfig();
if (proxyConfig != null && proxyConfig.getPassword() != null) {
try {
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
proxyConfig.getUser(),
proxyConfig.getPassword().toCharArray()
);
}
});
} catch (Exception e) {
e.printStackTrace();
if (checkNotNull(proxyConfig) && proxyConfig.isEnable()) {
if (checkNotNull(proxyConfig.getPassword())) {
try {
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
proxyConfig.getUser(),
proxyConfig.getPassword().toCharArray()
);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
if (proxyConfig != null && proxyConfig.getHost() != null) {
System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
System.setProperty("javax.net.debug", "all");
botOptions.setProxyHost(proxyConfig.getHost());
botOptions.setProxyPort(proxyConfig.getPort());
botOptions.setProxyType(convertProxyType(proxyConfig.getType()));
if (checkNotNull(proxyConfig.getHost())) {
System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
System.setProperty("javax.net.debug", "all");
botOptions.setProxyHost(proxyConfig.getHost());
botOptions.setProxyPort(proxyConfig.getPort());
botOptions.setProxyType(convertProxyType(proxyConfig.getType()));
}
}
return new DefaultAbsSender(botOptions) {
@ -59,16 +63,11 @@ public class TelegramDefaultConnect implements TelegramConnect {
}
private DefaultBotOptions.ProxyType convertProxyType(ProxyConfig.Type type) {
switch (type) {
case SOCKS5:
return DefaultBotOptions.ProxyType.SOCKS5;
case SOCKS4:
return DefaultBotOptions.ProxyType.SOCKS4;
case HTTP:
return DefaultBotOptions.ProxyType.HTTP;
default:
return DefaultBotOptions.ProxyType.NO_PROXY;
}
return switch (type) {
case SOCKS5 -> DefaultBotOptions.ProxyType.SOCKS5;
case SOCKS4 -> DefaultBotOptions.ProxyType.SOCKS4;
case HTTP -> DefaultBotOptions.ProxyType.HTTP;
};
}
@Override

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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