Skip to content

Commit

Permalink
feat: Add support for multiple host license
Browse files Browse the repository at this point in the history
  • Loading branch information
ssongliu committed Dec 9, 2024
1 parent e5660a0 commit 4734451
Show file tree
Hide file tree
Showing 51 changed files with 697 additions and 504 deletions.
2 changes: 1 addition & 1 deletion agent/app/api/v2/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func (b *BaseApi) CheckHealth(c *gin.Context) {
_, err := xpack.RequestToMaster("/api/v2/agent/health", http.MethodGet, nil)
_, err := xpack.RequestToMaster("/api/v2/agent/xpack/health", http.MethodGet, nil)
if err != nil {
helper.InternalServer(c, err)
return
Expand Down
4 changes: 2 additions & 2 deletions agent/app/service/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func NewBackupClientWithID(id uint) (*model.BackupAccount, cloud_storage.CloudSt
if err != nil {
return nil, nil, err
}
data, err := xpack.RequestToMaster("/api/v2/agent/backup", http.MethodPost, bytes.NewReader(bodyItem))
data, err := xpack.RequestToMaster("/api/v2/agent/xpack/backup", http.MethodPost, bytes.NewReader(bodyItem))
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -358,7 +358,7 @@ func NewBackupClientMap(ids []string) (map[string]backupClientHelper, error) {
if err != nil {
return nil, err
}
data, err := xpack.RequestToMaster("/api/v2/agent/backup/list", http.MethodPost, bytes.NewReader(bodyItem))
data, err := xpack.RequestToMaster("/api/v2/agent/xpack/backup/list", http.MethodPost, bytes.NewReader(bodyItem))
if err != nil {
return nil, err
}
Expand Down
9 changes: 5 additions & 4 deletions agent/configs/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package configs
type System struct {
Mode string `mapstructure:"mode"`

Version string `mapstructure:"version"`
BaseDir string `mapstructure:"base_dir"`
MasterAddr string `mapstructure:"master_addr"`
EncryptKey string `mapstructure:"encrypt_key"`
Version string `mapstructure:"version"`
BaseDir string `mapstructure:"base_dir"`
CurrentNode string `mapstructure:"base_dir"`
MasterAddr string `mapstructure:"master_addr"`
EncryptKey string `mapstructure:"encrypt_key"`

DbFile string `mapstructure:"db_agent_file"`
DbPath string `mapstructure:"db_path"`
Expand Down
10 changes: 1 addition & 9 deletions agent/constant/errs.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var (
ErrDemoEnvironment = "ErrDemoEnvironment"
ErrCmdIllegal = "ErrCmdIllegal"
ErrXpackNotFound = "ErrXpackNotFound"
ErrXpackNotActive = "ErrXpackNotActive"
ErrXpackExceptional = "ErrXpackExceptional"
ErrXpackOutOfDate = "ErrXpackOutOfDate"
)

Expand Down Expand Up @@ -134,11 +134,3 @@ var (
var (
ErrNotExistUser = "ErrNotExistUser"
)

// license
var (
ErrLicense = "ErrLicense"
ErrLicenseCheck = "ErrLicenseCheck"
ErrLicenseSave = "ErrLicenseSave"
ErrLicenseSync = "ErrLicenseSync"
)
10 changes: 0 additions & 10 deletions agent/i18n/lang/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -240,16 +240,6 @@ cc: 'Access Frequency Limit'
defaultUrlBlack: 'URL Rules'
sqlInject: 'SQL Injection'


#license
ErrLicense: "License format error, please check and try again!"
ErrLicenseCheck: "License verification failed, please check and try again!"
ErrLicenseSave: "Failed to save license information, error {{ .err }}, please try again!"
ErrLicenseSync: "Failed to sync license information, no license information detected in the database!"
ErrXpackNotFound: "This section is a professional edition feature, please import the license first in Panel Settings-License interface"
ErrXpackNotActive: "This section is a professional edition feature, please synchronize the license status first in Panel Settings-License interface"
ErrXpackOutOfDate: "The current license has expired, please re-import the license in Panel Settings-License interface"

#task
TaskStart: "{{.name}} Start [START]"
TaskEnd: "{{.name}} End [COMPLETED]"
Expand Down
10 changes: 0 additions & 10 deletions agent/i18n/lang/zh-Hant.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -241,16 +241,6 @@ cc: '訪問頻率限制'
defaultUrlBlack: 'URL 規則'
sqlInject: 'SQL 注入'


#license
ErrLicense: "許可證格式錯誤,請檢查後重試!"
ErrLicenseCheck: "許可證校驗失敗,請檢查後重試!"
ErrLicenseSave: "許可證信息保存失敗,錯誤 {{ .err }}, 請重試!"
ErrLicenseSync: "許可證信息同步失敗,資料庫中未檢測到許可證信息!"
ErrXpackNotFound: "該部分為專業版功能,請先在 面板設置-許可證 界面導入許可證"
ErrXpackNotActive: "該部分為專業版功能,請先在 面板設置-許可證 界面同步許可證狀態"
ErrXpackOutOfDate: "當前許可證已過期,請重新在 面板設置-許可證 界面導入許可證"

#task
TaskStart: "{{.name}} 開始 [START]"
TaskEnd: "{{.name}} 結束 [COMPLETED]"
Expand Down
10 changes: 0 additions & 10 deletions agent/i18n/lang/zh.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,6 @@ cc: '访问频率限制'
defaultUrlBlack: 'URL 规则'
sqlInject: 'SQL 注入'


#license
ErrLicense: "许可证格式错误,请检查后重试!"
ErrLicenseCheck: "许可证校验失败,请检查后重试!"
ErrLicenseSave: "许可证信息保存失败,错误 {{ .err }},请重试!"
ErrLicenseSync: "许可证信息同步失败,数据库中未检测到许可证信息!"
ErrXpackNotFound: "该部分为专业版功能,请先在 面板设置-许可证 界面导入许可证"
ErrXpackNotActive: "该部分为专业版功能,请先在 面板设置-许可证 界面同步许可证状态"
ErrXpackOutOfDate: "当前许可证已过期,请重新在 面板设置-许可证 界面导入许可证"

#task
TaskStart: "{{.name}} 任务开始 [START]"
TaskEnd: "{{.name}} 任务结束 [COMPLETED]"
Expand Down
4 changes: 2 additions & 2 deletions agent/init/hook/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ func initGlobalData() {
global.CONF.System.BaseDir, _ = settingRepo.GetValueByKey("BaseDir")
global.CONF.System.Version, _ = settingRepo.GetValueByKey("SystemVersion")
global.CONF.System.EncryptKey, _ = settingRepo.GetValueByKey("EncryptKey")
currentNode, _ := settingRepo.GetValueByKey("CurrentNode")
global.CONF.System.CurrentNode, _ = settingRepo.GetValueByKey("CurrentNode")

global.IsMaster = currentNode == "127.0.0.1" || len(currentNode) == 0
global.IsMaster = global.CONF.System.CurrentNode == "127.0.0.1" || len(global.CONF.System.CurrentNode) == 0
if global.IsMaster {
global.CoreDB = common.LoadDBConnByPath(path.Join(global.CONF.System.DbPath, "core.db"), "core")
} else {
Expand Down
17 changes: 10 additions & 7 deletions agent/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package server
import (
"crypto/tls"
"fmt"
"net"
"net/http"
"os"

"github.com/1Panel-dev/1Panel/agent/app/repo"
"github.com/1Panel-dev/1Panel/agent/cron"
"github.com/1Panel-dev/1Panel/agent/global"
Expand All @@ -18,12 +22,10 @@ import (
"github.com/1Panel-dev/1Panel/agent/init/validator"
"github.com/1Panel-dev/1Panel/agent/init/viper"
"github.com/1Panel-dev/1Panel/agent/utils/encrypt"
"net"
"net/http"
"os"

"github.com/gin-gonic/gin"
_ "net/http/pprof"

"github.com/gin-gonic/gin"
)

func Start() {
Expand All @@ -47,12 +49,13 @@ func Start() {
}

go func() {
http.ListenAndServe("0.0.0.0:6060", nil)
_ = http.ListenAndServe("0.0.0.0:6060", nil)
}()

if global.IsMaster {
_ = os.Remove("/tmp/agent.sock")
listener, err := net.Listen("unix", "/tmp/agent.sock")
_ = os.Remove("/etc/1panel/agent.sock")
_ = os.Mkdir("/etc/1panel", 0755)
listener, err := net.Listen("unix", "/etc/1panel/agent.sock")
if err != nil {
panic(err)
}
Expand Down
1 change: 0 additions & 1 deletion agent/utils/xpack/xpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func LoadNodeInfo() (bool, model.NodeInfo, error) {
var info model.NodeInfo
info.BaseDir = loadParams("BASE_DIR")
info.Version = loadParams("ORIGINAL_VERSION")
info.CurrentNode = "127.0.0.1"
info.EncryptKey = common.RandStr(16)
return false, info, nil
}
Expand Down
52 changes: 20 additions & 32 deletions core/app/repo/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,78 +8,66 @@ import (
"gorm.io/gorm"
)

type ICommonRepo interface {
WithByID(id uint) global.DBOption
WithByGroupID(id uint) global.DBOption

WithByName(name string) global.DBOption
WithByType(ty string) global.DBOption
WithByKey(key string) global.DBOption
WithOrderBy(orderStr string) global.DBOption
WithByStatus(status string) global.DBOption
WithByGroupBelong(group string) global.DBOption

WithByIDs(ids []uint) global.DBOption

WithOrderRuleBy(orderBy, order string) global.DBOption
}

type CommonRepo struct{}

func NewICommonRepo() ICommonRepo {
return &CommonRepo{}
}

func (c *CommonRepo) WithByID(id uint) global.DBOption {
func WithByID(id uint) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("id = ?", id)
}
}
func (c *CommonRepo) WithByGroupID(id uint) global.DBOption {
func WithByGroupID(id uint) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("group_id = ?", id)
}
}

func (c *CommonRepo) WithByIDs(ids []uint) global.DBOption {
func WithByIDs(ids []uint) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("id in (?)", ids)
}
}
func (c *CommonRepo) WithByName(name string) global.DBOption {
func WithByName(name string) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("`name` = ?", name)
}
}
func WithoutByName(name string) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("`name` != ?", name)
}
}

func (c *CommonRepo) WithByType(ty string) global.DBOption {
func WithByType(ty string) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("`type` = ?", ty)
}
}
func (c *CommonRepo) WithByKey(key string) global.DBOption {
func WithByAddr(addr string) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("addr = ?", addr)
}
}
func WithByKey(key string) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("key = ?", key)
}
}
func (c *CommonRepo) WithByStatus(status string) global.DBOption {
func WithByStatus(status string) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("status = ?", status)
}
}
func (c *CommonRepo) WithByGroupBelong(group string) global.DBOption {
func WithByGroupBelong(group string) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("group_belong = ?", group)
}
}

func (c *CommonRepo) WithOrderBy(orderStr string) global.DBOption {
func WithOrderBy(orderStr string) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Order(orderStr)
}
}

func (c *CommonRepo) WithOrderRuleBy(orderBy, order string) global.DBOption {
func WithOrderRuleBy(orderBy, order string) global.DBOption {
switch order {
case constant.OrderDesc:
order = "desc"
Expand Down
7 changes: 4 additions & 3 deletions core/app/service/app_launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"github.com/1Panel-dev/1Panel/core/app/dto"
"github.com/1Panel-dev/1Panel/core/app/repo"
"github.com/1Panel-dev/1Panel/core/constant"
)

Expand All @@ -17,7 +18,7 @@ func NewIAppLauncher() IAppLauncher {
}

func (u *LauncherService) Search() ([]string, error) {
launchers, err := launcherRepo.List(commonRepo.WithOrderBy("created_at"))
launchers, err := launcherRepo.List(repo.WithOrderBy("created_at"))
if err != nil {
return nil, err
}
Expand All @@ -29,7 +30,7 @@ func (u *LauncherService) Search() ([]string, error) {
}

func (u *LauncherService) ChangeShow(req dto.SettingUpdate) error {
launcher, _ := launcherRepo.Get(commonRepo.WithByKey(req.Key))
launcher, _ := launcherRepo.Get(repo.WithByKey(req.Key))
if req.Value == constant.StatusEnable {
if launcher.ID != 0 {
return nil
Expand All @@ -41,5 +42,5 @@ func (u *LauncherService) ChangeShow(req dto.SettingUpdate) error {
return nil
}

return launcherRepo.Delete(commonRepo.WithByKey(req.Key))
return launcherRepo.Delete(repo.WithByKey(req.Key))
}
Loading

0 comments on commit 4734451

Please sign in to comment.