private bool LoadChn(IEnumerable <string?> lines) { try { var hasRule = false; foreach (var line in lines) { if (line is null) { continue; } var strings = line.Split('/', 2, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries); if (strings.Length != 2 || !IPAddress.TryParse(strings[0], out var ip) || !byte.TryParse(strings[1], out var mask)) { continue; } _ipMatcher.Update(ip, mask, Rule.Direct); hasRule = true; } return(hasRule); } catch { return(false); } }
public static void TestIpMatcher(IIPAddressMatcher <Rule> tree) { tree.Update(IPAddress.Parse(@"1.0.1.0"), 28, Direct); tree.Update(IPAddress.Parse(@"1.1.1.1"), 32, Block); tree.Update(IPAddress.Parse(@"8.8.8.8"), 32, Proxy); tree.Update(IPAddress.Parse(@"192.168.0.0"), 16, Direct); tree.Update(IPAddress.Parse(@"192.168.20.16"), 28, Proxy); Assert.AreEqual(Unknown, tree.Match(IPAddress.Parse(@"10.0.0.123"))); Assert.AreEqual(Direct, tree.Match(IPAddress.Parse(@"1.0.1.0"))); Assert.AreEqual(Direct, tree.Match(IPAddress.Parse(@"1.0.1.1"))); Assert.AreEqual(Direct, tree.Match(IPAddress.Parse(@"1.0.1.15"))); Assert.AreEqual(Unknown, tree.Match(IPAddress.Parse(@"1.0.1.16"))); Assert.AreEqual(Unknown, tree.Match(IPAddress.Parse(@"1.0.1.250"))); Assert.AreEqual(Unknown, tree.Match(IPAddress.Parse(@"1.0.2.0"))); Assert.AreEqual(Block, tree.Match(IPAddress.Parse(@"1.1.1.1"))); Assert.AreEqual(Proxy, tree.Match(IPAddress.Parse(@"8.8.8.8"))); Assert.AreEqual(Direct, tree.Match(IPAddress.Parse(@"192.168.20.15"))); Assert.AreEqual(Proxy, tree.Match(IPAddress.Parse(@"192.168.20.19"))); }