1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-12-25 18:17:13 +00:00

add private key config for webhook and opportunity to work with ssl directly in bot

This commit is contained in:
InsanusMokrassar 2019-03-06 12:23:28 +08:00
parent 336e76450f
commit 49fb020678
2 changed files with 46 additions and 2 deletions

View File

@ -0,0 +1,25 @@
package com.github.insanusmokrassar.TelegramBotAPI.utils.extensions
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import java.io.FileInputStream
import java.security.KeyStore
@Serializable
data class WebhookPrivateKeyConfig(
@SerialName("keyStore")
private val keyStorePath: String,
private val keyStorePassword: String,
val aliasName: String,
private val aliasPassword: String
) {
@Transient
val keyStore = KeyStore.getInstance("JKS").apply {
load(FileInputStream(keyStorePath), keyStorePassword())
}
fun keyStorePassword(): CharArray = keyStorePassword.toCharArray()
fun aliasPassword(): CharArray = aliasPassword.toCharArray()
}

View File

@ -17,9 +17,13 @@ import io.ktor.server.netty.Netty
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.serialization.json.Json
import java.io.FileInputStream
import java.security.KeyStore
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
/**
* Reverse proxy webhook.
*
@ -33,6 +37,7 @@ suspend fun RequestsExecutor.setWebhook(
url: String,
port: Int,
certificate: InputFile? = null,
privateKeyConfig: WebhookPrivateKeyConfig? = null,
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
allowedUpdates: List<String>? = null,
maxAllowedConnections: Int? = null,
@ -62,6 +67,7 @@ suspend fun RequestsExecutor.setWebhook(
scope = scope
)
val env = applicationEngineEnvironment {
module {
fun Application.main() {
routing {
@ -78,10 +84,21 @@ suspend fun RequestsExecutor.setWebhook(
}
main()
}
connector {
host = "0.0.0.0"
privateKeyConfig ?.let {
sslConnector(
privateKeyConfig.keyStore,
privateKeyConfig.aliasName,
privateKeyConfig::keyStorePassword,
privateKeyConfig::aliasPassword
) {
host = "0.0.0.0"
this.port = port
}
} ?: connector {
host = "localhost"
this.port = port
}
}
val engine = embeddedServer(engineFactory, env)
@ -120,6 +137,7 @@ suspend fun RequestsExecutor.setWebhook(
port: Int,
filter: UpdatesFilter,
certificate: InputFile? = null,
privateKeyConfig: WebhookPrivateKeyConfig? = null,
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
maxAllowedConnections: Int? = null,
engineFactory: ApplicationEngineFactory<*, *> = Netty
@ -127,6 +145,7 @@ suspend fun RequestsExecutor.setWebhook(
url,
port,
certificate,
privateKeyConfig,
scope,
filter.allowedUpdates,
maxAllowedConnections,