diff --git a/handlers/wireguard/analyzer.go b/handlers/wireguard/analyzer.go index d03f6ba..235ec3f 100644 --- a/handlers/wireguard/analyzer.go +++ b/handlers/wireguard/analyzer.go @@ -26,6 +26,7 @@ func (p wireguardParser) analyze() []protocol.Diagnostic { diagnostics := []protocol.Diagnostic{} diagnostics = append(diagnostics, p.checkForDuplicateProperties()...) diagnostics = append(diagnostics, p.analyzeDNSContainsFallback()...) + diagnostics = append(diagnostics, p.analyzeKeepAliveIsSet()...) return diagnostics } @@ -89,7 +90,8 @@ func (p wireguardParser) analyzeDNSContainsFallback() []protocol.Diagnostic { dnsAmount := len(strings.Split(property.Value.Value, ",")) if dnsAmount == 1 { - severity := protocol.DiagnosticSeverityWarning + severity := protocol.DiagnosticSeverityHint + return []protocol.Diagnostic{ { 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{} } +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. // Assumes that sections have been analyzed already. func (p wireguardParser) checkIfValuesAreValid() []protocol.Diagnostic { diff --git a/handlers/wireguard/wg-section.go b/handlers/wireguard/wg-section.go index db8b0c0..62b65b8 100644 --- a/handlers/wireguard/wg-section.go +++ b/handlers/wireguard/wg-section.go @@ -62,6 +62,16 @@ func (s wireguardSection) String() string { 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) { property, found := s.Properties[lineNumber]