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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
import Foundation
import SwiftData
enum BookmarkType: String, Codable {
case tracker = "tracker"
case server = "server"
}
struct BookmarkServer: Hashable, Identifiable {
let id = UUID()
let name: String?
let address: String
let port: Int
let description: String?
let users: Int
var server: Server {
Server(name: self.name, description: self.description, address: self.address, port: self.port, users: self.users)
}
init(server: Server) {
self.name = server.name
self.address = server.address
self.port = server.port
self.description = server.description
self.users = server.users
}
}
@Model
final class Bookmark {
var type: BookmarkType = BookmarkType.server
var order: Int = 0
var name: String = ""
var address: String = ""
var port: Int = HotlinePorts.DefaultServerPort
@Attribute(.allowsCloudEncryption)
var login: String?
@Attribute(.allowsCloudEncryption)
var password: String?
@Transient
var displayAddress: String {
switch self.type {
case .tracker:
if self.port == HotlinePorts.DefaultTrackerPort {
return self.address
}
else {
return "\(self.address):\(String(self.port))"
}
case .server:
if self.port == HotlinePorts.DefaultServerPort {
return self.address
}
else {
return "\(self.address):\(String(self.port))"
}
}
}
@Transient
var server: Server? {
switch self.type {
case .tracker:
return nil
case .server:
return Server(name: self.name, description: nil, address: self.address, port: self.port, login: self.login, password: self.password)
}
}
static let DefaultBookmarks: [Bookmark] = [
Bookmark(type: .server, name: "The Mobius Strip", address: "67.174.208.111", port: HotlinePorts.DefaultServerPort),
Bookmark(type: .server, name: "System 7 Today", address: "hotline.system7today.com", port: HotlinePorts.DefaultServerPort),
Bookmark(type: .tracker, name: "Featured Servers", address: "hltracker.com", port: HotlinePorts.DefaultTrackerPort)
]
init(type: BookmarkType, name: String, address: String, port: Int, login: String? = nil, password: String? = nil) {
self.type = type
self.name = name
self.address = address
self.port = port
self.login = login
self.password = password
}
init?(fileData: Data, name: String? = nil) {
guard fileData.count <= 2000 else {
return nil
}
var fileDataArray: [UInt8] = [UInt8](fileData)
guard let headerValue = fileDataArray.consumeUInt32(),
headerValue.fourCharCode() == "HTsc",
let versionNumber = fileDataArray.consumeUInt16(),
versionNumber == 1,
fileDataArray.consume(128), // Skip 128 reserved bytes.
let loginLength = fileDataArray.consumeUInt16(),
let loginData: Data = fileDataArray.consumeBytes(32),
let passwordLength = fileDataArray.consumeUInt16(),
let passwordData: Data = fileDataArray.consumeBytes(32),
let addressLength = fileDataArray.consumeUInt16(),
let addressData: Data = fileDataArray.consumeBytes(256),
let addressString = String(data: addressData[0..<Int(addressLength)], encoding: .ascii),
let loginString = String(data: loginData[0..<Int(loginLength)], encoding: .ascii),
let passwordString = String(data: passwordData[0..<Int(passwordLength)], encoding: .ascii) else {
return nil
}
let (addressHost, addressPort) = Server.parseServerAddressAndPort(addressString)
self.type = .server
if let name = name, !name.isEmpty {
self.name = name
}
else {
self.name = addressHost
}
self.address = addressHost
self.port = addressPort
self.login = loginString.isEmpty ? nil : loginString
self.password = passwordString.isEmpty ? nil : passwordString
}
convenience init?(fileURL bookmarkFileURL: URL) {
guard bookmarkFileURL.isFileURL,
let fileAttributes = try? FileManager.default.attributesOfItem(atPath: bookmarkFileURL.path(percentEncoded: false)),
let fileSize = fileAttributes[FileAttributeKey.size] as? UInt64,
fileSize <= 2000,
let fileData = try? Data(contentsOf: bookmarkFileURL) else {
return nil
}
print("Bookmark: Parsing Hotline bookmark file...")
let fileName = bookmarkFileURL.deletingPathExtension().lastPathComponent.trimmingCharacters(in: .whitespacesAndNewlines)
self.init(fileData: fileData, name: fileName)
// var fileDataArray: [UInt8] = [UInt8](fileData)
//
// guard let headerValue = fileDataArray.consumeUInt32(),
// headerValue.fourCharCode() == "HTsc",
// let versionNumber = fileDataArray.consumeUInt16(),
// versionNumber == 1,
// fileDataArray.consume(128), // Skip 128 reserved bytes.
// let loginLength = fileDataArray.consumeUInt16(),
// let loginData: Data = fileDataArray.consumeBytes(32),
// let passwordLength = fileDataArray.consumeUInt16(),
// let passwordData: Data = fileDataArray.consumeBytes(32),
// let addressLength = fileDataArray.consumeUInt16(),
// let addressData: Data = fileDataArray.consumeBytes(256),
// let addressString = String(data: addressData[0..<Int(addressLength)], encoding: .ascii),
// let loginString = String(data: loginData[0..<Int(loginLength)], encoding: .ascii),
// let passwordString = String(data: passwordData[0..<Int(passwordLength)], encoding: .ascii) else {
// return nil
// }
//
// let (addressHost, addressPort) = Server.parseServerAddressAndPort(addressString)
//
// self.type = .server
// self.name = fileName.isEmpty ? addressHost : fileName
// self.address = addressHost
// self.port = addressPort
// self.login = loginString.isEmpty ? nil : loginString
// self.password = passwordString.isEmpty ? nil : passwordString
}
func bookmarkFileData() -> Data? {
guard let addressData = self.displayAddress.data(using: .ascii) else {
return nil
}
let loginData: Data = self.login?.data(using: .ascii) ?? Data()
let passwordData: Data = self.password?.data(using: .ascii) ?? Data()
var fileData: Data = Data()
fileData.appendUInt32("HTsc".fourCharCode()) // magic
fileData.appendUInt16(0x0001) // version
fileData.append(Data(repeating: 0x00, count: 128)) // reserved
fileData.appendUInt16(UInt16(loginData.count))
fileData.append(loginData)
if loginData.count < 32 {
// Pad login data to 32 bytes
fileData.append(Data(repeating: 0x00, count: 32 - loginData.count))
}
fileData.appendUInt16(UInt16(passwordData.count))
fileData.append(passwordData)
if passwordData.count < 32 {
// Pad password data to 32 bytes
fileData.append(Data(repeating: 0x00, count: 32 - passwordData.count))
}
fileData.appendUInt16(UInt16(addressData.count))
fileData.append(addressData)
if passwordData.count < 256 {
// Pad address data to 256 bytes
fileData.append(Data(repeating: 0x00, count: 256 - addressData.count))
}
return fileData
}
static func populateDefaults(force: Bool = false, context: ModelContext) {
if force || Bookmark.fetchCount(context: context) == 0 {
Bookmark.add(Bookmark.DefaultBookmarks, context: context)
}
}
static func fetchAll(context: ModelContext) -> [Bookmark] {
let fetchDescriptor = FetchDescriptor<Bookmark>(sortBy: [.init(\.order)])
do {
let bookmarks: [Bookmark] = try context.fetch(fetchDescriptor)
return bookmarks
}
catch {
return []
}
}
static func fetchCount(context: ModelContext) -> Int {
let descriptor = FetchDescriptor<Bookmark>()
return (try? context.fetchCount(descriptor)) ?? 0
}
static func deleteAll(context: ModelContext) {
try? context.delete(model: Bookmark.self)
}
static func add(_ bookmark: Bookmark, context: ModelContext) {
let existingBookmarks = Bookmark.fetchAll(context: context)
// Reindex bookmarks before insert.
for existingBookmark in existingBookmarks {
existingBookmark.order += 1
}
// Insert new bookmark at start.
bookmark.order = 0
context.insert(bookmark)
}
static func add(_ bookmarks: [Bookmark], context: ModelContext) {
let existingBookmarks = Bookmark.fetchAll(context: context)
// Reindex bookmarks before insert.
for existingBookmark in existingBookmarks {
existingBookmark.order += bookmarks.count
}
// Insert new bookmarks at start.
var bookmarkIndex = 0
for newBookmark in bookmarks {
newBookmark.order = bookmarkIndex
context.insert(newBookmark)
bookmarkIndex += 1
print("Bookmark: added \(newBookmark.name)")
}
}
static func delete(_ bookmark: Bookmark, context: ModelContext) {
// Delete bookmark
context.delete(bookmark)
// Reindex bookmarks
let existingBookmarks = Bookmark.fetchAll(context: context)
var index = 0
for existingBookmark in existingBookmarks {
existingBookmark.order = index
index += 1
}
}
static func delete(at indexes: IndexSet, context: ModelContext) {
var existingBookmarks = Bookmark.fetchAll(context: context)
// existingBookmarks.remove(atOffsets: indexes)
let bookmarksToDelete = indexes.map { existingBookmarks[$0] }
// Delete bookmark
for bookmark in bookmarksToDelete {
context.delete(bookmark)
}
// Reindex bookmarks
var index = 0
existingBookmarks.remove(atOffsets: indexes)
for existingBookmark in existingBookmarks {
existingBookmark.order = index
index += 1
}
do {
try context.save()
}
catch {
print("Bookmark: Failed to save bookmark deletions")
}
}
static func move(_ indexes: IndexSet, to newIndex: Int, context: ModelContext) {
guard Bookmark.fetchCount(context: context) >= indexes.count else {
print("Bookmark: Not enough bookmarks to move requested set")
return
}
// Perform move
var existingBookmarks = Bookmark.fetchAll(context: context)
existingBookmarks.move(fromOffsets: indexes, toOffset: newIndex)
// Reindex bookmarks
var index = 0
for existingBookmark in existingBookmarks {
existingBookmark.order = index
index += 1
}
do {
try context.save()
}
catch {
print("Bookmark: Failed to save bookmark reordering")
}
}
func fetchServers() async -> [BookmarkServer] {
guard self.type == .tracker else {
return []
}
var fetchedBookmarks: [BookmarkServer] = []
let client = HotlineTrackerClient()
if let fetchedServers: [HotlineServer] = try? await client.fetchServers(address: self.address, port: self.port) {
for fetchedServer in fetchedServers {
if let serverName = fetchedServer.name {
let server = Server(name: serverName, description: fetchedServer.description, address: fetchedServer.address, port: Int(fetchedServer.port), users: Int(fetchedServer.users))
fetchedBookmarks.append(BookmarkServer(server: server))
}
}
}
return fetchedBookmarks
}
}
|