From dfba267b405c0883e7fbdc885fc4adf2ee1ba660 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sat, 17 Aug 2024 14:26:14 +0200 Subject: [PATCH] current stand --- flake.lock | 116 +++++++++++++++++++++ flake.nix | 39 +++++++ handlers/wireguard/analyzer.go | 4 + handlers/wireguard/documentation-fields.go | 10 +- handlers/wireguard/wg-parser.go | 32 ++---- handlers/wireguard/wg-section.go | 12 +-- nvim-lsp-debug.lua | 2 +- 7 files changed, 180 insertions(+), 35 deletions(-) create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..77814e0 --- /dev/null +++ b/flake.lock @@ -0,0 +1,116 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1694529238, + "narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "ff7b65b44d01cf9ba6a71320833626af21126384", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "gomod2nix": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1722589758, + "narHash": "sha256-sbbA8b6Q2vB/t/r1znHawoXLysCyD4L/6n6/RykiSnA=", + "owner": "tweag", + "repo": "gomod2nix", + "rev": "4e08ca09253ef996bd4c03afa383b23e35fe28a1", + "type": "github" + }, + "original": { + "owner": "tweag", + "repo": "gomod2nix", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1723637854, + "narHash": "sha256-med8+5DSWa2UnOqtdICndjDAEjxr5D7zaIiK4pn0Q7c=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "c3aa7b8938b17aebd2deecf7be0636000d62a2b9", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "gomod2nix": "gomod2nix", + "nixpkgs": "nixpkgs", + "utils": "utils" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "systems_2": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "utils": { + "inputs": { + "systems": "systems_2" + }, + "locked": { + "lastModified": 1710146030, + "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..d64a7ac --- /dev/null +++ b/flake.nix @@ -0,0 +1,39 @@ +{ + description = "Build config-lsp"; + + inputs = { + nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; + gomod2nix = { + url = "github:tweag/gomod2nix"; + inputs.nixpkgs.follows = "nixpkgs"; + inputs.utils.follows = "utils"; + }; + utils.url = "github:numtide/flake-utils"; + }; + + outputs = { self, nixpkgs, utils, gomod2nix }: + utils.lib.eachDefaultSystem (system: + let + pkgs = import nixpkgs { + inherit system; + overlays = [ + (final: prev: { + go = prev.go_1_22; + buildGoModule = prev.buildGo122Module; + }) + gomod2nix.overlays.default + ]; + }; + in { + defaultPackage = pkgs.buildGoModule { + pname = "github.com/Myzel394/config-lsp"; + version = "v0.0.1"; + src = ./.; + vendorHash = "sha256-gv73FvSEhNKPnlyfTgSxum1T2T711+xrhJFmKMuCAQs="; + }; + devShell = pkgs.mkShell { + buildInputs = [ pkgs.go_1_22 ]; + }; + } + ); +} diff --git a/handlers/wireguard/analyzer.go b/handlers/wireguard/analyzer.go index acab531..d5a30df 100644 --- a/handlers/wireguard/analyzer.go +++ b/handlers/wireguard/analyzer.go @@ -84,3 +84,7 @@ func (p wireguardSection) analyzeInterfaceSection() []protocol.Diagnostic { return diagnostics } + +func (p wireguardParser) analyzeAllowedIPIsInRange() []protocol.Diagnostic { + +} diff --git a/handlers/wireguard/documentation-fields.go b/handlers/wireguard/documentation-fields.go index 4ef1081..6bf383f 100644 --- a/handlers/wireguard/documentation-fields.go +++ b/handlers/wireguard/documentation-fields.go @@ -214,10 +214,6 @@ Remove the iptables rule that forwards packets on the WireGuard interface `, Value: docvalues.StringValue{}, }, - "PreSharedKey": { - Documentation: "Optionally defines a pre-shared key for the peer, used to authenticate the connection. This is not necessary, but strongly recommended for security.", - Value: docvalues.StringValue{}, - }, } var interfaceAllowedDuplicateFields = map[string]struct{}{ @@ -309,10 +305,12 @@ Oocal NAT-ed node to remote public node `, Value: docvalues.PositiveNumberValue(), }, - "PreSharedKey": { + "PresharedKey": { Documentation: "Optionally defines a pre-shared key for the peer, used to authenticate the connection. This is not necessary, but strongly recommended for security.", Value: docvalues.StringValue{}, }, } -var peerAllowedDuplicateFields = map[string]struct{}{} +var peerAllowedDuplicateFields = map[string]struct{}{ + "AllowedIPs": {}, +} diff --git a/handlers/wireguard/wg-parser.go b/handlers/wireguard/wg-parser.go index 64c68d8..a9ece8e 100644 --- a/handlers/wireguard/wg-parser.go +++ b/handlers/wireguard/wg-parser.go @@ -19,30 +19,22 @@ type characterLocation struct { } type wireguardParser struct { - Sections []wireguardSection + GlobalSection *wireguardSection + // : if nil then does not belong to a section + Sections map[string]wireguardSection // Used to identify where not to show diagnostics CommentLines map[uint32]struct{} } func (p *wireguardParser) clear() { - p.Sections = []wireguardSection{} + p.Sections = map[string]wireguardSection{} p.CommentLines = map[uint32]struct{}{} } -func (p wireguardParser) hasInterfaceSection() bool { - for _, section := range p.Sections { - if section.Name != nil && *section.Name == "Interface" { - return true - } - } - - return false -} - func (p wireguardParser) getRootCompletionsForEmptyLine() []protocol.CompletionItem { completions := []protocol.CompletionItem{} - if !p.hasInterfaceSection() { + if _, found := p.Sections["Interface"]; !found { completions = append(completions, headerInterfaceEnum.ToCompletionItem()) } @@ -134,13 +126,14 @@ func (p *wireguardParser) parseFromString(input string) []common.ParseError { lastLine = *lastPropertyLine } - section := createWireguardSection( + name, section := createWireguardSection( currentLineNumber, lastLine, line, collectedProperties, ) - p.Sections = append(p.Sections, section) + + p.Sections[name] = section // Reset collectedProperties = wireguardProperties{} @@ -149,18 +142,13 @@ func (p *wireguardParser) parseFromString(input string) []common.ParseError { } if len(collectedProperties) > 0 { - p.Sections = append(p.Sections, wireguardSection{ + p.GlobalSection = &wireguardSection{ StartLine: *earliestPropertyLine, EndLine: *lastPropertyLine, - Name: nil, Properties: collectedProperties, - }) + } } - // Since we parse the content from bottom to top, - // we need to reverse the order - slices.Reverse(p.Sections) - return errors } diff --git a/handlers/wireguard/wg-section.go b/handlers/wireguard/wg-section.go index 0670127..f14f3a3 100644 --- a/handlers/wireguard/wg-section.go +++ b/handlers/wireguard/wg-section.go @@ -5,6 +5,7 @@ import ( "config-lsp/utils" "fmt" "maps" + "math" "regexp" protocol "github.com/tliron/glsp/protocol_3_16" @@ -25,8 +26,6 @@ func (e propertyNotFullyTypedError) Error() string { type wireguardSection struct { StartLine uint32 EndLine uint32 - // nil = do not belong to a section - Name *string Properties wireguardProperties } @@ -34,7 +33,7 @@ func (s wireguardSection) String() string { var name string if s.Name == nil { - name = "////" + name = "" } else { name = *s.Name } @@ -177,12 +176,14 @@ func (p wireguardSection) getCompletionsForPropertyLine( var validHeaderPattern = regexp.MustCompile(`^\s*\[(?P
.+?)\]\s*$`) +// Create a new create section +// Return (, ) func createWireguardSection( startLine uint32, endLine uint32, headerLine string, props wireguardProperties, -) wireguardSection { +) (string, wireguardSection) { match := validHeaderPattern.FindStringSubmatch(headerLine) var header string @@ -194,10 +195,9 @@ func createWireguardSection( header = match[1] } - return wireguardSection{ + return header, wireguardSection{ StartLine: startLine, EndLine: endLine, - Name: &header, Properties: props, } } diff --git a/nvim-lsp-debug.lua b/nvim-lsp-debug.lua index c869925..20bc0b2 100644 --- a/nvim-lsp-debug.lua +++ b/nvim-lsp-debug.lua @@ -1,5 +1,5 @@ vim.lsp.start { name = "config-lsp", - cmd = { "./bin/config-lsp" }, + cmd = { "./result/bin/config-lsp" }, root_dir = vim.fn.getcwd(), };