-
Notifications
You must be signed in to change notification settings - Fork 85
/
ProtectedFileAccess.swift
154 lines (131 loc) · 4.72 KB
/
ProtectedFileAccess.swift
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
//
// AppGroupStorage.swift
// LockdowniOS
//
// Created by Oleg Dreyman on 16.10.2020.
// Copyright © 2020 Confirmed Inc. All rights reserved.
//
import Foundation
import CocoaLumberjackSwift
enum ProtectedFileAccess {
private static let fileURL = FileManager.default
.containerURL(forSecurityApplicationGroupIdentifier: "group.com.confirmed")!
.appendingPathComponent("protectionAccess.check")
static var isAvailable: Bool {
do {
let data = try Data.init(contentsOf: fileURL, options: [.mappedIfSafe])
let string = String(data: data, encoding: .utf8)
return string == "CHECK"
} catch {
return false
}
}
@available(iOS 10.0, *)
static func createProtectionAccessCheckFile() {
if !isAvailable {
let result = FileManager.default.createFile(
atPath: fileURL.path,
contents: "CHECK".data(using: .utf8),
attributes: [FileAttributeKey.protectionKey: FileProtectionType.completeUntilFirstUserAuthentication]
)
if result {
DDLogInfo("Created protectionAccess.check file")
} else {
DDLogError("Failed to create protectionAccess.check file")
}
} else {
DDLogInfo("protectionAccess.check file already exists")
}
}
}
enum PacketTunnelProviderLogs {
static let dateFormatter: DateFormatter = {
$0.dateFormat = "yyyy-MM-dd HH:mm:ss"
$0.formatterBehavior = .behavior10_4
$0.locale = .init(identifier: "en_US_POSIX")
return $0
}(DateFormatter())
static let userDefaultsKey = "com.confirmed.lockdown.ne_temporaryLogs"
static func log(_ string: String) {
guard ProtectedFileAccess.isAvailable else {
return
}
let string = "\(dateFormatter.string(from: Date())) \(string)"
if var array = defaults.stringArray(forKey: userDefaultsKey) {
array.append(string)
defaults.setValue(array, forKey: userDefaultsKey)
} else {
defaults.setValue([string], forKey: userDefaultsKey)
}
}
static var allEntries: [String] {
return defaults.stringArray(forKey: userDefaultsKey) ?? []
}
static func clear() {
defaults.setValue(Array<String>(), forKey: userDefaultsKey)
}
}
#if DEBUG
final class AppGroupStorage {
struct Key<Value: Codable>: RawRepresentable {
let rawValue: String
init(rawValue: String) {
self.rawValue = rawValue
}
}
private static let json: (encoder: JSONEncoder, decoder: JSONDecoder) = (JSONEncoder(), JSONDecoder())
static let directoryURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.confirmed")!.appendingPathComponent("AppGroupStorage", isDirectory: true)
static let shared = AppGroupStorage()
init() {
AppGroupStorage.createDirectoryIfNeeded(at: AppGroupStorage.directoryURL)
}
func read<Content>(key: Key<Content>) -> Content? {
do {
let url = self.url(forKey: key)
let data = try Data(contentsOf: url)
let content = try AppGroupStorage.json.decoder.decode(Content.self, from: data)
return content
} catch {
DDLogError(error)
return nil
}
}
func write<Content>(content: Content, key: Key<Content>) {
do {
let url = self.url(forKey: key)
let data = try AppGroupStorage.json.encoder.encode(content)
try data.write(to: url, options: [.atomic, .noFileProtection])
} catch {
DDLogError(error)
return
}
}
func delete<Content>(forKey key: Key<Content>) {
let url = self.url(forKey: key)
let fileManager = FileManager()
do {
try fileManager.removeItem(at: url)
} catch {
DDLogError(error)
}
}
func url<Content>(forKey key: Key<Content>) -> URL {
return AppGroupStorage.directoryURL.appendingPathComponent(key.rawValue)
}
}
extension AppGroupStorage {
static func createDirectoryIfNeeded(at url: URL) {
if FileManager.default.fileExists(atPath: url.path) {
DDLogInfo("AppGroupStorage Directory exists: \(url.path)")
return
} else {
do {
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
DDLogInfo("AppGroupStorage Directory created: \(url.path)")
} catch {
DDLogError(error)
}
}
}
}
#endif