Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Support For Mesh Peers #109

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
7 changes: 7 additions & 0 deletions model/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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
}
32 changes: 29 additions & 3 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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 }}
Expand All @@ -230,29 +248,37 @@ 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
}

return dump(t, struct {
Client *model.Client
Peers []*model.Client
Server *model.Server
}{
Client: client,
Peers: peers,
Server: server,
})
}
Expand Down
24 changes: 24 additions & 0 deletions ui/src/components/Clients.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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, },
Expand Down Expand Up @@ -519,6 +541,8 @@
this.client = {
name: "",
email: "",
endpoint: "",
listenPort: 0,
enable: true,
allowedIPs: this.server.allowedips,
address: this.server.address,
Expand Down