From e6721adc80ecdd47fd67c9e5d4091d975751c174 Mon Sep 17 00:00:00 2001 From: Peter Iakovlev Date: Fri, 20 Apr 2018 14:24:17 +0400 Subject: [PATCH] no message --- TelegramCore/Network.swift | 2 +- TelegramCore/ProxySettings.swift | 33 +++++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/TelegramCore/Network.swift b/TelegramCore/Network.swift index 73f9bd7cac..5b49950e4b 100644 --- a/TelegramCore/Network.swift +++ b/TelegramCore/Network.swift @@ -336,7 +336,7 @@ func initializedNetwork(arguments: NetworkInitializationArguments, supplementary apiEnvironment.disableUpdates = supplementary apiEnvironment = apiEnvironment.withUpdatedLangPackCode(languageCode ?? "en") - if let activeServer = proxySettings?.activeServer { + if let effectiveActiveServer = proxySettings?.effectiveActiveServer { apiEnvironment = apiEnvironment.withUpdatedSocksProxySettings(MTSocksProxySettings(ip: activeServer.host, port: UInt16(activeServer.port), username: activeServer.username, password: activeServer.password)) } diff --git a/TelegramCore/ProxySettings.swift b/TelegramCore/ProxySettings.swift index 4ac4a08cef..fde7cbbcee 100644 --- a/TelegramCore/ProxySettings.swift +++ b/TelegramCore/ProxySettings.swift @@ -46,27 +46,42 @@ public struct ProxyServerSettings: PostboxCoding, Equatable { } public struct ProxySettings: PreferencesEntry, Equatable { + public var enabled: Bool public var servers: [ProxyServerSettings] public var activeServer: ProxyServerSettings? public var useForCalls: Bool public static var defaultSettings: ProxySettings { - return ProxySettings(servers: [], activeServer: nil, useForCalls: false) + return ProxySettings(enabled: false, servers: [], activeServer: nil, useForCalls: false) } - public init(servers: [ProxyServerSettings], activeServer: ProxyServerSettings?, useForCalls: Bool) { + public init(enabled: Bool, servers: [ProxyServerSettings], activeServer: ProxyServerSettings?, useForCalls: Bool) { + self.enabled = enabled self.servers = servers self.activeServer = activeServer self.useForCalls = useForCalls } public init(decoder: PostboxDecoder) { - self.servers = decoder.decodeObjectArrayWithDecoderForKey("servers") + if let _ = decoder.decodeOptionalStringForKey("server") { + let legacyServer = ProxyServerSettings(decoder: decoder) + if !legacyServer.host.isEmpty && legacyServer.port != 0 { + self.enabled = true + self.servers = [legacyServer] + } else { + self.enabled = false + self.servers = [] + } + } else { + self.enabled = decoder.decodeInt32ForKey("enabled", orElse: 0) != 0 + self.servers = decoder.decodeObjectArrayWithDecoderForKey("servers") + } self.activeServer = decoder.decodeObjectForKey("activeServer", decoder: ProxyServerSettings.init(decoder:)) as? ProxyServerSettings self.useForCalls = decoder.decodeInt32ForKey("useForCalls", orElse: 0) != 0 } public func encode(_ encoder: PostboxEncoder) { + encoder.encodeInt32(self.enabled ? 1 : 0, forKey: "enabled") encoder.encodeObjectArray(self.servers, forKey: "servers") if let activeServer = self.activeServer { encoder.encodeObject(activeServer, forKey: "activeServer") @@ -83,6 +98,14 @@ public struct ProxySettings: PreferencesEntry, Equatable { return self == to } + + var effectiveActiveServer: ProxyServerSettings? { + if self.enabled, let activeServer = self.activeServer { + return activeServer + } else { + return nil + } + } } public func updateProxySettingsInteractively(postbox: Postbox, network: Network, _ f: @escaping (ProxySettings) -> ProxySettings) -> Signal { @@ -93,7 +116,7 @@ public func updateProxySettingsInteractively(postbox: Postbox, network: Network, let previous = (current as? ProxySettings) ?? ProxySettings.defaultSettings let updated = f(previous) updatedSettings = updated - if updated.activeServer != previous.activeServer { + if updated.effectiveActiveServer != previous.effectiveActiveServer { updateNetwork = true } return updated @@ -101,7 +124,7 @@ public func updateProxySettingsInteractively(postbox: Postbox, network: Network, if updateNetwork, let updatedSettings = updatedSettings { network.context.updateApiEnvironment { current in - return current?.withUpdatedSocksProxySettings(updatedSettings.activeServer.flatMap { activeServer -> MTSocksProxySettings? in + return current?.withUpdatedSocksProxySettings(updatedSettings.effectiveActiveServer.flatMap { activeServer -> MTSocksProxySettings? in return MTSocksProxySettings(ip: activeServer.host, port: UInt16(activeServer.port), username: activeServer.username, password: activeServer.password) }) }