From 4b1e52d71b6a652365dffd96bc77bece1576e6e4 Mon Sep 17 00:00:00 2001 From: jeremy bolster Date: Wed, 31 Aug 2022 17:17:42 -0700 Subject: [PATCH] Adds Support For Mesh Peers Adds support for mesh peers by adding fields to the clients. Support for "VPN" clients was not added -- there were no preUp, preDown fields added. --- core/client.go | 7 ++++++- model/client.go | 7 +++++++ template/template.go | 32 +++++++++++++++++++++++++++++--- ui/src/components/Clients.vue | 24 ++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 4 deletions(-) diff --git a/core/client.go b/core/client.go index 2deaacec..73087b93 100644 --- a/core/client.go +++ b/core/client.go @@ -185,6 +185,11 @@ func ReadClients() ([]*model.Client, error) { // ReadClientConfig in wg format func ReadClientConfig(id string) ([]byte, error) { + peers, err := ReadClients() + if err != nil { + return nil, err + } + client, err := ReadClient(id) if err != nil { return nil, err @@ -195,7 +200,7 @@ func ReadClientConfig(id string) ([]byte, error) { return nil, err } - configDataWg, err := template.DumpClientWg(client, server) + configDataWg, err := template.DumpClientWg(client, peers, server) if err != nil { return nil, err } diff --git a/model/client.go b/model/client.go index bc61bada..68cd9613 100644 --- a/model/client.go +++ b/model/client.go @@ -12,11 +12,14 @@ type Client struct { Name string `json:"name"` Email string `json:"email"` Enable bool `json:"enable"` + Endpoint string `json:"endpoint"` IgnorePersistentKeepalive bool `json:"ignorePersistentKeepalive"` PresharedKey string `json:"presharedKey"` AllowedIPs []string `json:"allowedIPs"` Address []string `json:"address"` Tags []string `json:"tags"` + ListenPort int `json:"listenPort"` + PersistentKeepalive int `json:"persistentKeepalive"` PrivateKey string `json:"privateKey"` PublicKey string `json:"publicKey"` CreatedBy string `json:"createdBy"` @@ -64,5 +67,9 @@ func (a Client) IsValid() []error { } } + if a.Endpoint != "" && a.ListenPort == 0 { + errs = append(errs, fmt.Errorf("if an endpoint is specified, a port to listen on is required")) + } + return errs } diff --git a/template/template.go b/template/template.go index abf443c3..0f72c3c7 100644 --- a/template/template.go +++ b/template/template.go @@ -206,6 +206,10 @@ DNS = {{ StringsJoin .Server.Dns ", " }} {{ if ne .Server.Mtu 0 -}} MTU = {{.Server.Mtu}} {{- end}} +{{ if ne .Client.Endpoint "" -}} +ListenPort = {{ .Client.ListenPort }} +{{- end }} + [Peer] PublicKey = {{ .Server.PublicKey }} PresharedKey = {{ .Client.PresharedKey }} @@ -214,6 +218,20 @@ Endpoint = {{ .Server.Endpoint }} {{ if and (ne .Server.PersistentKeepalive 0) (not .Client.IgnorePersistentKeepalive) -}} PersistentKeepalive = {{.Server.PersistentKeepalive}} {{- end}} +{{- range .Peers }} +{{ if and (ne .Id $.Client.Id) (and .Enable (ne .Endpoint "")) -}} +[Peer] +PublicKey = {{ .PublicKey }} +PresharedKey = {{ .PresharedKey }} +AllowedIPs = {{ StringsJoin .Address ", " }} +{{ if ne .Endpoint "" -}} +Endpoint = {{ .Endpoint }} +{{- end }} +{{ if and (ne $.Server.PersistentKeepalive 0) (not $.Client.IgnorePersistentKeepalive) -}} +PersistentKeepalive = {{ $.Server.PersistentKeepalive }} +{{- end }} +{{- end }} +{{ end }} ` wgTpl = `# Updated: {{ .Server.Updated }} / Created: {{ .Server.Created }} @@ -230,19 +248,25 @@ PreUp = {{ .Server.PreUp }} PostUp = {{ .Server.PostUp }} PreDown = {{ .Server.PreDown }} PostDown = {{ .Server.PostDown }} -{{- range .Clients }} +{{ range .Clients }} {{ if .Enable -}} # {{.Name}} / {{.Email}} / Updated: {{.Updated}} / Created: {{.Created}} [Peer] PublicKey = {{ .PublicKey }} PresharedKey = {{ .PresharedKey }} AllowedIPs = {{ StringsJoin .Address ", " }} +{{ if ne .Endpoint "" -}} +Endpoint = {{ .Endpoint }} +{{- end }} +{{ if ne .PersistentKeepalive 0 -}} +PersistentKeepalive = {{ .PersistentKeepalive }} +{{- end }} {{- end }} -{{ end }}` +{{- end }}` ) // DumpClientWg dump client wg config with go template -func DumpClientWg(client *model.Client, server *model.Server) ([]byte, error) { +func DumpClientWg(client *model.Client, peers []*model.Client, server *model.Server) ([]byte, error) { t, err := template.New("client").Funcs(template.FuncMap{"StringsJoin": strings.Join}).Parse(clientTpl) if err != nil { return nil, err @@ -250,9 +274,11 @@ func DumpClientWg(client *model.Client, server *model.Server) ([]byte, error) { return dump(t, struct { Client *model.Client + Peers []*model.Client Server *model.Server }{ Client: client, + Peers: peers, Server: server, }) } diff --git a/ui/src/components/Clients.vue b/ui/src/components/Clients.vue index 305e6954..4bc80bb8 100644 --- a/ui/src/components/Clients.vue +++ b/ui/src/components/Clients.vue @@ -256,6 +256,16 @@ label="Client email" :rules="[ v => (/.+@.+\..+/.test(v) || v === '') || 'E-mail must be valid',]" /> + + + +