2018-10-11 07:13:19 +00:00
|
|
|
//
|
|
|
|
// TransientStore.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 7/16/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
|
2019-03-22 18:09:13 +00:00
|
|
|
import TunnelKit
|
2018-10-11 07:13:19 +00:00
|
|
|
import SwiftyBeaver
|
|
|
|
|
|
|
|
private let log = SwiftyBeaver.self
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public class TransientStore {
|
2018-10-18 07:52:23 +00:00
|
|
|
private struct Keys {
|
|
|
|
static let didHandleSubreddit = "DidHandleSubreddit"
|
2019-03-22 18:04:33 +00:00
|
|
|
|
|
|
|
static let masksPrivateData = "MasksPrivateData"
|
2019-04-25 12:54:09 +00:00
|
|
|
|
|
|
|
// migrations
|
|
|
|
|
|
|
|
static let didMigrateHostsRoutingPolicies = "DidMigrateHostsRoutingPolicies"
|
2018-10-18 07:52:23 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public static let shared = TransientStore()
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2018-11-01 12:31:08 +00:00
|
|
|
private static var serviceURL: URL {
|
2019-03-10 13:44:36 +00:00
|
|
|
return GroupConstants.App.documentsURL.appendingPathComponent(AppConstants.Store.serviceFilename)
|
2018-11-01 12:31:08 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public let service: ConnectionService
|
2018-10-26 08:00:28 +00:00
|
|
|
|
2019-03-22 18:09:13 +00:00
|
|
|
public static var didHandleSubreddit: Bool {
|
2018-10-18 07:52:23 +00:00
|
|
|
get {
|
|
|
|
return UserDefaults.standard.bool(forKey: Keys.didHandleSubreddit)
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
UserDefaults.standard.set(newValue, forKey: Keys.didHandleSubreddit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-22 18:09:13 +00:00
|
|
|
public static var masksPrivateData: Bool {
|
2019-03-22 18:04:33 +00:00
|
|
|
get {
|
|
|
|
return UserDefaults.standard.bool(forKey: Keys.masksPrivateData)
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
UserDefaults.standard.set(newValue, forKey: Keys.masksPrivateData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-25 12:54:09 +00:00
|
|
|
public static var didMigrateHostsRoutingPolicies: Bool {
|
|
|
|
get {
|
|
|
|
return UserDefaults.standard.bool(forKey: Keys.didMigrateHostsRoutingPolicies)
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
UserDefaults.standard.set(newValue, forKey: Keys.didMigrateHostsRoutingPolicies)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-22 18:09:13 +00:00
|
|
|
public static var baseVPNConfiguration: TunnelKitProvider.ConfigurationBuilder {
|
2019-04-04 09:21:51 +00:00
|
|
|
let sessionBuilder = SessionProxy.ConfigurationBuilder()
|
2019-03-22 18:09:13 +00:00
|
|
|
var builder = TunnelKitProvider.ConfigurationBuilder(sessionConfiguration: sessionBuilder.build())
|
|
|
|
builder.mtu = 1250
|
|
|
|
builder.shouldDebug = true
|
|
|
|
// builder.debugLogFormat = "$Dyyyy-MM-dd HH:mm:ss.SSS$d $L $N.$F:$l - $M"
|
|
|
|
// builder.debugLogFormat = "$DHH:mm:ss$d $N.$F:$l - $M"
|
|
|
|
builder.debugLogFormat = AppConstants.Log.debugFormat
|
|
|
|
builder.masksPrivateData = masksPrivateData
|
|
|
|
return builder
|
|
|
|
}
|
|
|
|
|
2018-10-11 07:13:19 +00:00
|
|
|
private init() {
|
2019-03-22 18:04:33 +00:00
|
|
|
UserDefaults.standard.register(defaults: [
|
|
|
|
Keys.didHandleSubreddit: false,
|
|
|
|
Keys.masksPrivateData: true
|
|
|
|
])
|
|
|
|
|
2019-03-10 13:58:01 +00:00
|
|
|
TransientStore.migrateDocumentsToAppGroup()
|
2019-03-06 15:57:47 +00:00
|
|
|
|
|
|
|
// this must be graceful
|
|
|
|
ConnectionService.migrateJSON(from: TransientStore.serviceURL, to: TransientStore.serviceURL)
|
2019-04-25 12:54:09 +00:00
|
|
|
|
2019-03-22 18:09:13 +00:00
|
|
|
let cfg = TransientStore.baseVPNConfiguration.build()
|
2018-10-11 07:13:19 +00:00
|
|
|
do {
|
2018-11-01 12:31:08 +00:00
|
|
|
let data = try Data(contentsOf: TransientStore.serviceURL)
|
2018-10-11 07:13:19 +00:00
|
|
|
if let content = String(data: data, encoding: .utf8) {
|
|
|
|
log.verbose("Service JSON:")
|
|
|
|
log.verbose(content)
|
|
|
|
}
|
|
|
|
service = try JSONDecoder().decode(ConnectionService.self, from: data)
|
2018-10-25 16:42:11 +00:00
|
|
|
service.baseConfiguration = cfg
|
2018-10-26 08:00:28 +00:00
|
|
|
service.loadProfiles()
|
2019-04-25 12:54:09 +00:00
|
|
|
|
|
|
|
// do migrations
|
|
|
|
if !TransientStore.didMigrateHostsRoutingPolicies {
|
|
|
|
if service.reloadHostProfilesFromConfigurationFiles() {
|
|
|
|
service.saveProfiles()
|
|
|
|
}
|
|
|
|
TransientStore.didMigrateHostsRoutingPolicies = true
|
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
} catch let e {
|
|
|
|
log.error("Could not decode service: \(e)")
|
|
|
|
service = ConnectionService(
|
|
|
|
withAppGroup: GroupConstants.App.appGroup,
|
2018-10-25 16:42:11 +00:00
|
|
|
baseConfiguration: cfg
|
2018-10-11 07:13:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// // hardcoded loading
|
|
|
|
// _ = service.addProfile(ProviderConnectionProfile(name: .pia), credentials: nil)
|
|
|
|
// _ = service.addProfile(HostConnectionProfile(title: "vps"), credentials: Credentials(username: "foo", password: "bar"))
|
|
|
|
// service.activateProfile(service.profiles.first!)
|
|
|
|
}
|
2019-03-29 10:17:50 +00:00
|
|
|
service.observeVPNDataCount(interval: TimeInterval(GroupConstants.VPN.dataCountInterval) / 1000.0)
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public func serialize(withProfiles: Bool) {
|
2018-11-01 12:31:08 +00:00
|
|
|
try? JSONEncoder().encode(service).write(to: TransientStore.serviceURL)
|
2018-11-04 13:35:12 +00:00
|
|
|
if withProfiles {
|
|
|
|
service.saveProfiles()
|
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
2019-03-10 13:58:01 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
private static func migrateDocumentsToAppGroup() {
|
2019-03-11 09:09:35 +00:00
|
|
|
var hasMigrated = false
|
2019-03-10 13:58:01 +00:00
|
|
|
let oldDocumentsURL = FileManager.default.userURL(for: .documentDirectory, appending: nil)
|
|
|
|
let newDocumentsURL = GroupConstants.App.documentsURL
|
|
|
|
log.debug("App documentsURL: \(oldDocumentsURL)")
|
|
|
|
log.debug("Group documentsURL: \(newDocumentsURL)")
|
|
|
|
let fm = FileManager.default
|
|
|
|
do {
|
|
|
|
for c in try fm.contentsOfDirectory(atPath: oldDocumentsURL.path) {
|
2019-03-11 09:12:16 +00:00
|
|
|
guard c != "Inbox" else {
|
|
|
|
continue
|
|
|
|
}
|
2019-03-10 13:58:01 +00:00
|
|
|
let old = oldDocumentsURL.appendingPathComponent(c)
|
|
|
|
let new = newDocumentsURL.appendingPathComponent(c)
|
|
|
|
log.verbose("Move:")
|
|
|
|
log.verbose("\tFROM: \(old)")
|
|
|
|
log.verbose("\tTO: \(new)")
|
|
|
|
try fm.moveItem(at: old, to: new)
|
2019-03-11 09:09:35 +00:00
|
|
|
hasMigrated = true
|
2019-03-10 13:58:01 +00:00
|
|
|
}
|
|
|
|
} catch let e {
|
2019-03-11 09:09:35 +00:00
|
|
|
hasMigrated = false
|
2019-03-10 13:58:01 +00:00
|
|
|
log.error("Could not migrate documents to App Group: \(e)")
|
|
|
|
}
|
2019-03-11 09:09:35 +00:00
|
|
|
if hasMigrated {
|
|
|
|
log.debug("Documents migrated to App Group")
|
|
|
|
}
|
2019-03-10 13:58:01 +00:00
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|