Skip to content

Commit

Permalink
Implant web proxy support (#1556)
Browse files Browse the repository at this point in the history
  • Loading branch information
ItzMiracleOwO committed Mar 14, 2024
1 parent 3c98388 commit 338f384
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 5 deletions.
37 changes: 33 additions & 4 deletions src/main/java/github/scarsz/discordsrv/DiscordSRV.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,15 @@
import net.dv8tion.jda.api.utils.MemberCachePolicy;
import net.dv8tion.jda.api.utils.cache.CacheFlag;
import net.kyori.adventure.text.Component;
import okhttp3.Authenticator;
import okhttp3.ConnectionPool;
import okhttp3.Credentials;
import okhttp3.Dispatcher;
import okhttp3.Dns;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Route;
import okhttp3.internal.Util;
import okhttp3.internal.tls.OkHostnameVerifier;
import org.apache.commons.codec.digest.DigestUtils;
Expand Down Expand Up @@ -726,7 +731,12 @@ private List<InetAddress> lookupPublic(String host) throws UnknownHostException
dispatcher.setMaxRequestsPerHost(20); // most requests are to discord.com
ConnectionPool connectionPool = new ConnectionPool(5, 10, TimeUnit.SECONDS);

OkHttpClient httpClient = new OkHttpClient.Builder()
String proxyHost = config.getString("ProxyHost");
int proxyPort = config.getInt("ProxyPort");
String authUser = config.getString("ProxyUser");
String authPassword = config.getString("ProxyPassword");

OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder()
.dispatcher(dispatcher)
.connectionPool(connectionPool)
.dns(dns)
Expand All @@ -736,9 +746,28 @@ private List<InetAddress> lookupPublic(String host) throws UnknownHostException
.writeTimeout(20, TimeUnit.SECONDS)
.hostnameVerifier(noopHostnameVerifier.isPresent() && noopHostnameVerifier.get()
? (hostname, sslSession) -> true
: OkHostnameVerifier.INSTANCE
)
.build();
: OkHostnameVerifier.INSTANCE);

if (proxyHost != null && !proxyHost.isEmpty() && !proxyHost.equals("https://example.com")) {
try {
// This had to be set to empty string to avoid issue with basic auth
// Reference: https://stackoverflow.com/questions/41806422/java-web-start-unable-to-tunnel-through-proxy-since-java-8-update-111
System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost.trim(), proxyPort));
httpClientBuilder = httpClientBuilder.proxy(proxy);

if (!authPassword.isEmpty()) {
httpClientBuilder = httpClientBuilder.proxyAuthenticator((route, response) -> {
String credential = Credentials.basic(authUser.trim(), authPassword.trim());
return response.request().newBuilder().header("Proxy-Authorization", credential).build();
});
}
} catch (Exception e) {
DiscordSRV.error("Failed to generate a proxy from config options.", e);
}
}

OkHttpClient httpClient = httpClientBuilder.build();

// set custom RestAction failure handler
Consumer<? super Throwable> defaultFailure = RestAction.getDefaultFailure();
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/config/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ ConfigVersion: ${version}
# Sie müssen Ihren Server neu starten, nachdem Sie diese Option geändert haben
BotToken: "BOTTOKEN"

# The proxy to be used for Discord
# Leave this alone if you don't understand what it does
ProxyHost: "https://example.com"
ProxyPort: 1234
ProxyUser: "USERNAME"
ProxyPassword: "PASSWORD"

# Kanalverbindungen vom Spiel zu Discord
# Syntax ist Channels: {"Kanalname im Spiel von Minecraft": "numerische Kanal-ID von Discord", "ein anderer Kanalname im Spiel von Minecraft": "eine andere numerische Kanal-ID von Discord"}
#
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/config/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ ConfigVersion: ${version}
# You must restart your server after changing this option
BotToken: "BOTTOKEN"

# The proxy to be used for Discord
# Leave this alone if you don't understand what it does
ProxyHost: "https://example.com"
ProxyPort: 1234
ProxyUser: "USERNAME"
ProxyPassword: "PASSWORD"

# Channel links from game to Discord
# syntax is Channels: {"in-game channel name from Minecraft": "numerical channel ID from Discord", "another in-game channel name from Minecraft": "another numerical channel ID from Discord"}
#
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/config/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ ConfigVersion: ${version}
# Debe reiniciar su servidor después de cambiar esta opción
BotToken: "BOTTOKEN"

# The proxy to be used for Discord
# Leave this alone if you don't understand what it does
ProxyHost: "https://example.com"
ProxyPort: 1234
ProxyUser: "USERNAME"
ProxyPassword: "PASSWORD"

# Enlaces de canal del juego a Discord
# la sintaxis es Channels: {"nombre de canal en el juego de Minecraft": "ID de canal numérico de Discord", "otro nombre de canal en el juego de Minecraft": "otro ID de canal numérico de Discord"}
#
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/config/et.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ ConfigVersion: ${version}
# You must restart your server after changing this option
BotToken: "BOTTOKEN"

# The proxy to be used for Discord
# Leave this alone if you don't understand what it does
ProxyHost: "https://example.com"
ProxyPort: 1234
ProxyUser: "USERNAME"
ProxyPassword: "PASSWORD"

# Channel links from game to Discord
# syntax is Channels: {"in-game channel name from Minecraft": "numerical channel ID from Discord", "another in-game channel name from Minecraft": "another numerical channel ID from Discord"}
#
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/config/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ ConfigVersion: ${version}
# Vous devez redémarrer votre serveur après avoir modifié cette option
BotToken: "BOTTOKEN"

