From 6fa958815bc94ad358cb5d5e47108815e7eb3147 Mon Sep 17 00:00:00 2001 From: Toby Date: Mon, 14 Aug 2023 13:24:23 -0700 Subject: [PATCH] feat: TextRule line num --- extras/outbounds/acl/compile.go | 12 ++++++------ extras/outbounds/acl/parse.go | 6 ++++-- extras/outbounds/acl/parse_test.go | 14 +++++++------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/extras/outbounds/acl/compile.go b/extras/outbounds/acl/compile.go index 359add1..366ffd6 100644 --- a/extras/outbounds/acl/compile.go +++ b/extras/outbounds/acl/compile.go @@ -84,12 +84,12 @@ func (s *compiledRuleSetImpl[O]) Match(host HostInfo, proto protocol, port uint1 } type CompilationError struct { - Index int + LineNum int Message string } func (e *CompilationError) Error() string { - return fmt.Sprintf("error at index %d: %s", e.Index, e.Message) + return fmt.Sprintf("error at line %d: %s", e.LineNum, e.Message) } func Compile[O Outbound](rules []TextRule, outbounds map[string]O, @@ -99,21 +99,21 @@ func Compile[O Outbound](rules []TextRule, outbounds map[string]O, for i, rule := range rules { outbound, ok := outbounds[rule.Outbound] if !ok { - return nil, &CompilationError{i, fmt.Sprintf("outbound %s not found", rule.Outbound)} + return nil, &CompilationError{rule.LineNum, fmt.Sprintf("outbound %s not found", rule.Outbound)} } hm, errStr := compileHostMatcher(rule.Address, geoipFunc) if errStr != "" { - return nil, &CompilationError{i, errStr} + return nil, &CompilationError{rule.LineNum, errStr} } proto, port, ok := parseProtoPort(rule.ProtoPort) if !ok { - return nil, &CompilationError{i, fmt.Sprintf("invalid protocol/port: %s", rule.ProtoPort)} + return nil, &CompilationError{rule.LineNum, fmt.Sprintf("invalid protocol/port: %s", rule.ProtoPort)} } var hijackAddress net.IP if rule.HijackAddress != "" { hijackAddress = net.ParseIP(rule.HijackAddress) if hijackAddress == nil { - return nil, &CompilationError{i, fmt.Sprintf("invalid hijack address (must be an IP address): %s", rule.HijackAddress)} + return nil, &CompilationError{rule.LineNum, fmt.Sprintf("invalid hijack address (must be an IP address): %s", rule.HijackAddress)} } } compiledRules[i] = compiledRule[O]{outbound, hm, proto, port, hijackAddress} diff --git a/extras/outbounds/acl/parse.go b/extras/outbounds/acl/parse.go index f9b4eef..11cae44 100644 --- a/extras/outbounds/acl/parse.go +++ b/extras/outbounds/acl/parse.go @@ -31,9 +31,10 @@ type TextRule struct { Address string ProtoPort string HijackAddress string + LineNum int } -func parseLine(line string) *TextRule { +func parseLine(line string, num int) *TextRule { matches := linePattern.FindStringSubmatch(line) if matches == nil { return nil @@ -43,6 +44,7 @@ func parseLine(line string) *TextRule { Address: strings.TrimSpace(matches[2]), ProtoPort: strings.TrimSpace(matches[3]), HijackAddress: strings.TrimSpace(matches[4]), + LineNum: num, } } @@ -61,7 +63,7 @@ func ParseTextRules(text string) ([]TextRule, error) { continue } // Parse line - rule := parseLine(line) + rule := parseLine(line, lineNum) if rule == nil { return nil, &InvalidSyntaxError{line, lineNum} } diff --git a/extras/outbounds/acl/parse_test.go b/extras/outbounds/acl/parse_test.go index 250e8a1..249821c 100644 --- a/extras/outbounds/acl/parse_test.go +++ b/extras/outbounds/acl/parse_test.go @@ -32,13 +32,13 @@ my_custom_outbound1(9.9.9.9,*, 8.8.8.8) # bebop my_custom_outbound2(all) `, want: []TextRule{ - {Outbound: "direct", Address: "1.1.1.1"}, - {Outbound: "direct", Address: "8.8.8.0/24"}, - {Outbound: "reject", Address: "all", ProtoPort: "udp/443"}, - {Outbound: "reject", Address: "geoip:cn"}, - {Outbound: "reject", Address: "*.v2ex.com"}, - {Outbound: "my_custom_outbound1", Address: "9.9.9.9", ProtoPort: "*", HijackAddress: "8.8.8.8"}, - {Outbound: "my_custom_outbound2", Address: "all"}, + {Outbound: "direct", Address: "1.1.1.1", LineNum: 4}, + {Outbound: "direct", Address: "8.8.8.0/24", LineNum: 5}, + {Outbound: "reject", Address: "all", ProtoPort: "udp/443", LineNum: 6}, + {Outbound: "reject", Address: "geoip:cn", LineNum: 7}, + {Outbound: "reject", Address: "*.v2ex.com", LineNum: 8}, + {Outbound: "my_custom_outbound1", Address: "9.9.9.9", ProtoPort: "*", HijackAddress: "8.8.8.8", LineNum: 9}, + {Outbound: "my_custom_outbound2", Address: "all", LineNum: 10}, }, wantErr: false, },