From 74698bb8c51d959135b1d553d66b771f36c94aaa Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sun, 18 Aug 2024 16:08:00 +0200 Subject: [PATCH] feat(wireguard): Add DNS fallback analyzer --- handlers/wireguard/analyzer.go | 34 +++++++++++++++++++++++++++++++++ handlers/wireguard/wg-parser.go | 14 ++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/handlers/wireguard/analyzer.go b/handlers/wireguard/analyzer.go index 916ea43..38f8afd 100644 --- a/handlers/wireguard/analyzer.go +++ b/handlers/wireguard/analyzer.go @@ -5,6 +5,7 @@ import ( "config-lsp/utils" "fmt" "slices" + "strings" protocol "github.com/tliron/glsp/protocol_3_16" ) @@ -18,10 +19,43 @@ func (p wireguardParser) analyze() []protocol.Diagnostic { diagnostics := []protocol.Diagnostic{} diagnostics = append(diagnostics, p.checkForDuplicateProperties()...) + diagnostics = append(diagnostics, p.analyzeDNSContainsFallback()...) return diagnostics } +func (p wireguardParser) analyzeDNSContainsFallback() []protocol.Diagnostic { + lineNumber, property := p.fetchPropertyByName("DNS") + + if property == nil { + return []protocol.Diagnostic{} + } + + dnsAmount := len(strings.Split(property.Value.Value, ",")) + + if dnsAmount == 1 { + severity := protocol.DiagnosticSeverityWarning + return []protocol.Diagnostic{ + { + Message: "There is one DNS server specified. It is recommended to set up fallback DNS servers", + Severity: &severity, + Range: protocol.Range{ + Start: protocol.Position{ + Line: *lineNumber, + Character: property.Value.Location.Start, + }, + End: protocol.Position{ + Line: *lineNumber, + Character: property.Value.Location.End, + }, + }, + }, + } + } + + return []protocol.Diagnostic{} +} + func (p wireguardParser) checkIfValuesAreValid() []protocol.Diagnostic { diagnostics := []protocol.Diagnostic{} diff --git a/handlers/wireguard/wg-parser.go b/handlers/wireguard/wg-parser.go index 9eb81fe..1969aa7 100644 --- a/handlers/wireguard/wg-parser.go +++ b/handlers/wireguard/wg-parser.go @@ -33,6 +33,20 @@ type wireguardParser struct { LineIndexes map[uint32]wireguardLineIndex } +// Search for a property by name +// Returns (line number, property) +func (p *wireguardParser) fetchPropertyByName(name string) (*uint32, *wireguardProperty) { + for _, section := range p.Sections { + for lineNumber, property := range section.Properties { + if property.Key.Name == name { + return &lineNumber, &property + } + } + } + + return nil, nil +} + func (p *wireguardParser) clear() { p.Sections = []*wireguardSection{} p.CommentLines = map[uint32]struct{}{}