Skip to content

Commit

Permalink
Support for notes about clients (#527)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xCA committed Jan 9, 2024
1 parent 73108f7 commit ecea82d
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 3 deletions.
6 changes: 6 additions & 0 deletions custom/js/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ function renderClientList(data) {
subnetRangesString = obj.Client.subnet_ranges.join(',')
}

let additionalNotesHtml = "";
if (obj.Client.additional_notes && obj.Client.additional_notes.length > 0) {
additionalNotesHtml = `<span class="info-box-text" style="display: none"><i class="fas fa-additional_notes"></i>${obj.Client.additional_notes}</span>`
}

// render client html content
let html = `<div class="col-sm-6 col-md-6 col-lg-4" id="client_${obj.Client.id}">
<div class="info-box">
Expand Down Expand Up @@ -81,6 +86,7 @@ function renderClientList(data) {
<span class="info-box-text" style="display: none"><i class="fas fa-key"></i> ${obj.Client.public_key}</span>
<span class="info-box-text" style="display: none"><i class="fas fa-subnetrange"></i>${subnetRangesString}</span>
${telegramHtml}
${additionalNotesHtml}
<span class="info-box-text"><i class="fas fa-envelope"></i> ${obj.Client.email}</span>
<span class="info-box-text"><i class="fas fa-clock"></i>
${prettyDateTime(obj.Client.created_at)}</span>
Expand Down
1 change: 1 addition & 0 deletions handler/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,7 @@ func UpdateClient(db store.IStore) echo.HandlerFunc {
client.PublicKey = _client.PublicKey
client.PresharedKey = _client.PresharedKey
client.UpdatedAt = time.Now().UTC()
client.AdditionalNotes = strings.ReplaceAll(strings.Trim(_client.AdditionalNotes, "\r\n"), "\r\n", "\n")

// write to the database
if err := db.SaveClient(client); err != nil {
Expand Down
1 change: 1 addition & 0 deletions model/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Client struct {
AllowedIPs []string `json:"allowed_ips"`
ExtraAllowedIPs []string `json:"extra_allowed_ips"`
Endpoint string `json:"endpoint"`
AdditionalNotes string `json:"additional_notes"`
UseServerDNS bool `json:"use_server_dns"`
Enabled bool `json:"enabled"`
CreatedAt time.Time `json:"created_at"`
Expand Down
9 changes: 8 additions & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@ <h4 class="modal-title">New Wireguard Client</h4>
<label for="client_telegram_userid" class="control-label">Telegram userid</label>
<input type="text" class="form-control" id="client_telegram_userid" name="client_telegram_userid">
</div>
<div class="form-group">
<label for="additional_notes" class="control-label">Notes</label>
<textarea class="form-control" style="min-height: 6rem;" id="additional_notes" name="additional_notes" placeholder="Additional notes about this client"></textarea>
</div>
</details>
</div>
<div class="modal-footer justify-content-between">
Expand Down Expand Up @@ -480,10 +484,12 @@ <h1>{{template "page_title" .}}</h1>
}
const public_key = $("#client_public_key").val();
const preshared_key = $("#client_preshared_key").val();

const additional_notes = $("#additional_notes").val();

const data = {"name": name, "email": email, "telegram_userid": telegram_userid, "allocated_ips": allocated_ips, "allowed_ips": allowed_ips,
"extra_allowed_ips": extra_allowed_ips, "endpoint": endpoint, "use_server_dns": use_server_dns, "enabled": enabled,
"public_key": public_key, "preshared_key": preshared_key};
"public_key": public_key, "preshared_key": preshared_key, "additional_notes": additional_notes};

$.ajax({
cache: false,
Expand Down Expand Up @@ -624,6 +630,7 @@ <h1>{{template "page_title" .}}</h1>
$("#client_extra_allowed_ips").importTags('');
$("#client_endpoint").val('');
$("#client_telegram_userid").val('');
$("#additional_notes").val('');
updateSubnetRangesList("#subnet_ranges");
updateIPAllocationSuggestion(true);
});
Expand Down
15 changes: 14 additions & 1 deletion templates/clients.html
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ <h4 class="modal-title">Edit Client</h4>
<label for="_client_telegram_userid" class="control-label">Telegram userid</label>
<input type="text" class="form-control" id="_client_telegram_userid" name="_client_telegram_userid">
</div>
<div class="form-group">
<label for="_additional_notes" class="control-label">Notes</label>
<textarea class="form-control" style="min-height: 6rem;" id="_additional_notes" name="_additional_notes" placeholder="Additional notes about this client"></textarea>
</div>
</details>
</div>
<div class="modal-footer justify-content-between">
Expand Down Expand Up @@ -426,6 +430,11 @@ <h4 class="modal-title">Remove</h4>
$(this).closest('.col-lg-4').show();
}
})
$(".fa-additional_notes").each(function () {
if ($(this).parent().text().trim().indexOf(query.trim()) != -1) {
$(this).closest('.col-lg-4').show();
}
})
})

$("#status-selector").on('change', function () {
Expand Down Expand Up @@ -649,6 +658,8 @@ <h4 class="modal-title">Remove</h4>

modal.find("#_client_public_key").val(client.public_key);
modal.find("#_client_preshared_key").val(client.preshared_key);

modal.find("#_additional_notes").val(client.additional_notes);

// handle subnet range select
$('#_subnet_ranges').on('select2:select', function (e) {
Expand Down Expand Up @@ -770,9 +781,11 @@ <h4 class="modal-title">Remove</h4>
enabled = true;
}

const additional_notes = $("#_additional_notes").val();

const data = {"id": client_id, "name": name, "email": email, "telegram_userid": telegram_userid, "allocated_ips": allocated_ips,
"allowed_ips": allowed_ips, "extra_allowed_ips": extra_allowed_ips, "endpoint": endpoint,
"use_server_dns": use_server_dns, "enabled": enabled, "public_key": public_key, "preshared_key": preshared_key};
"use_server_dns": use_server_dns, "enabled": enabled, "public_key": public_key, "preshared_key": preshared_key, "additional_notes": additional_notes};

$.ajax({
cache: false,
Expand Down
4 changes: 4 additions & 0 deletions templates/wg.conf
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Table = {{ .globalSettings.Table }}
# Telegram: {{ .Client.TgUserid }}
# Created at: {{ .Client.CreatedAt }}
# Update at: {{ .Client.UpdatedAt }}
{{- if .Client.AdditionalNotes}}

# Notes:
# {{ .Client.AdditionalNotes }}{{end}}
[Peer]
PublicKey = {{ .Client.PublicKey }}
{{if .Client.PresharedKey}}PresharedKey = {{ .Client.PresharedKey }}{{end}}
Expand Down
11 changes: 10 additions & 1 deletion util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,15 @@ func WriteWireGuardServerConfig(tmplDir fs.FS, serverConfig model.Server, client
tmplWireguardConf = fileContent
}

// escape multiline notes
escapedClientDataList := []model.ClientData{}
for _, cd := range clientDataList {
if cd.Client.AdditionalNotes != "" {
cd.Client.AdditionalNotes = strings.ReplaceAll(cd.Client.AdditionalNotes, "\n", "\n# ")
}
escapedClientDataList = append(escapedClientDataList, cd)
}

// parse the template
t, err := template.New("wg_config").Parse(tmplWireguardConf)
if err != nil {
Expand All @@ -574,7 +583,7 @@ func WriteWireGuardServerConfig(tmplDir fs.FS, serverConfig model.Server, client

config := map[string]interface{}{
"serverConfig": serverConfig,
"clientDataList": clientDataList,
"clientDataList": escapedClientDataList,
"globalSettings": globalSettings,
"usersList": usersList,
}
Expand Down

0 comments on commit ecea82d

Please sign in to comment.