Skip to content

Commit 07fc280

Browse files
Merge pull request #122 from InsanusMokrassar/11.1.0
11.1.0
2 parents 57abe22 + 7cb5490 commit 07fc280

File tree

8 files changed

+84
-3
lines changed

8 files changed

+84
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 11.1.0
4+
5+
* `Bot`:
6+
* Add opportunity to setup proxy
7+
38
## 11.0.0
49

510
* `Versions`:

bot/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ dependencies {
2222
api libs.microutils.koin
2323
api libs.microutils.startup.launcher
2424

25+
api libs.ktor.engine.okhttp
26+
2527
api libs.sqlite
2628

2729
testImplementation libs.kt.test.junit

bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package dev.inmo.plagubot
33
import dev.inmo.kslog.common.*
44
import dev.inmo.micro_utils.common.Warning
55
import dev.inmo.micro_utils.coroutines.runCatchingLogging
6-
import dev.inmo.micro_utils.coroutines.runCatchingSafely
76
import dev.inmo.micro_utils.fsm.common.State
87
import dev.inmo.micro_utils.fsm.common.StatesManager
98
import dev.inmo.micro_utils.fsm.common.managers.*
@@ -16,6 +15,10 @@ import dev.inmo.tgbotapi.bot.ktor.telegramBot
1615
import dev.inmo.tgbotapi.extensions.api.webhook.deleteWebhook
1716
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
1817
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.startGettingOfUpdatesByLongPolling
18+
import io.ktor.client.HttpClient
19+
import io.ktor.client.engine.HttpClientEngine
20+
import io.ktor.client.engine.HttpClientEngineFactory
21+
import io.ktor.client.engine.okhttp.OkHttp
1922
import kotlinx.coroutines.*
2023
import kotlinx.serialization.Serializable
2124
import kotlinx.serialization.json.*
@@ -58,6 +61,19 @@ object PlaguBot : Plugin {
5861
* @param params Raw JSON params of the bot part of the configuration
5962
*/
6063
override fun KtorRequestsExecutorBuilder.setupBotClient(scope: Scope, params: JsonObject) {
64+
val config = scope.get<Config>()
65+
if (config.proxy != null) {
66+
val initialClient = config.proxy.createDefaultClient()
67+
val clientFromHttpClientEngine = scope.getOrNull<HttpClientEngine>() ?.let {
68+
HttpClient(it)
69+
}
70+
val clientFromKoin = clientFromHttpClientEngine ?: (scope.getOrNull<HttpClientEngineFactory<*>>() ?: OkHttp).let {
71+
HttpClient(it)
72+
}
73+
this@setupBotClient.client = initialClient.config {
74+
install(clientFromKoin)
75+
}
76+
}
6177
scope.plugins.filter { it !== this@PlaguBot }.forEach {
6278
with(it) {
6379
setupBotClient(scope, params)

bot/src/main/kotlin/dev/inmo/plagubot/config/Config.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ data class Config(
1414
@SerialName("database")
1515
val databaseConfig: DatabaseConfig = DatabaseConfig(),
1616
val botApiServer: String = telegramBotAPIDefaultUrl,
17-
val testServer: Boolean = false
17+
val testServer: Boolean = false,
18+
val proxy: ProxyConfig? = null,
1819
)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package dev.inmo.plagubot.config
2+
3+
import io.ktor.client.*
4+
import io.ktor.client.engine.okhttp.OkHttp
5+
import io.ktor.client.plugins.*
6+
import io.ktor.client.request.*
7+
import io.ktor.http.*
8+
import kotlinx.serialization.Serializable
9+
import kotlinx.serialization.Transient
10+
import java.net.Authenticator
11+
import java.net.InetSocketAddress
12+
import java.net.PasswordAuthentication
13+
import java.net.Proxy
14+
import kotlin.io.encoding.Base64
15+
16+
@Serializable
17+
data class ProxyConfig(
18+
val host: String,
19+
val port: Int,
20+
val username: String? = null,
21+
val password: String? = null
22+
) {
23+
@Transient
24+
private val proxy = Proxy(Proxy.Type.SOCKS, InetSocketAddress(host, port))
25+
fun createDefaultClient() = HttpClient(OkHttp) {
26+
engine {
27+
config {
28+
proxy(this@ProxyConfig.proxy)
29+
if (username != null && password != null) {
30+
val passwordAuthentication = PasswordAuthentication(
31+
username,
32+
password.toCharArray()
33+
)
34+
Authenticator.setDefault(object : Authenticator() {
35+
override fun getPasswordAuthentication(): PasswordAuthentication? {
36+
return if (requestingHost.lowercase() == host.lowercase()) {
37+
passwordAuthentication
38+
} else {
39+
null
40+
}
41+
}
42+
})
43+
}
44+
}
45+
}
46+
}
47+
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ kotlin.js.generate.externals=true
55
kotlin.incremental=true
66

77
group=dev.inmo
8-
version=11.0.0
8+
version=11.1.0

gradle/libs.versions.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ kt-coroutines = "1.10.2"
66

77
microutils = "0.29.1"
88
tgbotapi = "32.0.0"
9+
ktor = "3.4.1"
910

1011
ksp = "2.3.6"
1112

@@ -35,6 +36,8 @@ microutils-koin-generator = { module = "dev.inmo:micro_utils.koin.generator", ve
3536
microutils-startup-launcher = { module = "dev.inmo:micro_utils.startup.launcher", version.ref = "microutils" }
3637
microutils-startup-plugin = { module = "dev.inmo:micro_utils.startup.plugin", version.ref = "microutils" }
3738

39+
ktor-engine-okhttp = { module = "io.ktor:ktor-client-okhttp", version.ref = "ktor" }
40+
3841
koin = { module = "io.insert-koin:koin-core", version.ref = "koin" }
3942

4043
jb-exposed-jdbc = { module = "org.jetbrains.exposed:exposed-jdbc", version.ref = "jb-exposed" }

template.config.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
}
1111
},
1212
"botToken": "1234567890:ABCDEFGHIJKLMNOP_qrstuvwxyz12345678",
13+
"proxy": {
14+
"host": "127.0.0.1",
15+
"port": 1080,
16+
"username": "OPTIONAL username",
17+
"password": "OPTIONAL password",
18+
"_note": "THIS OBJECT IS OPTIONAL"
19+
},
1320
"plugins": [
1421
"dev.inmo.plagubot.HelloPlugin"
1522
],

0 commit comments

Comments
 (0)