Bootstrapper.swift 5.87 KB
Newer Older
Davide De Rosa's avatar
Davide De Rosa committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
//
//  Bootstrapper.swift
//  PIA VPN
//
//  Created by Davide De Rosa on 12/4/17.
//  Copyright © 2017 London Trust Media. All rights reserved.
//

import Foundation
import PIALibrary
import PIATunnel
import SwiftyBeaver
import iRate
#if PIA_DEV
import HockeySDK
#endif

class Bootstrapper {
    static let shared = Bootstrapper()
    
    private init() {
    }
    
    private var isSimulator: Bool {
        #if arch(i386) || arch(x86_64)
            return true
        #else
            return false
        #endif
    }

    func bootstrap() {
        let console = ConsoleDestination()
Davide De Rosa's avatar
Davide De Rosa committed
34
        #if PIA_DEV
Davide De Rosa's avatar
Davide De Rosa committed
35
        console.minLevel = .debug
Davide De Rosa's avatar
Davide De Rosa committed
36 37 38
        #else
        console.minLevel = .info
        #endif
Davide De Rosa's avatar
Davide De Rosa committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
        SwiftyBeaver.addDestination(console)
        
        AppPreferences.shared.migrate()

        // PIALibrary
        
        guard let bundledRegionsURL = AppConstants.Regions.bundleURL else {
            fatalError("Could not find bundled regions file")
        }
        let bundledServersJSON: Data
        do {
            try bundledServersJSON = Data(contentsOf: bundledRegionsURL)
        } catch let e {
            fatalError("Could not parse bundled regions file: \(e)")
        }

        Client.environment = AppConfiguration.clientEnvironment
        Client.configuration.isDevelopment = Flags.shared.usesDevelopmentClient

58 59 60
        if let stagingUrl = AppConstants.Web.stagingEndpointURL {
            Client.configuration.setBaseURL(stagingUrl.absoluteString, for: .staging)
        }
61 62 63 64 65
        if Client.configuration.isDevelopment, let customServers = AppConstants.Servers.customServers {
            for server in customServers {
                Client.configuration.addCustomServer(server)
            }
        }
66

Davide De Rosa's avatar
Davide De Rosa committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
        Client.configuration.enablesConnectivityUpdates = true
        Client.configuration.enablesServerUpdates = true
        Client.configuration.enablesServerPings = true
        Client.configuration.bundledServersJSON = bundledServersJSON
        Client.configuration.webTimeout = AppConfiguration.ClientConfiguration.webTimeout
        Client.configuration.vpnProfileName = AppConfiguration.VPN.profileName
        Client.configuration.addVPNProfile(PIATunnelProfile(bundleIdentifier: AppConstants.Extensions.tunnelBundleIdentifier))

        let defaults = Client.preferences.defaults
        defaults.isPersistentConnection = true
        defaults.mace = false
        defaults.vpnType = IPSecProfile.vpnType
        defaults.vpnCustomConfigurations = [
            PIATunnelProfile.vpnType: AppConfiguration.VPN.piaDefaultConfigurationBuilder.build()
        ]

        Client.configuration.setPlan(.yearly, forProductIdentifier: AppConstants.InApp.yearlyProductIdentifier)
        Client.configuration.setPlan(.monthly, forProductIdentifier: AppConstants.InApp.monthlyProductIdentifier)

        Client.database = Client.Database(team: AppConstants.teamId, group: AppConstants.appGroup)

        if (isSimulator || Flags.shared.usesMockVPN) {
            Client.configuration.enablesConnectivityUpdates = false
            Client.useMockVPNProvider()
        }
        if Flags.shared.usesMockInApp {
            Client.useMockInAppProvider()
        }
        if Flags.shared.usesMockAccount {
            Client.useMockAccountProvider(AppConfiguration.Mock.accountProvider)
        }
        
        // as per App Store guidelines
        let pref = Client.preferences.editable()
101 102
        let tunnelConfiguration = pref.vpnCustomConfiguration(for: PIATunnelProfile.vpnType) as? PIATunnelProvider.Configuration
        var tunnelConfigurationBuilder = tunnelConfiguration?.builder()
103
        if !Flags.shared.enablesMACESetting {
Davide De Rosa's avatar
Davide De Rosa committed
104 105 106 107 108
            pref.mace = false
        }
        if !Flags.shared.enablesRemotePortSetting {
            pref.preferredPort = nil
        }
109 110 111 112 113 114 115
        if !Flags.shared.enablesSocketSetting {
            pref.preferredPort = nil
            tunnelConfigurationBuilder?.socketType = .udp
        }
        if let newConfiguration = tunnelConfigurationBuilder?.build() {
            pref.setVPNCustomConfiguration(newConfiguration, for: PIATunnelProfile.vpnType)
        }
Davide De Rosa's avatar
Davide De Rosa committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
        pref.commit()

        Client.bootstrap()

        // Business objects
        
        AccountObserver.shared.start()
//        DataCounter.shared.startCounting()

        // Third parties
        
        let rater = iRate.sharedInstance()!
        rater.usesUntilPrompt = AppConfiguration.Rating.usesUntilPrompt
        rater.eventsUntilPrompt = AppConfiguration.Rating.eventsUntilPrompt
        rater.daysUntilPrompt = AppConfiguration.Rating.daysUntilPrompt
        rater.remindPeriod = AppConfiguration.Rating.remindPeriod
        
        #if PIA_DEV
        let hockey = BITHockeyManager.shared()
        hockey.isMetricsManagerDisabled = true
        hockey.configure(withIdentifier: AppConstants.hockeyAppId)
        hockey.start()
        #endif

        // Notifications
        
        let nc = NotificationCenter.default
        nc.addObserver(self, selector: #selector(reloadTheme), name: .PIAThemeDidChange, object: nil)
        nc.addObserver(self, selector: #selector(vpnStatusDidChange(notification:)), name: .PIADaemonsDidUpdateVPNStatus, object: nil)

        // PIALibrary (Theme)
        
        AppPreferences.shared.currentThemeCode.apply(theme: Theme.current, reload: true)

        // show walkthrough on upgrade except for logged in users
        if Client.providers.accountProvider.isLoggedIn {
            AppPreferences.shared.wasLaunched = true
        }
    }

    func dispose() {
        Client.dispose()
    }
    
    // MARK: Notifications

    @objc private func reloadTheme() {
        Theme.current.applySideMenu()
        Theme.current.applyAppearance()
    }
    
    @objc private func vpnStatusDidChange(notification: Notification) {
        guard (Client.providers.vpnProvider.vpnStatus == .connected) else {
            return
        }
        iRate.sharedInstance()!.logEvent(false)
    }
}