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
|
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
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
import ObjFW
import UIKit
class SelectKeyFileController: UITableViewController {
public var addSiteController: AddSiteController?
private var keyFiles: [String] = []
private var httpServer: OFHTTPServer
private var httpServerDelegate: HTTPServerDelegate
private var httpServerThread: OFThread
required init?(coder aDecoder: NSCoder) {
httpServer = OFHTTPServer()
httpServer.host = "127.0.0.1".ofObject
httpServerDelegate = HTTPServerDelegate()
httpServer.delegate = self.httpServerDelegate
httpServerThread = OFThread()
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
guard let documentDirectory = NSSearchPathForDirectoriesInDomains(
.documentDirectory, .userDomainMask, true).first
else {
print("Could not get key files: No documents directory")
navigationController?.popViewController(animated: true)
return
}
do {
keyFiles = try FileManager.default.contentsOfDirectory(
atPath: documentDirectory).sorted()
} catch let error as NSError {
print("Could not get key files: \(error)")
navigationController?.popViewController(animated: true)
return
}
httpServerThread.start()
}
override func viewDidDisappear(_ animated: Bool) {
httpServerThread.runLoop.stop()
httpServerThread.join()
}
override func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return keyFiles.count + 1
}
|
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
|
66
67
68
69
70
71
72
73
|
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
addSiteController?.keyFile =
indexPath.row > 0 ? keyFiles[indexPath.row - 1] : nil
addSiteController?.keyFileLabel?.text =
indexPath.row > 0 ? keyFiles[indexPath.row - 1] : "None"
self.navigationController?.popViewController(animated: true)
}
}
@IBAction func upload(_ sender: Any?) {
let timer = OFTimer.scheduledTimer(withTimeInterval: 0,
repeats: false) { (OFTimer) in
self.httpServer.port = 0
self.httpServer.start()
let message =
"Navigate to http://\(self.httpServer.host!.nsObject):" +
"\(self.httpServer.port)/ in your browser.\n\n" +
"Press OK when done."
let alert = UIAlertController(title: "Server Running",
message: message,
preferredStyle: .alert)
alert.addAction(
UIAlertAction(title: "OK", style: .default, handler: nil))
DispatchQueue.main.sync {
self.present(alert, animated: true) {
self.httpServer.stop()
}
}
}
httpServerThread.runLoop.add(timer)
}
}
|