# The proxy to be used for Discord
# Leave this alone if you don't understand what it does
ProxyHost: "https://example.com"
ProxyPort: 1234
ProxyUser: "USERNAME"
ProxyPassword: "PASSWORD"

# Liens de chaîne du jeu vers Discord
# la syntaxe est Channels: {"nom de chaîne dans le jeu de Minecraft": "ID de chaîne numérique de Discord", "autre nom de chaîne de jeu de Minecraft": "autre ID de chaîne numérique de Discord"}
#
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/config/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ ConfigVersion: ${version}
# このオプションを変更した後、サーバーを再起動する必要があります
BotToken: "BOTTOKEN"

# The proxy to be used for Discord
# Leave this alone if you don't understand what it does
ProxyHost: "https://example.com"
ProxyPort: 1234
ProxyUser: "USERNAME"
ProxyPassword: "PASSWORD"

# ゲームからDiscordへのチャンネルリンク
# 構文は Channels: {"Minecraft内のチャンネル名": "Discord内のチャンネルID(数値)", "Minecraft内の別のチャンネル名": "Discord内の別のチャンネルID(数値)"}
#
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/config/ko.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ ConfigVersion: ${version}
# 이 옵션을 변경한 후에는 서버를 다시 시작해야 합니다.
BotToken: "BOTTOKEN"

# The proxy to be used for Discord
# Leave this alone if you don't understand what it does
ProxyHost: "https://example.com"
ProxyPort: 1234
ProxyUser: "USERNAME"
ProxyPassword: "PASSWORD"

# 게임에서 디스코드 채널로 연결하기
# 작성방법은 Channels: {"마인크래프트 인게임 채팅 채널 이름": "숫자로 된 디스코드 채널 ID", "또 다른 마인크래프트 채팅 채널 이름": "숫자로 된 디스코드 채널 ID"} 와 같은 방식입니다.
#
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/config/nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ ConfigVersion: ${version}
# U moet uw server opnieuw opstarten nadat u deze optie hebt gewijzigd
BotToken: "BOTTOKEN"

# The proxy to be used for Discord
# Leave this alone if you don't understand what it does
ProxyHost: "https://example.com"
ProxyPort: 1234
ProxyUser: "USERNAME"
ProxyPassword: "PASSWORD"

# Kanaallinks van game naar Discord
# syntaxis is Kanalen: {"in-game kanaalnaam van Minecraft": "numerieke kanaal-ID van Discord", "nog een in-game kanaalnaam van Minecraft": "nog een numerieke kanaal-ID van Discord"}
#
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/config/pl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ ConfigVersion: ${version}
# Po zmianie tej opcji musisz ponownie uruchomić serwer
BotToken: "BOTTOKEN"

# The proxy to be used for Discord
# Leave this alone if you don't understand what it does
ProxyHost: "https://example.com"
ProxyPort: 1234
ProxyUser: "USERNAME"
ProxyPassword: "PASSWORD"

# Linki kanału z gry do Discord
# składnia to Kanały: {"nazwa kanału w grze z Minecrafta": "numeryczny identyfikator kanału z Discord", "inna nazwa kanału w grze z Minecrafta": "inny numeryczny identyfikator kanału z Discord"}
#
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/config/ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ ConfigVersion: ${version}
# После изменения этого параметра необходимо перезагрузить сервер.
BotToken: "BOTTOKEN"

# The proxy to be used for Discord
# Leave this alone if you don't understand what it does
ProxyHost: "https://example.com"
ProxyPort: 1234
ProxyUser: "USERNAME"
ProxyPassword: "PASSWORD"

# Ссылки на каналы из игры в Discord
# синтаксис: Channels: {"название внутриигрового канала из Minecraft": "числовой идентификатор канала из Discord", "другое название внутриигрового канала из Minecraft": "другой числовой идентификатор канала из Discord"}
#
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/config/uk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ ConfigVersion: ${version}
# Після зміни цього параметра необхідно перезавантажити сервер.
BotToken: "BOTTOKEN"

# The proxy to be used for Discord
# Leave this alone if you don't understand what it does
ProxyHost: "https://example.com"
ProxyPort: 1234
ProxyUser: "USERNAME"
ProxyPassword: "PASSWORD"

# Посилання на канали з гри в Discord
# синтаксис: Channels: {"назва внутрішньоігрового каналу з Minecraft": "числовий ідентифікатор каналу з Discord", "інша назва внутрішньоігрового каналу з Minecraft": "інший числовий ідентифікатор каналу з Discord"}
#
Expand Down
9 changes: 8 additions & 1 deletion src/main/resources/config/zh.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ ConfigVersion: ${version}
# 更改此選項後必須重新啟動服務器
BotToken: "BOTTOKEN"

# 从游戏到不和谐的频道链接
# 使用代理服务器连接到Discord
# 如果你不知道它的功能,请不要修改参数
ProxyHost: "https://example.com"
ProxyPort: 1234
ProxyUser: "USERNAME"
ProxyPassword: "PASSWORD"

# 从游戏到Discord的频道链接
# 语法为 Channels: {"来自Minecraft的游戏中频道名称": "来自Discord的数字频道ID", "来自Minecraft的另一个游戏中频道名称": "来自Discord的另一个数字频道ID”}
#
# DiscordSRV的所有消息将转到第一个通道,除非为该消息定义了一个通道:
Expand Down

0 comments on commit 338f384

Please sign in to comment.