feat(api): migrate server and node data handling, update related structures and logic
This commit is contained in:
@@ -1,3 +1,83 @@
|
||||
package server
|
||||
|
||||
const Unchanged = "Unchanged"
|
||||
const (
|
||||
Unchanged = "Unchanged"
|
||||
ShadowSocks = "shadowsocks"
|
||||
Vmess = "vmess"
|
||||
Vless = "vless"
|
||||
Trojan = "trojan"
|
||||
AnyTLS = "anytls"
|
||||
Tuic = "tuic"
|
||||
Hysteria2 = "hysteria2"
|
||||
)
|
||||
|
||||
type SecurityConfig struct {
|
||||
SNI string `json:"sni"`
|
||||
AllowInsecure *bool `json:"allow_insecure"`
|
||||
Fingerprint string `json:"fingerprint"`
|
||||
RealityServerAddress string `json:"reality_server_addr"`
|
||||
RealityServerPort int `json:"reality_server_port"`
|
||||
RealityPrivateKey string `json:"reality_private_key"`
|
||||
RealityPublicKey string `json:"reality_public_key"`
|
||||
RealityShortId string `json:"reality_short_id"`
|
||||
RealityMldsa65seed string `json:"reality_mldsa65seed"`
|
||||
}
|
||||
|
||||
type TransportConfig struct {
|
||||
Path string `json:"path"`
|
||||
Host string `json:"host"`
|
||||
ServiceName string `json:"service_name"`
|
||||
DisableSNI bool `json:"disable_sni"`
|
||||
ReduceRtt bool `json:"reduce_rtt"`
|
||||
UDPRelayMode string `json:"udp_relay_mode"`
|
||||
CongestionController string `json:"congestion_controller"`
|
||||
}
|
||||
|
||||
type VlessNode struct {
|
||||
Port uint16 `json:"port"`
|
||||
Flow string `json:"flow"`
|
||||
Network string `json:"transport"`
|
||||
TransportConfig *TransportConfig `json:"transport_config"`
|
||||
Security string `json:"security"`
|
||||
SecurityConfig *SecurityConfig `json:"security_config"`
|
||||
}
|
||||
|
||||
type VmessNode struct {
|
||||
Port uint16 `json:"port"`
|
||||
Network string `json:"transport"`
|
||||
TransportConfig *TransportConfig `json:"transport_config"`
|
||||
Security string `json:"security"`
|
||||
SecurityConfig *SecurityConfig `json:"security_config"`
|
||||
}
|
||||
|
||||
type ShadowsocksNode struct {
|
||||
Port uint16 `json:"port"`
|
||||
Cipher string `json:"method"`
|
||||
ServerKey string `json:"server_key"`
|
||||
}
|
||||
|
||||
type TrojanNode struct {
|
||||
Port uint16 `json:"port"`
|
||||
Network string `json:"transport"`
|
||||
TransportConfig *TransportConfig `json:"transport_config"`
|
||||
Security string `json:"security"`
|
||||
SecurityConfig *SecurityConfig `json:"security_config"`
|
||||
}
|
||||
|
||||
type AnyTLSNode struct {
|
||||
Port uint16 `json:"port"`
|
||||
SecurityConfig *SecurityConfig `json:"security_config"`
|
||||
}
|
||||
|
||||
type TuicNode struct {
|
||||
Port uint16 `json:"port"`
|
||||
SecurityConfig *SecurityConfig `json:"security_config"`
|
||||
}
|
||||
|
||||
type Hysteria2Node struct {
|
||||
Port uint16 `json:"port"`
|
||||
HopPorts string `json:"hop_ports"`
|
||||
HopInterval int `json:"hop_interval"`
|
||||
ObfsPassword string `json:"obfs_password"`
|
||||
SecurityConfig *SecurityConfig `json:"security_config"`
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/perfect-panel/server/internal/model/node"
|
||||
|
||||
"github.com/perfect-panel/server/internal/config"
|
||||
"github.com/perfect-panel/server/internal/svc"
|
||||
@@ -51,21 +51,21 @@ func (l *GetServerConfigLogic) GetServerConfig(req *types.GetServerConfigRequest
|
||||
return resp, nil
|
||||
}
|
||||
}
|
||||
nodeInfo, err := l.svcCtx.ServerModel.FindOne(l.ctx, req.ServerId)
|
||||
data, err := l.svcCtx.NodeModel.FindOneServer(l.ctx, req.ServerId)
|
||||
if err != nil {
|
||||
l.Errorw("[GetServerConfig] FindOne error", logger.Field("error", err.Error()))
|
||||
return nil, err
|
||||
}
|
||||
cfg := make(map[string]interface{})
|
||||
err = json.Unmarshal([]byte(nodeInfo.Config), &cfg)
|
||||
|
||||
protocols, err := data.UnmarshalProtocols()
|
||||
if err != nil {
|
||||
l.Errorw("[GetServerConfig] json unmarshal error", logger.Field("error", err.Error()))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if nodeInfo.Protocol == "shadowsocks" {
|
||||
if value, ok := cfg["server_key"]; ok && value != "" {
|
||||
cfg["server_key"] = base64.StdEncoding.EncodeToString([]byte(value.(string)))
|
||||
var cfg map[string]interface{}
|
||||
for _, protocol := range protocols {
|
||||
if protocol.Type == req.Protocol {
|
||||
cfg = l.compatible(protocol)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,18 +74,162 @@ func (l *GetServerConfigLogic) GetServerConfig(req *types.GetServerConfigRequest
|
||||
PullInterval: l.svcCtx.Config.Node.NodePullInterval,
|
||||
PushInterval: l.svcCtx.Config.Node.NodePushInterval,
|
||||
},
|
||||
Protocol: nodeInfo.Protocol,
|
||||
Protocol: req.Protocol,
|
||||
Config: cfg,
|
||||
}
|
||||
data, err := json.Marshal(resp)
|
||||
c, err := json.Marshal(resp)
|
||||
if err != nil {
|
||||
l.Errorw("[GetServerConfig] json marshal error", logger.Field("error", err.Error()))
|
||||
return nil, err
|
||||
}
|
||||
etag := tool.GenerateETag(data)
|
||||
etag := tool.GenerateETag(c)
|
||||
l.ctx.Header("ETag", etag)
|
||||
if err = l.svcCtx.Redis.Set(l.ctx, cacheKey, data, -1).Err(); err != nil {
|
||||
if err = l.svcCtx.Redis.Set(l.ctx, cacheKey, c, -1).Err(); err != nil {
|
||||
l.Errorw("[GetServerConfig] redis set error", logger.Field("error", err.Error()))
|
||||
}
|
||||
// Check If-None-Match header
|
||||
match := l.ctx.GetHeader("If-None-Match")
|
||||
if match == etag {
|
||||
return nil, xerr.StatusNotModified
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (l *GetServerConfigLogic) compatible(config node.Protocol) map[string]interface{} {
|
||||
var result interface{}
|
||||
switch config.Type {
|
||||
case ShadowSocks:
|
||||
result = ShadowsocksNode{
|
||||
Port: config.Port,
|
||||
Cipher: config.Cipher,
|
||||
ServerKey: config.ServerKey,
|
||||
}
|
||||
case Vless:
|
||||
result = VlessNode{
|
||||
Port: config.Port,
|
||||
Flow: config.Flow,
|
||||
Network: config.Transport,
|
||||
TransportConfig: &TransportConfig{
|
||||
Path: config.Path,
|
||||
Host: config.Host,
|
||||
ServiceName: config.ServiceName,
|
||||
DisableSNI: config.DisableSNI,
|
||||
ReduceRtt: config.ReduceRtt,
|
||||
UDPRelayMode: config.UDPRelayMode,
|
||||
CongestionController: config.CongestionController,
|
||||
},
|
||||
Security: config.Security,
|
||||
SecurityConfig: &SecurityConfig{
|
||||
SNI: config.SNI,
|
||||
AllowInsecure: &config.AllowInsecure,
|
||||
Fingerprint: config.Fingerprint,
|
||||
RealityServerAddress: config.RealityServerAddr,
|
||||
RealityServerPort: config.RealityServerPort,
|
||||
RealityPrivateKey: config.RealityPrivateKey,
|
||||
RealityPublicKey: config.RealityPublicKey,
|
||||
RealityShortId: config.RealityShortId,
|
||||
},
|
||||
}
|
||||
case Vmess:
|
||||
result = VmessNode{
|
||||
Port: config.Port,
|
||||
Network: config.Transport,
|
||||
TransportConfig: &TransportConfig{
|
||||
Path: config.Path,
|
||||
Host: config.Host,
|
||||
ServiceName: config.ServiceName,
|
||||
DisableSNI: config.DisableSNI,
|
||||
ReduceRtt: config.ReduceRtt,
|
||||
UDPRelayMode: config.UDPRelayMode,
|
||||
CongestionController: config.CongestionController,
|
||||
},
|
||||
Security: config.Security,
|
||||
SecurityConfig: &SecurityConfig{
|
||||
SNI: config.SNI,
|
||||
AllowInsecure: &config.AllowInsecure,
|
||||
Fingerprint: config.Fingerprint,
|
||||
RealityServerAddress: config.RealityServerAddr,
|
||||
RealityServerPort: config.RealityServerPort,
|
||||
RealityPrivateKey: config.RealityPrivateKey,
|
||||
RealityPublicKey: config.RealityPublicKey,
|
||||
RealityShortId: config.RealityShortId,
|
||||
},
|
||||
}
|
||||
case Trojan:
|
||||
result = TrojanNode{
|
||||
Port: config.Port,
|
||||
Network: config.Transport,
|
||||
TransportConfig: &TransportConfig{
|
||||
Path: config.Path,
|
||||
Host: config.Host,
|
||||
ServiceName: config.ServiceName,
|
||||
DisableSNI: config.DisableSNI,
|
||||
ReduceRtt: config.ReduceRtt,
|
||||
UDPRelayMode: config.UDPRelayMode,
|
||||
CongestionController: config.CongestionController,
|
||||
},
|
||||
Security: config.Security,
|
||||
SecurityConfig: &SecurityConfig{
|
||||
SNI: config.SNI,
|
||||
AllowInsecure: &config.AllowInsecure,
|
||||
Fingerprint: config.Fingerprint,
|
||||
RealityServerAddress: config.RealityServerAddr,
|
||||
RealityServerPort: config.RealityServerPort,
|
||||
RealityPrivateKey: config.RealityPrivateKey,
|
||||
RealityPublicKey: config.RealityPublicKey,
|
||||
RealityShortId: config.RealityShortId,
|
||||
},
|
||||
}
|
||||
case AnyTLS:
|
||||
result = AnyTLSNode{
|
||||
Port: config.Port,
|
||||
SecurityConfig: &SecurityConfig{
|
||||
SNI: config.SNI,
|
||||
AllowInsecure: &config.AllowInsecure,
|
||||
Fingerprint: config.Fingerprint,
|
||||
RealityServerAddress: config.RealityServerAddr,
|
||||
RealityServerPort: config.RealityServerPort,
|
||||
RealityPrivateKey: config.RealityPrivateKey,
|
||||
RealityPublicKey: config.RealityPublicKey,
|
||||
RealityShortId: config.RealityShortId,
|
||||
},
|
||||
}
|
||||
case Tuic:
|
||||
result = TuicNode{
|
||||
Port: config.Port,
|
||||
SecurityConfig: &SecurityConfig{
|
||||
SNI: config.SNI,
|
||||
AllowInsecure: &config.AllowInsecure,
|
||||
Fingerprint: config.Fingerprint,
|
||||
RealityServerAddress: config.RealityServerAddr,
|
||||
RealityServerPort: config.RealityServerPort,
|
||||
RealityPrivateKey: config.RealityPrivateKey,
|
||||
RealityPublicKey: config.RealityPublicKey,
|
||||
RealityShortId: config.RealityShortId,
|
||||
},
|
||||
}
|
||||
case Hysteria2:
|
||||
result = Hysteria2Node{
|
||||
Port: config.Port,
|
||||
HopPorts: config.HopPorts,
|
||||
HopInterval: config.HopInterval,
|
||||
ObfsPassword: config.ObfsPassword,
|
||||
SecurityConfig: &SecurityConfig{
|
||||
SNI: config.SNI,
|
||||
AllowInsecure: &config.AllowInsecure,
|
||||
Fingerprint: config.Fingerprint,
|
||||
RealityServerAddress: config.RealityServerAddr,
|
||||
RealityServerPort: config.RealityServerPort,
|
||||
RealityPrivateKey: config.RealityPrivateKey,
|
||||
RealityPublicKey: config.RealityPublicKey,
|
||||
RealityShortId: config.RealityShortId,
|
||||
},
|
||||
}
|
||||
|
||||
}
|
||||
var resp map[string]interface{}
|
||||
s, _ := json.Marshal(result)
|
||||
_ = json.Unmarshal(s, &resp)
|
||||
return resp
|
||||
}
|
||||
|
||||
@@ -3,8 +3,10 @@ package server
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/perfect-panel/server/internal/model/node"
|
||||
|
||||
"github.com/perfect-panel/server/internal/config"
|
||||
"github.com/perfect-panel/server/internal/svc"
|
||||
@@ -33,28 +35,46 @@ func NewGetServerUserListLogic(ctx *gin.Context, svcCtx *svc.ServiceContext) *Ge
|
||||
func (l *GetServerUserListLogic) GetServerUserList(req *types.GetServerUserListRequest) (resp *types.GetServerUserListResponse, err error) {
|
||||
cacheKey := fmt.Sprintf("%s%d", config.ServerUserListCacheKey, req.ServerId)
|
||||
cache, err := l.svcCtx.Redis.Get(l.ctx, cacheKey).Result()
|
||||
if err == nil {
|
||||
if cache != "" {
|
||||
etag := tool.GenerateETag([]byte(cache))
|
||||
resp := &types.GetServerUserListResponse{}
|
||||
// Check If-None-Match header
|
||||
if match := l.ctx.GetHeader("If-None-Match"); match == etag {
|
||||
return nil, xerr.StatusNotModified
|
||||
}
|
||||
l.ctx.Header("ETag", etag)
|
||||
err = json.Unmarshal([]byte(cache), resp)
|
||||
if err != nil {
|
||||
l.Errorw("[ServerUserListCacheKey] json unmarshal error", logger.Field("error", err.Error()))
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
if cache != "" {
|
||||
etag := tool.GenerateETag([]byte(cache))
|
||||
resp = &types.GetServerUserListResponse{}
|
||||
// Check If-None-Match header
|
||||
if match := l.ctx.GetHeader("If-None-Match"); match == etag {
|
||||
return nil, xerr.StatusNotModified
|
||||
}
|
||||
l.ctx.Header("ETag", etag)
|
||||
err = json.Unmarshal([]byte(cache), resp)
|
||||
if err != nil {
|
||||
l.Errorw("[ServerUserListCacheKey] json unmarshal error", logger.Field("error", err.Error()))
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
server, err := l.svcCtx.ServerModel.FindOne(l.ctx, req.ServerId)
|
||||
server, err := l.svcCtx.NodeModel.FindOneServer(l.ctx, req.ServerId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
subs, err := l.svcCtx.SubscribeModel.QuerySubscribeIdsByServerIdAndServerGroupId(l.ctx, server.Id, server.GroupId)
|
||||
|
||||
_, nodes, err := l.svcCtx.NodeModel.FilterNodeList(l.ctx, &node.FilterNodeParams{
|
||||
Page: 1,
|
||||
Size: 1000,
|
||||
ServerId: []int64{server.Id},
|
||||
Protocol: req.Protocol,
|
||||
})
|
||||
if err != nil {
|
||||
l.Errorw("FilterNodeList error", logger.Field("error", err.Error()))
|
||||
return nil, err
|
||||
}
|
||||
var nodeTag []string
|
||||
var nodeIds []int64
|
||||
for _, n := range nodes {
|
||||
nodeIds = append(nodeIds, n.Id)
|
||||
if n.Tags != "" {
|
||||
nodeTag = append(nodeTag, strings.Split(n.Tags, ",")...)
|
||||
}
|
||||
}
|
||||
|
||||
subs, err := l.svcCtx.SubscribeModel.QuerySubscribeIdsByNodeIdAndNodeTag(l.ctx, nodeIds, nodeTag)
|
||||
if err != nil {
|
||||
l.Errorw("QuerySubscribeIdsByServerIdAndServerGroupId error", logger.Field("error", err.Error()))
|
||||
return nil, err
|
||||
@@ -76,16 +96,10 @@ func (l *GetServerUserListLogic) GetServerUserList(req *types.GetServerUserListR
|
||||
return nil, err
|
||||
}
|
||||
for _, datum := range data {
|
||||
speedLimit := server.SpeedLimit
|
||||
if (int(sub.SpeedLimit) < server.SpeedLimit && sub.SpeedLimit != 0) ||
|
||||
(int(sub.SpeedLimit) > server.SpeedLimit && sub.SpeedLimit == 0) {
|
||||
speedLimit = int(sub.SpeedLimit)
|
||||
}
|
||||
|
||||
users = append(users, types.ServerUser{
|
||||
Id: datum.Id,
|
||||
UUID: datum.UUID,
|
||||
SpeedLimit: int64(speedLimit),
|
||||
SpeedLimit: sub.SpeedLimit,
|
||||
DeviceLimit: sub.DeviceLimit,
|
||||
})
|
||||
}
|
||||
@@ -106,5 +120,9 @@ func (l *GetServerUserListLogic) GetServerUserList(req *types.GetServerUserListR
|
||||
if err != nil {
|
||||
l.Errorw("[ServerUserListCacheKey] redis set error", logger.Field("error", err.Error()))
|
||||
}
|
||||
// Check If-None-Match header
|
||||
if match := l.ctx.GetHeader("If-None-Match"); match == etag {
|
||||
return nil, xerr.StatusNotModified
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ func (l *PushOnlineUsersLogic) PushOnlineUsers(req *types.OnlineUsersRequest) er
|
||||
}
|
||||
|
||||
// Find server info
|
||||
_, err := l.svcCtx.ServerModel.FindOne(l.ctx, req.ServerId)
|
||||
_, err := l.svcCtx.NodeModel.FindOneServer(l.ctx, req.ServerId)
|
||||
if err != nil {
|
||||
l.Errorw("[PushOnlineUsers] FindOne error", logger.Field("error", err))
|
||||
return fmt.Errorf("server not found: %w", err)
|
||||
|
||||
@@ -27,7 +27,7 @@ func NewServerPushStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *
|
||||
|
||||
func (l *ServerPushStatusLogic) ServerPushStatus(req *types.ServerPushStatusRequest) error {
|
||||
// Find server info
|
||||
serverInfo, err := l.svcCtx.ServerModel.FindOne(l.ctx, req.ServerId)
|
||||
serverInfo, err := l.svcCtx.NodeModel.FindOneServer(l.ctx, req.ServerId)
|
||||
if err != nil || serverInfo.Id <= 0 {
|
||||
l.Errorw("[PushOnlineUsers] FindOne error", logger.Field("error", err))
|
||||
return errors.New("server not found")
|
||||
|
||||
@@ -32,7 +32,7 @@ func NewServerPushUserTrafficLogic(ctx context.Context, svcCtx *svc.ServiceConte
|
||||
|
||||
func (l *ServerPushUserTrafficLogic) ServerPushUserTraffic(req *types.ServerPushUserTrafficRequest) error {
|
||||
// Find server info
|
||||
serverInfo, err := l.svcCtx.ServerModel.FindOne(l.ctx, req.ServerId)
|
||||
serverInfo, err := l.svcCtx.NodeModel.FindOneServer(l.ctx, req.ServerId)
|
||||
if err != nil {
|
||||
l.Errorw("[PushOnlineUsers] FindOne error", logger.Field("error", err))
|
||||
return errors.New("server not found")
|
||||
|
||||
Reference in New Issue
Block a user