Hi Meaton, sure to simplify things a bit, traffic from 192.168.4.2 does not hit the rule in this case:
let remoteNetwork = NWHostEndpoint(hostname: "192.168.4.2", port: "0")
let inboundNetworkRule = NENetworkRule(remoteNetwork: remoteNetwork,
remotePrefix: 32,
localNetwork: nil,
localPrefix: 0,
protocol: .TCP,
direction: .inbound)
If I change the port to "" which according to the docs sounds like it should be the same as "0" port I now match the rule but other remote IP's also match the rule. This seems like another issue.
The below case does not seem to do what I want either. With the configuration below I see multiple remote IP's hit the rule. To me that makes sense because the prefix is 0 not 32. However I also saw loopback traffic hit in this case as well which did not make any sense given the documentation I've read seems to indicate loopback traffic will only hit an explicit rule for 127.0.0.1.
let remoteNetwork = NWHostEndpoint(hostname: "192.168.4.2", port: "0")
let inboundNetworkRule = NENetworkRule(remoteNetwork: remoteNetwork,
remotePrefix: 0,
localNetwork: nil,
localPrefix: 0,
protocol: .TCP,
direction: .inbound)
I also tried the below and the behavior is the same as the localNetwork nil case above.
let localNetwork = NWHostEndpoint(hostname: "0.0.0.0", port: "0")
let remoteNetwork = NWHostEndpoint(hostname: "192.168.4.2", port: "0")
let inboundNetworkRule = NENetworkRule(remoteNetwork: remoteNetwork,
remotePrefix: 0,
localNetwork: localNetwork,
localPrefix: 0,
protocol: .TCP,
direction: .inbound)
In the case below the behavior is the same as the above (multiple remote ip's hit the rule) except I no longer see loopback traffic (likely because the local port filter is working).
let localNetwork = NWHostEndpoint(hostname: "0.0.0.0", port: "8888")
let remoteNetwork = NWHostEndpoint(hostname: "192.168.4.2", port: "0")
let inboundNetworkRule = NENetworkRule(remoteNetwork: remoteNetwork,
remotePrefix: 0,
localNetwork: localNetwork,
localPrefix: 0,
protocol: .TCP,
direction: .inbound)
And finally the case I really want is below. A connection from remote IP 192.168.4.2 to local port 8888 does not get caught by the rule below. It appears this doesn't have anything to do with the localNetwork based on the examples above which used a nil or 0.0.0.0/0 local network and still didn't work.
let localNetwork = NWHostEndpoint(hostname: "0.0.0.0", port: "8888")
let remoteNetwork = NWHostEndpoint(hostname: "192.168.4.2", port: "0")
let inboundNetworkRule = NENetworkRule(remoteNetwork: remoteNetwork,
remotePrefix: 32,
localNetwork: localNetwork,
localPrefix: 0,
protocol: .TCP,
direction: .inbound)