41 lines
1.1 KiB
Swift
41 lines
1.1 KiB
Swift
|
|
import Foundation
|
||
|
|
|
||
|
|
/// A coarse traffic category derived from protocol + port, used to colour the
|
||
|
|
/// cars of light. Direction (in/out) is conveyed by motion, so colour is free
|
||
|
|
/// to mean "what kind of traffic is this".
|
||
|
|
public enum TrafficClass: String, Sendable, CaseIterable {
|
||
|
|
case dns
|
||
|
|
case https
|
||
|
|
case http
|
||
|
|
case quic
|
||
|
|
case other
|
||
|
|
|
||
|
|
/// Human label for the legend.
|
||
|
|
public var label: String {
|
||
|
|
switch self {
|
||
|
|
case .dns: return "DNS"
|
||
|
|
case .https: return "HTTPS"
|
||
|
|
case .http: return "HTTP"
|
||
|
|
case .quic: return "QUIC"
|
||
|
|
case .other: return "Other"
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public static func classify(proto: NetProtocol, port: Int?) -> TrafficClass {
|
||
|
|
// nettop labels QUIC explicitly; trust that first.
|
||
|
|
if proto == .quic4 || proto == .quic6 { return .quic }
|
||
|
|
|
||
|
|
let isUDP = (proto == .udp4 || proto == .udp6)
|
||
|
|
switch port {
|
||
|
|
case 53, 5353:
|
||
|
|
return .dns
|
||
|
|
case 443:
|
||
|
|
return isUDP ? .quic : .https // UDP/443 is almost always QUIC
|
||
|
|
case 80, 8080:
|
||
|
|
return .http
|
||
|
|
default:
|
||
|
|
return .other
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|