Add PostUp and PostDown script for clients interface

This commit is contained in:
kevin 2022-08-28 14:09:52 +08:00
parent efbc36d61f
commit 05cfe56ab9
5 changed files with 43 additions and 3 deletions

View file

@ -331,6 +331,8 @@ func UpdateClient(db store.IStore) echo.HandlerFunc {
client.AllocatedIPs = _client.AllocatedIPs
client.AllowedIPs = _client.AllowedIPs
client.ExtraAllowedIPs = _client.ExtraAllowedIPs
client.PostUp = _client.PostUp
client.PostDown = _client.PostDown
client.UpdatedAt = time.Now().UTC()
// write to the database

View file

@ -17,6 +17,8 @@ type Client struct {
ExtraAllowedIPs []string `json:"extra_allowed_ips"`
UseServerDNS bool `json:"use_server_dns"`
Enabled bool `json:"enabled"`
PostUp string `json:"post_up"`
PostDown string `json:"post_down"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}

View file

@ -186,6 +186,14 @@
</label>
<input type="text" data-role="tagsinput" class="form-control" id="client_extra_allowed_ips" value="{{ StringsJoin .client_defaults.ExtraAllowedIps "," }}">
</div>
<div class="form-group">
<label for="client_post_up">Post Up Script</label>
<input type="text" class="form-control" id="client_post_up" name="client_post_up" placeholder="Post Up Script">
</div>
<div class="form-group">
<label for="client_post_down">Post Down Script</label>
<input type="text" class="form-control" id="client_post_down" name="client_post_down" placeholder="Post Down Script">
</div>
<div class="form-group">
<div class="icheck-primary d-inline">
<input type="checkbox" id="use_server_dns" {{ if .client_defaults.UseServerDNS }}checked{{ end }}>
@ -343,6 +351,8 @@
const allowed_ips = $("#client_allowed_ips").val().split(",");
let use_server_dns = false;
let extra_allowed_ips = [];
const post_up = $("#client_post_up").val();
const post_down = $("#client_post_down").val();
if ($("#client_extra_allowed_ips").val() !== "") {
extra_allowed_ips = $("#client_extra_allowed_ips").val().split(",");
@ -362,8 +372,8 @@
const preshared_key = $("#client_preshared_key").val();
const data = {"name": name, "email": email, "allocated_ips": allocated_ips, "allowed_ips": allowed_ips,
"extra_allowed_ips": extra_allowed_ips, "use_server_dns": use_server_dns, "enabled": enabled,
"public_key": public_key, "preshared_key": preshared_key};
"extra_allowed_ips": extra_allowed_ips, "post_up": post_up, "post_down": post_down, "use_server_dns": use_server_dns,
"enabled": enabled, "public_key": public_key, "preshared_key": preshared_key};
$.ajax({
cache: false,
@ -485,6 +495,8 @@
$("#client_preshared_key").val("");
$("#client_allocated_ips").importTags('');
$("#client_extra_allowed_ips").importTags('');
$("#client_post_up").val("");
$("#client_post_down").val("");
updateIPAllocationSuggestion();
});
});

View file

@ -109,6 +109,14 @@ Wireguard Clients
<input type="text" data-role="tagsinput" class="form-control"
id="_client_extra_allowed_ips">
</div>
<div class="form-group">
<label for="_client_post_up">Post Up Script</label>
<input type="text" class="form-control" id="_client_post_up"placeholder="Post Up Script">
</div>
<div class="form-group">
<label for="_client_post_down">Post Down Script</label>
<input type="text" class="form-control" id="_client_post_down" placeholder="Post Down Script">
</div>
<div class="form-group">
<div class="icheck-primary d-inline">
<input type="checkbox" id="_use_server_dns">
@ -365,6 +373,9 @@ Wireguard Clients
modal.find("#_client_extra_allowed_ips").addTag(obj);
});
modal.find("#_client_post_up").val(client.post_up);
modal.find("#_client_post_down").val(client.post_down);
modal.find("#_use_server_dns").prop("checked", client.use_server_dns);
modal.find("#_enabled").prop("checked", client.enabled);
},
@ -408,6 +419,8 @@ Wireguard Clients
const email = $("#_client_email").val();
const allocated_ips = $("#_client_allocated_ips").val().split(",");
const allowed_ips = $("#_client_allowed_ips").val().split(",");
const post_up = $("#_client_post_up").val();
const post_down = $("#_client_post_down").val();
let use_server_dns = false;
let extra_allowed_ips = [];
@ -426,7 +439,8 @@ Wireguard Clients
}
const data = {"id": client_id, "name": name, "email": email, "allocated_ips": allocated_ips,
"allowed_ips": allowed_ips, "extra_allowed_ips": extra_allowed_ips, "use_server_dns": use_server_dns, "enabled": enabled};
"allowed_ips": allowed_ips, "extra_allowed_ips": extra_allowed_ips, "post_up": post_up, "post_down": post_down,
"use_server_dns": use_server_dns, "enabled": enabled};
$.ajax({
cache: false,

View file

@ -28,6 +28,14 @@ func BuildClientConfig(client model.Client, server model.Server, setting model.G
if client.UseServerDNS {
clientDNS = fmt.Sprintf("DNS = %s\n", strings.Join(setting.DNSServers, ","))
}
clientPostUp := ""
if strings.TrimSpace(client.PostUp) != "" {
clientPostUp = fmt.Sprintf("PostUp = %s\n", client.PostUp)
}
clientPostDown := ""
if strings.TrimSpace(client.PostDown) != "" {
clientPostDown = fmt.Sprintf("PostDown = %s\n", client.PostDown)
}
// Peer section
peerPublicKey := fmt.Sprintf("PublicKey = %s\n", server.KeyPair.PublicKey)
@ -67,6 +75,8 @@ func BuildClientConfig(client model.Client, server model.Server, setting model.G
clientPrivateKey +
clientDNS +
forwardMark +
clientPostUp +
clientPostDown +
"\n[Peer]\n" +
peerPublicKey +
peerPresharedKey +