Skip to content

Commit ae37b08

Browse files
committed
[FIX] Telegram proxy
1 parent a8f1089 commit ae37b08

6 files changed

Lines changed: 94 additions & 37 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ makeevrserg.java.ktarget=21
1313
# Project
1414
makeevrserg.project.name=MessageBridge
1515
makeevrserg.project.group=ru.astrainteractive.messagebridge
16-
makeevrserg.project.version.string=0.18.0
16+
makeevrserg.project.version.string=0.19.0
1717
makeevrserg.project.description=Bridge for TG and Discord
1818
makeevrserg.project.developers=makeevrserg|Makeev Roman|makeevrserg@gmail.com
1919
makeevrserg.project.url=https://empireprojekt.ru

instances/bukkit/build.gradle.kts

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,25 +75,28 @@ shadowJar.configure {
7575

7676
dependencies {
7777
// Dependencies
78-
exclude("mozilla/**")
79-
exclude("javax/**")
80-
exclude("it/unimi/dsi/**")
8178
exclude("ch/qos/logback/**")
79+
exclude("com/ibm/icu/**")
80+
exclude("it/unimi/dsi/**")
81+
exclude("javax/**")
82+
exclude("mozilla/**")
83+
exclude("org/apache/batik/**")
84+
exclude("org/apache/commons/logging/**")
85+
exclude("org/apache/xmlgraphics/**")
8286
exclude("org/intellij/lang/annotations/**")
8387
exclude("org/jetbrains/annotations/**")
8488
exclude("org/slf4j/**")
85-
exclude("org/apache/xmlgraphics/**")
86-
exclude("org/apache/batik/**")
87-
exclude("org/apache/commons/logging/**")
88-
exclude("com/ibm/icu/**")
89+
exclude("org/w3c/dom/**")
8990
// Root
91+
exclude("**LICENCE**")
92+
exclude("**LICENSE**")
9093
exclude("_COROUTINE/**")
9194
exclude("DebugProbesKt.bin")
9295
exclude("jetty-dir.css")
96+
exclude("LICENSE")
9397
exclude("license/**")
9498
exclude("licenses/**")
95-
exclude("**LICENCE**")
96-
exclude("**LICENSE**")
99+
exclude("natives/**")
97100
// META
98101
exclude("META-INF/**.md")
99102
exclude("META-INF/**.MD")
@@ -111,28 +114,49 @@ shadowJar.configure {
111114
exclude("META-INF/rewrite/**")
112115
exclude("META-INF/services/kotlin.reflect.**")
113116
exclude("META-INF/versions/**")
114-
exclude(dependency("mysql:mysql-connector-java"))
115-
exclude(dependency("com.mysql:mysql-connector-j"))
116-
exclude(dependency("org.xerial:sqlite-jdbc"))
117+
// DEPENDENCIES
118+
exclude(dependency("com.fasterxml.jackson.core:.*"))
119+
exclude(dependency("com.google.code.gson:.*"))
120+
exclude(dependency("com.google.crypto.tink:.*"))
121+
exclude(dependency("com.google.errorprone:.*"))
117122
exclude(dependency("com.mojang:brigadier"))
123+
exclude(dependency("com.mysql:mysql-connector-j"))
124+
exclude(dependency("mysql:mysql-connector-java"))
125+
exclude(dependency("net.java.dev.jna:.*"))
118126
exclude(dependency("net.kyori:.*"))
127+
exclude(dependency("org.apache.xmlgraphics:.*"))
128+
exclude(dependency("org.bouncycastle:.*"))
129+
exclude(dependency("org.checkerframework:.*"))
130+
exclude(dependency("org.conscrypt:.*"))
131+
exclude(dependency("org.eclipse.jetty.toolchain:.*"))
132+
exclude(dependency("org.eclipse.jetty:.*"))
133+
exclude(dependency("org.xerial:sqlite-jdbc"))
119134
}
120135
relocate("org.bstats", projectInfo.group)
121136
listOf(
122137
"ch.qos.logback",
138+
"club.minnced.discord",
139+
"club.minnced.opus",
140+
"co.touchlab.stately",
123141
"com.charleskorn.kaml",
124142
"com.ibm.icu",
143+
"com.neovisionaries.ws",
144+
"gnu.trove",
145+
"google.protobuf",
146+
"io.github.reactivecircus",
125147
"it.krzeminski.snakeyaml",
148+
"net.dv8tion",
126149
"net.thauvin.erik",
150+
"okhttp3",
127151
"okio",
128152
"org.apache",
129153
"org.intellij",
130154
"org.jetbrains.annotations",
131-
"ru.astrainteractive.klibs",
155+
"org.json",
156+
"org.telegram.telegrambots",
132157
"ru.astrainteractive.astralibs",
133-
"io.github.reactivecircus",
134-
"co.touchlab.stately",
135-
"google.protobuf",
158+
"ru.astrainteractive.klibs",
159+
"tomp2p.opuswrapper",
136160
).forEach { pattern -> relocate(pattern, "${projectInfo.group}.$pattern") }
137161
listOf(
138162
"kotlinx",

modules/core/api/build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ plugins {
44
}
55

66
dependencies {
7+
compileOnly(libs.minecraft.luckperms)
8+
79
implementation(libs.klibs.kstorage)
810
implementation(libs.klibs.mikro.core)
911
implementation(libs.kotlin.coroutines.core)
1012
implementation(libs.kotlin.serialization.json)
1113
implementation(libs.kotlin.serialization.kaml)
1214
implementation(libs.minecraft.astralibs.core)
1315
implementation(libs.minecraft.astralibs.core.bukkit)
14-
implementation(libs.minecraft.luckperms)
1516
}

modules/core/api/src/main/kotlin/ru/astrainteractive/messagebridge/core/PluginConfiguration.kt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@ data class PluginConfiguration(
1818
val linkLuckPermsRole: String
1919
)
2020

21+
@Serializable
22+
data class Proxy(
23+
val host: String,
24+
val port: Int,
25+
val username: String,
26+
val password: String
27+
)
28+
2129
@Serializable
2230
data class JdaConfig(
2331
val token: String = "",
2432
val activity: String = "",
2533
val channelId: String = "",
2634
val proxy: Proxy? = null
27-
) {
28-
@Serializable
29-
data class Proxy(
30-
val host: String,
31-
val port: Int,
32-
val username: String,
33-
val password: String
34-
)
35-
}
35+
)
3636

3737
@Serializable
3838
data class TelegramConfig(
@@ -44,5 +44,7 @@ data class PluginConfiguration(
4444
val topicID: String = "",
4545
@SerialName("max_telegram_message_length")
4646
val maxTelegramMessageLength: Int = 90,
47+
@SerialName("proxy")
48+
val proxy: Proxy? = null
4749
)
4850
}

modules/link/src/main/kotlin/ru/astrainteractive/messagebridge/link/database/di/LinkDatabaseModule.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ interface LinkDatabaseModule {
3030
).mapCached(scope) { dbConfig, previous ->
3131
previous?.connector?.invoke()?.close()
3232
previous?.run(TransactionManager::closeAndUnregister)
33-
3433
val database = dbConfig.connect()
3534
TransactionManager.manager.defaultIsolationLevel = java.sql.Connection.TRANSACTION_SERIALIZABLE
3635
transaction(database) {

modules/messenger/telegram/src/main/kotlin/ru/astrainteractive/messagebridge/messenger/telegram/di/TelegramMessengerModule.kt

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ package ru.astrainteractive.messagebridge.messenger.telegram.di
33
import kotlinx.coroutines.Dispatchers
44
import kotlinx.coroutines.GlobalScope
55
import kotlinx.coroutines.cancel
6+
import kotlinx.coroutines.flow.SharingStarted
7+
import kotlinx.coroutines.flow.combine
68
import kotlinx.coroutines.flow.distinctUntilChanged
79
import kotlinx.coroutines.flow.firstOrNull
810
import kotlinx.coroutines.flow.map
11+
import kotlinx.coroutines.flow.shareIn
912
import kotlinx.coroutines.launch
13+
import okhttp3.Credentials
14+
import okhttp3.OkHttpClient
1015
import org.telegram.telegrambots.client.okhttp.OkHttpTelegramClient
1116
import org.telegram.telegrambots.longpolling.TelegramBotsLongPollingApplication
1217
import org.telegram.telegrambots.longpolling.exceptions.TelegramApiErrorResponseException
@@ -20,23 +25,49 @@ import ru.astrainteractive.messagebridge.core.di.CoreModule
2025
import ru.astrainteractive.messagebridge.link.di.LinkModule
2126
import ru.astrainteractive.messagebridge.messenger.telegram.events.TelegramChatConsumer
2227
import ru.astrainteractive.messagebridge.messenger.telegram.messaging.TelegramBEventConsumer
28+
import java.net.InetSocketAddress
29+
import java.net.Proxy
2330

2431
class TelegramMessengerModule(
2532
coreModule: CoreModule,
2633
onlinePlayersProvider: OnlinePlayersProvider,
2734
linkModule: LinkModule,
2835
) : Logger by JUtiltLogger("MessageBridge-TelegramModule") {
2936

30-
private val telegramClientFlow = coreModule.configKrate.cachedStateFlow
31-
.mapCached<PluginConfiguration, OkHttpTelegramClient>(
32-
scope = coreModule.ioScope,
33-
transform = { config, _ ->
34-
val tgConfig = config.tgConfig
35-
val client = OkHttpTelegramClient(tgConfig.token)
36-
info { "#telegramClientFlow telegram client created!" }
37-
client
37+
private val okHttpClient = coreModule.configKrate.cachedStateFlow
38+
.map { pluginConfiguration -> pluginConfiguration.tgConfig.proxy }
39+
.distinctUntilChanged()
40+
.map { proxy ->
41+
if (proxy == null) {
42+
OkHttpClient.Builder().build()
43+
} else {
44+
OkHttpClient
45+
.Builder()
46+
.proxy(Proxy(Proxy.Type.HTTP, InetSocketAddress(proxy.host, proxy.port)))
47+
.proxyAuthenticator { route, response ->
48+
var builder = response.request.newBuilder()
49+
if (route?.socketAddress?.hostString == proxy.host) {
50+
val credential: String = Credentials.basic(proxy.username, proxy.password)
51+
builder = builder.header("Proxy-Authorization", credential)
52+
}
53+
builder.build()
54+
}
55+
.build()
3856
}
39-
)
57+
}
58+
.shareIn(coreModule.ioScope, SharingStarted.Lazily, 1)
59+
60+
private val telegramClientFlow = coreModule.configKrate
61+
.cachedStateFlow
62+
.map { tgConfig -> tgConfig.tgConfig }
63+
.distinctUntilChanged()
64+
.combine(okHttpClient) { tgConfig, okHttpClient ->
65+
OkHttpTelegramClient(
66+
okHttpClient,
67+
tgConfig.token
68+
)
69+
}
70+
.shareIn(coreModule.ioScope, SharingStarted.Eagerly, 1)
4071

4172
private val telegramMessageController = TelegramBEventConsumer(
4273
configKrate = coreModule.configKrate,

0 commit comments

Comments
 (0)