This commit is contained in:
@@ -5,8 +5,11 @@ import (
|
||||
"fmt"
|
||||
"gitee.ltd/lxh/logger/log"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/spf13/cast"
|
||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
"wireguard-dashboard/client"
|
||||
"wireguard-dashboard/http/param"
|
||||
"wireguard-dashboard/model/entity"
|
||||
@@ -75,6 +78,84 @@ func (clients) Save(c *gin.Context) {
|
||||
utils.GinResponse(c).OK()
|
||||
}
|
||||
|
||||
// AssignIPAndAllowedIP
|
||||
// @description: 分配客户端IP和允许访问的IP段
|
||||
// @receiver clients
|
||||
// @param c
|
||||
func (clients) AssignIPAndAllowedIP(c *gin.Context) {
|
||||
var p param.AssignIPAndAllowedIP
|
||||
if err := c.ShouldBind(&p); err != nil {
|
||||
utils.GinResponse(c).FailedWithErr("参数错误", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取一下服务端信息,因为IP分配需要根据服务端的IP制定
|
||||
serverInfo, err := repository.Server().GetServer()
|
||||
if err != nil {
|
||||
utils.GinResponse(c).FailedWithErr("获取服务端信息失败", err)
|
||||
return
|
||||
}
|
||||
|
||||
var assignIPS []string
|
||||
assignIPS = append(assignIPS, serverInfo.IpScope)
|
||||
switch p.Rule {
|
||||
case "AUTO":
|
||||
// 只获取最新的一个
|
||||
var clientInfo *entity.Client
|
||||
if err = repository.Client().Order("created_at DESC").Take(&clientInfo).Error; err == nil {
|
||||
if cast.ToInt64(utils.Wireguard().GetIPSuffix(clientInfo.IpAllocation)) >= 255 {
|
||||
utils.GinResponse(c).FailedWithMsg("当前IP分配错误,请手动进行分配")
|
||||
return
|
||||
}
|
||||
assignIPS = append(assignIPS, clientInfo.IpAllocation)
|
||||
}
|
||||
|
||||
case "RANDOM":
|
||||
// 查询全部客户端不管是禁用还是没禁用的
|
||||
var clientsInfo []entity.Client
|
||||
if err = repository.Client().Find(&clientsInfo).Error; err != nil {
|
||||
utils.GinResponse(c).FailedWithErr("获取失败", err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, v := range clientsInfo {
|
||||
assignIPS = append(assignIPS, v.IpAllocation)
|
||||
}
|
||||
}
|
||||
|
||||
clientIP := utils.Wireguard().GenerateClientIP(serverInfo.IpScope, p.Rule, assignIPS...)
|
||||
|
||||
utils.GinResponse(c).OKWithData(map[string]any{
|
||||
"clientIP": []string{fmt.Sprintf("%s/32", clientIP)},
|
||||
"serverIP": []string{serverInfo.IpScope},
|
||||
})
|
||||
}
|
||||
|
||||
// GenerateKeys
|
||||
// @description: 生成密钥对
|
||||
// @receiver clients
|
||||
// @param c
|
||||
func (clients) GenerateKeys(c *gin.Context) {
|
||||
// 为空,新增
|
||||
privateKey, err := wgtypes.GeneratePrivateKey()
|
||||
if err != nil {
|
||||
utils.GinResponse(c).FailedWithErr("生成密钥对失败", err)
|
||||
return
|
||||
}
|
||||
publicKey := privateKey.PublicKey().String()
|
||||
presharedKey, err := wgtypes.GenerateKey()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
keys := template_data.Keys{
|
||||
PrivateKey: privateKey.String(),
|
||||
PublicKey: publicKey,
|
||||
PresharedKey: presharedKey.String(),
|
||||
}
|
||||
|
||||
utils.GinResponse(c).OKWithData(keys)
|
||||
}
|
||||
|
||||
// Delete
|
||||
// @description: 删除客户端
|
||||
// @receiver clients
|
||||
@@ -127,12 +208,17 @@ func (clients) Download(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var serverDNS []string
|
||||
if *data.UseServerDns == 1 {
|
||||
serverDNS = setting.DnsServer
|
||||
}
|
||||
|
||||
// 处理一下数据
|
||||
execData := template_data.ClientConfig{
|
||||
PrivateKey: keys.PrivateKey,
|
||||
IpAllocation: data.IpAllocation,
|
||||
MTU: setting.MTU,
|
||||
DNS: strings.Join(setting.DnsServer, ","),
|
||||
DNS: strings.Join(serverDNS, ","),
|
||||
PublicKey: data.Server.PublicKey,
|
||||
PresharedKey: keys.PresharedKey,
|
||||
AllowedIPS: data.AllowedIps,
|
||||
@@ -199,11 +285,17 @@ func (clients) GenerateQrCode(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var serverDNS []string
|
||||
if *data.UseServerDns == 1 {
|
||||
serverDNS = setting.DnsServer
|
||||
}
|
||||
|
||||
// 处理一下数据
|
||||
execData := template_data.ClientConfig{
|
||||
PrivateKey: keys.PrivateKey,
|
||||
IpAllocation: data.IpAllocation,
|
||||
MTU: setting.MTU,
|
||||
DNS: strings.Join(serverDNS, ","),
|
||||
PublicKey: data.Server.PublicKey,
|
||||
PresharedKey: keys.PresharedKey,
|
||||
AllowedIPS: data.AllowedIps,
|
||||
@@ -281,7 +373,7 @@ func (clients) Status(c *gin.Context) {
|
||||
ipAllocation += iaip.String() + ","
|
||||
}
|
||||
ipAllocation = strings.TrimRight(ipAllocation, ",")
|
||||
isOnline := p.LastHandshakeTime.Minute() < 1
|
||||
isOnline := time.Since(p.LastHandshakeTime).Minutes() < 1
|
||||
data = append(data, vo.ClientStatus{
|
||||
ID: clientInfo.Id,
|
||||
Name: clientInfo.Name,
|
||||
|
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"gitee.ltd/lxh/logger/log"
|
||||
"github.com/gin-gonic/gin"
|
||||
"wireguard-dashboard/config"
|
||||
"wireguard-dashboard/http/param"
|
||||
"wireguard-dashboard/model/entity"
|
||||
"wireguard-dashboard/queues"
|
||||
@@ -92,3 +93,13 @@ func (setting) GetPublicNetworkIP(c *gin.Context) {
|
||||
"IP": utils.Network().GetHostPublicIP(),
|
||||
})
|
||||
}
|
||||
|
||||
// GetServerRestartRule
|
||||
// @description: 获取服务重启规则
|
||||
// @receiver setting
|
||||
// @param c
|
||||
func (setting) GetServerRestartRule(c *gin.Context) {
|
||||
utils.GinResponse(c).OKWithData(map[string]string{
|
||||
"rule": config.Config.Wireguard.ListenConfig,
|
||||
})
|
||||
}
|
||||
|
@@ -42,3 +42,9 @@ type SaveClient struct {
|
||||
type ControlServer struct {
|
||||
Status string `json:"status" form:"status" binding:"required,oneof=START STOP RESTART"`
|
||||
}
|
||||
|
||||
// AssignIPAndAllowedIP
|
||||
// @description: 分配IP和允许访问的IP段
|
||||
type AssignIPAndAllowedIP struct {
|
||||
Rule string `json:"rule" form:"rule" binding:"required,oneof=RANDOM AUTO"` // 分配IP的规则 RANDOM - 固定 | AUTO - 自动生成
|
||||
}
|
||||
|
Reference in New Issue
Block a user