chore(wireguard): Improvements

This commit is contained in:
Myzel394 2024-08-19 22:33:42 +02:00
parent 394914a271
commit bc529ddfe8
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
6 changed files with 80 additions and 75 deletions

View File

@ -53,29 +53,18 @@ func (p wireguardParser) analyzeOnlyOneInterfaceSectionSpecified() []protocol.Di
diagnostics := []protocol.Diagnostic{} diagnostics := []protocol.Diagnostic{}
alreadyFound := false alreadyFound := false
for _, section := range p.Sections { for _, section := range p.getSectionsByName("Interface") {
if *section.Name == "Interface" {
if alreadyFound { if alreadyFound {
severity := protocol.DiagnosticSeverityError severity := protocol.DiagnosticSeverityError
diagnostics = append(diagnostics, protocol.Diagnostic{ diagnostics = append(diagnostics, protocol.Diagnostic{
Message: "Only one [Interface] section is allowed", Message: "Only one [Interface] section is allowed",
Severity: &severity, Severity: &severity,
Range: protocol.Range{ Range: section.getHeaderLineRange(),
Start: protocol.Position{
Line: section.StartLine,
Character: 0,
},
End: protocol.Position{
Line: section.StartLine,
Character: 99999999,
},
},
}) })
} }
alreadyFound = true alreadyFound = true
} }
}
return diagnostics return diagnostics
} }
@ -116,30 +105,17 @@ func (p wireguardParser) analyzeDNSContainsFallback() []protocol.Diagnostic {
func (p wireguardParser) analyzeKeepAliveIsSet() []protocol.Diagnostic { func (p wireguardParser) analyzeKeepAliveIsSet() []protocol.Diagnostic {
diagnostics := make([]protocol.Diagnostic, 0) diagnostics := make([]protocol.Diagnostic, 0)
for _, section := range p.Sections { for _, section := range p.getSectionsByName("Peer") {
if section.Name != nil && *section.Name == "Peer" {
// If an endpoint is set, then we should only check for the keepalive property // If an endpoint is set, then we should only check for the keepalive property
if section.fetchFirstProperty("Endpoint") != nil { if section.existsProperty("Endpoint") && !section.existsProperty("PersistentKeepalive") {
if section.fetchFirstProperty("PersistentKeepalive") == nil {
severity := protocol.DiagnosticSeverityHint severity := protocol.DiagnosticSeverityHint
diagnostics = append(diagnostics, protocol.Diagnostic{ 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", 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, Severity: &severity,
Range: protocol.Range{ Range: section.getRange(),
Start: protocol.Position{
Line: section.StartLine,
Character: 0,
},
End: protocol.Position{
Line: section.StartLine,
Character: 99999999,
},
},
}) })
} }
} }
}
}
return diagnostics return diagnostics
} }
@ -161,36 +137,27 @@ func (p wireguardParser) checkIfValuesAreValid() []protocol.Diagnostic {
return diagnostics return diagnostics
} }
func (p wireguardSection) analyzeSection() []protocol.Diagnostic { func (s wireguardSection) analyzeSection() []protocol.Diagnostic {
diagnostics := []protocol.Diagnostic{} diagnostics := []protocol.Diagnostic{}
if p.Name == nil { if s.Name == nil {
// No section name // No section name
severity := protocol.DiagnosticSeverityError severity := protocol.DiagnosticSeverityError
diagnostics = append(diagnostics, protocol.Diagnostic{ diagnostics = append(diagnostics, protocol.Diagnostic{
Message: "This section is missing a name", Message: "This section is missing a name",
Severity: &severity, Severity: &severity,
Range: p.getRange(), Range: s.getRange(),
}) })
return diagnostics return diagnostics
} }
if _, found := optionsHeaderMap[*p.Name]; !found { if _, found := optionsHeaderMap[*s.Name]; !found {
// Unknown section // Unknown section
severity := protocol.DiagnosticSeverityError severity := protocol.DiagnosticSeverityError
diagnostics = append(diagnostics, protocol.Diagnostic{ diagnostics = append(diagnostics, protocol.Diagnostic{
Message: fmt.Sprintf("Unknown section '%s'. It must be one of: [Interface], [Peer]", *p.Name), Message: fmt.Sprintf("Unknown section '%s'. It must be one of: [Interface], [Peer]", *s.Name),
Severity: &severity, Severity: &severity,
Range: protocol.Range{ Range: s.getHeaderLineRange(),
Start: protocol.Position{
Line: p.StartLine,
Character: 0,
},
End: protocol.Position{
Line: p.StartLine,
Character: 99999999,
},
},
}) })
return diagnostics return diagnostics
@ -238,16 +205,7 @@ func (p wireguardProperty) analyzeProperty(
{ {
Message: "Property is missing a value", Message: "Property is missing a value",
Severity: &severity, Severity: &severity,
Range: protocol.Range{ Range: p.getLineRange(propertyLine),
Start: protocol.Position{
Line: propertyLine,
Character: 0,
},
End: protocol.Position{
Line: propertyLine,
Character: 99999999,
},
},
}, },
} }
} }

View File

@ -46,6 +46,7 @@ You can also specify multiple subnets or IPv6 subnets like so:
Value: docvalues.IPAddressValue{ Value: docvalues.IPAddressValue{
AllowIPv4: true, AllowIPv4: true,
AllowIPv6: true, AllowIPv6: true,
AllowRange: true,
}, },
}, },
"ListenPort": { "ListenPort": {

View File

@ -10,7 +10,7 @@ func getKeepaliveCodeActions(
for index, section := range parser.Sections { for index, section := range parser.Sections {
if section.StartLine >= line && line <= section.EndLine && section.Name != nil && *section.Name == "Peer" { if section.StartLine >= line && line <= section.EndLine && section.Name != nil && *section.Name == "Peer" {
if section.fetchFirstProperty("Endpoint") != nil && section.fetchFirstProperty("PersistentKeepalive") == nil { if section.existsProperty("Endpoint") && !section.existsProperty("PersistentKeepalive") {
commandID := "wireguard." + codeActionAddKeepalive commandID := "wireguard." + codeActionAddKeepalive
command := protocol.Command{ command := protocol.Command{
Title: "Add PersistentKeepalive", Title: "Add PersistentKeepalive",

View File

@ -343,3 +343,15 @@ func (p *wireguardParser) getPropertyByLine(line uint32) (*wireguardSection, *wi
return section, property return section, property
} }
func (p *wireguardParser) getSectionsByName(name string) []*wireguardSection {
var sections []*wireguardSection
for _, section := range p.Sections {
if section.Name != nil && *section.Name == name {
sections = append(sections, section)
}
}
return sections
}

View File

@ -5,6 +5,8 @@ import (
"config-lsp/utils" "config-lsp/utils"
"regexp" "regexp"
"strings" "strings"
protocol "github.com/tliron/glsp/protocol_3_16"
) )
var linePattern = regexp.MustCompile(`^\s*(?P<key>.+?)\s*(?P<separator>=)\s*(?P<value>\S.*?)?\s*(?:(?:;|#).*)?\s*$`) var linePattern = regexp.MustCompile(`^\s*(?P<key>.+?)\s*(?P<separator>=)\s*(?P<value>\S.*?)?\s*(?:(?:;|#).*)?\s*$`)
@ -37,6 +39,19 @@ func (p wireguardProperty) String() string {
return p.Key.Name + "=" + p.Value.Value return p.Key.Name + "=" + p.Value.Value
} }
func (p wireguardProperty) getLineRange(line uint32) protocol.Range {
return protocol.Range{
Start: protocol.Position{
Line: line,
Character: p.Key.Location.Start,
},
End: protocol.Position{
Line: line,
Character: p.Key.Location.End,
},
}
}
func createWireguardProperty(line string) (*wireguardProperty, error) { func createWireguardProperty(line string) (*wireguardProperty, error) {
if !strings.Contains(line, "=") { if !strings.Contains(line, "=") {
indexes := utils.GetTrimIndex(line) indexes := utils.GetTrimIndex(line)

View File

@ -37,6 +37,19 @@ type wireguardSection struct {
Properties wireguardProperties Properties wireguardProperties
} }
func (s wireguardSection) getHeaderLineRange() protocol.Range {
return protocol.Range{
Start: protocol.Position{
Line: s.StartLine,
Character: 0,
},
End: protocol.Position{
Line: s.StartLine,
Character: 99999999,
},
}
}
func (s wireguardSection) getRange() protocol.Range { func (s wireguardSection) getRange() protocol.Range {
return protocol.Range{ return protocol.Range{
Start: protocol.Position{ Start: protocol.Position{
@ -62,14 +75,20 @@ 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 { func (s *wireguardSection) fetchFirstProperty(name string) (*uint32, *wireguardProperty) {
for _, property := range s.Properties { for line, property := range s.Properties {
if property.Key.Name == name { if property.Key.Name == name {
return &property return &line, &property
} }
} }
return nil return nil, nil
}
func (s *wireguardSection) existsProperty(name string) bool {
_, property := s.fetchFirstProperty(name)
return property != nil
} }
func (s *wireguardSection) findProperty(lineNumber uint32) (*wireguardProperty, error) { func (s *wireguardSection) findProperty(lineNumber uint32) (*wireguardProperty, error) {