-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpacketFilter.kt
33 lines (27 loc) · 1.44 KB
/
packetFilter.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import me.marvin.proxy.networking.*
import me.marvin.proxy.networking.packet.*
import java.util.*
interface Validator {
fun validate(type: PacketType): Boolean
infix fun xor(validator: Validator) = combine(validator, Boolean::xor)
infix fun and(validator: Validator) = combine(validator, Boolean::and)
infix fun or(validator: Validator) = combine(validator, Boolean::or)
private fun combine(validator: Validator, combiner: (Boolean, Boolean) -> Boolean): Validator {
return object : Validator {
override fun validate(type: PacketType): Boolean {
return combiner([email protected](type), validator.validate(type))
}
}
}
class PhaseValidator(phase: ProtocolPhase, vararg phases: ProtocolPhase, private val whitelist: Boolean = true): Validator {
val set: EnumSet<ProtocolPhase> = EnumSet.of(phase, *phases)
override fun validate(type: PacketType): Boolean = (set.contains(type.phase()) == whitelist)
}
class TypeValidator(type: PacketType, vararg types: PacketType, private val whitelist: Boolean = true): Validator {
val set: Set<PacketType> = setOf(type, *types)
override fun validate(type: PacketType): Boolean = (set.contains(type) == whitelist)
}
class DirectionValidator(private val direction: ProtocolDirection): Validator {
override fun validate(type: PacketType): Boolean = (direction == type.direction())
}
}