1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-25 11:38:45 +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.*
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import java.io.FileInputStream
import java.security.KeyStore
import java.util.concurrent.Executors import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
/** /**
* Reverse proxy webhook. * Reverse proxy webhook.
* *
@ -33,6 +37,7 @@ suspend fun RequestsExecutor.setWebhook(
url: String, url: String,
port: Int, port: Int,
certificate: InputFile? = null, certificate: InputFile? = null,
privateKeyConfig: WebhookPrivateKeyConfig? = null,
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
allowedUpdates: List<String>? = null, allowedUpdates: List<String>? = null,
maxAllowedConnections: Int? = null, maxAllowedConnections: Int? = null,
@ -62,6 +67,7 @@ suspend fun RequestsExecutor.setWebhook(
scope = scope scope = scope
) )
val env = applicationEngineEnvironment { val env = applicationEngineEnvironment {
module { module {
fun Application.main() { fun Application.main() {
routing { routing {
@ -78,10 +84,21 @@ suspend fun RequestsExecutor.setWebhook(
} }
main() main()
} }
connector { privateKeyConfig ?.let {
host = "0.0.0.0" sslConnector(
privateKeyConfig.keyStore,
privateKeyConfig.aliasName,
privateKeyConfig::keyStorePassword,
privateKeyConfig::aliasPassword
) {
host = "0.0.0.0"
this.port = port
}
} ?: connector {
host = "localhost"
this.port = port this.port = port
} }
} }
val engine = embeddedServer(engineFactory, env) val engine = embeddedServer(engineFactory, env)
@ -120,6 +137,7 @@ suspend fun RequestsExecutor.setWebhook(
port: Int, port: Int,
filter: UpdatesFilter, filter: UpdatesFilter,
certificate: InputFile? = null, certificate: InputFile? = null,
privateKeyConfig: WebhookPrivateKeyConfig? = null,
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
maxAllowedConnections: Int? = null, maxAllowedConnections: Int? = null,
engineFactory: ApplicationEngineFactory<*, *> = Netty engineFactory: ApplicationEngineFactory<*, *> = Netty
@ -127,6 +145,7 @@ suspend fun RequestsExecutor.setWebhook(
url, url,
port, port,
certificate, certificate,
privateKeyConfig,
scope, scope,
filter.allowedUpdates, filter.allowedUpdates,
maxAllowedConnections, maxAllowedConnections,