Added router priority to webui's list and detail page
This commit is contained in:
parent
cd90b9761a
commit
8cd4923e72
43 changed files with 2913 additions and 131 deletions
|
@ -72,6 +72,38 @@ func (m Muxer) Match(meta ConnData) (tcp.Handler, bool) {
|
|||
return nil, false
|
||||
}
|
||||
|
||||
// GetRulePriority computes the priority for a given rule.
|
||||
// The priority is calculated using the length of rule.
|
||||
// There is a special case where the HostSNI(`*`) has a priority of -1.
|
||||
func GetRulePriority(rule string) int {
|
||||
catchAllParser, err := rules.NewParser([]string{"HostSNI"})
|
||||
if err != nil {
|
||||
return len(rule)
|
||||
}
|
||||
|
||||
parse, err := catchAllParser.Parse(rule)
|
||||
if err != nil {
|
||||
return len(rule)
|
||||
}
|
||||
|
||||
buildTree, ok := parse.(rules.TreeBuilder)
|
||||
if !ok {
|
||||
return len(rule)
|
||||
}
|
||||
|
||||
ruleTree := buildTree()
|
||||
|
||||
// Special case for when the catchAll fallback is present.
|
||||
// When no user-defined priority is found, the lowest computable priority minus one is used,
|
||||
// in order to make the fallback the last to be evaluated.
|
||||
if ruleTree.RuleLeft == nil && ruleTree.RuleRight == nil && len(ruleTree.Value) == 1 &&
|
||||
ruleTree.Value[0] == "*" && strings.EqualFold(ruleTree.Matcher, "HostSNI") {
|
||||
return -1
|
||||
}
|
||||
|
||||
return len(rule)
|
||||
}
|
||||
|
||||
// AddRoute adds a new route, associated to the given handler, at the given
|
||||
// priority, to the muxer.
|
||||
func (m *Muxer) AddRoute(rule string, priority int, handler tcp.Handler) error {
|
||||
|
@ -98,18 +130,6 @@ func (m *Muxer) AddRoute(rule string, priority int, handler tcp.Handler) error {
|
|||
catchAll = ruleTree.Value[0] == "*" && strings.EqualFold(ruleTree.Matcher, "HostSNI")
|
||||
}
|
||||
|
||||
// Special case for when the catchAll fallback is present.
|
||||
// When no user-defined priority is found, the lowest computable priority minus one is used,
|
||||
// in order to make the fallback the last to be evaluated.
|
||||
if priority == 0 && catchAll {
|
||||
priority = -1
|
||||
}
|
||||
|
||||
// Default value, which means the user has not set it, so we'll compute it.
|
||||
if priority == 0 {
|
||||
priority = len(rule)
|
||||
}
|
||||
|
||||
newRoute := &route{
|
||||
handler: handler,
|
||||
matchers: matchers,
|
||||
|
|
|
@ -444,6 +444,39 @@ func Test_Priority(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestGetRulePriority(t *testing.T) {
|
||||
testCases := []struct {
|
||||
desc string
|
||||
rule string
|
||||
expected int
|
||||
}{
|
||||
{
|
||||
desc: "simple rule",
|
||||
rule: "HostSNI(`example.org`)",
|
||||
expected: 22,
|
||||
},
|
||||
{
|
||||
desc: "HostSNI(`*`) rule",
|
||||
rule: "HostSNI(`*`)",
|
||||
expected: -1,
|
||||
},
|
||||
{
|
||||
desc: "strange HostSNI(`*`) rule",
|
||||
rule: " HostSNI ( `*` ) ",
|
||||
expected: -1,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
test := test
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert.Equal(t, test.expected, GetRulePriority(test.rule))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type fakeConn struct {
|
||||
call map[string]int
|
||||
remoteAddr net.Addr
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue