11/*
22 * Onion Browser
3- * Copyright (c) 2012-2018 , Tigas Ventures, LLC (Mike Tigas)
3+ * Copyright (c) 2012-2021 , Tigas Ventures, LLC (Mike Tigas)
44 *
55 * This file is part of Onion Browser. See LICENSE file for redistribution terms.
66 */
@@ -15,153 +15,143 @@ class Migration: NSObject {
1515 . strict,
1616 . blockXhr,
1717 . open
18- ]
18+ ]
1919
20- /**
21- Migrates bookmarks, bridge settings and miscelaneous other settings of version 1.x to 2.x.
22- */
23- @objc class func migrate( ) {
24- let settings = UserDefaults . standard
20+ /**
21+ Migrates bookmarks, bridge settings and miscelaneous other settings of version 1.x to 2.x.
22+ */
23+ @objc class func migrate( ) {
24+ let settings = UserDefaults . standard
2525
26- let storeUrl = FileManager . default. urls ( for: . documentDirectory, in: . userDomainMask) . last?
27- . appendingPathComponent ( " Settings.sqlite " )
26+ let storeUrl = FileManager . default. urls ( for: . documentDirectory, in: . userDomainMask) . last?
27+ . appendingPathComponent ( " Settings.sqlite " )
2828
29- var isReachable = try ? storeUrl? . checkResourceIsReachable ( )
29+ var isReachable = try ? storeUrl? . checkResourceIsReachable ( )
3030
31- // Check, if CoreData SQLite file is there, if so migrate bookmarks and bridge settings.
32- if isReachable ?? false {
31+ // Check, if CoreData SQLite file is there, if so migrate bookmarks and bridge settings.
32+ if isReachable ?? false {
3333
34- // Initialize CoreData.
35- if let mom = NSManagedObjectModel . mergedModel ( from: nil ) {
36- let psc = NSPersistentStoreCoordinator ( managedObjectModel: mom)
34+ // Initialize CoreData.
35+ if let mom = NSManagedObjectModel . mergedModel ( from: nil ) {
36+ let psc = NSPersistentStoreCoordinator ( managedObjectModel: mom)
3737
38- let moc = NSManagedObjectContext ( concurrencyType: . mainQueueConcurrencyType)
39- moc. persistentStoreCoordinator = psc
38+ let moc = NSManagedObjectContext ( concurrencyType: . mainQueueConcurrencyType)
39+ moc. persistentStoreCoordinator = psc
4040
41- let store = try ? psc. addPersistentStore ( ofType: NSSQLiteStoreType,
42- configurationName: nil ,
43- at: storeUrl,
44- options: nil )
41+ let store = try ? psc. addPersistentStore ( ofType: NSSQLiteStoreType,
42+ configurationName: nil ,
43+ at: storeUrl,
44+ options: nil )
4545
46- // Migrate bridges. Needs to be done in the main thread, otherwise it's too late.
47- let request = NSFetchRequest< Bridge> . init( entityName: " Bridge " )
48- let oldBridges = try ? moc. fetch ( request)
46+ // Migrate bridges. Needs to be done in the main thread, otherwise it's too late.
47+ let request = NSFetchRequest< Bridge> . init( entityName: " Bridge " )
48+ let oldBridges = try ? moc. fetch ( request)
4949
50- if ( oldBridges? . count ?? 0 ) > 0 {
51- // Don't show intro to bridge users - otherwise these settings are lost.
50+ if ( oldBridges? . count ?? 0 ) > 0 {
51+ // Don't show intro to bridge users - otherwise these settings are lost.
5252 Settings . didIntro = true
5353
54- // Detect default Meek bridges.
55- if oldBridges!. count == 1 {
56- let ob = oldBridges![ 0 ] ;
54+ var newBridges = [ String] ( )
5755
58- if ob. conf == OnionManager . meekAzureBridge. first {
59- Settings . currentlyUsedBridges = . meekazure
60- }
61- }
62- else {
63- var newBridges = [ String] ( )
56+ for ob in oldBridges! {
57+ newBridges. append ( ob. conf)
58+ }
6459
65- for ob in oldBridges! {
66- newBridges. append ( ob. conf)
67- }
60+ Settings . currentlyUsedBridges = . custom
61+ Settings . customBridges = newBridges
6862
69- Settings . currentlyUsedBridges = . custom
70- Settings . customBridges = newBridges
71- }
63+ settings. synchronize ( )
64+ }
7265
73- settings. synchronize ( )
74- }
66+ // Jump into a background thread to do the rest of the migration.
67+ DispatchQueue . global ( qos: . background) . async {
68+ // Migrate bookmarks.
69+ let request = NSFetchRequest< OldBookmark> . init( entityName: " Bookmark " )
70+ request. sortDescriptors = [ NSSortDescriptor . init ( key: " order " , ascending: true ) ]
7571
76- // Jump into a background thread to do the rest of the migration.
77- DispatchQueue . global ( qos: . background) . async {
78- // Migrate bookmarks.
79- let request = NSFetchRequest< OldBookmark> . init( entityName: " Bookmark " )
80- request. sortDescriptors = [ NSSortDescriptor . init ( key: " order " , ascending: true ) ]
81-
82- if let oldBookmarks = try ? moc. fetch ( request) {
83- for ob in oldBookmarks {
72+ if let oldBookmarks = try ? moc. fetch ( request) {
73+ for ob in oldBookmarks {
8474 Bookmark . add ( ob. title, ob. url) . acquireIcon {
8575 Bookmark . store ( )
8676 }
87- }
88-
89- Bookmark . store ( )
90- }
91-
92- // Remove old CoreData storage.
93- do {
94- if store != nil {
95- try moc. persistentStoreCoordinator? . remove ( store!)
96- }
97-
98- try FileManager . default. removeItem ( at: storeUrl!)
99- } catch {
100- // This should not happen.
101- // Can't do anything now. We tried...
102- }
103- }
104- }
105- }
106-
107- let settingsUrl = FileManager . default. urls ( for: . documentDirectory, in: . userDomainMask)
108- . last? . appendingPathComponent ( " Settings.plist " )
109-
110- isReachable = try ? settingsUrl? . checkResourceIsReachable ( )
111-
112- // Check, if Settings.plist file is there, if so, migrate some, which apply to Endless, too.
113- if isReachable ?? false {
114-
115- DispatchQueue . global ( qos: . background) . async {
116- if let raw = FileManager . default. contents ( atPath: settingsUrl!. path) {
117- let oldSettings = try ? PropertyListSerialization . propertyList (
118- from: raw,
119- options: . mutableContainersAndLeaves,
120- format: nil )
121- as? [ String : Any ]
122-
123- // Do-Not-Track header.
124- if let dnt = oldSettings ? [ " dnt " ] as? Int
125- {
126- // 1.X had 3 settings: 0 = unset, 1 = cantrack, 2 = notrack
127- // Endless has only two options "send_dnt" true or false.
128- // Translation table: 0 => false, 1 => false, 2 => true
77+ }
78+
79+ Bookmark . store ( )
80+ }
81+
82+ // Remove old CoreData storage.
83+ do {
84+ if store != nil {
85+ try moc. persistentStoreCoordinator? . remove ( store!)
86+ }
87+
88+ try FileManager . default. removeItem ( at: storeUrl!)
89+ } catch {
90+ // This should not happen.
91+ // Can't do anything now. We tried...
92+ }
93+ }
94+ }
95+ }
96+
97+ let settingsUrl = FileManager . default. urls ( for: . documentDirectory, in: . userDomainMask)
98+ . last? . appendingPathComponent ( " Settings.plist " )
99+
100+ isReachable = try ? settingsUrl? . checkResourceIsReachable ( )
101+
102+ // Check, if Settings.plist file is there, if so, migrate some, which apply to Endless, too.
103+ if isReachable ?? false {
104+
105+ DispatchQueue . global ( qos: . background) . async {
106+ if let raw = FileManager . default. contents ( atPath: settingsUrl!. path) {
107+ let oldSettings = try ? PropertyListSerialization . propertyList (
108+ from: raw,
109+ options: . mutableContainersAndLeaves,
110+ format: nil )
111+ as? [ String : Any ]
112+
113+ // Do-Not-Track header.
114+ if let dnt = oldSettings ? [ " dnt " ] as? Int
115+ {
116+ // 1.X had 3 settings: 0 = unset, 1 = cantrack, 2 = notrack
117+ // Endless has only two options "send_dnt" true or false.
118+ // Translation table: 0 => false, 1 => false, 2 => true
129119 Settings . sendDnt = dnt == 2
130- }
120+ }
131121
132- // Content security policy setting. For legacy reasons named "javascript".
133- if let csp = oldSettings ? [ " javascript " ] as? Int {
134- // From the 1.X sources:
135- // #define CONTENTPOLICY_STRICT 0 // Blocks nearly every CSP type
136- // #define CONTENTPOLICY_BLOCK_CONNECT 1 // Blocks `connect-src` (XHR, CORS, WebSocket)
137- // #define CONTENTPOLICY_PERMISSIVE 2 // Allows all content (DANGEROUS: websockets leak outside tor)
122+ // Content security policy setting. For legacy reasons named "javascript".
123+ if let csp = oldSettings ? [ " javascript " ] as? Int {
124+ // From the 1.X sources:
125+ // #define CONTENTPOLICY_STRICT 0 // Blocks nearly every CSP type
126+ // #define CONTENTPOLICY_BLOCK_CONNECT 1 // Blocks `connect-src` (XHR, CORS, WebSocket)
127+ // #define CONTENTPOLICY_PERMISSIVE 2 // Allows all content (DANGEROUS: websockets leak outside tor)
138128
139129 let hs = HostSettings . forDefault ( )
140130 hs. contentPolicy = cspTranslation [ csp]
141131 hs. save ( ) . store ( )
142- }
143-
144- // Minimal TLS version. Only the "1.2 only" setting will be migrated, as
145- // the "SSL v3" setting is not supported in Endless.
146- if let tlsver = oldSettings ? [ " tlsver " ] as? Int {
147- // From the 1.X sources:
148- // #define X_TLSVER_ANY 0
149- // #define X_TLSVER_TLS1 1
150- // #define X_TLSVER_TLS1_2_ONLY 2
151-
152- if tlsver == 2 {
153- Settings . tlsVersion = . tls12
154- }
155- }
156- }
157-
158- do {
159- try FileManager . default. removeItem ( at: settingsUrl!)
160- } catch {
161- // This should not happen.
162- // Can't do anything now. We tried...
163- }
164- }
165- }
166- }
132+ }
133+
134+ // Minimal TLS version. Only the "1.2 only" setting will be migrated, as
135+ // the "SSL v3" setting is not supported in Endless.
136+ if let tlsver = oldSettings ? [ " tlsver " ] as? Int {
137+ // From the 1.X sources:
138+ // #define X_TLSVER_ANY 0
139+ // #define X_TLSVER_TLS1 1
140+ // #define X_TLSVER_TLS1_2_ONLY 2
141+
142+ if tlsver == 2 {
143+ Settings . tlsVersion = . tls12
144+ }
145+ }
146+ }
147+
148+ do {
149+ try FileManager . default. removeItem ( at: settingsUrl!)
150+ } catch {
151+ // This should not happen.
152+ // Can't do anything now. We tried...
153+ }
154+ }
155+ }
156+ }
167157}
0 commit comments