-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-bridges.swift
68 lines (46 loc) · 1.51 KB
/
update-bridges.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
//
// update-bridges.swift
// IPtProxyUI
//
// Created by Benjamin Erhart on 03.12.19.
// Copyright © 2019 - 2022 Guardian Project. All rights reserved.
//
import Foundation
// MARK: Config
let request = MoatApi.buildRequest(.builtin)
let outfile = resolve("IPtProxyUI/Assets/Shared/builtin-bridges.json")
// MARK: Helper Methods
func exit(_ msg: String) {
print(msg)
exit(1)
}
func resolve(_ path: String) -> URL {
let cwd = URL(fileURLWithPath: FileManager.default.currentDirectoryPath)
let base = URL(fileURLWithPath: ProcessInfo.processInfo.environment["UPDATE_BRIDGES_BASE"]!, relativeTo: cwd)
return URL(fileURLWithPath: path, relativeTo: base)
}
// MARK: Main
let modified = (try? outfile.resourceValues(forKeys: [.contentModificationDateKey]).contentModificationDate) ?? Date(timeIntervalSince1970: 0)
guard Calendar.current.dateComponents([.day], from: modified, to: Date()).day ?? 2 > 1 else {
print("File too young, won't update!")
exit(0)
}
let task = URLSession.shared.apiTask(with: request!) { (response: Data?, error) in
// print("response=\(String(describing: response)), error=\(String(describing: error))")
if let error = error {
return exit(error.localizedDescription)
}
guard let response = response else {
return exit("Empty response!")
}
do {
try response.write(to: outfile, options: .atomic)
}
catch {
exit("JSON file could not be written! error=\(error)")
}
exit(0)
}
task.resume()
// Wait on explicit exit.
_ = DispatchSemaphore(value: 0).wait(timeout: .distantFuture)