add client postup & postdown
This commit is contained in:
		
							parent
							
								
									a89fb34555
								
							
						
					
					
						commit
						03428646e1
					
				| 
						 | 
					@ -3,11 +3,12 @@ package handler
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	rice "github.com/GeertJohan/go.rice"
 | 
					 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						rice "github.com/GeertJohan/go.rice"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/gorilla/sessions"
 | 
						"github.com/gorilla/sessions"
 | 
				
			||||||
	"github.com/labstack/echo-contrib/session"
 | 
						"github.com/labstack/echo-contrib/session"
 | 
				
			||||||
	"github.com/labstack/echo/v4"
 | 
						"github.com/labstack/echo/v4"
 | 
				
			||||||
| 
						 | 
					@ -193,7 +194,6 @@ func NewClient() echo.HandlerFunc {
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
		// write client to the database
 | 
							// write client to the database
 | 
				
			||||||
		db.Write("clients", client.ID, client)
 | 
							db.Write("clients", client.ID, client)
 | 
				
			||||||
		log.Infof("Created wireguard client: %v", client)
 | 
							log.Infof("Created wireguard client: %v", client)
 | 
				
			||||||
| 
						 | 
					@ -256,6 +256,8 @@ func UpdateClient() echo.HandlerFunc {
 | 
				
			||||||
		client.AllocatedIPs = _client.AllocatedIPs
 | 
							client.AllocatedIPs = _client.AllocatedIPs
 | 
				
			||||||
		client.AllowedIPs = _client.AllowedIPs
 | 
							client.AllowedIPs = _client.AllowedIPs
 | 
				
			||||||
		client.PrivateSubnets = _client.PrivateSubnets
 | 
							client.PrivateSubnets = _client.PrivateSubnets
 | 
				
			||||||
 | 
							client.PostUp = _client.PostUp
 | 
				
			||||||
 | 
							client.PostDown = _client.PostDown
 | 
				
			||||||
		client.UpdatedAt = time.Now().UTC()
 | 
							client.UpdatedAt = time.Now().UTC()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		client.HasPrivateSubnet = false
 | 
							client.HasPrivateSubnet = false
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -19,6 +19,8 @@ type Client struct {
 | 
				
			||||||
	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"`
 | 
				
			||||||
 | 
						PostUp           string    `json:"post_up"`
 | 
				
			||||||
 | 
						PostDown         string    `json:"post_down"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// ClientData includes the Client and extra data
 | 
					// ClientData includes the Client and extra data
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -157,6 +157,16 @@
 | 
				
			||||||
                                <label for="client_private_subnets" class="control-label">Private Subnets</label>
 | 
					                                <label for="client_private_subnets" class="control-label">Private Subnets</label>
 | 
				
			||||||
                                <input type="text" data-role="tagsinput" class="form-control" id="client_private_subnets">
 | 
					                                <input type="text" data-role="tagsinput" class="form-control" id="client_private_subnets">
 | 
				
			||||||
                            </div>
 | 
					                            </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="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>
 | 
				
			||||||
| 
						 | 
					@ -279,6 +289,8 @@
 | 
				
			||||||
            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(",");
 | 
					            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;
 | 
					            let enabled = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if ($("#enabled").is(':checked')){
 | 
					            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,
 | 
					            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({
 | 
					            $.ajax({
 | 
				
			||||||
                cache: false,
 | 
					                cache: false,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -62,6 +62,14 @@ Wireguard Clients
 | 
				
			||||||
                        <label for="_client_private_subnets" class="control-label">Private Subnet</label>
 | 
					                        <label for="_client_private_subnets" class="control-label">Private Subnet</label>
 | 
				
			||||||
                        <input type="text" data-role="tagsinput" class="form-control" id="_client_private_subnets">
 | 
					                        <input type="text" data-role="tagsinput" class="form-control" id="_client_private_subnets">
 | 
				
			||||||
                    </div>
 | 
					                    </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="form-group">
 | 
				
			||||||
                        <div class="icheck-primary d-inline">
 | 
					                        <div class="icheck-primary d-inline">
 | 
				
			||||||
                            <input type="checkbox" id="_enabled">
 | 
					                            <input type="checkbox" id="_enabled">
 | 
				
			||||||
| 
						 | 
					@ -312,6 +320,8 @@ Wireguard Clients
 | 
				
			||||||
                            modal.find("#_client_private_subnets").addTag(obj);
 | 
					                            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);
 | 
					                        modal.find("#_enabled").prop("checked", client.enabled);
 | 
				
			||||||
                    },
 | 
					                    },
 | 
				
			||||||
                    error: function (jqXHR, exception) {
 | 
					                    error: function (jqXHR, exception) {
 | 
				
			||||||
| 
						 | 
					@ -330,6 +340,8 @@ Wireguard Clients
 | 
				
			||||||
            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(",");
 | 
					            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;
 | 
					            let enabled = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if ($("#_enabled").is(':checked')){
 | 
					            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,
 | 
					            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({
 | 
					            $.ajax({
 | 
				
			||||||
                cache: false,
 | 
					                cache: false,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -22,6 +22,8 @@ func BuildClientConfig(client model.Client, server model.Server, setting model.G
 | 
				
			||||||
	clientAddress := fmt.Sprintf("Address = %s", strings.Join(client.AllocatedIPs, ","))
 | 
						clientAddress := fmt.Sprintf("Address = %s", strings.Join(client.AllocatedIPs, ","))
 | 
				
			||||||
	clientPrivateKey := fmt.Sprintf("PrivateKey = %s", client.PrivateKey)
 | 
						clientPrivateKey := fmt.Sprintf("PrivateKey = %s", client.PrivateKey)
 | 
				
			||||||
	clientDNS := fmt.Sprintf("DNS = %s", strings.Join(setting.DNSServers, ","))
 | 
						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
 | 
						// Peer section
 | 
				
			||||||
	peerPublicKey := fmt.Sprintf("PublicKey = %s", server.KeyPair.PublicKey)
 | 
						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" +
 | 
						strConfig := "[Interface]\n" +
 | 
				
			||||||
		clientAddress + "\n" +
 | 
							clientAddress + "\n" +
 | 
				
			||||||
		clientPrivateKey + "\n" +
 | 
							clientPrivateKey + "\n" +
 | 
				
			||||||
		clientDNS + "\n\n" +
 | 
							clientDNS + "\n" +
 | 
				
			||||||
 | 
							clientPostUp + "\n" +
 | 
				
			||||||
 | 
							clientPostDown + "\n\n" +
 | 
				
			||||||
		"[Peer]" + "\n" +
 | 
							"[Peer]" + "\n" +
 | 
				
			||||||
		peerPublicKey + "\n" +
 | 
							peerPublicKey + "\n" +
 | 
				
			||||||
		peerPresharedKey + "\n" +
 | 
							peerPresharedKey + "\n" +
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue