feat(wireguard): Add analyzer to check if PersistentKeepalive is set

This commit is contained in:
Myzel394 2024-08-18 22:30:30 +02:00
parent 911e080fbb
commit cdb9017c4d
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
2 changed files with 44 additions and 1 deletions

View File

@ -26,6 +26,7 @@ func (p wireguardParser) analyze() []protocol.Diagnostic {
diagnostics := []protocol.Diagnostic{} diagnostics := []protocol.Diagnostic{}
diagnostics = append(diagnostics, p.checkForDuplicateProperties()...) diagnostics = append(diagnostics, p.checkForDuplicateProperties()...)
diagnostics = append(diagnostics, p.analyzeDNSContainsFallback()...) diagnostics = append(diagnostics, p.analyzeDNSContainsFallback()...)
diagnostics = append(diagnostics, p.analyzeKeepAliveIsSet()...)
return diagnostics return diagnostics
} }
@ -89,7 +90,8 @@ func (p wireguardParser) analyzeDNSContainsFallback() []protocol.Diagnostic {
dnsAmount := len(strings.Split(property.Value.Value, ",")) dnsAmount := len(strings.Split(property.Value.Value, ","))
if dnsAmount == 1 { if dnsAmount == 1 {
severity := protocol.DiagnosticSeverityWarning severity := protocol.DiagnosticSeverityHint
return []protocol.Diagnostic{ return []protocol.Diagnostic{
{ {
Message: "There is one DNS server specified. It is recommended to set up fallback DNS servers", Message: "There is one DNS server specified. It is recommended to set up fallback DNS servers",
@ -111,6 +113,37 @@ func (p wireguardParser) analyzeDNSContainsFallback() []protocol.Diagnostic {
return []protocol.Diagnostic{} return []protocol.Diagnostic{}
} }
func (p wireguardParser) analyzeKeepAliveIsSet() []protocol.Diagnostic {
diagnostics := make([]protocol.Diagnostic, 0)
for _, section := range p.Sections {
if section.Name != nil && *section.Name == "Peer" {
// If an endpoint is set, then we should only check for the keepalive property
if section.fetchFirstProperty("Endpoint") != nil {
if section.fetchFirstProperty("PersistentKeepalive") == nil {
severity := protocol.DiagnosticSeverityHint
diagnostics = append(diagnostics, protocol.Diagnostic{
Message: "PersistentKeepalive is not set. It is recommended to set this property, as it helps to maintain the connection when users are behind NAT",
Severity: &severity,
Range: protocol.Range{
Start: protocol.Position{
Line: section.StartLine,
Character: 0,
},
End: protocol.Position{
Line: section.StartLine,
Character: 99999999,
},
},
})
}
}
}
}
return diagnostics
}
// Check if the values are valid. // Check if the values are valid.
// Assumes that sections have been analyzed already. // Assumes that sections have been analyzed already.
func (p wireguardParser) checkIfValuesAreValid() []protocol.Diagnostic { func (p wireguardParser) checkIfValuesAreValid() []protocol.Diagnostic {

View File

@ -62,6 +62,16 @@ func (s wireguardSection) String() string {
return fmt.Sprintf("[%s]; %d-%d: %v", name, s.StartLine, s.EndLine, s.Properties) return fmt.Sprintf("[%s]; %d-%d: %v", name, s.StartLine, s.EndLine, s.Properties)
} }
func (s *wireguardSection) fetchFirstProperty(name string) *wireguardProperty {
for _, property := range s.Properties {
if property.Key.Name == name {
return &property
}
}
return nil
}
func (s *wireguardSection) findProperty(lineNumber uint32) (*wireguardProperty, error) { func (s *wireguardSection) findProperty(lineNumber uint32) (*wireguardProperty, error) {
property, found := s.Properties[lineNumber] property, found := s.Properties[lineNumber]