feat(wireguard): Add DNS fallback analyzer

This commit is contained in:
Myzel394 2024-08-18 16:08:00 +02:00
parent ffdf5bac46
commit 74698bb8c5
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
2 changed files with 48 additions and 0 deletions

View File

@ -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{}

View File

@ -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{}{}