passepartout-apple/Passepartout/Sources/Services/InfrastructurePreset.swift

141 lines
6.3 KiB
Swift
Raw Normal View History

2018-10-11 07:13:19 +00:00
//
// InfrastructurePreset.swift
// Passepartout
//
// Created by Davide De Rosa on 8/30/18.
2019-03-09 10:44:44 +00:00
// Copyright (c) 2019 Davide De Rosa. All rights reserved.
2018-10-11 07:13:19 +00:00
//
2018-11-03 21:33:30 +00:00
// https://github.com/passepartoutvpn
2018-10-11 07:13:19 +00:00
//
// This file is part of Passepartout.
//
// Passepartout is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Passepartout is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
//
import Foundation
import TunnelKit
// supports a subset of TunnelKitProvider.Configuration
// ignores new JSON keys
public struct InfrastructurePreset: Codable {
public enum PresetKeys: String, CodingKey {
2018-10-11 07:13:19 +00:00
case id
case name
case comment
case configuration = "cfg"
}
public enum ConfigurationKeys: String, CodingKey {
2018-10-11 07:13:19 +00:00
case endpointProtocols = "ep"
case cipher
case digest = "auth"
case ca
case clientCertificate = "client"
case clientKey = "key"
case compressionFraming = "frame"
2019-03-25 22:39:23 +00:00
case compressionAlgorithm = "compression"
2018-10-11 07:13:19 +00:00
case keepAliveSeconds = "ping"
case renegotiatesAfterSeconds = "reneg"
2018-10-18 11:00:58 +00:00
2019-03-23 15:15:22 +00:00
case tlsWrap = "wrap"
2018-10-18 11:00:58 +00:00
case usesPIAPatches = "pia"
2019-03-25 09:34:57 +00:00
2019-03-25 23:11:11 +00:00
case checksEKU = "eku"
2019-03-25 09:34:57 +00:00
case randomizeEndpoint = "random"
2018-10-11 07:13:19 +00:00
}
public let id: String
2018-10-11 07:13:19 +00:00
public let name: String
2018-10-11 07:13:19 +00:00
public let comment: String
2018-10-11 07:13:19 +00:00
public let configuration: TunnelKitProvider.Configuration
2018-10-11 07:13:19 +00:00
public func hasProtocol(_ proto: EndpointProtocol) -> Bool {
2018-10-11 07:13:19 +00:00
return configuration.endpointProtocols.index(of: proto) != nil
}
// MARK: Codable
public init(from decoder: Decoder) throws {
2018-10-11 07:13:19 +00:00
let container = try decoder.container(keyedBy: PresetKeys.self)
id = try container.decode(String.self, forKey: .id)
name = try container.decode(String.self, forKey: .name)
comment = try container.decode(String.self, forKey: .comment)
let cfgContainer = try container.nestedContainer(keyedBy: ConfigurationKeys.self, forKey: .configuration)
let ca = try cfgContainer.decode(CryptoContainer.self, forKey: .ca)
var sessionBuilder = SessionProxy.ConfigurationBuilder(ca: ca)
sessionBuilder.cipher = try cfgContainer.decode(SessionProxy.Cipher.self, forKey: .cipher)
2018-10-11 07:13:19 +00:00
if let digest = try cfgContainer.decodeIfPresent(SessionProxy.Digest.self, forKey: .digest) {
sessionBuilder.digest = digest
2018-10-11 07:13:19 +00:00
}
sessionBuilder.clientCertificate = try cfgContainer.decodeIfPresent(CryptoContainer.self, forKey: .clientCertificate)
sessionBuilder.clientKey = try cfgContainer.decodeIfPresent(CryptoContainer.self, forKey: .clientKey)
sessionBuilder.compressionFraming = try cfgContainer.decode(SessionProxy.CompressionFraming.self, forKey: .compressionFraming)
2019-03-25 22:39:23 +00:00
sessionBuilder.compressionAlgorithm = try cfgContainer.decodeIfPresent(SessionProxy.CompressionAlgorithm.self, forKey: .compressionAlgorithm) ?? .disabled
2019-03-23 15:15:22 +00:00
sessionBuilder.tlsWrap = try cfgContainer.decodeIfPresent(SessionProxy.TLSWrap.self, forKey: .tlsWrap)
sessionBuilder.keepAliveInterval = try cfgContainer.decodeIfPresent(TimeInterval.self, forKey: .keepAliveSeconds)
sessionBuilder.renegotiatesAfter = try cfgContainer.decodeIfPresent(TimeInterval.self, forKey: .renegotiatesAfterSeconds)
sessionBuilder.usesPIAPatches = try cfgContainer.decodeIfPresent(Bool.self, forKey: .usesPIAPatches) ?? false
2019-03-25 23:11:11 +00:00
sessionBuilder.checksEKU = try cfgContainer.decodeIfPresent(Bool.self, forKey: .checksEKU) ?? false
2019-03-25 09:34:57 +00:00
sessionBuilder.randomizeEndpoint = try cfgContainer.decodeIfPresent(Bool.self, forKey: .randomizeEndpoint) ?? false
var builder = TunnelKitProvider.ConfigurationBuilder(sessionConfiguration: sessionBuilder.build())
2018-11-10 09:29:51 +00:00
builder.endpointProtocols = try cfgContainer.decode([EndpointProtocol].self, forKey: .endpointProtocols)
2018-10-11 07:13:19 +00:00
configuration = builder.build()
}
public func encode(to encoder: Encoder) throws {
2018-10-11 07:13:19 +00:00
var container = encoder.container(keyedBy: PresetKeys.self)
try container.encode(id, forKey: .id)
try container.encode(name, forKey: .name)
try container.encode(comment, forKey: .comment)
var cfgContainer = container.nestedContainer(keyedBy: ConfigurationKeys.self, forKey: .configuration)
try cfgContainer.encode(configuration.endpointProtocols, forKey: .endpointProtocols)
try cfgContainer.encode(configuration.sessionConfiguration.cipher, forKey: .cipher)
try cfgContainer.encode(configuration.sessionConfiguration.digest, forKey: .digest)
try cfgContainer.encodeIfPresent(configuration.sessionConfiguration.ca, forKey: .ca)
try cfgContainer.encodeIfPresent(configuration.sessionConfiguration.clientCertificate, forKey: .clientCertificate)
try cfgContainer.encodeIfPresent(configuration.sessionConfiguration.clientKey, forKey: .clientKey)
try cfgContainer.encode(configuration.sessionConfiguration.compressionFraming, forKey: .compressionFraming)
2019-03-25 22:39:23 +00:00
try cfgContainer.encodeIfPresent(configuration.sessionConfiguration.compressionAlgorithm, forKey: .compressionAlgorithm)
2019-03-23 15:15:22 +00:00
try cfgContainer.encodeIfPresent(configuration.sessionConfiguration.tlsWrap, forKey: .tlsWrap)
try cfgContainer.encodeIfPresent(configuration.sessionConfiguration.keepAliveInterval, forKey: .keepAliveSeconds)
try cfgContainer.encodeIfPresent(configuration.sessionConfiguration.renegotiatesAfter, forKey: .renegotiatesAfterSeconds)
try cfgContainer.encodeIfPresent(configuration.sessionConfiguration.usesPIAPatches, forKey: .usesPIAPatches)
2019-03-25 23:11:11 +00:00
try cfgContainer.encodeIfPresent(configuration.sessionConfiguration.checksEKU, forKey: .checksEKU)
2019-03-25 09:34:57 +00:00
try cfgContainer.encodeIfPresent(configuration.sessionConfiguration.randomizeEndpoint, forKey: .randomizeEndpoint)
2018-10-11 07:13:19 +00:00
}
}