From b2821404054900b127c5ea7c11f2f7d09adb39d0 Mon Sep 17 00:00:00 2001 From: givip Date: Mon, 20 Aug 2018 23:27:45 +0300 Subject: [PATCH] Added check for existing webhooks certificate and removed private cert property as unnesessary --- Sources/EchoBot/main.swift | 1 - Sources/HelloBot/main.swift | 1 - Sources/Telegrammer/Bot/Bot.swift | 1 - Sources/Telegrammer/Network/Webhooks.swift | 17 ++++++++++------- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Sources/EchoBot/main.swift b/Sources/EchoBot/main.swift index 7c419d1..86cce59 100644 --- a/Sources/EchoBot/main.swift +++ b/Sources/EchoBot/main.swift @@ -14,7 +14,6 @@ var settings = Bot.Settings(token: token) //settings.webhooksUrl = Enviroment.get("TELEGRAM_BOT_WEBHOOK_URL")! //settings.webhooksPort = Int(Enviroment.get("TELEGRAM_BOT_PORT")!)! //settings.webhooksPublicCert = Enviroment.get("TELEGRAM_BOT_PUBLIC_KEY")! -//settings.webhooksPrivateKey = Enviroment.get("TELEGRAM_BOT_PRIVATE_KEY")! let bot = try! Bot(settings: settings) diff --git a/Sources/HelloBot/main.swift b/Sources/HelloBot/main.swift index 1fe2f7b..9262ece 100644 --- a/Sources/HelloBot/main.swift +++ b/Sources/HelloBot/main.swift @@ -13,7 +13,6 @@ var settings = Bot.Settings(token: token, debugMode: true) //settings.webhooksUrl = Enviroment.get("TELEGRAM_BOT_WEBHOOK_URL")! //settings.webhooksPort = Int(Enviroment.get("TELEGRAM_BOT_PORT")!)! //settings.webhooksPublicCert = Enviroment.get("TELEGRAM_BOT_PUBLIC_KEY")! -//settings.webhooksPrivateKey = Enviroment.get("TELEGRAM_BOT_PRIVATE_KEY")! let bot = try! Bot(settings: settings) diff --git a/Sources/Telegrammer/Bot/Bot.swift b/Sources/Telegrammer/Bot/Bot.swift index 40206e7..52911f3 100644 --- a/Sources/Telegrammer/Bot/Bot.swift +++ b/Sources/Telegrammer/Bot/Bot.swift @@ -22,7 +22,6 @@ public final class Bot { public var webhooksUrl: String? = nil public var webhooksPort: Int? = nil public var webhooksPublicCert: String? = nil - public var webhooksPrivateKey: String? = nil public init(token: String, debugMode: Bool = true) { self.token = token diff --git a/Sources/Telegrammer/Network/Webhooks.swift b/Sources/Telegrammer/Network/Webhooks.swift index 30c68ee..5ec2353 100644 --- a/Sources/Telegrammer/Network/Webhooks.swift +++ b/Sources/Telegrammer/Network/Webhooks.swift @@ -34,19 +34,22 @@ class Webhooks: Connection { public func start() throws -> Future { guard let ip = bot.settings.webhooksIp, let url = bot.settings.webhooksUrl, - let port = bot.settings.webhooksPort, - let publicCert = bot.settings.webhooksPublicCert else { + let port = bot.settings.webhooksPort else { throw CoreError(identifier: "Webhooks", reason: "Initialization parameters wasn't found in enviroment variables") } - guard let fileHandle = FileHandle(forReadingAtPath: publicCert) else { - let errorDescription = "Public key '\(publicCert)' for HTTPS server wasn't found" - Log.error(errorDescription) - throw CoreError(identifier: "FileIO", reason: errorDescription) + var cert: InputFile? = nil + + if let publicCert = bot.settings.webhooksPublicCert { + guard let fileHandle = FileHandle(forReadingAtPath: publicCert) else { + let errorDescription = "Public key '\(publicCert)' was specified for HTTPS server, but wasn't found" + Log.error(errorDescription) + throw CoreError(identifier: "FileIO", reason: errorDescription) + } + cert = InputFile(data: fileHandle.readDataToEndOfFile(), filename: publicCert) } - let cert = InputFile(data: fileHandle.readDataToEndOfFile(), filename: publicCert) let params = Bot.SetWebhookParams(url: url, certificate: cert, maxConnections: maxConnections, allowedUpdates: nil) return try bot.setWebhook(params: params).flatMap { (success) -> Future in Log.info("setWebhook request result: \(success)")