add client postup & postdown

This commit is contained in:
sunyu 2020-09-25 15:35:23 +08:00
parent a89fb34555
commit 03428646e1
5 changed files with 37 additions and 5 deletions

View file

@ -3,11 +3,12 @@ package handler
import (
"encoding/json"
"fmt"
rice "github.com/GeertJohan/go.rice"
"net/http"
"strings"
"time"
rice "github.com/GeertJohan/go.rice"
"github.com/gorilla/sessions"
"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/v4"
@ -192,7 +193,6 @@ func NewClient() echo.HandlerFunc {
client.HasPrivateSubnet = true
}
}
// write client to the database
db.Write("clients", client.ID, client)
@ -256,6 +256,8 @@ func UpdateClient() echo.HandlerFunc {
client.AllocatedIPs = _client.AllocatedIPs
client.AllowedIPs = _client.AllowedIPs
client.PrivateSubnets = _client.PrivateSubnets
client.PostUp = _client.PostUp
client.PostDown = _client.PostDown
client.UpdatedAt = time.Now().UTC()
client.HasPrivateSubnet = false

View file

@ -19,6 +19,8 @@ type Client struct {
Enabled bool `json:"enabled"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
PostUp string `json:"post_up"`
PostDown string `json:"post_down"`
}
// ClientData includes the Client and extra data

View file

@ -157,6 +157,16 @@
<label for="client_private_subnets" class="control-label">Private Subnets</label>
<input type="text" data-role="tagsinput" class="form-control" id="client_private_subnets">
</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="enabled" checked>
@ -279,6 +289,8 @@
const allocated_ips = $("#client_allocated_ips").val().split(",");
const allowed_ips = $("#client_allowed_ips").val().split(",");
const private_subnets = $("#client_private_subnets").val().split(",");
const post_up = $("#client_post_up").val();
const post_down = $("#client_post_down").val();
let enabled = false;
if ($("#enabled").is(':checked')){
@ -286,7 +298,7 @@
}
const data = {"name": name, "email": email, "allocated_ips": allocated_ips, "allowed_ips": allowed_ips, "private_subnets": private_subnets,
"enabled": enabled};
"post_up": post_up, "post_down": post_down, "enabled": enabled};
$.ajax({
cache: false,

View file

@ -62,6 +62,14 @@ Wireguard Clients
<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">
<label for="_client_post_up" class="control-label">Post Up</label>
<input type="text" class="form-control" id="_client_post_up" name="_client_post_up">
</div>
<div class="form-group">
<label for="_client_post_down" class="control-label">Post Down</label>
<input type="text" class="form-control" id="_client_post_down" name="_client_post_down">
</div>
<div class="form-group">
<div class="icheck-primary d-inline">
<input type="checkbox" id="_enabled">
@ -312,6 +320,8 @@ Wireguard Clients
modal.find("#_client_private_subnets").addTag(obj);
});
modal.find("#_client_post_up").val(client.post_up);
modal.find("#_client_post_down").val(client.post_down);
modal.find("#_enabled").prop("checked", client.enabled);
},
error: function (jqXHR, exception) {
@ -330,6 +340,8 @@ Wireguard Clients
const allocated_ips = $("#_client_allocated_ips").val().split(",");
const allowed_ips = $("#_client_allowed_ips").val().split(",");
const private_subnets = $("#_client_private_subnets").val().split(",");
const post_up = $("#_client_post_up").val();
const post_down = $("#_client_post_down").val();
let enabled = false;
if ($("#_enabled").is(':checked')){
@ -337,7 +349,7 @@ Wireguard Clients
}
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, "post_up": post_up, "post_down": post_down, "enabled": enabled};
$.ajax({
cache: false,

View file

@ -22,6 +22,8 @@ func BuildClientConfig(client model.Client, server model.Server, setting model.G
clientAddress := fmt.Sprintf("Address = %s", strings.Join(client.AllocatedIPs, ","))
clientPrivateKey := fmt.Sprintf("PrivateKey = %s", client.PrivateKey)
clientDNS := fmt.Sprintf("DNS = %s", strings.Join(setting.DNSServers, ","))
clientPostUp := fmt.Sprintf("PostUp = %s", client.PostUp)
clientPostDown := fmt.Sprintf("PostDown = %s", client.PostDown)
// Peer section
peerPublicKey := fmt.Sprintf("PublicKey = %s", server.KeyPair.PublicKey)
@ -34,7 +36,9 @@ func BuildClientConfig(client model.Client, server model.Server, setting model.G
strConfig := "[Interface]\n" +
clientAddress + "\n" +
clientPrivateKey + "\n" +
clientDNS + "\n\n" +
clientDNS + "\n" +
clientPostUp + "\n" +
clientPostDown + "\n\n" +
"[Peer]" + "\n" +
peerPublicKey + "\n" +
peerPresharedKey + "\n" +