feat: 完成节点添加及删除功能 (#6013)

This commit is contained in:
ssongliu 2024-08-02 16:49:43 +08:00 committed by GitHub
parent 25b189069c
commit 1ae5f945b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 132 additions and 40 deletions

View File

@ -46,7 +46,6 @@ func Init() {
if err != nil {
panic(err)
}
_ = db.Exec("PRAGMA journal_mode = WAL;")
sqlDB, dbError := db.DB()
if dbError != nil {
panic(dbError)

View File

@ -10,6 +10,7 @@ import (
"github.com/1Panel-dev/1Panel/agent/app/repo"
"github.com/1Panel-dev/1Panel/agent/constant"
"github.com/1Panel-dev/1Panel/agent/global"
"github.com/1Panel-dev/1Panel/agent/utils/encrypt"
)
func Init() {
@ -38,6 +39,7 @@ func Init() {
handleSnapStatus()
loadLocalDir()
initDir()
_ = initSSL()
}
func handleSnapStatus() {
@ -145,3 +147,37 @@ func initDir() {
}
}
}
func initSSL() error {
settingRepo := repo.NewISettingRepo()
if _, err := os.Stat("/opt/1panel/nodeJson"); err != nil {
return nil
}
type nodeInfo struct {
ServerCrt string `json:"serverCrt"`
ServerKey string `json:"serverKey"`
CurrentNode string `json:"currentNode"`
}
nodeJson, err := os.ReadFile("/opt/1panel/nodeJson")
if err != nil {
return err
}
var node nodeInfo
if err := json.Unmarshal(nodeJson, &node); err != nil {
return err
}
itemKey, _ := encrypt.StringEncrypt(node.ServerKey)
if err := settingRepo.Update("ServerKey", itemKey); err != nil {
return err
}
itemCrt, _ := encrypt.StringEncrypt(node.ServerCrt)
if err := settingRepo.Update("ServerCrt", itemCrt); err != nil {
return err
}
if err := settingRepo.Update("CurrentNode", node.CurrentNode); err != nil {
return err
}
global.CurrentNode = node.CurrentNode
_ = os.Remove(("/opt/1panel/nodeJson"))
return nil
}

View File

@ -1,9 +1,9 @@
package migrations
import (
"encoding/json"
"fmt"
"os"
"path"
"github.com/1Panel-dev/1Panel/agent/app/dto/request"
"github.com/1Panel-dev/1Panel/agent/app/model"
@ -92,6 +92,34 @@ var InitSetting = &gormigrate.Migration{
if err := tx.Create(&model.Setting{Key: "EncryptKey", Value: encryptKey}).Error; err != nil {
return err
}
if _, err := os.Stat("/opt/1panel/nodeJson"); err != nil {
return err
}
type nodeInfo struct {
ServerCrt string `json:"serverCrt"`
ServerKey string `json:"serverKey"`
CurrentNode string `json:"currentNode"`
}
nodeJson, err := os.ReadFile("/opt/1panel/nodeJson")
if err != nil {
return err
}
var node nodeInfo
if err := json.Unmarshal(nodeJson, &node); err != nil {
return err
}
itemKey, _ := encrypt.StringEncrypt(node.ServerKey)
if err := tx.Create(&model.Setting{Key: "ServerKey", Value: itemKey}).Error; err != nil {
return err
}
itemCrt, _ := encrypt.StringEncrypt(node.ServerCrt)
if err := tx.Create(&model.Setting{Key: "ServerCrt", Value: itemCrt}).Error; err != nil {
return err
}
if err := tx.Create(&model.Setting{Key: "CurrentNode", Value: node.CurrentNode}).Error; err != nil {
return err
}
global.CurrentNode = node.CurrentNode
if err := tx.Create(&model.Setting{Key: "SystemIP", Value: ""}).Error; err != nil {
return err
@ -163,23 +191,7 @@ var InitSetting = &gormigrate.Migration{
return err
}
if _, err := os.Stat(path.Join(global.CONF.System.DataDir, "ssl", "server.key")); err != nil {
return err
}
serverKey, _ := os.ReadFile(path.Join(global.CONF.System.DataDir, "ssl", "server.key"))
itemKey, _ := encrypt.StringEncrypt(string(serverKey))
if err := tx.Create(&model.Setting{Key: "ServerKey", Value: itemKey}).Error; err != nil {
return err
}
if _, err := os.Stat(path.Join(global.CONF.System.DataDir, "ssl", "server.crt")); err != nil {
return err
}
serverCrt, _ := os.ReadFile(path.Join(global.CONF.System.DataDir, "ssl", "server.crt"))
itemCrt, _ := encrypt.StringEncrypt(string(serverCrt))
if err := tx.Create(&model.Setting{Key: "ServerCert", Value: string(itemCrt)}).Error; err != nil {
return err
}
_ = os.Remove(("/opt/1panel/nodeJson"))
return nil
},
}

View File

@ -1,6 +1,7 @@
package router
import (
"github.com/1Panel-dev/1Panel/agent/global"
"github.com/1Panel-dev/1Panel/agent/i18n"
"github.com/1Panel-dev/1Panel/agent/middleware"
rou "github.com/1Panel-dev/1Panel/agent/router"
@ -25,7 +26,9 @@ func Routers() *gin.Engine {
PublicGroup.Static("/api/v1/images", "./uploads")
}
PrivateGroup := Router.Group("/api/v2")
PrivateGroup.Use(middleware.Certificate())
if global.CurrentNode != "127.0.0.1" {
PrivateGroup.Use(middleware.Certificate())
}
for _, router := range rou.RouterGroupApp {
router.InitRouter(PrivateGroup)
}

View File

@ -55,7 +55,7 @@ func Start() {
} else {
server.Addr = "0.0.0.0:9999"
settingRepo := repo.NewISettingRepo()
certItem, err := settingRepo.Get(settingRepo.WithByKey("ServerCert"))
certItem, err := settingRepo.Get(settingRepo.WithByKey("ServerCrt"))
if err != nil {
panic(err)
}

View File

@ -47,4 +47,5 @@ var (
ErrDemoEnvironment = "ErrDemoEnvironment"
ErrEntrance = "ErrEntrance"
ErrProxy = "ErrProxy"
ErrLocalDelete = "ErrLocalDelete"
)

View File

@ -4,6 +4,12 @@ const (
StatusSuccess = "Success"
StatusFailed = "Failed"
StatusHealthy = "Healthy"
StatusUnhealthy = "Unhealthy"
// node
StatusWaiting = "waiting"
StatusDownloading = "downloading"
StatusPacking = "packing"
StatusSending = "sending"
StatusStarting = "starting"
StatusHealthy = "healthy"
StatusUnhealthy = "unhealthy"
)

View File

@ -15,6 +15,7 @@ ErrNameIsExist: "Name is already exist"
ErrDemoEnvironment: "Demo server, prohibit this operation!"
ErrEntrance: "Security entrance information error. Please check and try again!"
ErrGroupIsUsed: "The group is in use and cannot be deleted"
ErrLocalDelete: "Unable to delete local node!"
#app
ErrPortInUsed: "{{ .detail }} port already in use"

View File

@ -15,6 +15,7 @@ ErrNameIsExist: "名稱已存在"
ErrDemoEnvironment: "演示伺服器,禁止此操作!"
ErrEntrance: "安全入口信息錯誤,請檢查後重試!"
ErrGroupIsUsed: "分組正在使用中,無法刪除"
ErrLocalDelete: "無法刪除本地節點!"
#app
ErrPortInUsed: "{{ .detail }} 端口已被佔用!"

View File

@ -15,6 +15,7 @@ ErrDemoEnvironment: "演示服务器,禁止此操作!"
ErrCmdTimeout: "命令执行超时!"
ErrEntrance: "安全入口信息错误,请检查后重试!"
ErrGroupIsUsed: "分组正在使用中,无法删除"
ErrLocalDelete: "无法删除本地节点!"
#app
ErrPortInUsed: "{{ .detail }} 端口已被占用!"

View File

@ -2,7 +2,6 @@ package middleware
import (
"context"
"fmt"
"net"
"net/http"
"net/http/httputil"
@ -24,7 +23,6 @@ func Proxy() gin.HandlerFunc {
currentNode := c.Request.Header.Get("CurrentNode")
if len(currentNode) != 0 && currentNode != "127.0.0.1" {
if err := proxy.Proxy(c, currentNode); err != nil {
fmt.Println(err)
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrProxy, err)
return
}

View File

@ -1,11 +1,14 @@
package common
import (
"fmt"
mathRand "math/rand"
"net"
"strconv"
"strings"
"time"
"github.com/1Panel-dev/1Panel/core/utils/cmd"
)
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
@ -94,3 +97,26 @@ func SplitStr(str string, spi ...string) []string {
}
return results
}
func LoadArch() (string, error) {
std, err := cmd.Exec("uname -a")
if err != nil {
return "", fmt.Errorf("std: %s, err: %s", std, err.Error())
}
if strings.Contains(std, "x86_64") {
return "amd64", nil
}
if strings.Contains(std, "arm64") || strings.Contains(std, "aarch64") {
return "arm64", nil
}
if strings.Contains(std, "armv7l") {
return "armv7", nil
}
if strings.Contains(std, "ppc64le") {
return "ppc64le", nil
}
if strings.Contains(std, "s390x") {
return "s390x", nil
}
return "", fmt.Errorf("unsupported such arch: %s", std)
}

View File

@ -127,23 +127,20 @@ const systemLogOut = async () => {
};
const loadNodes = async () => {
if (globalStore.isProductPro) {
let listXNodes;
const xpackModules = import.meta.glob('../../../xpack/api/modules/node.ts', { eager: true });
if (xpackModules['../../../xpack/api/modules/node.ts']) {
console.log('dqwdqwd');
listXNodes = xpackModules['../../../xpack/api/modules/node.ts']['listNodes'] || {};
const res = await listXNodes();
if (!res) {
nodes.value = [];
return;
}
console.log('dqwdqwd');
nodes.value = res.data;
let listXNodes;
const xpackModules = import.meta.glob('../../../xpack/api/modules/node.ts', { eager: true });
if (xpackModules['../../../xpack/api/modules/node.ts']) {
listXNodes = xpackModules['../../../xpack/api/modules/node.ts']['listNodes'] || {};
const res = await listXNodes();
if (!res) {
nodes.value = [];
return;
}
nodes.value = [];
console.log('dqwdqwd');
nodes.value = res.data;
return;
}
nodes.value = [];
};
const changeNode = (command: string) => {
globalStore.currentNode = command || '127.0.0.1';

4
go.mod
View File

@ -16,6 +16,7 @@ require (
github.com/mojocn/base64Captcha v1.3.6
github.com/nicksnyder/go-i18n/v2 v2.4.0
github.com/pkg/errors v0.9.1
github.com/pkg/sftp v1.13.6
github.com/robfig/cron/v3 v3.0.1
github.com/sirupsen/logrus v1.9.3
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
@ -25,6 +26,7 @@ require (
github.com/swaggo/gin-swagger v1.6.0
github.com/swaggo/swag v1.16.3
github.com/xlzd/gotp v0.1.0
golang.org/x/crypto v0.23.0
golang.org/x/sys v0.22.0
golang.org/x/term v0.22.0
golang.org/x/text v0.16.0
@ -68,6 +70,7 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.2 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/kr/fs v0.1.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
@ -90,7 +93,6 @@ require (
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/image v0.13.0 // indirect
golang.org/x/net v0.25.0 // indirect

9
go.sum
View File

@ -145,6 +145,8 @@ github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa02
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
@ -179,6 +181,8 @@ github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.13.6 h1:JFZT4XbOU7l77xGSpOdW+pwIMqP044IyjXX6FGyEKFo=
github.com/pkg/sftp v1.13.6/go.mod h1:tz1ryNURKu77RL+GuCzmoJYxQczL3wLNNpPWagdg4Qk=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
@ -255,6 +259,7 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
@ -283,6 +288,7 @@ golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwY
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210421230115-4e50805a0758/go.mod h1:72T/g9IO56b78aLF+1Kcs5dz7/ng1VjMUvfKvpfy+jM=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
@ -308,12 +314,14 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk=
golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4=
@ -321,6 +329,7 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=