How do I implement a working demo? I'm building a mac application that blocks URLs based
on categories. For eg. If a user tries to open vimeo.com, It must be blocked. Is there
any alternative to achieve that?
Right, so on macOS with NEFilterDataProvider
if you know the address or hostname and it's always going to be static then you can setup a default NEFilterRule
for this traffic. For example:
let exampleRule = NENetworkRule(
remoteNetwork: NWHostEndpoint(hostname: "example.com", port: "0"),
remotePrefix: 0,
localNetwork: nil,
localPrefix: 0,
protocol: .TCP,
direction: .any
)
let filterRule = NEFilterRule(networkRule: exampleRule, action: .drop)
The rule above will drop all traffic to example.com if that is what you want. The same can be done to allow traffic. Now, if you need to filter traffic further, you would create a NENetworkRule
that catches all TCP traffic with the action to filterData
. This rule will give your provider all TCP traffic in handleNewFlow
:
let exampleRule = NENetworkRule(
remoteNetwork: nil,
remotePrefix: 0,
localNetwork: nil,
localPrefix: 0,
protocol: .TCP,
direction: .any
)
let filterRule = NEFilterRule(networkRule: exampleRule, action: .filterData)
The gotcha here is that you will receive an address for the inbound or outbound traffic and it will be up to you to decide whether this is a known address to allow or if it should be investigated further.
Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com