🎨服务端的初始化
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package script
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"gitee.ltd/lxh/logger/log"
|
||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||
"wireguard-dashboard/client"
|
||||
"wireguard-dashboard/constant"
|
||||
"wireguard-dashboard/model/entity"
|
||||
@@ -24,6 +26,10 @@ func (s Script) Do() error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.InitServer(); err != nil {
|
||||
log.Error(err.Error())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -36,6 +42,7 @@ func (s Script) DBMigrate() error {
|
||||
new(entity.User),
|
||||
new(entity.Server),
|
||||
new(entity.Client),
|
||||
new(entity.Setting),
|
||||
}
|
||||
|
||||
return client.DB.AutoMigrate(ent...)
|
||||
@@ -77,3 +84,64 @@ func (s Script) CreateSuperAdmin() error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// InitServer
|
||||
// @description: 初始化服务端信息
|
||||
// @receiver s
|
||||
// @return error
|
||||
func (s Script) InitServer() error {
|
||||
var count int64
|
||||
if err := client.DB.Model(&entity.Server{}).Count(&count).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 初始化服务端的全局配置
|
||||
var data = map[string]any{
|
||||
"endpointAddress": utils.Network().GetHostPublicIP(),
|
||||
"dnsServer": "10.10.10.1/24",
|
||||
"MTU": 1450,
|
||||
"persistentKeepalive": 15,
|
||||
"firewallMark": "",
|
||||
"table": "",
|
||||
"configFilePath": "/etc/wireguard/wg0.conf",
|
||||
}
|
||||
dataJ, _ := json.Marshal(data)
|
||||
|
||||
globalSet := &entity.Setting{
|
||||
Code: "SERVER_SETTING",
|
||||
Data: string(dataJ),
|
||||
Describe: "服务端全局配置",
|
||||
}
|
||||
if err := repository.System().Save(globalSet); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 生成密钥
|
||||
privateKey, err := wgtypes.GeneratePrivateKey()
|
||||
if err != nil {
|
||||
log.Errorf("生成密钥失败: %v", err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
// 根据密钥生成公钥
|
||||
publicKey := privateKey.PublicKey()
|
||||
|
||||
// 没有服务端,开始初始化
|
||||
if err := repository.Server().Save(&entity.Server{
|
||||
IpScope: "10.10.10.1/24",
|
||||
ListenPort: 51820,
|
||||
PrivateKey: privateKey.String(),
|
||||
PublicKey: publicKey.String(),
|
||||
PostUpScript: "",
|
||||
PreDownScript: "",
|
||||
PostDownScript: "",
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user