refactor: 更新项目引用路径从perfect-panel/ppanel-server到perfect-panel/server
Build docker and publish / build (20.15.1) (push) Failing after 6m27s
Build docker and publish / build (20.15.1) (push) Failing after 6m27s
feat: 添加版本和构建时间变量 fix: 修正短信队列类型注释错误 style: 清理未使用的代码和测试文件 docs: 更新安装文档中的下载链接 chore: 迁移数据库脚本添加日志和订阅配置
This commit is contained in:
@@ -5,9 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/perfect-panel/ppanel-server/internal/config"
|
||||
|
||||
"github.com/perfect-panel/ppanel-server/pkg/cache"
|
||||
"github.com/perfect-panel/server/pkg/cache"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -23,10 +21,10 @@ type (
|
||||
customServerLogicModel
|
||||
}
|
||||
serverModel interface {
|
||||
Insert(ctx context.Context, data *Server) error
|
||||
Insert(ctx context.Context, data *Server, tx ...*gorm.DB) error
|
||||
FindOne(ctx context.Context, id int64) (*Server, error)
|
||||
Update(ctx context.Context, data *Server) error
|
||||
Delete(ctx context.Context, id int64) error
|
||||
Update(ctx context.Context, data *Server, tx ...*gorm.DB) error
|
||||
Delete(ctx context.Context, id int64, tx ...*gorm.DB) error
|
||||
Transaction(ctx context.Context, fn func(db *gorm.DB) error) error
|
||||
}
|
||||
|
||||
@@ -62,23 +60,32 @@ func (m *defaultServerModel) batchGetCacheKeys(Servers ...*Server) []string {
|
||||
return keys
|
||||
|
||||
}
|
||||
|
||||
func (m *defaultServerModel) getCacheKeys(data *Server) []string {
|
||||
if data == nil {
|
||||
return []string{}
|
||||
}
|
||||
detailsKey := fmt.Sprintf("%s%v", CacheServerDetailPrefix, data.Id)
|
||||
ServerIdKey := fmt.Sprintf("%s%v", cacheServerIdPrefix, data.Id)
|
||||
configIdKey := fmt.Sprintf("%s%v", config.ServerConfigCacheKey, data.Id)
|
||||
//configIdKey := fmt.Sprintf("%s%v", config.ServerConfigCacheKey, data.Id)
|
||||
//userIDKey := fmt.Sprintf("%s%d", config.ServerUserListCacheKey, data.Id)
|
||||
|
||||
// query protocols to get config keys
|
||||
|
||||
cacheKeys := []string{
|
||||
ServerIdKey,
|
||||
detailsKey,
|
||||
configIdKey,
|
||||
//configIdKey,
|
||||
//userIDKey,
|
||||
}
|
||||
return cacheKeys
|
||||
}
|
||||
|
||||
func (m *defaultServerModel) Insert(ctx context.Context, data *Server) error {
|
||||
func (m *defaultServerModel) Insert(ctx context.Context, data *Server, tx ...*gorm.DB) error {
|
||||
err := m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
||||
if len(tx) > 0 {
|
||||
conn = tx[0]
|
||||
}
|
||||
return conn.Create(&data).Error
|
||||
}, m.getCacheKeys(data)...)
|
||||
return err
|
||||
@@ -98,19 +105,21 @@ func (m *defaultServerModel) FindOne(ctx context.Context, id int64) (*Server, er
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultServerModel) Update(ctx context.Context, data *Server) error {
|
||||
func (m *defaultServerModel) Update(ctx context.Context, data *Server, tx ...*gorm.DB) error {
|
||||
old, err := m.FindOne(ctx, data.Id)
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
err = m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
||||
db := conn
|
||||
return db.Save(data).Error
|
||||
if len(tx) > 0 {
|
||||
conn = tx[0]
|
||||
}
|
||||
return conn.Save(data).Error
|
||||
}, m.getCacheKeys(old)...)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultServerModel) Delete(ctx context.Context, id int64) error {
|
||||
func (m *defaultServerModel) Delete(ctx context.Context, id int64, tx ...*gorm.DB) error {
|
||||
data, err := m.FindOne(ctx, id)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -119,8 +128,10 @@ func (m *defaultServerModel) Delete(ctx context.Context, id int64) error {
|
||||
return err
|
||||
}
|
||||
err = m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
||||
db := conn
|
||||
return db.Delete(&Server{}, id).Error
|
||||
if len(tx) > 0 {
|
||||
conn = tx[0]
|
||||
}
|
||||
return conn.Delete(&Server{}, id).Error
|
||||
}, m.getCacheKeys(data)...)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -3,8 +3,8 @@ package server
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/perfect-panel/ppanel-server/internal/config"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -29,6 +29,10 @@ type customServerLogicModel interface {
|
||||
UpdateRuleGroup(ctx context.Context, data *RuleGroup) error
|
||||
DeleteRuleGroup(ctx context.Context, id int64) error
|
||||
QueryAllRuleGroup(ctx context.Context) ([]*RuleGroup, error)
|
||||
FindServersByTag(ctx context.Context, tag string) ([]*Server, error)
|
||||
FindServerTags(ctx context.Context) ([]string, error)
|
||||
|
||||
SetDefaultRuleGroup(ctx context.Context, id int64) error
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -40,9 +44,10 @@ var (
|
||||
// ClearCache Clear Cache
|
||||
func (m *customServerModel) ClearCache(ctx context.Context, id int64) error {
|
||||
serverIdKey := fmt.Sprintf("%s%v", cacheServerIdPrefix, id)
|
||||
configKey := fmt.Sprintf("%s%d", config.ServerConfigCacheKey, id)
|
||||
//configKey := fmt.Sprintf("%s%d", config.ServerConfigCacheKey, id)
|
||||
//userListKey := fmt.Sprintf("%s%v", config.ServerUserListCacheKey, id)
|
||||
|
||||
return m.DelCacheCtx(ctx, serverIdKey, configKey)
|
||||
return m.DelCacheCtx(ctx, serverIdKey)
|
||||
}
|
||||
|
||||
// QueryServerCountByServerGroups Query Server Count By Server Groups
|
||||
@@ -114,13 +119,16 @@ func (m *customServerModel) FindServerDetailByGroupIdsAndIds(ctx context.Context
|
||||
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
|
||||
conn = conn.
|
||||
Model(&Server{}).
|
||||
Where("enable = ?", true)
|
||||
if len(groupId) > 0 {
|
||||
conn = conn.Where("group_id IN ?", groupId)
|
||||
}
|
||||
if len(ids) > 0 {
|
||||
conn = conn.Where("id IN ?", ids)
|
||||
Where("`enable` = ?", true)
|
||||
if len(groupId) > 0 && len(ids) > 0 {
|
||||
// OR is used to connect group_id and id conditions
|
||||
conn = conn.Where("(`group_id` IN ? OR `id` IN ?)", groupId, ids)
|
||||
} else if len(groupId) > 0 {
|
||||
conn = conn.Where("`group_id` IN ?", groupId)
|
||||
} else if len(ids) > 0 {
|
||||
conn = conn.Where("`id` IN ?", ids)
|
||||
}
|
||||
|
||||
return conn.Order("sort ASC").Find(v).Error
|
||||
})
|
||||
return list, err
|
||||
@@ -227,10 +235,16 @@ func (m *customServerModel) FindServerListByFilter(ctx context.Context, filter *
|
||||
query = conn.Where("group_id = ?", filter.Group)
|
||||
}
|
||||
if filter.Search != "" {
|
||||
query = query.Where("name LIKE ? OR server_addr LIKE ?", "%"+filter.Search+"%", "%"+filter.Search+"%")
|
||||
query = query.Where("name LIKE ? OR server_addr LIKE ? OR tags LIKE ?", "%"+filter.Search+"%", "%"+filter.Search+"%", "%"+filter.Search+"%")
|
||||
}
|
||||
if filter.Tag != "" {
|
||||
query = query.Where("tag LIKE ?", "%"+filter.Tag+"%")
|
||||
if len(filter.Tags) > 0 {
|
||||
for i, tag := range filter.Tags {
|
||||
if i == 0 {
|
||||
query = query.Where("tags LIKE ?", "%"+tag+"%")
|
||||
} else {
|
||||
query = query.Or("tags LIKE ?", "%"+tag+"%")
|
||||
}
|
||||
}
|
||||
}
|
||||
return query.Count(&total).Limit(filter.Size).Offset((filter.Page - 1) * filter.Size).Find(v).Error
|
||||
})
|
||||
@@ -239,3 +253,40 @@ func (m *customServerModel) FindServerListByFilter(ctx context.Context, filter *
|
||||
}
|
||||
return total, data, nil
|
||||
}
|
||||
|
||||
func (m *customServerModel) FindServerTags(ctx context.Context) ([]string, error) {
|
||||
var data []string
|
||||
err := m.QueryNoCacheCtx(ctx, &data, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Model(&Server{}).Distinct("tags").Pluck("tags", v).Error
|
||||
})
|
||||
var tags []string
|
||||
for _, tag := range data {
|
||||
if strings.Contains(tag, ",") {
|
||||
tags = append(tags, strings.Split(tag, ",")...)
|
||||
} else {
|
||||
tags = append(tags, tag)
|
||||
}
|
||||
}
|
||||
return tags, err
|
||||
}
|
||||
|
||||
func (m *customServerModel) FindServersByTag(ctx context.Context, tag string) ([]*Server, error) {
|
||||
var data []*Server
|
||||
err := m.QueryNoCacheCtx(ctx, &data, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Model(&Server{}).Where("FIND_IN_SET(?, tags)", tag).Order("sort ASC").Find(v).Error
|
||||
})
|
||||
return data, err
|
||||
}
|
||||
|
||||
// SetDefaultRuleGroup sets the default rule group.
|
||||
|
||||
func (m *customServerModel) SetDefaultRuleGroup(ctx context.Context, id int64) error {
|
||||
return m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
||||
// Reset all groups to not default
|
||||
if err := conn.Model(&RuleGroup{}).Where("`id` != ?", id).Update("default", false).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
// Set the specified group as default
|
||||
return conn.Model(&RuleGroup{}).Where("`id` = ?", id).Update("default", true).Error
|
||||
}, cacheServerRuleGroupAllKeys, fmt.Sprintf("cache:serverRuleGroup:%v", id))
|
||||
}
|
||||
|
||||
@@ -3,26 +3,30 @@ package server
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/perfect-panel/ppanel-server/pkg/logger"
|
||||
"github.com/perfect-panel/server/pkg/logger"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const (
|
||||
RelayModeNone = "none"
|
||||
RelayModeAll = "all"
|
||||
RelayModeRandom = "random"
|
||||
RelayModeNone = "none"
|
||||
RelayModeAll = "all"
|
||||
RelayModeRandom = "random"
|
||||
RuleGroupTypeReject = "reject"
|
||||
RuleGroupTypeDefault = "default"
|
||||
RuleGroupTypeDirect = "direct"
|
||||
)
|
||||
|
||||
type ServerFilter struct {
|
||||
Id int64
|
||||
Tag string
|
||||
Tags []string
|
||||
Group int64
|
||||
Search string
|
||||
Page int
|
||||
Size int
|
||||
}
|
||||
|
||||
// Deprecated: use internal/model/node/server.go
|
||||
type Server struct {
|
||||
Id int64 `gorm:"primary_key"`
|
||||
Name string `gorm:"type:varchar(100);not null;default:'';comment:Node Name"`
|
||||
@@ -52,33 +56,32 @@ func (*Server) TableName() string {
|
||||
|
||||
func (s *Server) BeforeDelete(tx *gorm.DB) error {
|
||||
logger.Debugf("[Server] BeforeDelete")
|
||||
|
||||
if err := tx.Exec("UPDATE `server` SET sort = sort - 1 WHERE sort > ?", s.Sort).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
// 删除后重新排序,防止因 sort 缺口导致问题
|
||||
if err := reorderSort(tx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) BeforeUpdate(tx *gorm.DB) error {
|
||||
logger.Debugf("[Server] BeforeUpdate")
|
||||
|
||||
var count int64
|
||||
if err := tx.Model(&Server{}).Where("sort = ? AND id != ?", s.Sort, s.Id).Count(&count).Error; err != nil {
|
||||
if err := tx.Set("gorm:query_option", "FOR UPDATE").Model(&Server{}).
|
||||
Where("sort = ? AND id != ?", s.Sort, s.Id).Count(&count).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
logger.Debugf("[Server] Duplicate sort found, reordering...")
|
||||
if count > 1 {
|
||||
// reorder sort
|
||||
if err := reorderSort(tx); err != nil {
|
||||
logger.Errorf("[Server] BeforeUpdate reorderSort error: %v", err.Error())
|
||||
return err
|
||||
}
|
||||
// get max sort
|
||||
var maxSort int64
|
||||
if err := tx.Model(&Server{}).Select("MAX(sort)").Scan(&maxSort).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
s.Sort = maxSort + 1
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -136,6 +139,15 @@ type Hysteria2 struct {
|
||||
}
|
||||
|
||||
type Tuic struct {
|
||||
Port int `json:"port"`
|
||||
DisableSNI bool `json:"disable_sni"`
|
||||
ReduceRtt bool `json:"reduce_rtt"`
|
||||
UDPRelayMode string `json:"udp_relay_mode"`
|
||||
CongestionController string `json:"congestion_controller"`
|
||||
SecurityConfig SecurityConfig `json:"security_config"`
|
||||
}
|
||||
|
||||
type AnyTLS struct {
|
||||
Port int `json:"port"`
|
||||
SecurityConfig SecurityConfig `json:"security_config"`
|
||||
}
|
||||
@@ -179,9 +191,11 @@ type RuleGroup struct {
|
||||
Id int64 `gorm:"primary_key"`
|
||||
Icon string `gorm:"type:MEDIUMTEXT;comment:Rule Group Icon"`
|
||||
Name string `gorm:"type:varchar(100);not null;default:'';comment:Rule Group Name"`
|
||||
Type string `gorm:"type:varchar(100);not null;default:'';comment:Rule Group Type"`
|
||||
Tags string `gorm:"type:text;comment:Selected Node Tags"`
|
||||
Rules string `gorm:"type:MEDIUMTEXT;comment:Rules"`
|
||||
Enable bool `gorm:"type:tinyint(1);not null;default:1;comment:Rule Group Enable"`
|
||||
Default bool `gorm:"type:tinyint(1);not null;default:0;comment:Rule Group is Default"`
|
||||
CreatedAt time.Time `gorm:"<-:create;comment:Creation Time"`
|
||||
UpdatedAt time.Time `gorm:"comment:Update Time"`
|
||||
}
|
||||
@@ -189,19 +203,14 @@ type RuleGroup struct {
|
||||
func (RuleGroup) TableName() string {
|
||||
return "server_rule_group"
|
||||
}
|
||||
|
||||
func reorderSort(tx *gorm.DB) error {
|
||||
var servers []*Server
|
||||
if err := tx.Model(&Server{}).Order("sort ASC").Find(&servers).Error; err != nil {
|
||||
var servers []Server
|
||||
if err := tx.Order("sort, id").Find(&servers).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for i, server := range servers {
|
||||
newSort := int64(i + 1)
|
||||
if server.Sort != newSort {
|
||||
if err := tx.Model(&Server{}).
|
||||
Where("id = ?", server.Id).
|
||||
Update("sort", newSort).Error; err != nil {
|
||||
if server.Sort != int64(i)+1 {
|
||||
if err := tx.Exec("UPDATE `server` SET sort = ? WHERE id = ?", i+1, server.Id).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user