diff --git a/core/client.go b/core/client.go index 2deaace..73087b9 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 bc61bad..68cd961 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 abf443c..0f72c3c 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 }} -{{ end }}` +{{ if ne .PersistentKeepalive 0 -}} +PersistentKeepalive = {{ .PersistentKeepalive }} +{{- 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 351b937..3bd0339 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',]" /> + + + +