2019-01-24 00:37:39 +00:00
|
|
|
package com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor
|
|
|
|
|
2019-01-24 03:49:27 +00:00
|
|
|
import com.github.insanusmokrassar.TelegramBotAPI.bot.settings.ProxySettings
|
|
|
|
import io.ktor.http.HttpHeaders
|
2019-01-24 00:37:39 +00:00
|
|
|
import okhttp3.Credentials
|
|
|
|
import okhttp3.OkHttpClient
|
|
|
|
import java.net.InetSocketAddress
|
|
|
|
import java.net.Proxy
|
|
|
|
|
|
|
|
fun OkHttpClient.Builder.useWith(proxySettings: ProxySettings) {
|
|
|
|
proxy(
|
|
|
|
Proxy(
|
|
|
|
Proxy.Type.SOCKS,
|
|
|
|
InetSocketAddress(
|
|
|
|
proxySettings.host,
|
|
|
|
proxySettings.port
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
proxySettings.password ?.let {
|
|
|
|
password ->
|
|
|
|
proxyAuthenticator {
|
|
|
|
_, response ->
|
|
|
|
response.request().newBuilder().apply {
|
|
|
|
addHeader(
|
2019-01-24 03:49:27 +00:00
|
|
|
HttpHeaders.ProxyAuthorization,
|
2019-01-24 00:37:39 +00:00
|
|
|
Credentials.basic(proxySettings.username ?: "", password)
|
|
|
|
)
|
|
|
|
}.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|