let rules = allowedHosts.map { hostname in
let rule = NENetworkRule(destinationNetwork: NWHostEndpoint(hostname: hostname, port: "0"), prefix: 32, protocol: .any)
return NEFilterRule(networkRule: rule, action: .allow)
}
let rules1 = ["0.0.0.0", "127.0.0.1"].map { hostname in
let rule = NENetworkRule(destinationNetwork: NWHostEndpoint(hostname: hostname, port: "0"), prefix: 32, protocol: .any)
return NEFilterRule(networkRule: rule, action: .drop)
}
let filterSettings = NEFilterSettings(rules: rules + rules1, defaultAction: .filterData)
apply(filterSettings) { error in
if let applyError = error {
os_log(.default, log: OSLog(subsystem: "com.nesedemo.extension", category: "content-filter"), "Failed to apply filter settings: %{public}@", applyError.localizedDescription)
}
completionHandler(error)
}
For endpoint level, I've tried this rule just to blocks all the connection except the host I allow, it works,. Please share your insights on this.
But I don't find an api to control in network traffic from app level.