ipcd/src/ipcd/rules.cr

91 lines
1.6 KiB
Crystal

module IPCd
class Redirection
include YAML::Serializable
property origin : String
property destination : String
property originurl : String?
property destinationurl : String?
def initialize (@origin, @destination)
end
def initialize (@origin, @destination, @originurl, @destinationurl)
end
def to_s
"Redirection #{origin} #{destination}"
# if @originurl
# "#{origin} #{destination}"
# else
# "#{origin} #{destination} #{originurl} #{destinationurl}"
# end
end
end
class RedirectionSet < Array(Redirection)
def to_s
map(&.to_s).join("\n")
end
end
class Rule
include YAML::Serializable
enum Type
Allow
Deny
end
enum Direction
In
Out
end
getter service : String
getter url : String
getter type : Type
getter direction : Direction
def initialize (@type, @direction, @service, @url)
end
def matches_service?(service : String)
@service == "*" || @service == service
end
def matches_uri?(uri)
u = Regex.new @url
!@u.match(uri).nil?
end
def self.from_args(args : Array(String))
Rule.new(
Type.parse(args[0][2..]),
Rule::Direction.parse(args[1]),
args[2],
args[3]
)
end
def to_s
"#{type} #{direction} #{service} #{url}"
end
end
class RuleSet < Array(Rule)
def authorized?(direction : Rule::Direction, service : String, uri : String) : Bool
self.select(&.direction.==(direction))
.select(&.matches_service?(service))
.select(&.matches_uri?(uri))
.[0]?.try &.type.allow? || false
end
def to_s
map(&.to_s).join("\n")
end
end
end