re-implementation of optional extraAllowedIPs in client entry

This commit is contained in:
Britt Dodd 2022-01-10 10:34:19 -05:00
parent 655375258e
commit a61c4fa65f
5 changed files with 66 additions and 14 deletions

View file

@ -160,6 +160,12 @@ func NewClient(db store.IStore) echo.HandlerFunc {
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Allowed IPs must be in CIDR format"}) return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Allowed IPs must be in CIDR format"})
} }
// validate extra AllowedIPs
if util.ValidateAllowedIPs(client.ExtraAllowedIPs) == false {
log.Warnf("Invalid Extra AllowedIPs input from user: %v", client.ExtraAllowedIPs)
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Extra AllowedIPs must be in CIDR format"})
}
// gen ID // gen ID
guid := xid.New() guid := xid.New()
client.ID = guid.String() client.ID = guid.String()
@ -274,6 +280,13 @@ func UpdateClient(db store.IStore) echo.HandlerFunc {
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Allowed IPs must be in CIDR format"}) return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Allowed IPs must be in CIDR format"})
} }
log.Infof("array length: %d", len(_client.ExtraAllowedIPs) )
log.Infof("extraAllowedIPs: %v", _client.ExtraAllowedIPs)
if len(_client.ExtraAllowedIPs) > 0 && util.ValidateAllowedIPs(_client.ExtraAllowedIPs) == false {
log.Warnf("Invalid Allowed IPs input from user: %v", _client.ExtraAllowedIPs)
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Extra Allowed IPs must be in CIDR format"})
}
// map new data // map new data
client.Name = _client.Name client.Name = _client.Name
client.Email = _client.Email client.Email = _client.Email
@ -281,6 +294,7 @@ func UpdateClient(db store.IStore) echo.HandlerFunc {
client.UseServerDNS = _client.UseServerDNS client.UseServerDNS = _client.UseServerDNS
client.AllocatedIPs = _client.AllocatedIPs client.AllocatedIPs = _client.AllocatedIPs
client.AllowedIPs = _client.AllowedIPs client.AllowedIPs = _client.AllowedIPs
client.ExtraAllowedIPs = _client.ExtraAllowedIPs
client.UpdatedAt = time.Now().UTC() client.UpdatedAt = time.Now().UTC()
// write to the database // write to the database

View file

@ -6,18 +6,19 @@ import (
// Client model // Client model
type Client struct { type Client struct {
ID string `json:"id"` ID string `json:"id"`
PrivateKey string `json:"private_key"` PrivateKey string `json:"private_key"`
PublicKey string `json:"public_key"` PublicKey string `json:"public_key"`
PresharedKey string `json:"preshared_key"` PresharedKey string `json:"preshared_key"`
Name string `json:"name"` Name string `json:"name"`
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"`
UseServerDNS bool `json:"use_server_dns"` ExtraAllowedIPs []string `json:"extra_allowed_ips"`
Enabled bool `json:"enabled"` UseServerDNS bool `json:"use_server_dns"`
CreatedAt time.Time `json:"created_at"` Enabled bool `json:"enabled"`
UpdatedAt time.Time `json:"updated_at"` CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
} }
// ClientData includes the Client and extra data // ClientData includes the Client and extra data

View file

@ -163,6 +163,11 @@
<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_extra_allowed_ips" class="control-label">Extra Allowed IPs</label>
<input type="text" data-role="tagsinput" class="form-control"
id="client_extra_allowed_ips">
</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="use_server_dns" checked> <input type="checkbox" id="use_server_dns" checked>
@ -376,6 +381,16 @@
'placeholderColor': '#666666' 'placeholderColor': '#666666'
}); });
$("#client_extra_allowed_ips").tagsInput({
'width': '100%',
'height': '75%',
'interactive': true,
'defaultText': 'Add More',
'removeWithBackspace': true,
'minChars': 0,
'placeholderColor': '#666666'
});
// New client form validation // New client form validation
$(document).ready(function () { $(document).ready(function () {
$.validator.setDefaults({ $.validator.setDefaults({
@ -414,6 +429,7 @@
$("#client_name").val(""); $("#client_name").val("");
$("#client_email").val(""); $("#client_email").val("");
$("#client_allocated_ips").importTags(''); $("#client_allocated_ips").importTags('');
$("#client_extra_allowed_ips").importTags('');
updateIPAllocationSuggestion(); updateIPAllocationSuggestion();
}); });
}); });

View file

@ -86,6 +86,11 @@ 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_extra_allowed_ips" class="control-label">Extra Allowed IPs</label>
<input type="text" data-role="tagsinput" class="form-control"
id="_client_extra_allowed_ips">
</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="_use_server_dns"> <input type="checkbox" id="_use_server_dns">
@ -302,6 +307,16 @@ Wireguard Clients
'placeholderColor': '#666666' 'placeholderColor': '#666666'
}); });
modal.find("#_client_extra_allowed_ips").tagsInput({
'width': '100%',
'height': '75%',
'interactive': true,
'defaultText': 'Add More',
'removeWithBackspace' : true,
'minChars': 0,
'placeholderColor': '#666666'
})
// update client modal data // update client modal data
$.ajax({ $.ajax({
cache: false, cache: false,
@ -327,6 +342,11 @@ Wireguard Clients
modal.find("#_client_allowed_ips").addTag(obj); modal.find("#_client_allowed_ips").addTag(obj);
}); });
modal.find("#_client_extra_allowed_ips").importTags('');
client.extra_allowed_ips.forEach(function (obj) {
modal.find("#_client_extra_allowed_ips").addTag(obj);
});
modal.find("#_use_server_dns").prop("checked", client.use_server_dns); modal.find("#_use_server_dns").prop("checked", client.use_server_dns);
modal.find("#_enabled").prop("checked", client.enabled); modal.find("#_enabled").prop("checked", client.enabled);
}, },
@ -370,6 +390,7 @@ Wireguard Clients
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 extra_allowed_ips = $("#_client_extra_allowed_ips").val().split(",");
let use_server_dns = false; let use_server_dns = false;
if ($("#_use_server_dns").is(':checked')){ if ($("#_use_server_dns").is(':checked')){
@ -383,7 +404,7 @@ Wireguard Clients
} }
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,
"allowed_ips": allowed_ips, "use_server_dns": use_server_dns, "enabled": enabled}; "allowed_ips": allowed_ips, "extra_allowed_ips": extra_allowed_ips, "use_server_dns": use_server_dns, "enabled": enabled};
$.ajax({ $.ajax({
cache: false, cache: false,

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}}{{$first :=true}}{{range .Client.AllowedIPs }}{{if ne . "0.0.0.0/0" }},{{.}}{{end}}{{end}} AllowedIPs = {{$first :=true}}{{range .Client.AllocatedIPs }}{{if $first}}{{$first = false}}{{else}},{{end}}{{.}}{{end}}{{$first :=true}}{{range .Client.ExtraAllowedIPs }},{{.}}{{end}}
{{end}}{{end}} {{end}}{{end}}