punchnet-macos/punchnet/Shared/Models/NetworkContext.swift
2026-04-17 15:18:08 +08:00

112 lines
2.6 KiB
Swift

//
// NetworkContext.swift
// punchnet
//
// Created by on 2026/4/17.
//
import Foundation
//
struct NetworkContext: Codable {
let ip: String
let maskLen: UInt8
//
let hostname: String
let identityId: UInt32
let resourceList: [Resource]
let nodeList: [Node]
let exitNodeList: [ExitNode]
enum CodingKeys: String, CodingKey {
case ip
case maskLen = "mask_len"
case hostname
case identityId = "identity_id"
case resourceList = "resource_list"
case nodeList = "node_list"
case exitNodeList = "exit_node"
}
func getNode(id: Int?) -> Node? {
return nodeList.first(where: { $0.id == id })
}
func firstNodeId() -> Int? {
return nodeList.first?.id
}
}
extension NetworkContext {
struct ExitNode: Codable {
let uuid = UUID().uuidString
let nnid: Int
let nodeName: String
enum CodingKeys: String, CodingKey {
case nnid = "node_id"
case nodeName = "node_name"
}
}
//
struct Resource: Codable {
var uuid = UUID().uuidString
var id: Int
var name: String
var url: String
var connectionStatus: String
enum CodingKeys: String, CodingKey {
case id
case name
case url
case connectionStatus = "connection_status"
}
}
//
struct Node: Codable {
var id: Int
var name: String
var ip: String
var system: String?
var connectionStatus: String
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
enum CodingKeys: String, CodingKey {
case id
case name
case ip
case system
case connectionStatus = "connection_status"
}
static func == (lhs: Self, rhs: Self) -> Bool {
return lhs.id == rhs.id
}
}
//
struct NodeDetail: Codable {
let id: Int
let name: String
let ip: String
let system: String?
let connectionStatus: String
let resourceList: [Resource]
enum CodingKeys: String, CodingKey {
case id
case name
case ip
case system
case connectionStatus = "connection_status"
case resourceList = "resource_list"
}
}
}