Commit fb519799 authored by Mygod's avatar Mygod

Handle subnets with duplications

parent 476a6257
...@@ -34,16 +34,24 @@ class AclMatcher { ...@@ -34,16 +34,24 @@ class AclMatcher {
private var bypass = false private var bypass = false
suspend fun init(id: String) { suspend fun init(id: String) {
fun Sequence<Subnet>.dedup() = sequence {
val iterator = map { it.toImmutable() }.sortedWith(Subnet.Immutable).iterator()
var current: Subnet.Immutable? = null
while (iterator.hasNext()) {
val next = iterator.next()
if (current?.matches(next) == true) continue
yield(next)
current = next
}
}.toList()
val time = measureNanoTime { val time = measureNanoTime {
val (bypass, subnets) = Acl.parse(Acl.getFile(id).bufferedReader(), { val (bypass, subnets) = Acl.parse(Acl.getFile(id).bufferedReader(), {
// bypassDomains.add(it.toRegex()) // bypassDomains.add(it.toRegex())
}, { }, {
if (it.startsWith("(?:^|\\.)googleapis")) proxyDomains.add(it.toRegex()) if (it.startsWith("(?:^|\\.)googleapis")) proxyDomains.add(it.toRegex())
}) })
subnetsIpv4 = subnets.asSequence().filter { it.address is Inet4Address }.map { it.toImmutable() } subnetsIpv4 = subnets.asSequence().filter { it.address is Inet4Address }.dedup()
.sortedWith(Subnet.Immutable).toList() subnetsIpv6 = subnets.asSequence().filter { it.address is Inet6Address }.dedup()
subnetsIpv6 = subnets.asSequence().filter { it.address is Inet6Address }.map { it.toImmutable() }
.sortedWith(Subnet.Immutable).toList()
this.bypass = bypass this.bypass = bypass
} }
Log.d("AclMatcher", "ACL initialized in $time ns") Log.d("AclMatcher", "ACL initialized in $time ns")
......
...@@ -56,6 +56,8 @@ class Subnet(val address: InetAddress, val prefixSize: Int) : Comparable<Subnet> ...@@ -56,6 +56,8 @@ class Subnet(val address: InetAddress, val prefixSize: Int) : Comparable<Subnet>
return 0 return 0
} }
} }
fun matches(b: Immutable) = matches(b.a)
fun matches(b: ByteArray): Boolean { fun matches(b: ByteArray): Boolean {
if (a.size != b.size) return false if (a.size != b.size) return false
var i = 0 var i = 0
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment