🎨同步配置文件改为队列

This commit is contained in:
coward 2024-03-11 11:30:36 +08:00
parent c274c59044
commit 16799ee5d8
13 changed files with 1467 additions and 3 deletions

View File

@ -6,5 +6,5 @@ const (
)
const (
SyncWgConfigFile = "queues:wg:"
SyncWgConfigFile = "queues:wg:sync-file"
)

View File

@ -6,6 +6,7 @@ import (
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"wireguard-dashboard/http/param"
"wireguard-dashboard/model/entity"
"wireguard-dashboard/queues"
"wireguard-dashboard/repository"
"wireguard-dashboard/utils"
)
@ -28,7 +29,9 @@ func (server) SaveServer(c *gin.Context) {
}
var err error
var serverId string
if p.Id != "" {
serverId = p.Id
if err = repository.Server().Update(p); err != nil {
log.Errorf("更改服务端信息失败: %v", err.Error())
}
@ -39,7 +42,7 @@ func (server) SaveServer(c *gin.Context) {
return
}
publicKey := privateKey.PublicKey()
if err = repository.Server().Save(&entity.Server{
serverInfo := &entity.Server{
IpScope: p.IpScope,
ListenPort: p.ListenPort,
PrivateKey: privateKey.String(),
@ -47,9 +50,12 @@ func (server) SaveServer(c *gin.Context) {
PostUpScript: p.PostUpScript,
PreDownScript: p.PreDownScript,
PostDownScript: p.PostDownScript,
}); err != nil {
}
if err = repository.Server().Save(serverInfo); err != nil {
log.Errorf("新增服务端失败: %v", err.Error())
}
serverId = serverInfo.Id
}
if err != nil {
@ -57,6 +63,13 @@ func (server) SaveServer(c *gin.Context) {
return
}
go func() {
if err = queues.PutAsyncWireguardConfigFile(serverId); err != nil {
log.Errorf("投递同步配置文件任务失败: %s", err.Error())
}
}()
utils.GinResponse(c).OK()
}
// GetServer

File diff suppressed because it is too large Load Diff

View File

@ -6,6 +6,7 @@ import (
"net/http"
"wireguard-dashboard/config"
"wireguard-dashboard/initialize"
"wireguard-dashboard/queues"
"wireguard-dashboard/route"
"wireguard-dashboard/script"
)
@ -15,6 +16,7 @@ func init() {
if err := script.NewScript().Do(); err != nil {
log.Errorf("执行脚本失败: %v", err.Error())
}
queues.StartConsumer() // 启动队列
}
func main() {

View File

@ -55,3 +55,11 @@ func (jt *JsonTime) Scan(v interface{}) error {
}
return fmt.Errorf("can not convert %v to timestamp", v)
}
func (jt JsonTime) String() string {
if jt.IsZero() {
return ""
}
output := fmt.Sprintf("%s", jt.Format("2006-01-02 15:04:05"))
return output
}

View File

@ -34,6 +34,7 @@ type Client struct {
EnableAfterCreation int `json:"enableAfterCreation" gorm:"type:int(1);default 1;comment:'是否创建后启用'"`
Keys string `json:"keys" gorm:"type:text;default null;comment:'公钥和密钥的json串'"`
UserId string `json:"userId" gorm:"type:char(36);not null;comment:'创建人id'"`
User *User `json:"user" gorm:"foreignKey:UserId"`
}
func (*Client) TableName() string {

View File

@ -13,9 +13,19 @@ type Server struct {
}
type Client struct {
ID string `json:"id"`
Name string `json:"name"`
PublicKey string `json:"publicKey"`
PresharedKey string `json:"presharedKey"`
AllowedIPS string `json:"allowedIps"`
PersistentKeepalive string `json:"persistentKeepalive"`
Endpoint string `json:"endpoint"`
CreateUser string `json:"createUser"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
}
type Keys struct {
PrivateKey string `json:"privateKey"`
PublicKey string `json:"publicKey"`
}

103
queues/async_wg_config.go Normal file
View File

@ -0,0 +1,103 @@
package queues
import (
"context"
"encoding/json"
"fmt"
"gitee.ltd/lxh/logger/log"
"os"
"strconv"
"wireguard-dashboard/client"
"wireguard-dashboard/component"
"wireguard-dashboard/constant"
"wireguard-dashboard/model/template_data"
"wireguard-dashboard/repository"
)
// asyncWireguardConfigFile
// @description: 同步配置文件
func asyncWireguardConfigFile() {
for {
result, err := client.Redis.BRPop(context.Background(), 0, fmt.Sprintf("%s", constant.SyncWgConfigFile)).Result()
if err != nil {
log.Errorf("获取任务失败")
continue
}
serverId := result[1]
// 使用serverId获取服务信息
serverEnt, err := repository.Server().GetServerWithClient(serverId)
if err != nil {
log.Errorf("获取服务端信息失败: %s", err.Error())
continue
}
// 获取服务端全局配置
globalSetting, err := repository.System().GetServerSetting()
if err != nil {
log.Errorf("获取服务端配置失败: %s", err.Error())
continue
}
if globalSetting.ConfigFilePath == "" {
globalSetting.ConfigFilePath = "/etc/wireguard/wg0.conf"
}
// 获取模板文件和输出目录
var templatePath, outFilePath string
if os.Getenv("GIN_MODE") != "release" {
templatePath = "E:\\Workspace\\Go\\wireguard-dashboard\\template\\wg.conf"
outFilePath = "E:\\Workspace\\Go\\wireguard-dashboard\\wg0.conf"
} else {
templatePath = "./template/wg.conf"
outFilePath = globalSetting.ConfigFilePath
}
// 组装数据
renderServer := template_data.Server{
Address: serverEnt.IpScope,
ListenPort: serverEnt.ListenPort,
PrivateKey: serverEnt.PrivateKey,
MTU: globalSetting.MTU,
PostUp: serverEnt.PostUpScript,
PreDown: serverEnt.PreDownScript,
PostDown: serverEnt.PostDownScript,
Table: globalSetting.Table,
}
var renderClients []template_data.Client
for _, v := range serverEnt.Clients {
var clientKey template_data.Keys
_ = json.Unmarshal([]byte(v.Keys), &clientKey)
var createUserName string
if v.User != nil {
createUserName = v.User.Name
}
renderClients = append(renderClients, template_data.Client{
ID: v.Id,
Name: v.Name,
PublicKey: clientKey.PublicKey,
PresharedKey: clientKey.PrivateKey,
AllowedIPS: v.AllowedIps,
PersistentKeepalive: strconv.Itoa(globalSetting.PersistentKeepalive),
Endpoint: v.Endpoint,
CreatedAt: v.CreatedAt.String(),
UpdatedAt: v.UpdatedAt.String(),
CreateUser: createUserName,
})
}
renderData := map[string]any{
"Server": renderServer,
"Clients": renderClients,
}
err = component.Wireguard().Apply(templatePath, outFilePath, renderData)
if err != nil {
log.Errorf("同步配置文件失败: %s", err.Error())
continue
}
}
}

7
queues/consumer.go Normal file
View File

@ -0,0 +1,7 @@
package queues
// StartConsumer
// @description: 启动消费者
func StartConsumer() {
go asyncWireguardConfigFile()
}

16
queues/producer.go Normal file
View File

@ -0,0 +1,16 @@
package queues
import (
"context"
"fmt"
"wireguard-dashboard/client"
"wireguard-dashboard/constant"
)
// PutAsyncWireguardConfigFile
// @description: 启动生产者
// @param serverId
// @return error
func PutAsyncWireguardConfigFile(serverId string) error {
return client.Redis.LPush(context.Background(), fmt.Sprintf("%s", constant.SyncWgConfigFile), serverId).Err()
}

View File

@ -28,6 +28,16 @@ func (r server) GetServer() (data *vo.Server, err error) {
return
}
// GetServerWithClient
// @description: 获取服务端信息以及所属客户端
// @receiver r
// @param data
// @param err
func (r server) GetServerWithClient(id string) (data *entity.Server, err error) {
err = r.Model(&entity.Server{}).Preload("Clients").Preload("Clients.User").Where("id = ?", id).First(&data).Error
return
}
// Save
// @description: 新增服务端信息
// @receiver r

View File

@ -11,5 +11,6 @@ func ServerApi(r *gin.Engine) {
{
apiGroup.GET("", api.Server().GetServer) // 获取服务端信息
apiGroup.GET("global-setting", api.Server().GetGlobalSetting) // 获取服务端全局配置
apiGroup.POST("", api.Server().SaveServer) // 新增/更新服务端信息
}
}