From d5ff0cb704ded3c4e693e70f6f23c8a93a337cee Mon Sep 17 00:00:00 2001 From: Khanh Ngo Date: Mon, 20 Apr 2020 00:15:25 +0700 Subject: [PATCH] Add wireguard server key pair generation --- handler/routes.go | 40 ++++++++++++++++++++-- main.go | 1 + model/server.go | 2 +- templates/clients.html | 2 +- templates/server.html | 76 +++++++++++++++++++++++++++++++++++++++--- 5 files changed, 113 insertions(+), 8 deletions(-) diff --git a/handler/routes.go b/handler/routes.go index 03a51b4..ed83c26 100644 --- a/handler/routes.go +++ b/handler/routes.go @@ -76,10 +76,11 @@ func NewClient() echo.HandlerFunc { guid := xid.New() client.ID = guid.String() - // gen Wireguard key pairs + // gen Wireguard key pair key, err := wgtypes.GeneratePrivateKey() if err != nil { - return err + log.Error("Cannot generate wireguard key pair: ", err) + return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, "Cannot generate Wireguard key pair"}) } client.PrivateKey = key.String() client.PublicKey = key.PublicKey().String() @@ -139,9 +140,15 @@ func WireGuardServer() echo.HandlerFunc { log.Error("Cannot fetch server interface config from database: ", err) } + serverKeyPair := model.ServerKeypair{} + if err := db.Read("server", "keypair", &serverKeyPair); err != nil { + log.Error("Cannot fetch server key pair from database: ", err) + } + return c.Render(http.StatusOK, "server.html", map[string]interface{}{ "name": "Khanh", "serverInterface": serverInterface, + "serverKeyPair": serverKeyPair, }) } } @@ -173,3 +180,32 @@ func WireGuardServerInterfaces() echo.HandlerFunc { return c.JSON(http.StatusOK, jsonHTTPResponse{true, "Updated interface addresses successfully"}) } } + +// WireGuardServerKeyPair handler to generate private and public keys +func WireGuardServerKeyPair() echo.HandlerFunc { + return func(c echo.Context) error { + // gen Wireguard key pair + key, err := wgtypes.GeneratePrivateKey() + if err != nil { + log.Error("Cannot generate wireguard key pair: ", err) + return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, "Cannot generate Wireguard key pair"}) + } + + serverKeyPair := new(model.ServerKeypair) + serverKeyPair.PrivateKey = key.String() + serverKeyPair.PublicKey = key.PublicKey().String() + serverKeyPair.UpdatedAt = time.Now().UTC() + + // write config to the database + dir := "./db" + db, err := scribble.New(dir, nil) + if err != nil { + log.Error("Cannot initialize the database: ", err) + return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, "Cannot access database"}) + } + db.Write("server", "keypair", serverKeyPair) + log.Infof("Updated wireguard server interfaces settings: %v", serverKeyPair) + + return c.JSON(http.StatusOK, serverKeyPair) + } +} diff --git a/main.go b/main.go index 3334bdb..7a6c3c1 100644 --- a/main.go +++ b/main.go @@ -13,5 +13,6 @@ func main() { app.POST("/remove-client", handler.RemoveClient()) app.GET("/wg-server", handler.WireGuardServer()) app.POST("wg-server/interfaces", handler.WireGuardServerInterfaces()) + app.POST("wg-server/keypair", handler.WireGuardServerKeyPair()) app.Logger.Fatal(app.Start("127.0.0.1:5000")) } diff --git a/model/server.go b/model/server.go index 06ab21a..b0a7867 100644 --- a/model/server.go +++ b/model/server.go @@ -13,7 +13,7 @@ type Server struct { // ServerKeypair model type ServerKeypair struct { PrivateKey string `json:"private_key"` - PublicKey string `json:"pulbic_key"` + PublicKey string `json:"public_key"` UpdatedAt time.Time `json:"updated_at"` } diff --git a/templates/clients.html b/templates/clients.html index 7347ce2..565224f 100644 --- a/templates/clients.html +++ b/templates/clients.html @@ -67,7 +67,7 @@ Wireguard Clients diff --git a/templates/server.html b/templates/server.html index 3e6c938..0bb4f39 100644 --- a/templates/server.html +++ b/templates/server.html @@ -57,21 +57,25 @@ Wireguard Server Settings
- + - +
- +
@@ -81,6 +85,30 @@ Wireguard Server Settings + + + {{end}} {{define "bottom_js"}} @@ -161,5 +189,45 @@ Wireguard Server Settings } }); }); + + // Wireguard Key Pair generation confirmation button + $(document).ready(function () { + $('#btn_generate_confirm').click(function () { + $.ajax({ + cache: false, + method: 'POST', + url: '/wg-server/keypair', + dataType: 'json', + contentType: "application/json", + success: function(data) { + $('#modal_keypair_confirmation').modal('hide'); + toastr.success('Generate new key pair successfully'); + // update the UI + $('#private_key').val(data['private_key']); + $('#public_key').val(data['public_key']); + }, + error: function(jqXHR, exception) { + var responseJson = jQuery.parseJSON(jqXHR.responseText); + toastr.error(responseJson['message']); + } + }); + }); + }); + + // Show private key button event + $(document).ready(function () { + $('#btn_show_private_key').click(function () { + var privateElement = document.getElementById("private_key"); + var btnElement = document.getElementById("btn_show_private_key"); + console.log(privateElement); + if (privateElement.type === 'password') { + privateElement.type = 'text'; + btnElement.innerText = 'Hide'; + } else { + privateElement.type = 'password'; + btnElement.innerText = 'Show'; + } + }); + }); {{end}} \ No newline at end of file