mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-08-17 11:00:07 +00:00
151 lines
5.5 KiB
Swift
151 lines
5.5 KiB
Swift
import Foundation
|
|
#if os(macOS)
|
|
import PostboxMac
|
|
import SwiftSignalKitMac
|
|
import MtProtoKitMac
|
|
#else
|
|
import Postbox
|
|
import SwiftSignalKit
|
|
import MtProtoKitDynamic
|
|
#endif
|
|
|
|
public struct ProxyServerSettings: PostboxCoding, Equatable, Hashable {
|
|
public let host: String
|
|
public let port: Int32
|
|
public let username: String?
|
|
public let password: String?
|
|
|
|
public init(host: String, port: Int32, username: String?, password: String?) {
|
|
self.host = host
|
|
self.port = port
|
|
self.username = username
|
|
self.password = password
|
|
}
|
|
|
|
public init(decoder: PostboxDecoder) {
|
|
self.host = decoder.decodeStringForKey("host", orElse: "")
|
|
self.port = decoder.decodeInt32ForKey("port", orElse: 0)
|
|
self.username = decoder.decodeOptionalStringForKey("username")
|
|
self.password = decoder.decodeOptionalStringForKey("password")
|
|
}
|
|
|
|
public func encode(_ encoder: PostboxEncoder) {
|
|
encoder.encodeString(self.host, forKey: "host")
|
|
encoder.encodeInt32(self.port, forKey: "port")
|
|
if let username = self.username {
|
|
encoder.encodeString(username, forKey: "username")
|
|
} else {
|
|
encoder.encodeNil(forKey: "username")
|
|
}
|
|
if let password = self.password {
|
|
encoder.encodeString(password, forKey: "password")
|
|
} else {
|
|
encoder.encodeNil(forKey: "password")
|
|
}
|
|
}
|
|
|
|
public var hashValue: Int {
|
|
var hash = self.host.hashValue
|
|
hash = hash &* 31 &+ self.port.hashValue
|
|
if let username = self.username {
|
|
hash = hash &* 31 &+ username.hashValue
|
|
}
|
|
if let password = self.password {
|
|
hash = hash &* 31 &+ password.hashValue
|
|
}
|
|
return hash
|
|
}
|
|
}
|
|
|
|
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(enabled: false, servers: [], activeServer: nil, useForCalls: false)
|
|
}
|
|
|
|
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) {
|
|
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")
|
|
} else {
|
|
encoder.encodeNil(forKey: "activeServer")
|
|
}
|
|
encoder.encodeInt32(self.useForCalls ? 1 : 0, forKey: "useForCalls")
|
|
}
|
|
|
|
public func isEqual(to: PreferencesEntry) -> Bool {
|
|
guard let to = to as? ProxySettings else {
|
|
return false
|
|
}
|
|
|
|
return self == to
|
|
}
|
|
|
|
public 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<Void, NoError> {
|
|
return postbox.modify { modifier -> Void in
|
|
updateProxySettingsInteractively(modifier: modifier, network: network, f)
|
|
}
|
|
}
|
|
|
|
public func updateProxySettingsInteractively(modifier: Modifier, network: Network, _ f: @escaping (ProxySettings) -> ProxySettings) {
|
|
var updateNetwork = false
|
|
var updatedSettings: ProxySettings?
|
|
modifier.updatePreferencesEntry(key: PreferencesKeys.proxySettings, { current in
|
|
let previous = (current as? ProxySettings) ?? ProxySettings.defaultSettings
|
|
let updated = f(previous)
|
|
updatedSettings = updated
|
|
if updated.effectiveActiveServer != previous.effectiveActiveServer {
|
|
updateNetwork = true
|
|
}
|
|
return updated
|
|
})
|
|
|
|
if updateNetwork, let updatedSettings = updatedSettings {
|
|
network.context.updateApiEnvironment { current 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)
|
|
})
|
|
}
|
|
network.dropConnectionStatus()
|
|
}
|
|
}
|