mirror of
https://github.com/Myzel394/config-lsp.git
synced 2025-06-18 23:15:26 +02:00
chore(wireguard): Improvements
This commit is contained in:
parent
394914a271
commit
bc529ddfe8
@ -53,29 +53,18 @@ func (p wireguardParser) analyzeOnlyOneInterfaceSectionSpecified() []protocol.Di
|
||||
diagnostics := []protocol.Diagnostic{}
|
||||
alreadyFound := false
|
||||
|
||||
for _, section := range p.Sections {
|
||||
if *section.Name == "Interface" {
|
||||
for _, section := range p.getSectionsByName("Interface") {
|
||||
if alreadyFound {
|
||||
severity := protocol.DiagnosticSeverityError
|
||||
diagnostics = append(diagnostics, protocol.Diagnostic{
|
||||
Message: "Only one [Interface] section is allowed",
|
||||
Severity: &severity,
|
||||
Range: protocol.Range{
|
||||
Start: protocol.Position{
|
||||
Line: section.StartLine,
|
||||
Character: 0,
|
||||
},
|
||||
End: protocol.Position{
|
||||
Line: section.StartLine,
|
||||
Character: 99999999,
|
||||
},
|
||||
},
|
||||
Range: section.getHeaderLineRange(),
|
||||
})
|
||||
}
|
||||
|
||||
alreadyFound = true
|
||||
}
|
||||
}
|
||||
|
||||
return diagnostics
|
||||
}
|
||||
@ -116,30 +105,17 @@ func (p wireguardParser) analyzeDNSContainsFallback() []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" {
|
||||
for _, section := range p.getSectionsByName("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 {
|
||||
if section.existsProperty("Endpoint") && !section.existsProperty("PersistentKeepalive") {
|
||||
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,
|
||||
},
|
||||
},
|
||||
Range: section.getRange(),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return diagnostics
|
||||
}
|
||||
@ -161,36 +137,27 @@ func (p wireguardParser) checkIfValuesAreValid() []protocol.Diagnostic {
|
||||
return diagnostics
|
||||
}
|
||||
|
||||
func (p wireguardSection) analyzeSection() []protocol.Diagnostic {
|
||||
func (s wireguardSection) analyzeSection() []protocol.Diagnostic {
|
||||
diagnostics := []protocol.Diagnostic{}
|
||||
|
||||
if p.Name == nil {
|
||||
if s.Name == nil {
|
||||
// No section name
|
||||
severity := protocol.DiagnosticSeverityError
|
||||
diagnostics = append(diagnostics, protocol.Diagnostic{
|
||||
Message: "This section is missing a name",
|
||||
Severity: &severity,
|
||||
Range: p.getRange(),
|
||||
Range: s.getRange(),
|
||||
})
|
||||
return diagnostics
|
||||
}
|
||||
|
||||
if _, found := optionsHeaderMap[*p.Name]; !found {
|
||||
if _, found := optionsHeaderMap[*s.Name]; !found {
|
||||
// Unknown section
|
||||
severity := protocol.DiagnosticSeverityError
|
||||
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,
|
||||
Range: protocol.Range{
|
||||
Start: protocol.Position{
|
||||
Line: p.StartLine,
|
||||
Character: 0,
|
||||
},
|
||||
End: protocol.Position{
|
||||
Line: p.StartLine,
|
||||
Character: 99999999,
|
||||
},
|
||||
},
|
||||
Range: s.getHeaderLineRange(),
|
||||
})
|
||||
|
||||
return diagnostics
|
||||
@ -238,16 +205,7 @@ func (p wireguardProperty) analyzeProperty(
|
||||
{
|
||||
Message: "Property is missing a value",
|
||||
Severity: &severity,
|
||||
Range: protocol.Range{
|
||||
Start: protocol.Position{
|
||||
Line: propertyLine,
|
||||
Character: 0,
|
||||
},
|
||||
End: protocol.Position{
|
||||
Line: propertyLine,
|
||||
Character: 99999999,
|
||||
},
|
||||
},
|
||||
Range: p.getLineRange(propertyLine),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -46,6 +46,7 @@ You can also specify multiple subnets or IPv6 subnets like so:
|
||||
Value: docvalues.IPAddressValue{
|
||||
AllowIPv4: true,
|
||||
AllowIPv6: true,
|
||||
AllowRange: true,
|
||||
},
|
||||
},
|
||||
"ListenPort": {
|
||||
|
@ -10,7 +10,7 @@ func getKeepaliveCodeActions(
|
||||
|
||||
for index, section := range parser.Sections {
|
||||
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
|
||||
command := protocol.Command{
|
||||
Title: "Add PersistentKeepalive",
|
||||
|
@ -343,3 +343,15 @@ func (p *wireguardParser) getPropertyByLine(line uint32) (*wireguardSection, *wi
|
||||
|
||||
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
|
||||
}
|
||||
|
@ -5,6 +5,8 @@ import (
|
||||
"config-lsp/utils"
|
||||
"regexp"
|
||||
"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*$`)
|
||||
@ -37,6 +39,19 @@ func (p wireguardProperty) String() string {
|
||||
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) {
|
||||
if !strings.Contains(line, "=") {
|
||||
indexes := utils.GetTrimIndex(line)
|
||||
|
@ -37,6 +37,19 @@ type wireguardSection struct {
|
||||
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 {
|
||||
return protocol.Range{
|
||||
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)
|
||||
}
|
||||
|
||||
func (s *wireguardSection) fetchFirstProperty(name string) *wireguardProperty {
|
||||
for _, property := range s.Properties {
|
||||
func (s *wireguardSection) fetchFirstProperty(name string) (*uint32, *wireguardProperty) {
|
||||
for line, property := range s.Properties {
|
||||
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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user