add private subnets

This commit is contained in:
sunyu 2020-09-24 19:38:52 +08:00
parent c205a04443
commit 453e9fcf5d
5 changed files with 63 additions and 15 deletions

View file

@ -18,6 +18,12 @@ function renderClientList(data) {
allowedIpsHtml += `<small class="badge badge-secondary">${obj}</small>&nbsp;`; allowedIpsHtml += `<small class="badge badge-secondary">${obj}</small>&nbsp;`;
}) })
// render client private subnets
let privateSubnetsHtml = "";
$.each(obj.Client.private_subnets, function(index, obj) {
privateSubnetsHtml += `<small class="badge badge-secondary">${obj}</small>&nbsp;`;
})
// render client html content // render client html content
let html = `<div class="col-sm-6" id="client_${obj.Client.id}"> let html = `<div class="col-sm-6" id="client_${obj.Client.id}">
<div class="info-box"> <div class="info-box">
@ -50,6 +56,8 @@ function renderClientList(data) {
+ allocatedIpsHtml + allocatedIpsHtml
+ `<span class="info-box-text"><strong>Allowed IPs</strong></span>` + `<span class="info-box-text"><strong>Allowed IPs</strong></span>`
+ allowedIpsHtml + allowedIpsHtml
+ `<span class="info-box-text"><strong>Private Subnets</strong></span>`
+ privateSubnetsHtml
+`</div> +`</div>
</div> </div>
</div>` </div>`

View file

@ -14,6 +14,7 @@ type Client struct {
Email string `json:"email"` Email string `json:"email"`
AllocatedIPs []string `json:"allocated_ips"` AllocatedIPs []string `json:"allocated_ips"`
AllowedIPs []string `json:"allowed_ips"` AllowedIPs []string `json:"allowed_ips"`
PrivateSubnets []string `json:"private_subnets"`
Enabled bool `json:"enabled"` Enabled bool `json:"enabled"`
CreatedAt time.Time `json:"created_at"` CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"` UpdatedAt time.Time `json:"updated_at"`

View file

@ -153,6 +153,10 @@
<input type="text" data-role="tagsinput" class="form-control" id="client_allowed_ips" <input type="text" data-role="tagsinput" class="form-control" id="client_allowed_ips"
value="0.0.0.0/0"> value="0.0.0.0/0">
</div> </div>
<div class="form-group">
<label for="client_private_subnets" class="control-label">Allowed IPs</label>
<input type="text" data-role="tagsinput" class="form-control" id="client_private_subnets">
</div>
<div class="form-group"> <div class="form-group">
<div class="icheck-primary d-inline"> <div class="icheck-primary d-inline">
<input type="checkbox" id="enabled" checked> <input type="checkbox" id="enabled" checked>
@ -274,13 +278,14 @@
const email = $("#client_email").val(); const email = $("#client_email").val();
const allocated_ips = $("#client_allocated_ips").val().split(","); const allocated_ips = $("#client_allocated_ips").val().split(",");
const allowed_ips = $("#client_allowed_ips").val().split(","); const allowed_ips = $("#client_allowed_ips").val().split(",");
const private_subnets = $("#client_private_subnets").val().split(",");
let enabled = false; let enabled = false;
if ($("#enabled").is(':checked')){ if ($("#enabled").is(':checked')){
enabled = true; enabled = true;
} }
const data = {"name": name, "email": email, "allocated_ips": allocated_ips, "allowed_ips": allowed_ips, const data = {"name": name, "email": email, "allocated_ips": allocated_ips, "allowed_ips": allowed_ips, "private_subnets": private_subnets,
"enabled": enabled}; "enabled": enabled};
$.ajax({ $.ajax({
@ -354,6 +359,18 @@
'placeholderColor': '#666666' 'placeholderColor': '#666666'
}); });
// PrivateSubnets tag input
$("#client_private_subnets").tagsInput({
'width': '100%',
'height': '75%',
'interactive': true,
'defaultText': 'Add More',
'removeWithBackspace': true,
'minChars': 0,
'maxChars': 18,
'placeholderColor': '#666666'
});
// New client form validation // New client form validation
$(document).ready(function () { $(document).ready(function () {
$.validator.setDefaults({ $.validator.setDefaults({

View file

@ -58,6 +58,10 @@ Wireguard Clients
<label for="_client_allowed_ips" class="control-label">Allowed IPs</label> <label for="_client_allowed_ips" class="control-label">Allowed IPs</label>
<input type="text" data-role="tagsinput" class="form-control" id="_client_allowed_ips"> <input type="text" data-role="tagsinput" class="form-control" id="_client_allowed_ips">
</div> </div>
<div class="form-group">
<label for="_client_private_subnets" class="control-label">Private Subnet</label>
<input type="text" data-role="tagsinput" class="form-control" id="_client_private_subnets">
</div>
<div class="form-group"> <div class="form-group">
<div class="icheck-primary d-inline"> <div class="icheck-primary d-inline">
<input type="checkbox" id="_enabled"> <input type="checkbox" id="_enabled">
@ -266,6 +270,18 @@ Wireguard Clients
'placeholderColor': '#666666' 'placeholderColor': '#666666'
}); });
// PrivateSubnet tag input
modal.find("#_client_private_subnets").tagsInput({
'width': '100%',
'height': '75%',
'interactive': true,
'defaultText': 'Add More',
'removeWithBackspace': true,
'minChars': 0,
'maxChars': 18,
'placeholderColor': '#666666'
});
// update client modal data // update client modal data
$.ajax({ $.ajax({
cache: false, cache: false,
@ -291,6 +307,11 @@ Wireguard Clients
modal.find("#_client_allowed_ips").addTag(obj); modal.find("#_client_allowed_ips").addTag(obj);
}); });
modal.find("#_client_private_subnets").importTags('');
client.private_subnets.forEach(function (obj) {
modal.find("#_client_private_subnets").addTag(obj);
});
modal.find("#_enabled").prop("checked", client.enabled); modal.find("#_enabled").prop("checked", client.enabled);
}, },
error: function (jqXHR, exception) { error: function (jqXHR, exception) {
@ -306,15 +327,16 @@ Wireguard Clients
const client_id = $("#_client_id").val(); const client_id = $("#_client_id").val();
const name = $("#_client_name").val(); const name = $("#_client_name").val();
const email = $("#_client_email").val(); const email = $("#_client_email").val();
const allocated_ips = $("#_client_allocated_ips").val().split(","); const allocated_ips = $("#1").val().split(",");
const allowed_ips = $("#_client_allowed_ips").val().split(","); const allowed_ips = $("#_client_allowed_ips").val().split(",");
const private_subnets = $("#_client_private_subnets").val().split(",");
let enabled = false; let enabled = false;
if ($("#_enabled").is(':checked')){ if ($("#_enabled").is(':checked')){
enabled = true; enabled = true;
} }
const data = {"id": client_id, "name": name, "email": email, "allocated_ips": allocated_ips, const data = {"id": client_id, "name": name, "email": email, "allocated_ips": allocated_ips, "private_subnets": private_subnets,
"allowed_ips": allowed_ips, "enabled": enabled}; "allowed_ips": allowed_ips, "enabled": enabled};
$.ajax({ $.ajax({

View file

@ -20,5 +20,5 @@ PostDown = {{ .serverConfig.Interface.PostDown }}
[Peer] [Peer]
PublicKey = {{ .Client.PublicKey }} PublicKey = {{ .Client.PublicKey }}
PresharedKey = {{ .Client.PresharedKey }} PresharedKey = {{ .Client.PresharedKey }}
AllowedIPs = {{$first :=true}}{{range .Client.AllocatedIPs }}{{if $first}}{{$first = false}}{{else}},{{end}}{{.}}{{end}} AllowedIPs = {{$first :=true}}{{range .Client.AllocatedIPs }}{{if $first}}{{$first = false}}{{else}},{{end}}{{.}}{{end}}{{if .Client.PrivateSubnets}},{{end}}{{$first :=true}}{{range .Client.PrivateSubnets }}{{if $first}}{{$first = false}}{{else}},{{end}}{{.}}{{end}}
{{end}}{{end}} {{end}}{{end}}