mirror of
https://github.com/ngoduykhanh/wireguard-ui.git
synced 2025-04-19 19:59:13 +03:00
Add download button
This commit is contained in:
parent
6f0d7bf69f
commit
91f06914d4
4 changed files with 50 additions and 3 deletions
|
@ -4,6 +4,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/sessions"
|
"github.com/gorilla/sessions"
|
||||||
|
@ -178,7 +179,34 @@ func SetClientStatus() echo.HandlerFunc {
|
||||||
db.Write("clients", clientID, &client)
|
db.Write("clients", clientID, &client)
|
||||||
log.Infof("Changed client %s enabled status to %v", client.ID, status)
|
log.Infof("Changed client %s enabled status to %v", client.ID, status)
|
||||||
|
|
||||||
return c.JSON(http.StatusOK, jsonHTTPResponse{true, "ok"})
|
return c.JSON(http.StatusOK, jsonHTTPResponse{true, "Changed client status successfully"})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// DownloadClient handler
|
||||||
|
func DownloadClient() echo.HandlerFunc {
|
||||||
|
return func(c echo.Context) error {
|
||||||
|
clientID := c.QueryParam("clientid")
|
||||||
|
if clientID == "" {
|
||||||
|
return c.JSON(http.StatusNotFound, jsonHTTPResponse{false, "Missing clientid parameter"})
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := util.GetClientByID(clientID)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Cannot generate client id %s config file for downloading: %v", clientID, err)
|
||||||
|
return c.JSON(http.StatusNotFound, jsonHTTPResponse{false, "Client not found"})
|
||||||
|
}
|
||||||
|
// build config
|
||||||
|
server, _ := util.GetServer()
|
||||||
|
globalSettings, _ := util.GetGlobalSettings()
|
||||||
|
config := util.BuildClientConfig(client, server, globalSettings)
|
||||||
|
|
||||||
|
// create io reader from string
|
||||||
|
reader := strings.NewReader(config)
|
||||||
|
|
||||||
|
// set response header for downloading
|
||||||
|
c.Response().Header().Set(echo.HeaderContentDisposition, "attachment; filename=wg0.conf")
|
||||||
|
return c.Stream(http.StatusOK, "text/plain", reader)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
1
main.go
1
main.go
|
@ -25,6 +25,7 @@ func main() {
|
||||||
app.POST("/new-client", handler.NewClient())
|
app.POST("/new-client", handler.NewClient())
|
||||||
app.POST("/client/set-status", handler.SetClientStatus())
|
app.POST("/client/set-status", handler.SetClientStatus())
|
||||||
app.POST("/remove-client", handler.RemoveClient())
|
app.POST("/remove-client", handler.RemoveClient())
|
||||||
|
app.GET("/download", handler.DownloadClient())
|
||||||
app.GET("/wg-server", handler.WireGuardServer())
|
app.GET("/wg-server", handler.WireGuardServer())
|
||||||
app.POST("wg-server/interfaces", handler.WireGuardServerInterfaces())
|
app.POST("wg-server/interfaces", handler.WireGuardServerInterfaces())
|
||||||
app.POST("wg-server/keypair", handler.WireGuardServerKeyPair())
|
app.POST("wg-server/keypair", handler.WireGuardServerKeyPair())
|
||||||
|
|
|
@ -36,8 +36,9 @@ Wireguard Clients
|
||||||
src="{{ .QRCode }}" />
|
src="{{ .QRCode }}" />
|
||||||
<div class="info-box-content">
|
<div class="info-box-content">
|
||||||
<div class="btn-group">
|
<div class="btn-group">
|
||||||
<button type="button" class="btn btn-outline-success btn-sm">Download</button>
|
<button onclick="location.href='/download?clientid={{ .Client.ID }}'" type="button"
|
||||||
<button type="button" class="btn btn-outline-primary btn-sm">Edit</button>
|
class="btn btn-outline-success btn-sm">Download</button>
|
||||||
|
<!-- <button type="button" class="btn btn-outline-primary btn-sm">Edit</button> -->
|
||||||
<button type="button" class="btn btn-outline-warning btn-sm" data-toggle="modal"
|
<button type="button" class="btn btn-outline-warning btn-sm" data-toggle="modal"
|
||||||
data-target="#modal_pause_client" data-clientid="{{ .Client.ID }}"
|
data-target="#modal_pause_client" data-clientid="{{ .Client.ID }}"
|
||||||
data-clientname="{{ .Client.Name }}">Disable</button>
|
data-clientname="{{ .Client.Name }}">Disable</button>
|
||||||
|
|
17
util/db.go
17
util/db.go
|
@ -224,3 +224,20 @@ func GetClients(hasQRCode bool) ([]model.ClientData, error) {
|
||||||
|
|
||||||
return clients, nil
|
return clients, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetClientByID func to query a client from the database
|
||||||
|
func GetClientByID(clientID string) (model.Client, error) {
|
||||||
|
client := model.Client{}
|
||||||
|
|
||||||
|
db, err := DBConn()
|
||||||
|
if err != nil {
|
||||||
|
return client, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// read client information
|
||||||
|
if err := db.Read("clients", clientID, &client); err != nil {
|
||||||
|
return client, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return client, nil
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue