This commit is contained in:
Jeremy Bolster 2023-02-01 06:20:34 +01:00 committed by GitHub
commit b38c1dd2e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 66 additions and 4 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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,
})
}

View File

@ -256,6 +256,16 @@
label="Client email"
:rules="[ v => (/.+@.+\..+/.test(v) || v === '') || 'E-mail must be valid',]"
/>
<v-text-field
v-model="client.endpoint"
label="Static Endpoint"
:rules="[ v => (/.+:.+/.test(v) || v === '') || 'Endpoint must contain port',]"
/>
<v-text-field
v-model.number="client.listenPort"
label="Listening Port"
type="number"
/>
<v-select
v-model="client.address"
:items="server.address"
@ -370,6 +380,16 @@
:rules="[ v => (/.+@.+\..+/.test(v) || v === '') || 'E-mail must be valid',]"
required
/>
<v-text-field
v-model="client.endpoint"
label="Static Endpoint"
:rules="[ v => (/.+:.+/.test(v) || v === '') || 'Endpoint must contain port',]"
/>
<v-text-field
v-model.number="client.listenPort"
label="Listening Port"
type="number"
/>
<v-combobox
v-model="client.address"
chips
@ -479,6 +499,8 @@
{ text: 'Name', value: 'name', },
{ text: 'Email', value: 'email', },
{ text: 'IP addresses', value: 'address', },
{ text: 'Endpoint', value: 'endpoint', },
{ text: 'ListenPort', value: 'listenPort', },
{ text: 'Tags', value: 'tags', },
{ text: 'Created', value: 'created', sortable: false, },
{ text: 'Updated', value: 'updated', sortable: false, },
@ -519,6 +541,8 @@
this.client = {
name: "",
email: "",
endpoint: "",
listenPort: 0,
enable: true,
allowedIPs: this.server.allowedips,
address: this.server.address,