💩代码改进

This commit is contained in:
coward
2024-03-14 15:23:16 +08:00
parent 91a1e61334
commit 8dfef5192e
23 changed files with 443 additions and 83 deletions

View File

@@ -1,10 +1,15 @@
package api
import (
"encoding/json"
"fmt"
"gitee.ltd/lxh/logger/log"
"github.com/gin-gonic/gin"
"os"
"strings"
"wireguard-dashboard/http/param"
"wireguard-dashboard/model/entity"
"wireguard-dashboard/model/template_data"
"wireguard-dashboard/queues"
"wireguard-dashboard/repository"
"wireguard-dashboard/utils"
@@ -55,7 +60,7 @@ func (clients) Save(c *gin.Context) {
_, err := repository.Client().Save(p, info.(*entity.User).Id)
if err != nil {
utils.GinResponse(c).FailedWithMsg("操作失败")
utils.GinResponse(c).FailedWithErr("操作失败", err)
return
}
@@ -67,3 +72,176 @@ func (clients) Save(c *gin.Context) {
utils.GinResponse(c).OK()
}
// Delete
// @description: 删除客户端
// @receiver clients
// @param c
func (clients) Delete(c *gin.Context) {
var id = c.Param("id")
if id == "" || id == "undefined" {
utils.GinResponse(c).FailedWithMsg("参数错误")
return
}
if err := repository.Client().Delete(id); err != nil {
utils.GinResponse(c).FailedWithMsg("操作失败")
return
}
utils.GinResponse(c).OK()
}
// Download
// @description: 下载配置文件
// @receiver clients
// @param c
func (clients) Download(c *gin.Context) {
var id = c.Param("id")
if id == "" || id == "undefined" {
utils.GinResponse(c).FailedWithMsg("参数错误")
return
}
data, err := repository.Client().GetById(id)
if err != nil {
utils.GinResponse(c).FailedWithMsg("获取失败")
return
}
var keys template_data.Keys
_ = json.Unmarshal([]byte(data.Keys), &keys)
setting, err := repository.System().GetServerSetting()
if err != nil {
utils.GinResponse(c).FailedWithMsg("获取设置失败")
return
}
// 处理一下数据
execData := template_data.ClientConfig{
PrivateKey: keys.PrivateKey,
IpAllocation: data.IpAllocation,
MTU: setting.MTU,
DNS: strings.Join(setting.DnsServer, ","),
PublicKey: keys.PublicKey,
PresharedKey: keys.PresharedKey,
AllowedIPS: data.AllowedIps,
Endpoint: setting.EndpointAddress,
ListenPort: data.Server.ListenPort,
PersistentKeepalive: setting.PersistentKeepalive,
}
// 不同环境下处理文件路径
var outPath = "/tmp/" + fmt.Sprintf("%s.conf", data.Name)
var templatePath = "./template/wg.client.conf"
if os.Getenv("GIN_MODE") != "release" {
outPath = "E:\\Workspace\\Go\\wireguard-dashboard\\template\\" + fmt.Sprintf("%s.conf", data.Name)
templatePath = "E:\\Workspace\\Go\\wireguard-dashboard\\template\\wg.client.conf"
}
// 渲染数据
parseTemplate, err := utils.Template().Parse(templatePath)
if err != nil {
utils.GinResponse(c).FailedWithMsg("读取模板文件失败")
return
}
err = utils.Template().Execute(parseTemplate, execData, outPath)
if err != nil {
utils.GinResponse(c).FailedWithMsg("文件渲染失败")
return
}
// 输出文件流
c.Header("Content-Type", "application/octet-stream")
c.Header("Content-Disposition", "attachment; filename="+outPath)
c.Header("Content-Transfer-Encoding", "binary")
c.Header("Connection", "keep-alive")
c.File(outPath)
if err = os.Remove(outPath); err != nil {
log.Errorf("删除临时文件失败: %s", err.Error())
}
}
// GenerateQrCode
// @description: 生成客户端信息二维码
// @receiver clients
// @param c
func (clients) GenerateQrCode(c *gin.Context) {
var id = c.Param("id")
if id == "" || id == "undefined" {
utils.GinResponse(c).FailedWithMsg("参数错误")
return
}
data, err := repository.Client().GetById(id)
if err != nil {
utils.GinResponse(c).FailedWithMsg("获取失败")
return
}
var keys template_data.Keys
_ = json.Unmarshal([]byte(data.Keys), &keys)
setting, err := repository.System().GetServerSetting()
if err != nil {
utils.GinResponse(c).FailedWithMsg("获取设置失败")
return
}
// 处理一下数据
execData := template_data.ClientConfig{
PrivateKey: keys.PrivateKey,
IpAllocation: data.IpAllocation,
MTU: setting.MTU,
PublicKey: keys.PublicKey,
PresharedKey: keys.PresharedKey,
AllowedIPS: data.AllowedIps,
Endpoint: setting.EndpointAddress,
ListenPort: data.Server.ListenPort,
PersistentKeepalive: setting.PersistentKeepalive,
}
// 不同环境下处理文件路径
var outPath = "/tmp/" + fmt.Sprintf("%s.conf", data.Name)
var templatePath = "./template/wg.client.conf"
if os.Getenv("GIN_MODE") != "release" {
outPath = "E:\\Workspace\\Go\\wireguard-dashboard\\template\\" + fmt.Sprintf("%s.conf", data.Name)
templatePath = "E:\\Workspace\\Go\\wireguard-dashboard\\template\\wg.client.conf"
}
// 渲染数据
parseTemplate, err := utils.Template().Parse(templatePath)
if err != nil {
utils.GinResponse(c).FailedWithMsg("读取模板文件失败")
return
}
err = utils.Template().Execute(parseTemplate, execData, outPath)
if err != nil {
utils.GinResponse(c).FailedWithMsg("文件渲染失败")
return
}
// 读取文件内容
fileContent, err := os.ReadFile(outPath)
if err != nil {
utils.GinResponse(c).FailedWithMsg("读取文件失败")
return
}
png, err := utils.QRCode().GenerateQrCodeBase64(fileContent, 256)
if err != nil {
utils.GinResponse(c).FailedWithErr("生成二维码失败", err)
return
}
if err = os.Remove(outPath); err != nil {
log.Errorf("删除临时文件失败: %s", err.Error())
}
utils.GinResponse(c).OKWithData(map[string]interface{}{
"qrCode": png,
})
}

View File

@@ -4,6 +4,7 @@ import (
"gitee.ltd/lxh/logger/log"
"github.com/gin-gonic/gin"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"strings"
"wireguard-dashboard/http/param"
"wireguard-dashboard/model/entity"
"wireguard-dashboard/queues"
@@ -43,7 +44,7 @@ func (server) SaveServer(c *gin.Context) {
}
publicKey := privateKey.PublicKey()
serverInfo := &entity.Server{
IpScope: p.IpScope,
IpScope: strings.Join(p.IpScope, ","),
ListenPort: p.ListenPort,
PrivateKey: privateKey.String(),
PublicKey: publicKey.String(),
@@ -82,17 +83,8 @@ func (server) GetServer(c *gin.Context) {
log.Errorf("获取服务端信息失败: %v", err.Error())
}
utils.GinResponse(c).OKWithData(data)
}
// GetGlobalSetting
// @description: 获取全局设置配置
// @receiver server
// @param c
func (server) GetGlobalSetting(c *gin.Context) {
data, err := repository.System().GetServerSetting()
if err != nil {
log.Errorf("获取配置失败: %v", err.Error())
if data.IpScopeStr != "" {
data.IpScope = strings.Split(data.IpScopeStr, ",")
}
utils.GinResponse(c).OKWithData(data)

51
http/api/setting.go Normal file
View File

@@ -0,0 +1,51 @@
package api
import (
"gitee.ltd/lxh/logger/log"
"github.com/gin-gonic/gin"
"wireguard-dashboard/http/param"
"wireguard-dashboard/repository"
"wireguard-dashboard/utils"
)
type setting struct{}
func Setting() setting {
return setting{}
}
// GetGlobalSetting
// @description: 获取全局设置配置
// @receiver setting
// @param c
func (setting) GetGlobalSetting(c *gin.Context) {
data, err := repository.System().GetServerSetting()
if err != nil {
log.Errorf("获取配置失败: %v", err.Error())
}
utils.GinResponse(c).OKWithData(data)
}
// SetGlobalServerSetting
// @description: 设置全局服务配置
// @receiver setting
// @param c
func (setting) SetGlobalServerSetting(c *gin.Context) {
var p param.SetSetting
if err := c.ShouldBind(&p); err != nil {
utils.GinResponse(c).FailedWithErr("参数错误", err)
return
}
}
// GetPublicNetworkIP
// @description: 获取当前机器的公网IP
// @receiver setting
// @param c
func (setting) GetPublicNetworkIP(c *gin.Context) {
utils.GinResponse(c).OKWithData(map[string]string{
"IP": utils.Network().GetHostPublicIP(),
})
}