diff --git a/apis/admin/log.api b/apis/admin/log.api index 727cb02..12dba0e 100644 --- a/apis/admin/log.api +++ b/apis/admin/log.api @@ -21,6 +21,149 @@ type ( Total int64 `json:"total"` List []MessageLog `json:"list"` } + FilterLogParams { + Page int `form:"page"` + Size int `form:"size"` + Date string `form:"date,optional"` + Search string `form:"search,optional"` + } + FilterEmailLogResponse { + Total int64 `json:"total"` + List []MessageLog `json:"list"` + } + FilterMobileLogResponse { + Total int64 `json:"total"` + List []MessageLog `json:"list"` + } + SubscribeLog { + UserId int64 `json:"user_id"` + Token string `json:"token"` + UserAgent string `json:"user_agent"` + ClientIP string `json:"client_ip"` + UserSubscribeId int64 `json:"user_subscribe_id"` + SubscribedAt int64 `json:"subscribed_at"` + } + FilterSubscribeLogRequest { + FilterLogParams + UserId int64 `form:"user_id,optional"` + } + FilterSubscribeLogResponse { + Total int64 `json:"total"` + List []SubscribeLog `json:"list"` + } + LoginLog { + UserId int64 `json:"user_id"` + Method string `json:"method"` + LoginIP string `json:"login_ip"` + UserAgent string `json:"user_agent"` + Success bool `json:"success"` + LoginTime int64 `json:"login_time"` + } + FilterLoginLogRequest { + FilterLogParams + UserId int64 `form:"user_id,optional"` + } + FilterLoginLogResponse { + Total int64 `json:"total"` + List []LoginLog `json:"list"` + } + RegisterLog { + UserId int64 `json:"user_id"` + AuthMethod string `json:"auth_method"` + Identifier string `json:"identifier"` + RegisterIP string `json:"register_ip"` + UserAgent string `json:"user_agent"` + RegisterTime int64 `json:"register_time"` + } + FilterRegisterLogRequest { + FilterLogParams + UserId int64 `form:"user_id,optional"` + } + FilterRegisterLogResponse { + Total int64 `json:"total"` + List []RegisterLog `json:"list"` + } + ResetSubscribeLog { + Type uint16 `json:"type"` + UserId int64 `json:"user_id"` + UserSubscribeId int64 `json:"user_subscribe_id"` + OrderNo string `json:"order_no,omitempty"` + ResetAt int64 `json:"reset_at"` + } + FilterResetSubscribeLogRequest { + FilterLogParams + UserSubscribeId int64 `form:"user_subscribe_id,optional"` + } + FilterResetSubscribeLogResponse { + Total int64 `json:"total"` + List []ResetSubscribeLog `json:"list"` + } + UserSubscribeTrafficLog { + SubscribeId int64 `json:"subscribe_id"` // Subscribe ID + UserId int64 `json:"user_id"` // User ID + Upload int64 `json:"upload"` // Upload traffic in bytes + Download int64 `json:"download"` // Download traffic in bytes + Total int64 `json:"total"` // Total traffic in bytes (Upload + Download) + Date string `json:"date"` // Date in YYYY-MM-DD format + } + FilterSubscribeTrafficRequest { + FilterLogParams + UserId int64 `form:"user_id,optional"` + UserSubscribeId int64 `form:"user_subscribe_id,optional"` + } + FilterSubscribeTrafficResponse { + Total int64 `json:"total"` + List []UserSubscribeTrafficLog `json:"list"` + } + ServerTrafficLog { + ServerId int64 `json:"server_id"` // Server ID + Upload int64 `json:"upload"` // Upload traffic in bytes + Download int64 `json:"download"` // Download traffic in bytes + Total int64 `json:"total"` // Total traffic in bytes (Upload + Download) + Date string `json:"date"` // Date in YYYY-MM-DD format + } + FilterServerTrafficLogRequest { + FilterLogParams + ServerId int64 `form:"server_id,optional"` + } + FilterServerTrafficLogResponse { + Total int64 `json:"total"` + List []ServerTrafficLog `json:"list"` + } + FilterBalanceLogRequest { + FilterLogParams + UserId int64 `form:"user_id,optional"` + } + FilterBalanceLogResponse { + Total int64 `json:"total"` + List []BalanceLog `json:"list"` + } + FilterCommissionLogRequest { + FilterLogParams + UserId int64 `form:"user_id,optional"` + } + FilterCommissionLogResponse { + Total int64 `json:"total"` + List []CommissionLog `json:"list"` + } + GiftLog { + Type uint16 `json:"type"` + userId int64 `json:"user_id"` + OrderNo string `json:"order_no"` + SubscribeId int64 `json:"subscribe_id"` + Amount int64 `json:"amount"` + Balance int64 `json:"balance"` + Remark string `json:"remark,omitempty"` + CreatedAt int64 `json:"created_at"` + } + FilterGiftLogRequest { + FilterLogParams + UserId int64 `form:"user_id,optional"` + } + FilterGiftLogResponse { + Total int64 `json:"total"` + List []GiftLog `json:"list"` + } ) @server ( @@ -32,5 +175,49 @@ service ppanel { @doc "Get message log list" @handler GetMessageLogList get /message/list (GetMessageLogListRequest) returns (GetMessageLogListResponse) + + @doc "Filter email log" + @handler FilterEmailLog + get /email/list (FilterLogParams) returns (FilterEmailLogResponse) + + @doc "Filter mobile log" + @handler FilterMobileLog + get /mobile/list (FilterLogParams) returns (FilterMobileLogResponse) + + @doc "Filter subscribe log" + @handler FilterSubscribeLog + get /subscribe/list (FilterSubscribeLogRequest) returns (FilterSubscribeLogResponse) + + @doc "Filter login log" + @handler FilterLoginLog + get /login/list (FilterLoginLogRequest) returns (FilterLoginLogResponse) + + @doc "Filter register log" + @handler FilterRegisterLog + get /register/list (FilterRegisterLogRequest) returns (FilterRegisterLogResponse) + + @doc "Filter reset subscribe log" + @handler FilterResetSubscribeLog + get /subscribe/reset/list (FilterResetSubscribeLogRequest) returns (FilterResetSubscribeLogResponse) + + @doc "Filter user subscribe traffic log" + @handler FilterUserSubscribeTrafficLog + get /subscribe/traffic/list (FilterSubscribeTrafficRequest) returns (FilterSubscribeTrafficResponse) + + @doc "Filter server traffic log" + @handler FilterServerTrafficLog + get /server/traffic/list (FilterServerTrafficLogRequest) returns (FilterServerTrafficLogResponse) + + @doc "Filter balance log" + @handler FilterBalanceLog + get /balance/list (FilterBalanceLogRequest) returns (FilterBalanceLogResponse) + + @doc "Filter commission log" + @handler FilterCommissionLog + get /commission/list (FilterCommissionLogRequest) returns (FilterCommissionLogResponse) + + @doc "Filter gift log" + @handler FilterGiftLog + get /gift/list (FilterGiftLogRequest) returns (FilterGiftLogResponse) } diff --git a/apis/admin/server.api b/apis/admin/server.api index f9c8393..a2a35eb 100644 --- a/apis/admin/server.api +++ b/apis/admin/server.api @@ -41,6 +41,7 @@ type ( } Protocol { Type string `json:"type"` + Port uint16 `json:"port"` Security string `json:"security,omitempty"` SNI string `json:"sni,omitempty"` AllowInsecure bool `json:"allow_insecure,omitempty"` @@ -103,32 +104,39 @@ type ( Protocols []Protocol `json:"protocols"` } Node { - Id int64 `json:"id"` - Name string `json:"name"` - Tags string `json:"tags"` - Port uint16 `json:"port"` - Address string `json:"address"` - ServerId int64 `json:"server_id"` - Protocol string `json:"protocol"` - CreatedAt int64 `json:"created_at"` - UpdatedAt int64 `json:"updated_at"` + Id int64 `json:"id"` + Name string `json:"name"` + Tags []string `json:"tags"` + Port uint16 `json:"port"` + Address string `json:"address"` + ServerId int64 `json:"server_id"` + Protocol string `json:"protocol"` + Enabled *bool `json:"enabled"` + CreatedAt int64 `json:"created_at"` + UpdatedAt int64 `json:"updated_at"` } CreateNodeRequest { - Name string `json:"name"` - Tags string `json:"tags,omitempty"` - Port uint16 `json:"port"` - Address string `json:"address"` - ServerId int64 `json:"server_id"` - Protocol string `json:"protocol"` + Name string `json:"name"` + Tags []string `json:"tags,omitempty"` + Port uint16 `json:"port"` + Address string `json:"address"` + ServerId int64 `json:"server_id"` + Protocol string `json:"protocol"` + Enabled *bool `json:"enabled"` } UpdateNodeRequest { - Id int64 `json:"id"` - Name string `json:"name"` - Tags string `json:"tags,omitempty"` - Port uint16 `json:"port"` - Address string `json:"address"` - ServerId int64 `json:"server_id"` - Protocol string `json:"protocol"` + Id int64 `json:"id"` + Name string `json:"name"` + Tags []string `json:"tags,omitempty"` + Port uint16 `json:"port"` + Address string `json:"address"` + ServerId int64 `json:"server_id"` + Protocol string `json:"protocol"` + Enabled *bool `json:"enabled"` + } + ToggleNodeStatusRequest { + Id int64 `json:"id"` + Enable *bool `json:"enable"` } DeleteNodeRequest { Id int64 `json:"id"` @@ -185,5 +193,9 @@ service ppanel { @doc "Filter Node List" @handler FilterNodeList get /node/list (FilterNodeListRequest) returns (FilterNodeListResponse) + + @doc "Toggle Node Status" + @handler ToggleNodeStatus + post /node/status/toggle (ToggleNodeStatusRequest) } diff --git a/apis/public/user.api b/apis/public/user.api index 66c8d08..3236bc5 100644 --- a/apis/public/user.api +++ b/apis/public/user.api @@ -25,15 +25,8 @@ type ( Total int64 `json:"total"` } QueryUserBalanceLogListResponse { - List []UserBalanceLog `json:"list"` - Total int64 `json:"total"` - } - CommissionLog { - Id int64 `json:"id"` - UserId int64 `json:"user_id"` - OrderNo string `json:"order_no"` - Amount int64 `json:"amount"` - CreatedAt int64 `json:"created_at"` + List []BalanceLog `json:"list"` + Total int64 `json:"total"` } QueryUserCommissionLogListRequest { Page int `form:"page"` diff --git a/apis/types.api b/apis/types.api index fe0b0ef..f4a1930 100644 --- a/apis/types.api +++ b/apis/types.api @@ -446,15 +446,6 @@ type ( CreatedAt int64 `json:"created_at"` UpdatedAt int64 `json:"updated_at"` } - UserBalanceLog { - Id int64 `json:"id"` - UserId int64 `json:"user_id"` - Amount int64 `json:"amount"` - Type uint8 `json:"type"` - OrderId int64 `json:"order_id"` - Balance int64 `json:"balance"` - CreatedAt int64 `json:"created_at"` - } UserAffiliate { Avatar string `json:"avatar"` Identifier string `json:"identifier"` @@ -756,10 +747,25 @@ type ( } ResetSubscribeTrafficLog { Id int64 `json:"id"` - Type uint8 `json:"type"` + Type uint16 `json:"type"` UserSubscribeId int64 `json:"user_subscribe_id"` OrderNo string `json:"order_no,omitempty"` ResetAt int64 `json:"reset_at"` } + BalanceLog { + Type uint16 `json:"type"` + UserId int64 `json:"user_id"` + Amount int64 `json:"amount"` + OrderId int64 `json:"order_id,omitempty"` + Balance int64 `json:"balance"` + Timestamp int64 `json:"timestamp"` + } + CommissionLog { + Type uint16 `json:"type"` + UserId int64 `json:"user_id"` + Amount int64 `json:"amount"` + OrderNo string `json:"order_no"` + CreatedAt int64 `json:"created_at"` + } ) diff --git a/initialize/migrate/database/02105_node.down.sql b/initialize/migrate/database/02105_node.down.sql new file mode 100644 index 0000000..4bd2529 --- /dev/null +++ b/initialize/migrate/database/02105_node.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS `servers`; \ No newline at end of file diff --git a/initialize/migrate/database/02105_node.up.sql b/initialize/migrate/database/02105_node.up.sql new file mode 100644 index 0000000..cb9a016 --- /dev/null +++ b/initialize/migrate/database/02105_node.up.sql @@ -0,0 +1,14 @@ +CREATE TABLE IF NOT EXISTS `servers` ( + `id` bigint NOT NULL AUTO_INCREMENT, + `name` varchar(100) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'Server Name', + `country` varchar(128) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'Country', + `city` varchar(128) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'City', + `ratio` decimal(4,2) NOT NULL DEFAULT '0.00' COMMENT 'Traffic Ratio', + `address` varchar(100) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'Server Address', + `sort` bigint NOT NULL DEFAULT '0' COMMENT 'Sort', + `protocols` text COLLATE utf8mb4_general_ci COMMENT 'Protocol', + `last_reported_at` datetime(3) DEFAULT NULL COMMENT 'Last Reported Time', + `created_at` datetime(3) DEFAULT NULL COMMENT 'Creation Time', + `updated_at` datetime(3) DEFAULT NULL COMMENT 'Update Time', + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; \ No newline at end of file diff --git a/internal/handler/admin/log/filterBalanceLogHandler.go b/internal/handler/admin/log/filterBalanceLogHandler.go new file mode 100644 index 0000000..c8bf7d1 --- /dev/null +++ b/internal/handler/admin/log/filterBalanceLogHandler.go @@ -0,0 +1,26 @@ +package log + +import ( + "github.com/gin-gonic/gin" + "github.com/perfect-panel/server/internal/logic/admin/log" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/result" +) + +// Filter balance log +func FilterBalanceLogHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { + return func(c *gin.Context) { + var req types.FilterBalanceLogRequest + _ = c.ShouldBind(&req) + validateErr := svcCtx.Validate(&req) + if validateErr != nil { + result.ParamErrorResult(c, validateErr) + return + } + + l := log.NewFilterBalanceLogLogic(c.Request.Context(), svcCtx) + resp, err := l.FilterBalanceLog(&req) + result.HttpResult(c, resp, err) + } +} diff --git a/internal/handler/admin/log/filterCommissionLogHandler.go b/internal/handler/admin/log/filterCommissionLogHandler.go new file mode 100644 index 0000000..07361cd --- /dev/null +++ b/internal/handler/admin/log/filterCommissionLogHandler.go @@ -0,0 +1,26 @@ +package log + +import ( + "github.com/gin-gonic/gin" + "github.com/perfect-panel/server/internal/logic/admin/log" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/result" +) + +// Filter commission log +func FilterCommissionLogHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { + return func(c *gin.Context) { + var req types.FilterCommissionLogRequest + _ = c.ShouldBind(&req) + validateErr := svcCtx.Validate(&req) + if validateErr != nil { + result.ParamErrorResult(c, validateErr) + return + } + + l := log.NewFilterCommissionLogLogic(c.Request.Context(), svcCtx) + resp, err := l.FilterCommissionLog(&req) + result.HttpResult(c, resp, err) + } +} diff --git a/internal/handler/admin/log/filterEmailLogHandler.go b/internal/handler/admin/log/filterEmailLogHandler.go new file mode 100644 index 0000000..6d9f03d --- /dev/null +++ b/internal/handler/admin/log/filterEmailLogHandler.go @@ -0,0 +1,26 @@ +package log + +import ( + "github.com/gin-gonic/gin" + "github.com/perfect-panel/server/internal/logic/admin/log" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/result" +) + +// Filter email log +func FilterEmailLogHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { + return func(c *gin.Context) { + var req types.FilterLogParams + _ = c.ShouldBind(&req) + validateErr := svcCtx.Validate(&req) + if validateErr != nil { + result.ParamErrorResult(c, validateErr) + return + } + + l := log.NewFilterEmailLogLogic(c.Request.Context(), svcCtx) + resp, err := l.FilterEmailLog(&req) + result.HttpResult(c, resp, err) + } +} diff --git a/internal/handler/admin/log/filterGiftLogHandler.go b/internal/handler/admin/log/filterGiftLogHandler.go new file mode 100644 index 0000000..e650a27 --- /dev/null +++ b/internal/handler/admin/log/filterGiftLogHandler.go @@ -0,0 +1,26 @@ +package log + +import ( + "github.com/gin-gonic/gin" + "github.com/perfect-panel/server/internal/logic/admin/log" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/result" +) + +// Filter gift log +func FilterGiftLogHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { + return func(c *gin.Context) { + var req types.FilterGiftLogRequest + _ = c.ShouldBind(&req) + validateErr := svcCtx.Validate(&req) + if validateErr != nil { + result.ParamErrorResult(c, validateErr) + return + } + + l := log.NewFilterGiftLogLogic(c.Request.Context(), svcCtx) + resp, err := l.FilterGiftLog(&req) + result.HttpResult(c, resp, err) + } +} diff --git a/internal/handler/admin/log/filterLoginLogHandler.go b/internal/handler/admin/log/filterLoginLogHandler.go new file mode 100644 index 0000000..c2dae41 --- /dev/null +++ b/internal/handler/admin/log/filterLoginLogHandler.go @@ -0,0 +1,26 @@ +package log + +import ( + "github.com/gin-gonic/gin" + "github.com/perfect-panel/server/internal/logic/admin/log" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/result" +) + +// Filter login log +func FilterLoginLogHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { + return func(c *gin.Context) { + var req types.FilterLoginLogRequest + _ = c.ShouldBind(&req) + validateErr := svcCtx.Validate(&req) + if validateErr != nil { + result.ParamErrorResult(c, validateErr) + return + } + + l := log.NewFilterLoginLogLogic(c.Request.Context(), svcCtx) + resp, err := l.FilterLoginLog(&req) + result.HttpResult(c, resp, err) + } +} diff --git a/internal/handler/admin/log/filterMobileLogHandler.go b/internal/handler/admin/log/filterMobileLogHandler.go new file mode 100644 index 0000000..0d45e27 --- /dev/null +++ b/internal/handler/admin/log/filterMobileLogHandler.go @@ -0,0 +1,26 @@ +package log + +import ( + "github.com/gin-gonic/gin" + "github.com/perfect-panel/server/internal/logic/admin/log" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/result" +) + +// Filter mobile log +func FilterMobileLogHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { + return func(c *gin.Context) { + var req types.FilterLogParams + _ = c.ShouldBind(&req) + validateErr := svcCtx.Validate(&req) + if validateErr != nil { + result.ParamErrorResult(c, validateErr) + return + } + + l := log.NewFilterMobileLogLogic(c.Request.Context(), svcCtx) + resp, err := l.FilterMobileLog(&req) + result.HttpResult(c, resp, err) + } +} diff --git a/internal/handler/admin/log/filterRegisterLogHandler.go b/internal/handler/admin/log/filterRegisterLogHandler.go new file mode 100644 index 0000000..a5ca9e8 --- /dev/null +++ b/internal/handler/admin/log/filterRegisterLogHandler.go @@ -0,0 +1,26 @@ +package log + +import ( + "github.com/gin-gonic/gin" + "github.com/perfect-panel/server/internal/logic/admin/log" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/result" +) + +// Filter register log +func FilterRegisterLogHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { + return func(c *gin.Context) { + var req types.FilterRegisterLogRequest + _ = c.ShouldBind(&req) + validateErr := svcCtx.Validate(&req) + if validateErr != nil { + result.ParamErrorResult(c, validateErr) + return + } + + l := log.NewFilterRegisterLogLogic(c.Request.Context(), svcCtx) + resp, err := l.FilterRegisterLog(&req) + result.HttpResult(c, resp, err) + } +} diff --git a/internal/handler/admin/log/filterResetSubscribeLogHandler.go b/internal/handler/admin/log/filterResetSubscribeLogHandler.go new file mode 100644 index 0000000..f4d96e5 --- /dev/null +++ b/internal/handler/admin/log/filterResetSubscribeLogHandler.go @@ -0,0 +1,26 @@ +package log + +import ( + "github.com/gin-gonic/gin" + "github.com/perfect-panel/server/internal/logic/admin/log" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/result" +) + +// Filter reset subscribe log +func FilterResetSubscribeLogHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { + return func(c *gin.Context) { + var req types.FilterResetSubscribeLogRequest + _ = c.ShouldBind(&req) + validateErr := svcCtx.Validate(&req) + if validateErr != nil { + result.ParamErrorResult(c, validateErr) + return + } + + l := log.NewFilterResetSubscribeLogLogic(c.Request.Context(), svcCtx) + resp, err := l.FilterResetSubscribeLog(&req) + result.HttpResult(c, resp, err) + } +} diff --git a/internal/handler/admin/log/filterServerTrafficLogHandler.go b/internal/handler/admin/log/filterServerTrafficLogHandler.go new file mode 100644 index 0000000..c2c8b51 --- /dev/null +++ b/internal/handler/admin/log/filterServerTrafficLogHandler.go @@ -0,0 +1,26 @@ +package log + +import ( + "github.com/gin-gonic/gin" + "github.com/perfect-panel/server/internal/logic/admin/log" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/result" +) + +// Filter server traffic log +func FilterServerTrafficLogHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { + return func(c *gin.Context) { + var req types.FilterSubscribeTrafficRequest + _ = c.ShouldBind(&req) + validateErr := svcCtx.Validate(&req) + if validateErr != nil { + result.ParamErrorResult(c, validateErr) + return + } + + l := log.NewFilterServerTrafficLogLogic(c.Request.Context(), svcCtx) + resp, err := l.FilterServerTrafficLog(&req) + result.HttpResult(c, resp, err) + } +} diff --git a/internal/handler/admin/log/filterSubscribeLogHandler.go b/internal/handler/admin/log/filterSubscribeLogHandler.go new file mode 100644 index 0000000..f01b61e --- /dev/null +++ b/internal/handler/admin/log/filterSubscribeLogHandler.go @@ -0,0 +1,26 @@ +package log + +import ( + "github.com/gin-gonic/gin" + "github.com/perfect-panel/server/internal/logic/admin/log" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/result" +) + +// Filter subscribe log +func FilterSubscribeLogHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { + return func(c *gin.Context) { + var req types.FilterSubscribeLogRequest + _ = c.ShouldBind(&req) + validateErr := svcCtx.Validate(&req) + if validateErr != nil { + result.ParamErrorResult(c, validateErr) + return + } + + l := log.NewFilterSubscribeLogLogic(c.Request.Context(), svcCtx) + resp, err := l.FilterSubscribeLog(&req) + result.HttpResult(c, resp, err) + } +} diff --git a/internal/handler/admin/log/filterUserSubscribeTrafficLogHandler.go b/internal/handler/admin/log/filterUserSubscribeTrafficLogHandler.go new file mode 100644 index 0000000..976e278 --- /dev/null +++ b/internal/handler/admin/log/filterUserSubscribeTrafficLogHandler.go @@ -0,0 +1,26 @@ +package log + +import ( + "github.com/gin-gonic/gin" + "github.com/perfect-panel/server/internal/logic/admin/log" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/result" +) + +// Filter user subscribe traffic log +func FilterUserSubscribeTrafficLogHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { + return func(c *gin.Context) { + var req types.FilterSubscribeTrafficRequest + _ = c.ShouldBind(&req) + validateErr := svcCtx.Validate(&req) + if validateErr != nil { + result.ParamErrorResult(c, validateErr) + return + } + + l := log.NewFilterUserSubscribeTrafficLogLogic(c.Request.Context(), svcCtx) + resp, err := l.FilterUserSubscribeTrafficLog(&req) + result.HttpResult(c, resp, err) + } +} diff --git a/internal/handler/admin/server/createNodeHandler.go b/internal/handler/admin/server/createNodeHandler.go index cac4455..e872b09 100644 --- a/internal/handler/admin/server/createNodeHandler.go +++ b/internal/handler/admin/server/createNodeHandler.go @@ -8,7 +8,7 @@ import ( "github.com/perfect-panel/server/pkg/result" ) -// Create node +// Create Node func CreateNodeHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { return func(c *gin.Context) { var req types.CreateNodeRequest diff --git a/internal/handler/admin/server/createServerHandler.go b/internal/handler/admin/server/createServerHandler.go new file mode 100644 index 0000000..2068122 --- /dev/null +++ b/internal/handler/admin/server/createServerHandler.go @@ -0,0 +1,26 @@ +package server + +import ( + "github.com/gin-gonic/gin" + "github.com/perfect-panel/server/internal/logic/admin/server" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/result" +) + +// CreateServerHandler Create Server +func CreateServerHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { + return func(c *gin.Context) { + var req types.CreateServerRequest + _ = c.ShouldBind(&req) + validateErr := svcCtx.Validate(&req) + if validateErr != nil { + result.ParamErrorResult(c, validateErr) + return + } + + l := server.NewCreateServerLogic(c.Request.Context(), svcCtx) + err := l.CreateServer(&req) + result.HttpResult(c, nil, err) + } +} diff --git a/internal/handler/admin/server/deleteNodeHandler.go b/internal/handler/admin/server/deleteNodeHandler.go index 76d3d49..37ac80b 100644 --- a/internal/handler/admin/server/deleteNodeHandler.go +++ b/internal/handler/admin/server/deleteNodeHandler.go @@ -8,7 +8,7 @@ import ( "github.com/perfect-panel/server/pkg/result" ) -// Delete node +// Delete Node func DeleteNodeHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { return func(c *gin.Context) { var req types.DeleteNodeRequest diff --git a/internal/handler/admin/server/deleteServerHandler.go b/internal/handler/admin/server/deleteServerHandler.go new file mode 100644 index 0000000..677fb17 --- /dev/null +++ b/internal/handler/admin/server/deleteServerHandler.go @@ -0,0 +1,26 @@ +package server + +import ( + "github.com/gin-gonic/gin" + "github.com/perfect-panel/server/internal/logic/admin/server" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/result" +) + +// Delete Server +func DeleteServerHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { + return func(c *gin.Context) { + var req types.DeleteServerRequest + _ = c.ShouldBind(&req) + validateErr := svcCtx.Validate(&req) + if validateErr != nil { + result.ParamErrorResult(c, validateErr) + return + } + + l := server.NewDeleteServerLogic(c.Request.Context(), svcCtx) + err := l.DeleteServer(&req) + result.HttpResult(c, nil, err) + } +} diff --git a/internal/handler/admin/server/filterNodeListHandler.go b/internal/handler/admin/server/filterNodeListHandler.go new file mode 100644 index 0000000..5e154ca --- /dev/null +++ b/internal/handler/admin/server/filterNodeListHandler.go @@ -0,0 +1,26 @@ +package server + +import ( + "github.com/gin-gonic/gin" + "github.com/perfect-panel/server/internal/logic/admin/server" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/result" +) + +// Filter Node List +func FilterNodeListHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { + return func(c *gin.Context) { + var req types.FilterNodeListRequest + _ = c.ShouldBind(&req) + validateErr := svcCtx.Validate(&req) + if validateErr != nil { + result.ParamErrorResult(c, validateErr) + return + } + + l := server.NewFilterNodeListLogic(c.Request.Context(), svcCtx) + resp, err := l.FilterNodeList(&req) + result.HttpResult(c, resp, err) + } +} diff --git a/internal/handler/admin/server/filterServerListHandler.go b/internal/handler/admin/server/filterServerListHandler.go new file mode 100644 index 0000000..9e6cb7b --- /dev/null +++ b/internal/handler/admin/server/filterServerListHandler.go @@ -0,0 +1,26 @@ +package server + +import ( + "github.com/gin-gonic/gin" + "github.com/perfect-panel/server/internal/logic/admin/server" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/result" +) + +// FilterServerListHandler Filter Server List +func FilterServerListHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { + return func(c *gin.Context) { + var req types.FilterServerListRequest + _ = c.ShouldBind(&req) + validateErr := svcCtx.Validate(&req) + if validateErr != nil { + result.ParamErrorResult(c, validateErr) + return + } + + l := server.NewFilterServerListLogic(c.Request.Context(), svcCtx) + resp, err := l.FilterServerList(&req) + result.HttpResult(c, resp, err) + } +} diff --git a/internal/handler/admin/server/getServerProtocolsHandler.go b/internal/handler/admin/server/getServerProtocolsHandler.go new file mode 100644 index 0000000..14238ca --- /dev/null +++ b/internal/handler/admin/server/getServerProtocolsHandler.go @@ -0,0 +1,26 @@ +package server + +import ( + "github.com/gin-gonic/gin" + "github.com/perfect-panel/server/internal/logic/admin/server" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/result" +) + +// Get Server Protocols +func GetServerProtocolsHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { + return func(c *gin.Context) { + var req types.GetServerProtocolsRequest + _ = c.ShouldBind(&req) + validateErr := svcCtx.Validate(&req) + if validateErr != nil { + result.ParamErrorResult(c, validateErr) + return + } + + l := server.NewGetServerProtocolsLogic(c.Request.Context(), svcCtx) + resp, err := l.GetServerProtocols(&req) + result.HttpResult(c, resp, err) + } +} diff --git a/internal/handler/admin/server/toggleNodeStatusHandler.go b/internal/handler/admin/server/toggleNodeStatusHandler.go new file mode 100644 index 0000000..67144ad --- /dev/null +++ b/internal/handler/admin/server/toggleNodeStatusHandler.go @@ -0,0 +1,26 @@ +package server + +import ( + "github.com/gin-gonic/gin" + "github.com/perfect-panel/server/internal/logic/admin/server" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/result" +) + +// Toggle Node Status +func ToggleNodeStatusHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { + return func(c *gin.Context) { + var req types.ToggleNodeStatusRequest + _ = c.ShouldBind(&req) + validateErr := svcCtx.Validate(&req) + if validateErr != nil { + result.ParamErrorResult(c, validateErr) + return + } + + l := server.NewToggleNodeStatusLogic(c.Request.Context(), svcCtx) + err := l.ToggleNodeStatus(&req) + result.HttpResult(c, nil, err) + } +} diff --git a/internal/handler/admin/server/updateNodeHandler.go b/internal/handler/admin/server/updateNodeHandler.go index 58e8690..af19537 100644 --- a/internal/handler/admin/server/updateNodeHandler.go +++ b/internal/handler/admin/server/updateNodeHandler.go @@ -8,7 +8,7 @@ import ( "github.com/perfect-panel/server/pkg/result" ) -// Update node +// Update Node func UpdateNodeHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { return func(c *gin.Context) { var req types.UpdateNodeRequest diff --git a/internal/handler/admin/server/updateServerHandler.go b/internal/handler/admin/server/updateServerHandler.go new file mode 100644 index 0000000..24570c6 --- /dev/null +++ b/internal/handler/admin/server/updateServerHandler.go @@ -0,0 +1,26 @@ +package server + +import ( + "github.com/gin-gonic/gin" + "github.com/perfect-panel/server/internal/logic/admin/server" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/result" +) + +// Update Server +func UpdateServerHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { + return func(c *gin.Context) { + var req types.UpdateServerRequest + _ = c.ShouldBind(&req) + validateErr := svcCtx.Validate(&req) + if validateErr != nil { + result.ParamErrorResult(c, validateErr) + return + } + + l := server.NewUpdateServerLogic(c.Request.Context(), svcCtx) + err := l.UpdateServer(&req) + result.HttpResult(c, nil, err) + } +} diff --git a/internal/handler/admin/server/batchDeleteNodeGroupHandler.go b/internal/handler/admin/server_bak/batchDeleteNodeGroupHandler.go similarity index 78% rename from internal/handler/admin/server/batchDeleteNodeGroupHandler.go rename to internal/handler/admin/server_bak/batchDeleteNodeGroupHandler.go index 22d0062..037f9ce 100644 --- a/internal/handler/admin/server/batchDeleteNodeGroupHandler.go +++ b/internal/handler/admin/server_bak/batchDeleteNodeGroupHandler.go @@ -1,8 +1,8 @@ -package server +package server_bak import ( "github.com/gin-gonic/gin" - "github.com/perfect-panel/server/internal/logic/admin/server" + "github.com/perfect-panel/server/internal/logic/admin/server_bak" "github.com/perfect-panel/server/internal/svc" "github.com/perfect-panel/server/internal/types" "github.com/perfect-panel/server/pkg/result" @@ -19,7 +19,7 @@ func BatchDeleteNodeGroupHandler(svcCtx *svc.ServiceContext) func(c *gin.Context return } - l := server.NewBatchDeleteNodeGroupLogic(c.Request.Context(), svcCtx) + l := server_bak.NewBatchDeleteNodeGroupLogic(c.Request.Context(), svcCtx) err := l.BatchDeleteNodeGroup(&req) result.HttpResult(c, nil, err) } diff --git a/internal/handler/admin/server/batchDeleteNodeHandler.go b/internal/handler/admin/server_bak/batchDeleteNodeHandler.go similarity index 78% rename from internal/handler/admin/server/batchDeleteNodeHandler.go rename to internal/handler/admin/server_bak/batchDeleteNodeHandler.go index ab5e3c3..5c01116 100644 --- a/internal/handler/admin/server/batchDeleteNodeHandler.go +++ b/internal/handler/admin/server_bak/batchDeleteNodeHandler.go @@ -1,8 +1,8 @@ -package server +package server_bak import ( "github.com/gin-gonic/gin" - "github.com/perfect-panel/server/internal/logic/admin/server" + "github.com/perfect-panel/server/internal/logic/admin/server_bak" "github.com/perfect-panel/server/internal/svc" "github.com/perfect-panel/server/internal/types" "github.com/perfect-panel/server/pkg/result" @@ -19,7 +19,7 @@ func BatchDeleteNodeHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { return } - l := server.NewBatchDeleteNodeLogic(c.Request.Context(), svcCtx) + l := server_bak.NewBatchDeleteNodeLogic(c.Request.Context(), svcCtx) err := l.BatchDeleteNode(&req) result.HttpResult(c, nil, err) } diff --git a/internal/handler/admin/server/createNodeGroupHandler.go b/internal/handler/admin/server_bak/createNodeGroupHandler.go similarity index 78% rename from internal/handler/admin/server/createNodeGroupHandler.go rename to internal/handler/admin/server_bak/createNodeGroupHandler.go index edbc8f9..acb22a1 100644 --- a/internal/handler/admin/server/createNodeGroupHandler.go +++ b/internal/handler/admin/server_bak/createNodeGroupHandler.go @@ -1,8 +1,8 @@ -package server +package server_bak import ( "github.com/gin-gonic/gin" - "github.com/perfect-panel/server/internal/logic/admin/server" + "github.com/perfect-panel/server/internal/logic/admin/server_bak" "github.com/perfect-panel/server/internal/svc" "github.com/perfect-panel/server/internal/types" "github.com/perfect-panel/server/pkg/result" @@ -19,7 +19,7 @@ func CreateNodeGroupHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { return } - l := server.NewCreateNodeGroupLogic(c.Request.Context(), svcCtx) + l := server_bak.NewCreateNodeGroupLogic(c.Request.Context(), svcCtx) err := l.CreateNodeGroup(&req) result.HttpResult(c, nil, err) } diff --git a/internal/handler/admin/server_bak/createNodeHandler.go b/internal/handler/admin/server_bak/createNodeHandler.go new file mode 100644 index 0000000..8e6d056 --- /dev/null +++ b/internal/handler/admin/server_bak/createNodeHandler.go @@ -0,0 +1,26 @@ +package server_bak + +import ( + "github.com/gin-gonic/gin" + "github.com/perfect-panel/server/internal/logic/admin/server_bak" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/result" +) + +// Create node +func CreateNodeHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { + return func(c *gin.Context) { + var req types.CreateNodeRequest + _ = c.ShouldBind(&req) + validateErr := svcCtx.Validate(&req) + if validateErr != nil { + result.ParamErrorResult(c, validateErr) + return + } + + l := server_bak.NewCreateNodeLogic(c.Request.Context(), svcCtx) + err := l.CreateNode(&req) + result.HttpResult(c, nil, err) + } +} diff --git a/internal/handler/admin/server/createRuleGroupHandler.go b/internal/handler/admin/server_bak/createRuleGroupHandler.go similarity index 79% rename from internal/handler/admin/server/createRuleGroupHandler.go rename to internal/handler/admin/server_bak/createRuleGroupHandler.go index 89a436a..80cde03 100644 --- a/internal/handler/admin/server/createRuleGroupHandler.go +++ b/internal/handler/admin/server_bak/createRuleGroupHandler.go @@ -1,8 +1,8 @@ -package server +package server_bak import ( "github.com/gin-gonic/gin" - "github.com/perfect-panel/server/internal/logic/admin/server" + "github.com/perfect-panel/server/internal/logic/admin/server_bak" "github.com/perfect-panel/server/internal/svc" "github.com/perfect-panel/server/internal/types" "github.com/perfect-panel/server/pkg/result" @@ -19,7 +19,7 @@ func CreateRuleGroupHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { return } - l := server.NewCreateRuleGroupLogic(c.Request.Context(), svcCtx) + l := server_bak.NewCreateRuleGroupLogic(c.Request.Context(), svcCtx) err := l.CreateRuleGroup(&req) result.HttpResult(c, nil, err) } diff --git a/internal/handler/admin/server/deleteNodeGroupHandler.go b/internal/handler/admin/server_bak/deleteNodeGroupHandler.go similarity index 78% rename from internal/handler/admin/server/deleteNodeGroupHandler.go rename to internal/handler/admin/server_bak/deleteNodeGroupHandler.go index d27c6b9..9d0c4ad 100644 --- a/internal/handler/admin/server/deleteNodeGroupHandler.go +++ b/internal/handler/admin/server_bak/deleteNodeGroupHandler.go @@ -1,8 +1,8 @@ -package server +package server_bak import ( "github.com/gin-gonic/gin" - "github.com/perfect-panel/server/internal/logic/admin/server" + "github.com/perfect-panel/server/internal/logic/admin/server_bak" "github.com/perfect-panel/server/internal/svc" "github.com/perfect-panel/server/internal/types" "github.com/perfect-panel/server/pkg/result" @@ -19,7 +19,7 @@ func DeleteNodeGroupHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { return } - l := server.NewDeleteNodeGroupLogic(c.Request.Context(), svcCtx) + l := server_bak.NewDeleteNodeGroupLogic(c.Request.Context(), svcCtx) err := l.DeleteNodeGroup(&req) result.HttpResult(c, nil, err) } diff --git a/internal/handler/admin/server_bak/deleteNodeHandler.go b/internal/handler/admin/server_bak/deleteNodeHandler.go new file mode 100644 index 0000000..f063bfa --- /dev/null +++ b/internal/handler/admin/server_bak/deleteNodeHandler.go @@ -0,0 +1,26 @@ +package server_bak + +import ( + "github.com/gin-gonic/gin" + "github.com/perfect-panel/server/internal/logic/admin/server_bak" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/result" +) + +// Delete node +func DeleteNodeHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { + return func(c *gin.Context) { + var req types.DeleteNodeRequest + _ = c.ShouldBind(&req) + validateErr := svcCtx.Validate(&req) + if validateErr != nil { + result.ParamErrorResult(c, validateErr) + return + } + + l := server_bak.NewDeleteNodeLogic(c.Request.Context(), svcCtx) + err := l.DeleteNode(&req) + result.HttpResult(c, nil, err) + } +} diff --git a/internal/handler/admin/server/deleteRuleGroupHandler.go b/internal/handler/admin/server_bak/deleteRuleGroupHandler.go similarity index 78% rename from internal/handler/admin/server/deleteRuleGroupHandler.go rename to internal/handler/admin/server_bak/deleteRuleGroupHandler.go index 8daddfd..c393369 100644 --- a/internal/handler/admin/server/deleteRuleGroupHandler.go +++ b/internal/handler/admin/server_bak/deleteRuleGroupHandler.go @@ -1,8 +1,8 @@ -package server +package server_bak import ( "github.com/gin-gonic/gin" - "github.com/perfect-panel/server/internal/logic/admin/server" + "github.com/perfect-panel/server/internal/logic/admin/server_bak" "github.com/perfect-panel/server/internal/svc" "github.com/perfect-panel/server/internal/types" "github.com/perfect-panel/server/pkg/result" @@ -19,7 +19,7 @@ func DeleteRuleGroupHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { return } - l := server.NewDeleteRuleGroupLogic(c.Request.Context(), svcCtx) + l := server_bak.NewDeleteRuleGroupLogic(c.Request.Context(), svcCtx) err := l.DeleteRuleGroup(&req) result.HttpResult(c, nil, err) } diff --git a/internal/handler/admin/server/getNodeDetailHandler.go b/internal/handler/admin/server_bak/getNodeDetailHandler.go similarity index 78% rename from internal/handler/admin/server/getNodeDetailHandler.go rename to internal/handler/admin/server_bak/getNodeDetailHandler.go index 06bcaab..a92d346 100644 --- a/internal/handler/admin/server/getNodeDetailHandler.go +++ b/internal/handler/admin/server_bak/getNodeDetailHandler.go @@ -1,8 +1,8 @@ -package server +package server_bak import ( "github.com/gin-gonic/gin" - "github.com/perfect-panel/server/internal/logic/admin/server" + "github.com/perfect-panel/server/internal/logic/admin/server_bak" "github.com/perfect-panel/server/internal/svc" "github.com/perfect-panel/server/internal/types" "github.com/perfect-panel/server/pkg/result" @@ -19,7 +19,7 @@ func GetNodeDetailHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { return } - l := server.NewGetNodeDetailLogic(c.Request.Context(), svcCtx) + l := server_bak.NewGetNodeDetailLogic(c.Request.Context(), svcCtx) resp, err := l.GetNodeDetail(&req) result.HttpResult(c, resp, err) } diff --git a/internal/handler/admin/server/getNodeGroupListHandler.go b/internal/handler/admin/server_bak/getNodeGroupListHandler.go similarity index 68% rename from internal/handler/admin/server/getNodeGroupListHandler.go rename to internal/handler/admin/server_bak/getNodeGroupListHandler.go index 3d741d0..3a8247a 100644 --- a/internal/handler/admin/server/getNodeGroupListHandler.go +++ b/internal/handler/admin/server_bak/getNodeGroupListHandler.go @@ -1,8 +1,8 @@ -package server +package server_bak import ( "github.com/gin-gonic/gin" - "github.com/perfect-panel/server/internal/logic/admin/server" + "github.com/perfect-panel/server/internal/logic/admin/server_bak" "github.com/perfect-panel/server/internal/svc" "github.com/perfect-panel/server/pkg/result" ) @@ -11,7 +11,7 @@ import ( func GetNodeGroupListHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { return func(c *gin.Context) { - l := server.NewGetNodeGroupListLogic(c.Request.Context(), svcCtx) + l := server_bak.NewGetNodeGroupListLogic(c.Request.Context(), svcCtx) resp, err := l.GetNodeGroupList() result.HttpResult(c, resp, err) } diff --git a/internal/handler/admin/server/getNodeListHandler.go b/internal/handler/admin/server_bak/getNodeListHandler.go similarity index 78% rename from internal/handler/admin/server/getNodeListHandler.go rename to internal/handler/admin/server_bak/getNodeListHandler.go index 3bfa07d..0cc5dc6 100644 --- a/internal/handler/admin/server/getNodeListHandler.go +++ b/internal/handler/admin/server_bak/getNodeListHandler.go @@ -1,8 +1,8 @@ -package server +package server_bak import ( "github.com/gin-gonic/gin" - "github.com/perfect-panel/server/internal/logic/admin/server" + "github.com/perfect-panel/server/internal/logic/admin/server_bak" "github.com/perfect-panel/server/internal/svc" "github.com/perfect-panel/server/internal/types" "github.com/perfect-panel/server/pkg/result" @@ -19,7 +19,7 @@ func GetNodeListHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { return } - l := server.NewGetNodeListLogic(c.Request.Context(), svcCtx) + l := server_bak.NewGetNodeListLogic(c.Request.Context(), svcCtx) resp, err := l.GetNodeList(&req) result.HttpResult(c, resp, err) } diff --git a/internal/handler/admin/server/getNodeTagListHandler.go b/internal/handler/admin/server_bak/getNodeTagListHandler.go similarity index 68% rename from internal/handler/admin/server/getNodeTagListHandler.go rename to internal/handler/admin/server_bak/getNodeTagListHandler.go index aa0e1ff..9361536 100644 --- a/internal/handler/admin/server/getNodeTagListHandler.go +++ b/internal/handler/admin/server_bak/getNodeTagListHandler.go @@ -1,8 +1,8 @@ -package server +package server_bak import ( "github.com/gin-gonic/gin" - "github.com/perfect-panel/server/internal/logic/admin/server" + "github.com/perfect-panel/server/internal/logic/admin/server_bak" "github.com/perfect-panel/server/internal/svc" "github.com/perfect-panel/server/pkg/result" ) @@ -11,7 +11,7 @@ import ( func GetNodeTagListHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { return func(c *gin.Context) { - l := server.NewGetNodeTagListLogic(c.Request.Context(), svcCtx) + l := server_bak.NewGetNodeTagListLogic(c.Request.Context(), svcCtx) resp, err := l.GetNodeTagList() result.HttpResult(c, resp, err) } diff --git a/internal/handler/admin/server/getRuleGroupListHandler.go b/internal/handler/admin/server_bak/getRuleGroupListHandler.go similarity index 68% rename from internal/handler/admin/server/getRuleGroupListHandler.go rename to internal/handler/admin/server_bak/getRuleGroupListHandler.go index 8f8fba2..20efc67 100644 --- a/internal/handler/admin/server/getRuleGroupListHandler.go +++ b/internal/handler/admin/server_bak/getRuleGroupListHandler.go @@ -1,8 +1,8 @@ -package server +package server_bak import ( "github.com/gin-gonic/gin" - "github.com/perfect-panel/server/internal/logic/admin/server" + "github.com/perfect-panel/server/internal/logic/admin/server_bak" "github.com/perfect-panel/server/internal/svc" "github.com/perfect-panel/server/pkg/result" ) @@ -11,7 +11,7 @@ import ( func GetRuleGroupListHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { return func(c *gin.Context) { - l := server.NewGetRuleGroupListLogic(c.Request.Context(), svcCtx) + l := server_bak.NewGetRuleGroupListLogic(c.Request.Context(), svcCtx) resp, err := l.GetRuleGroupList() result.HttpResult(c, resp, err) } diff --git a/internal/handler/admin/server/nodeSortHandler.go b/internal/handler/admin/server_bak/nodeSortHandler.go similarity index 78% rename from internal/handler/admin/server/nodeSortHandler.go rename to internal/handler/admin/server_bak/nodeSortHandler.go index 2b4f031..877a2b8 100644 --- a/internal/handler/admin/server/nodeSortHandler.go +++ b/internal/handler/admin/server_bak/nodeSortHandler.go @@ -1,8 +1,8 @@ -package server +package server_bak import ( "github.com/gin-gonic/gin" - "github.com/perfect-panel/server/internal/logic/admin/server" + "github.com/perfect-panel/server/internal/logic/admin/server_bak" "github.com/perfect-panel/server/internal/svc" "github.com/perfect-panel/server/internal/types" "github.com/perfect-panel/server/pkg/result" @@ -19,7 +19,7 @@ func NodeSortHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { return } - l := server.NewNodeSortLogic(c.Request.Context(), svcCtx) + l := server_bak.NewNodeSortLogic(c.Request.Context(), svcCtx) err := l.NodeSort(&req) result.HttpResult(c, nil, err) } diff --git a/internal/handler/admin/server/updateNodeGroupHandler.go b/internal/handler/admin/server_bak/updateNodeGroupHandler.go similarity index 78% rename from internal/handler/admin/server/updateNodeGroupHandler.go rename to internal/handler/admin/server_bak/updateNodeGroupHandler.go index 9994bf7..0c8d3ed 100644 --- a/internal/handler/admin/server/updateNodeGroupHandler.go +++ b/internal/handler/admin/server_bak/updateNodeGroupHandler.go @@ -1,8 +1,8 @@ -package server +package server_bak import ( "github.com/gin-gonic/gin" - "github.com/perfect-panel/server/internal/logic/admin/server" + "github.com/perfect-panel/server/internal/logic/admin/server_bak" "github.com/perfect-panel/server/internal/svc" "github.com/perfect-panel/server/internal/types" "github.com/perfect-panel/server/pkg/result" @@ -19,7 +19,7 @@ func UpdateNodeGroupHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { return } - l := server.NewUpdateNodeGroupLogic(c.Request.Context(), svcCtx) + l := server_bak.NewUpdateNodeGroupLogic(c.Request.Context(), svcCtx) err := l.UpdateNodeGroup(&req) result.HttpResult(c, nil, err) } diff --git a/internal/handler/admin/server_bak/updateNodeHandler.go b/internal/handler/admin/server_bak/updateNodeHandler.go new file mode 100644 index 0000000..729b801 --- /dev/null +++ b/internal/handler/admin/server_bak/updateNodeHandler.go @@ -0,0 +1,26 @@ +package server_bak + +import ( + "github.com/gin-gonic/gin" + "github.com/perfect-panel/server/internal/logic/admin/server_bak" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/result" +) + +// Update node +func UpdateNodeHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { + return func(c *gin.Context) { + var req types.UpdateNodeRequest + _ = c.ShouldBind(&req) + validateErr := svcCtx.Validate(&req) + if validateErr != nil { + result.ParamErrorResult(c, validateErr) + return + } + + l := server_bak.NewUpdateNodeLogic(c.Request.Context(), svcCtx) + err := l.UpdateNode(&req) + result.HttpResult(c, nil, err) + } +} diff --git a/internal/handler/admin/server/updateRuleGroupHandler.go b/internal/handler/admin/server_bak/updateRuleGroupHandler.go similarity index 78% rename from internal/handler/admin/server/updateRuleGroupHandler.go rename to internal/handler/admin/server_bak/updateRuleGroupHandler.go index 224cb35..61c8d46 100644 --- a/internal/handler/admin/server/updateRuleGroupHandler.go +++ b/internal/handler/admin/server_bak/updateRuleGroupHandler.go @@ -1,8 +1,8 @@ -package server +package server_bak import ( "github.com/gin-gonic/gin" - "github.com/perfect-panel/server/internal/logic/admin/server" + "github.com/perfect-panel/server/internal/logic/admin/server_bak" "github.com/perfect-panel/server/internal/svc" "github.com/perfect-panel/server/internal/types" "github.com/perfect-panel/server/pkg/result" @@ -19,7 +19,7 @@ func UpdateRuleGroupHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { return } - l := server.NewUpdateRuleGroupLogic(c.Request.Context(), svcCtx) + l := server_bak.NewUpdateRuleGroupLogic(c.Request.Context(), svcCtx) err := l.UpdateRuleGroup(&req) result.HttpResult(c, nil, err) } diff --git a/internal/handler/routes.go b/internal/handler/routes.go index 8f28dab..cdc821c 100644 --- a/internal/handler/routes.go +++ b/internal/handler/routes.go @@ -189,8 +189,41 @@ func RegisterHandlers(router *gin.Engine, serverCtx *svc.ServiceContext) { adminLogGroupRouter.Use(middleware.AuthMiddleware(serverCtx)) { + // Filter balance log + adminLogGroupRouter.GET("/balance/list", adminLog.FilterBalanceLogHandler(serverCtx)) + + // Filter commission log + adminLogGroupRouter.GET("/commission/list", adminLog.FilterCommissionLogHandler(serverCtx)) + + // Filter email log + adminLogGroupRouter.GET("/email/list", adminLog.FilterEmailLogHandler(serverCtx)) + + // Filter gift log + adminLogGroupRouter.GET("/gift/list", adminLog.FilterGiftLogHandler(serverCtx)) + + // Filter login log + adminLogGroupRouter.GET("/login/list", adminLog.FilterLoginLogHandler(serverCtx)) + // Get message log list adminLogGroupRouter.GET("/message/list", adminLog.GetMessageLogListHandler(serverCtx)) + + // Filter mobile log + adminLogGroupRouter.GET("/mobile/list", adminLog.FilterMobileLogHandler(serverCtx)) + + // Filter register log + adminLogGroupRouter.GET("/register/list", adminLog.FilterRegisterLogHandler(serverCtx)) + + // Filter server traffic log + adminLogGroupRouter.GET("/server/traffic/list", adminLog.FilterServerTrafficLogHandler(serverCtx)) + + // Filter subscribe log + adminLogGroupRouter.GET("/subscribe/list", adminLog.FilterSubscribeLogHandler(serverCtx)) + + // Filter reset subscribe log + adminLogGroupRouter.GET("/subscribe/reset/list", adminLog.FilterResetSubscribeLogHandler(serverCtx)) + + // Filter user subscribe traffic log + adminLogGroupRouter.GET("/subscribe/traffic/list", adminLog.FilterUserSubscribeTrafficLogHandler(serverCtx)) } adminMarketingGroupRouter := router.Group("/v1/admin/marketing") @@ -251,56 +284,35 @@ func RegisterHandlers(router *gin.Engine, serverCtx *svc.ServiceContext) { adminServerGroupRouter.Use(middleware.AuthMiddleware(serverCtx)) { - // Update node - adminServerGroupRouter.PUT("/", adminServer.UpdateNodeHandler(serverCtx)) + // Create Server + adminServerGroupRouter.POST("/create", adminServer.CreateServerHandler(serverCtx)) - // Create node - adminServerGroupRouter.POST("/", adminServer.CreateNodeHandler(serverCtx)) + // Delete Server + adminServerGroupRouter.POST("/delete", adminServer.DeleteServerHandler(serverCtx)) - // Delete node - adminServerGroupRouter.DELETE("/", adminServer.DeleteNodeHandler(serverCtx)) + // Filter Server List + adminServerGroupRouter.GET("/list", adminServer.FilterServerListHandler(serverCtx)) - // Batch delete node - adminServerGroupRouter.DELETE("/batch", adminServer.BatchDeleteNodeHandler(serverCtx)) + // Create Node + adminServerGroupRouter.POST("/node/create", adminServer.CreateNodeHandler(serverCtx)) - // Get node detail - adminServerGroupRouter.GET("/detail", adminServer.GetNodeDetailHandler(serverCtx)) + // Delete Node + adminServerGroupRouter.POST("/node/delete", adminServer.DeleteNodeHandler(serverCtx)) - // Create node group - adminServerGroupRouter.POST("/group", adminServer.CreateNodeGroupHandler(serverCtx)) + // Filter Node List + adminServerGroupRouter.GET("/node/list", adminServer.FilterNodeListHandler(serverCtx)) - // Update node group - adminServerGroupRouter.PUT("/group", adminServer.UpdateNodeGroupHandler(serverCtx)) + // Toggle Node Status + adminServerGroupRouter.POST("/node/status/toggle", adminServer.ToggleNodeStatusHandler(serverCtx)) - // Delete node group - adminServerGroupRouter.DELETE("/group", adminServer.DeleteNodeGroupHandler(serverCtx)) + // Update Node + adminServerGroupRouter.POST("/node/update", adminServer.UpdateNodeHandler(serverCtx)) - // Batch delete node group - adminServerGroupRouter.DELETE("/group/batch", adminServer.BatchDeleteNodeGroupHandler(serverCtx)) + // Get Server Protocols + adminServerGroupRouter.GET("/protocols", adminServer.GetServerProtocolsHandler(serverCtx)) - // Get node group list - adminServerGroupRouter.GET("/group/list", adminServer.GetNodeGroupListHandler(serverCtx)) - - // Get node list - adminServerGroupRouter.GET("/list", adminServer.GetNodeListHandler(serverCtx)) - - // Create rule group - adminServerGroupRouter.POST("/rule_group", adminServer.CreateRuleGroupHandler(serverCtx)) - - // Update rule group - adminServerGroupRouter.PUT("/rule_group", adminServer.UpdateRuleGroupHandler(serverCtx)) - - // Delete rule group - adminServerGroupRouter.DELETE("/rule_group", adminServer.DeleteRuleGroupHandler(serverCtx)) - - // Get rule group list - adminServerGroupRouter.GET("/rule_group_list", adminServer.GetRuleGroupListHandler(serverCtx)) - - // Node sort - adminServerGroupRouter.POST("/sort", adminServer.NodeSortHandler(serverCtx)) - - // Get node tag list - adminServerGroupRouter.GET("/tag/list", adminServer.GetNodeTagListHandler(serverCtx)) + // Update Server + adminServerGroupRouter.POST("/update", adminServer.UpdateServerHandler(serverCtx)) } adminSubscribeGroupRouter := router.Group("/v1/admin/subscribe") diff --git a/internal/logic/admin/console/queryServerTotalDataLogic.go b/internal/logic/admin/console/queryServerTotalDataLogic.go index ac7a5f8..68706f6 100644 --- a/internal/logic/admin/console/queryServerTotalDataLogic.go +++ b/internal/logic/admin/console/queryServerTotalDataLogic.go @@ -6,12 +6,9 @@ import ( "strings" "time" - "github.com/perfect-panel/server/pkg/xerr" - "github.com/perfect-panel/server/internal/svc" "github.com/perfect-panel/server/internal/types" "github.com/perfect-panel/server/pkg/logger" - "github.com/pkg/errors" ) type QueryServerTotalDataLogic struct { @@ -42,121 +39,6 @@ func (l *QueryServerTotalDataLogic) QueryServerTotalData() (resp *types.ServerTo UserTrafficRankingYesterday: make([]types.UserTrafficData, 0), } - // Query node server status - servers, err := l.svcCtx.ServerModel.FindAllServer(l.ctx) - if err != nil { - l.Errorw("[QueryServerTotalDataLogic] FindAllServer error", logger.Field("error", err.Error())) - return nil, errors.Wrapf(err, "FindAllServer error: %v", err) - } - onlineServers, err := l.svcCtx.NodeCache.GetOnlineNodeStatusCount(l.ctx) - if err != nil { - l.Errorw("[QueryServerTotalDataLogic] GetOnlineNodeStatusCount error", logger.Field("error", err.Error())) - return nil, errors.Wrapf(err, "GetOnlineNodeStatusCount error: %v", err) - } - resp.OnlineServers = onlineServers - resp.OfflineServers = int64(len(servers) - int(onlineServers)) - - // 获取所有节点在线用户 - allNodeOnlineUser, err := l.svcCtx.NodeCache.GetAllNodeOnlineUser(l.ctx) - if err != nil { - l.Errorw("[QueryServerTotalDataLogic] Get all node online user failed", logger.Field("error", err.Error())) - } - resp.OnlineUserIPs = int64(len(allNodeOnlineUser)) - - // 获取所有节点今日上传下载流量 - allNodeUploadTraffic, err := l.svcCtx.NodeCache.GetAllNodeUploadTraffic(l.ctx) - if err != nil { - l.Errorw("[QueryServerTotalDataLogic] Get all node upload traffic failed", logger.Field("error", err.Error())) - } - resp.TodayUpload = allNodeUploadTraffic - allNodeDownloadTraffic, err := l.svcCtx.NodeCache.GetAllNodeDownloadTraffic(l.ctx) - if err != nil { - l.Errorw("[QueryServerTotalDataLogic] Get all node download traffic failed", logger.Field("error", err.Error())) - } - resp.TodayDownload = allNodeDownloadTraffic - // 获取节点流量排行榜 前10 - nodeTrafficRankingToday, err := l.svcCtx.NodeCache.GetNodeTodayTotalTrafficRank(l.ctx, 10) - if err != nil { - l.Errorw("[QueryServerTotalDataLogic] Get node today total traffic rank failed", logger.Field("error", err.Error())) - } - if len(nodeTrafficRankingToday) > 0 { - var serverTrafficData []types.ServerTrafficData - for _, rank := range nodeTrafficRankingToday { - serverInfo, err := l.svcCtx.ServerModel.FindOne(l.ctx, rank.ID) - if err != nil { - l.Errorw("[QueryServerTotalDataLogic] FindOne error", logger.Field("error", err)) - continue - } - serverTrafficData = append(serverTrafficData, types.ServerTrafficData{ - ServerId: rank.ID, - Name: serverInfo.Name, - Upload: rank.Upload, - Download: rank.Download, - }) - } - resp.ServerTrafficRankingToday = serverTrafficData - } - // 获取用户流量排行榜 前10 - userTrafficRankingToday, err := l.svcCtx.NodeCache.GetUserTodayTotalTrafficRank(l.ctx, 10) - if err != nil { - l.Errorw("[QueryServerTotalDataLogic] Get user today total traffic rank failed", logger.Field("error", err.Error())) - } - - if len(userTrafficRankingToday) > 0 { - var userTrafficData []types.UserTrafficData - for _, rank := range userTrafficRankingToday { - userTrafficData = append(userTrafficData, types.UserTrafficData{ - SID: rank.SID, - Upload: rank.Upload, - Download: rank.Download, - }) - } - resp.UserTrafficRankingToday = userTrafficData - } - // 获取昨日节点流量排行榜 前10 - nodeTrafficRankingYesterday, err := l.svcCtx.NodeCache.GetYesterdayNodeTotalTrafficRank(l.ctx) - if err != nil { - l.Errorw("[QueryServerTotalDataLogic] Get yesterday node total traffic rank failed", logger.Field("error", err.Error())) - } - if len(nodeTrafficRankingYesterday) > 0 { - var serverTrafficData []types.ServerTrafficData - for _, rank := range nodeTrafficRankingYesterday { - serverTrafficData = append(serverTrafficData, types.ServerTrafficData{ - ServerId: rank.ID, - Name: rank.Name, - Upload: rank.Upload, - Download: rank.Download, - }) - } - resp.ServerTrafficRankingYesterday = serverTrafficData - } - // 获取昨日用户流量排行榜 前10 - userTrafficRankingYesterday, err := l.svcCtx.NodeCache.GetYesterdayUserTotalTrafficRank(l.ctx) - if err != nil { - l.Errorw("[QueryServerTotalDataLogic] Get yesterday user total traffic rank failed", logger.Field("error", err.Error())) - } - if len(userTrafficRankingYesterday) > 0 { - var userTrafficData []types.UserTrafficData - for _, rank := range userTrafficRankingYesterday { - userTrafficData = append(userTrafficData, types.UserTrafficData{ - SID: rank.SID, - Upload: rank.Upload, - Download: rank.Download, - }) - } - resp.UserTrafficRankingYesterday = userTrafficData - } - - // Query node traffic by monthly - nodeTraffic, err := l.svcCtx.TrafficLogModel.QueryTrafficByMonthly(l.ctx, time.Now()) - - if err != nil { - l.Errorw("[QueryServerTotalDataLogic] QueryTrafficByMonthly error", logger.Field("error", err.Error())) - return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "QueryTrafficByMonthly error: %v", err.Error()) - } - resp.MonthlyUpload = nodeTraffic.Upload - resp.MonthlyDownload = nodeTraffic.Download - return resp, nil } diff --git a/internal/logic/admin/log/filterBalanceLogLogic.go b/internal/logic/admin/log/filterBalanceLogLogic.go new file mode 100644 index 0000000..aac09fa --- /dev/null +++ b/internal/logic/admin/log/filterBalanceLogLogic.go @@ -0,0 +1,64 @@ +package log + +import ( + "context" + + "github.com/perfect-panel/server/internal/model/log" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/logger" + "github.com/perfect-panel/server/pkg/xerr" + "github.com/pkg/errors" +) + +type FilterBalanceLogLogic struct { + logger.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +// NewFilterBalanceLogLogic Filter balance log +func NewFilterBalanceLogLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FilterBalanceLogLogic { + return &FilterBalanceLogLogic{ + Logger: logger.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *FilterBalanceLogLogic) FilterBalanceLog(req *types.FilterBalanceLogRequest) (resp *types.FilterBalanceLogResponse, err error) { + data, total, err := l.svcCtx.LogModel.FilterSystemLog(l.ctx, &log.FilterParams{ + Page: req.Page, + Size: req.Size, + Type: log.TypeBalance.Uint8(), + Data: req.Date, + ObjectID: req.UserId, + }) + + if err != nil { + l.Errorw("[FilterBalanceLog] Query User Balance Log Error:", logger.Field("error", err.Error())) + return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "Query User Balance Log Error") + } + + list := make([]types.BalanceLog, 0) + for _, datum := range data { + var content log.Balance + if err = content.Unmarshal([]byte(datum.Content)); err != nil { + l.Errorf("[QueryUserBalanceLog] unmarshal balance log content failed: %v", err.Error()) + continue + } + list = append(list, types.BalanceLog{ + UserId: datum.ObjectID, + Amount: content.Amount, + Type: content.Type, + OrderId: content.OrderId, + Balance: content.Balance, + Timestamp: content.Timestamp, + }) + } + + return &types.FilterBalanceLogResponse{ + Total: total, + List: list, + }, nil +} diff --git a/internal/logic/admin/log/filterCommissionLogLogic.go b/internal/logic/admin/log/filterCommissionLogLogic.go new file mode 100644 index 0000000..ff31546 --- /dev/null +++ b/internal/logic/admin/log/filterCommissionLogLogic.go @@ -0,0 +1,61 @@ +package log + +import ( + "context" + + "github.com/perfect-panel/server/internal/model/log" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/logger" + "github.com/perfect-panel/server/pkg/xerr" + "github.com/pkg/errors" +) + +type FilterCommissionLogLogic struct { + logger.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +// NewFilterCommissionLogLogic Filter commission log +func NewFilterCommissionLogLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FilterCommissionLogLogic { + return &FilterCommissionLogLogic{ + Logger: logger.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *FilterCommissionLogLogic) FilterCommissionLog(req *types.FilterCommissionLogRequest) (resp *types.FilterCommissionLogResponse, err error) { + data, total, err := l.svcCtx.LogModel.FilterSystemLog(l.ctx, &log.FilterParams{ + Page: req.Page, + Size: req.Size, + Data: req.Date, + Type: log.TypeCommission.Uint8(), + ObjectID: req.UserId, + }) + if err != nil { + l.Errorw("Query User Commission Log failed", logger.Field("error", err.Error())) + return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "Query User Commission Log failed") + } + var list []types.CommissionLog + + for _, datum := range data { + var content log.Commission + if err = content.Unmarshal([]byte(datum.Content)); err != nil { + l.Errorf("unmarshal commission log content failed: %v", err.Error()) + continue + } + list = append(list, types.CommissionLog{ + UserId: datum.ObjectID, + Type: content.Type, + Amount: content.Amount, + OrderNo: content.OrderNo, + CreatedAt: content.CreatedAt, + }) + } + return &types.FilterCommissionLogResponse{ + Total: total, + List: list, + }, nil +} diff --git a/internal/logic/admin/log/filterEmailLogLogic.go b/internal/logic/admin/log/filterEmailLogLogic.go new file mode 100644 index 0000000..21ce204 --- /dev/null +++ b/internal/logic/admin/log/filterEmailLogLogic.go @@ -0,0 +1,68 @@ +package log + +import ( + "context" + + "github.com/perfect-panel/server/internal/model/log" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/logger" + "github.com/perfect-panel/server/pkg/xerr" + "github.com/pkg/errors" +) + +type FilterEmailLogLogic struct { + logger.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +// NewFilterEmailLogLogic Filter email log +func NewFilterEmailLogLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FilterEmailLogLogic { + return &FilterEmailLogLogic{ + Logger: logger.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *FilterEmailLogLogic) FilterEmailLog(req *types.FilterLogParams) (resp *types.FilterEmailLogResponse, err error) { + data, total, err := l.svcCtx.LogModel.FilterSystemLog(l.ctx, &log.FilterParams{ + Page: req.Page, + Size: req.Size, + Type: log.TypeEmailMessage.Uint8(), + Data: req.Date, + Search: req.Search, + }) + + if err != nil { + l.Errorf("[FilterEmailLog] failed to filter system log: %v", err.Error()) + return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "failed to filter system log: %v", err.Error()) + } + + var list []types.MessageLog + + for _, datum := range data { + var content log.Message + err = content.Unmarshal([]byte(datum.Content)) + if err != nil { + l.Errorf("[FilterEmailLog] failed to unmarshal content: %v", err.Error()) + continue + } + list = append(list, types.MessageLog{ + Id: datum.Id, + Type: datum.Type, + Platform: content.Platform, + To: content.To, + Subject: content.Subject, + Content: content.Content, + Status: content.Status, + CreatedAt: datum.CreatedAt.UnixMilli(), + }) + } + + return &types.FilterEmailLogResponse{ + Total: total, + List: list, + }, nil +} diff --git a/internal/logic/admin/log/filterGiftLogLogic.go b/internal/logic/admin/log/filterGiftLogLogic.go new file mode 100644 index 0000000..bab93e4 --- /dev/null +++ b/internal/logic/admin/log/filterGiftLogLogic.go @@ -0,0 +1,68 @@ +package log + +import ( + "context" + + "github.com/perfect-panel/server/internal/model/log" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/logger" + "github.com/perfect-panel/server/pkg/xerr" + "github.com/pkg/errors" +) + +type FilterGiftLogLogic struct { + logger.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +// Filter gift log +func NewFilterGiftLogLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FilterGiftLogLogic { + return &FilterGiftLogLogic{ + Logger: logger.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *FilterGiftLogLogic) FilterGiftLog(req *types.FilterGiftLogRequest) (resp *types.FilterGiftLogResponse, err error) { + data, total, err := l.svcCtx.LogModel.FilterSystemLog(l.ctx, &log.FilterParams{ + Page: req.Page, + Size: req.Size, + Type: log.TypeGift.Uint8(), + ObjectID: req.UserId, + Data: req.Date, + Search: req.Search, + }) + + if err != nil { + l.Errorf("[FilterGiftLog] failed to filter system log: %v", err.Error()) + return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "failed to filter system log: %v", err.Error()) + } + + var list []types.GiftLog + for _, datum := range data { + var content log.Gift + err = content.Unmarshal([]byte(datum.Content)) + if err != nil { + l.Errorf("[FilterGiftLog] failed to unmarshal content: %v", err.Error()) + continue + } + list = append(list, types.GiftLog{ + Type: content.Type, + UserId: datum.ObjectID, + OrderNo: content.OrderNo, + SubscribeId: content.SubscribeId, + Amount: content.Amount, + Balance: content.Balance, + Remark: content.Remark, + CreatedAt: content.CreatedAt, + }) + } + + return &types.FilterGiftLogResponse{ + Total: total, + List: list, + }, nil +} diff --git a/internal/logic/admin/log/filterLoginLogLogic.go b/internal/logic/admin/log/filterLoginLogLogic.go new file mode 100644 index 0000000..f8d4096 --- /dev/null +++ b/internal/logic/admin/log/filterLoginLogLogic.go @@ -0,0 +1,65 @@ +package log + +import ( + "context" + + "github.com/perfect-panel/server/internal/model/log" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/logger" + "github.com/perfect-panel/server/pkg/xerr" + "github.com/pkg/errors" +) + +type FilterLoginLogLogic struct { + logger.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +// NewFilterLoginLogLogic Filter login log +func NewFilterLoginLogLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FilterLoginLogLogic { + return &FilterLoginLogLogic{ + Logger: logger.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *FilterLoginLogLogic) FilterLoginLog(req *types.FilterLoginLogRequest) (resp *types.FilterLoginLogResponse, err error) { + data, total, err := l.svcCtx.LogModel.FilterSystemLog(l.ctx, &log.FilterParams{ + Page: req.Page, + Size: req.Size, + Type: log.TypeLogin.Uint8(), + ObjectID: req.UserId, + Data: req.Date, + Search: req.Search, + }) + + if err != nil { + l.Errorf("[FilterLoginLog] failed to filter system log: %v", err.Error()) + return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "failed to filter system log: %v", err.Error()) + } + var list []types.LoginLog + for _, datum := range data { + var item log.Login + err = item.Unmarshal([]byte(datum.Content)) + if err != nil { + l.Errorf("[FilterLoginLog] failed to unmarshal content: %v", err.Error()) + continue + } + list = append(list, types.LoginLog{ + UserId: datum.ObjectID, + Method: item.Method, + LoginIP: item.LoginIP, + UserAgent: item.UserAgent, + Success: item.Success, + LoginTime: item.LoginTime, + }) + } + + return &types.FilterLoginLogResponse{ + Total: total, + List: list, + }, nil +} diff --git a/internal/logic/admin/log/filterMobileLogLogic.go b/internal/logic/admin/log/filterMobileLogLogic.go new file mode 100644 index 0000000..f5f0f4c --- /dev/null +++ b/internal/logic/admin/log/filterMobileLogLogic.go @@ -0,0 +1,68 @@ +package log + +import ( + "context" + + "github.com/perfect-panel/server/internal/model/log" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/logger" + "github.com/perfect-panel/server/pkg/xerr" + "github.com/pkg/errors" +) + +type FilterMobileLogLogic struct { + logger.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +// Filter mobile log +func NewFilterMobileLogLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FilterMobileLogLogic { + return &FilterMobileLogLogic{ + Logger: logger.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *FilterMobileLogLogic) FilterMobileLog(req *types.FilterLogParams) (resp *types.FilterMobileLogResponse, err error) { + data, total, err := l.svcCtx.LogModel.FilterSystemLog(l.ctx, &log.FilterParams{ + Page: req.Page, + Size: req.Size, + Type: log.TypeMobileMessage.Uint8(), + Data: req.Date, + Search: req.Search, + }) + + if err != nil { + l.Errorf("[FilterMobileLog] failed to filter system log: %v", err.Error()) + return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "failed to filter system log: %v", err.Error()) + } + + var list []types.MessageLog + + for _, datum := range data { + var content log.Message + err = content.Unmarshal([]byte(datum.Content)) + if err != nil { + l.Errorf("[FilterMobileLog] failed to unmarshal content: %v", err.Error()) + continue + } + list = append(list, types.MessageLog{ + Id: datum.Id, + Type: datum.Type, + Platform: content.Platform, + To: content.To, + Subject: content.Subject, + Content: content.Content, + Status: content.Status, + CreatedAt: datum.CreatedAt.UnixMilli(), + }) + } + + return &types.FilterMobileLogResponse{ + Total: total, + List: list, + }, nil +} diff --git a/internal/logic/admin/log/filterRegisterLogLogic.go b/internal/logic/admin/log/filterRegisterLogLogic.go new file mode 100644 index 0000000..7913e8c --- /dev/null +++ b/internal/logic/admin/log/filterRegisterLogLogic.go @@ -0,0 +1,66 @@ +package log + +import ( + "context" + + "github.com/perfect-panel/server/internal/model/log" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/logger" + "github.com/perfect-panel/server/pkg/xerr" + "github.com/pkg/errors" +) + +type FilterRegisterLogLogic struct { + logger.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +// Filter register log +func NewFilterRegisterLogLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FilterRegisterLogLogic { + return &FilterRegisterLogLogic{ + Logger: logger.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *FilterRegisterLogLogic) FilterRegisterLog(req *types.FilterRegisterLogRequest) (resp *types.FilterRegisterLogResponse, err error) { + data, total, err := l.svcCtx.LogModel.FilterSystemLog(l.ctx, &log.FilterParams{ + Page: req.Page, + Size: req.Size, + Type: log.TypeRegister.Uint8(), + ObjectID: req.UserId, + Data: req.Date, + Search: req.Search, + }) + + if err != nil { + l.Errorf("[FilterRegisterLog] failed to filter system log: %v", err.Error()) + return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "failed to filter system log: %v", err.Error()) + } + + var list []types.RegisterLog + for _, datum := range data { + var item log.Register + err = item.Unmarshal([]byte(datum.Content)) + if err != nil { + l.Errorf("[FilterLoginLog] failed to unmarshal content: %v", err.Error()) + continue + } + list = append(list, types.RegisterLog{ + UserId: datum.ObjectID, + AuthMethod: item.AuthMethod, + Identifier: item.Identifier, + RegisterIP: item.RegisterIP, + UserAgent: item.UserAgent, + RegisterTime: item.RegisterTime, + }) + } + + return &types.FilterRegisterLogResponse{ + List: list, + Total: total, + }, nil +} diff --git a/internal/logic/admin/log/filterResetSubscribeLogLogic.go b/internal/logic/admin/log/filterResetSubscribeLogLogic.go new file mode 100644 index 0000000..2a5c851 --- /dev/null +++ b/internal/logic/admin/log/filterResetSubscribeLogLogic.go @@ -0,0 +1,66 @@ +package log + +import ( + "context" + + "github.com/perfect-panel/server/internal/model/log" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/logger" + "github.com/perfect-panel/server/pkg/xerr" + "github.com/pkg/errors" +) + +type FilterResetSubscribeLogLogic struct { + logger.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +// NewFilterResetSubscribeLogLogic Filter reset subscribe log +func NewFilterResetSubscribeLogLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FilterResetSubscribeLogLogic { + return &FilterResetSubscribeLogLogic{ + Logger: logger.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *FilterResetSubscribeLogLogic) FilterResetSubscribeLog(req *types.FilterResetSubscribeLogRequest) (resp *types.FilterResetSubscribeLogResponse, err error) { + data, total, err := l.svcCtx.LogModel.FilterSystemLog(l.ctx, &log.FilterParams{ + Page: req.Page, + Size: req.Size, + Type: log.TypeRegister.Uint8(), + ObjectID: req.UserSubscribeId, + Data: req.Date, + Search: req.Search, + }) + + if err != nil { + l.Errorf("[FilterResetSubscribeLog] failed to filter system log: %v", err.Error()) + return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "failed to filter system log: %v", err.Error()) + } + + var list []types.ResetSubscribeLog + + for _, item := range data { + var content log.ResetSubscribe + err = content.Unmarshal([]byte(item.Content)) + if err != nil { + l.Errorf("[FilterResetSubscribeLog] failed to unmarshal content: %v", err.Error()) + continue + } + list = append(list, types.ResetSubscribeLog{ + Type: content.Type, + UserId: content.UserId, + UserSubscribeId: item.ObjectID, + OrderNo: content.OrderNo, + ResetAt: content.ResetAt, + }) + } + + return &types.FilterResetSubscribeLogResponse{ + List: list, + Total: total, + }, nil +} diff --git a/internal/logic/admin/log/filterServerTrafficLogLogic.go b/internal/logic/admin/log/filterServerTrafficLogLogic.go new file mode 100644 index 0000000..5c05976 --- /dev/null +++ b/internal/logic/admin/log/filterServerTrafficLogLogic.go @@ -0,0 +1,43 @@ +package log + +import ( + "context" + "time" + + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/logger" +) + +type FilterServerTrafficLogLogic struct { + logger.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +// NewFilterServerTrafficLogLogic Filter server traffic log +func NewFilterServerTrafficLogLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FilterServerTrafficLogLogic { + return &FilterServerTrafficLogLogic{ + Logger: logger.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *FilterServerTrafficLogLogic) FilterServerTrafficLog(req *types.FilterServerTrafficLogRequest) (resp *types.FilterServerTrafficLogResponse, err error) { + today := time.Now().Format("2006-01-02") + if req.Date == "" || req.Date == today { + return l.handlerToday(req) + } else { + return l.handlerSpecify(req) + } +} + +func (l *FilterServerTrafficLogLogic) handlerToday(req *types.FilterServerTrafficLogRequest) (resp *types.FilterServerTrafficLogResponse, err error) { + + return +} + +func (l *FilterServerTrafficLogLogic) handlerSpecify(req *types.FilterServerTrafficLogRequest) (resp *types.FilterServerTrafficLogResponse, err error) { + return +} diff --git a/internal/logic/admin/log/filterSubscribeLogLogic.go b/internal/logic/admin/log/filterSubscribeLogLogic.go new file mode 100644 index 0000000..5da8221 --- /dev/null +++ b/internal/logic/admin/log/filterSubscribeLogLogic.go @@ -0,0 +1,64 @@ +package log + +import ( + "context" + + "github.com/perfect-panel/server/internal/model/log" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/logger" + "github.com/perfect-panel/server/pkg/xerr" + "github.com/pkg/errors" +) + +type FilterSubscribeLogLogic struct { + logger.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +// Filter subscribe log +func NewFilterSubscribeLogLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FilterSubscribeLogLogic { + return &FilterSubscribeLogLogic{ + Logger: logger.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *FilterSubscribeLogLogic) FilterSubscribeLog(req *types.FilterSubscribeLogRequest) (resp *types.FilterSubscribeLogResponse, err error) { + data, total, err := l.svcCtx.LogModel.FilterSystemLog(l.ctx, &log.FilterParams{ + Page: req.Page, + Size: req.Size, + Type: log.TypeSubscribe.Uint8(), + Data: req.Date, + ObjectID: req.UserId, + }) + if err != nil { + l.Errorf("[FilterSubscribeLog] failed to filter system log: %v", err.Error()) + return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "failed to filter system log") + } + + var list []types.SubscribeLog + for _, datum := range data { + var content log.Subscribe + err = content.Unmarshal([]byte(datum.Content)) + if err != nil { + l.Errorf("[FilterSubscribeLog] failed to unmarshal content: %v", err.Error()) + continue + } + list = append(list, types.SubscribeLog{ + UserId: datum.ObjectID, + Token: content.Token, + UserAgent: content.UserAgent, + ClientIP: content.ClientIP, + UserSubscribeId: content.UserSubscribeId, + SubscribedAt: datum.CreatedAt.UnixMilli(), + }) + } + + return &types.FilterSubscribeLogResponse{ + Total: total, + List: list, + }, nil +} diff --git a/internal/logic/admin/log/filterUserSubscribeTrafficLogLogic.go b/internal/logic/admin/log/filterUserSubscribeTrafficLogLogic.go new file mode 100644 index 0000000..a325175 --- /dev/null +++ b/internal/logic/admin/log/filterUserSubscribeTrafficLogLogic.go @@ -0,0 +1,30 @@ +package log + +import ( + "context" + + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/logger" +) + +type FilterUserSubscribeTrafficLogLogic struct { + logger.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +// NewFilterUserSubscribeTrafficLogLogic Filter user subscribe traffic log +func NewFilterUserSubscribeTrafficLogLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FilterUserSubscribeTrafficLogLogic { + return &FilterUserSubscribeTrafficLogLogic{ + Logger: logger.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *FilterUserSubscribeTrafficLogLogic) FilterUserSubscribeTrafficLog(req *types.FilterSubscribeTrafficRequest) (resp *types.FilterSubscribeTrafficResponse, err error) { + // todo: add your logic here and delete this line + + return +} diff --git a/internal/logic/admin/server/createNodeLogic.go b/internal/logic/admin/server/createNodeLogic.go index c25f9e1..f635f85 100644 --- a/internal/logic/admin/server/createNodeLogic.go +++ b/internal/logic/admin/server/createNodeLogic.go @@ -2,18 +2,13 @@ package server import ( "context" - "encoding/json" - "strings" - "time" - "github.com/hibiken/asynq" - "github.com/perfect-panel/server/internal/model/server" + "github.com/perfect-panel/server/internal/model/node" "github.com/perfect-panel/server/internal/svc" "github.com/perfect-panel/server/internal/types" "github.com/perfect-panel/server/pkg/logger" "github.com/perfect-panel/server/pkg/tool" "github.com/perfect-panel/server/pkg/xerr" - queue "github.com/perfect-panel/server/queue/types" "github.com/pkg/errors" ) @@ -23,6 +18,7 @@ type CreateNodeLogic struct { svcCtx *svc.ServiceContext } +// NewCreateNodeLogic Create Node func NewCreateNodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateNodeLogic { return &CreateNodeLogic{ Logger: logger.WithContext(ctx), @@ -32,97 +28,18 @@ func NewCreateNodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Create } func (l *CreateNodeLogic) CreateNode(req *types.CreateNodeRequest) error { - config, err := json.Marshal(req.Config) - if err != nil { - return err + data := node.Node{ + Name: req.Name, + Tags: tool.StringSliceToString(req.Tags), + Port: req.Port, + Address: req.Address, + ServerId: req.ServerId, + Protocol: req.Protocol, } - var serverInfo server.Server - tool.DeepCopy(&serverInfo, req) - serverInfo.Config = string(config) - nodeRelay, err := json.Marshal(req.RelayNode) - if err != nil { - l.Errorw("[UpdateNode] Marshal RelayNode Error: ", logger.Field("error", err.Error())) - return err - } - if len(req.Tags) > 0 { - serverInfo.Tags = strings.Join(req.Tags, ",") - } - - serverInfo.LastReportedAt = time.UnixMicro(1218124800) - - serverInfo.City = req.City - serverInfo.Country = req.Country - - serverInfo.RelayNode = string(nodeRelay) - if req.Protocol == "vless" { - var cfg types.Vless - if err = json.Unmarshal(config, &cfg); err != nil { - return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "json.Unmarshal error: %v", err.Error()) - } - if cfg.Security == "reality" && cfg.SecurityConfig.RealityPublicKey == "" { - public, private, err := tool.Curve25519Genkey(false, "") - if err != nil { - return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "generate curve25519 key error") - } - cfg.SecurityConfig.RealityPublicKey = public - cfg.SecurityConfig.RealityPrivateKey = private - cfg.SecurityConfig.RealityShortId = tool.GenerateShortID(private) - } - if cfg.SecurityConfig.RealityServerAddr == "" { - cfg.SecurityConfig.RealityServerAddr = cfg.SecurityConfig.SNI - } - if cfg.SecurityConfig.RealityServerPort == 0 { - cfg.SecurityConfig.RealityServerPort = 443 - } - config, _ = json.Marshal(cfg) - serverInfo.Config = string(config) - } else if req.Protocol == "shadowsocks" { - var cfg types.Shadowsocks - if err = json.Unmarshal(config, &cfg); err != nil { - l.Errorf("[CreateNode] Unmarshal Shadowsocks Config Error: %v", err.Error()) - return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "json.Unmarshal error: %v", err.Error()) - } - if strings.Contains(cfg.Method, "2022") { - var length int - switch cfg.Method { - case "2022-blake3-aes-128-gcm": - length = 16 - default: - length = 32 - } - if len(cfg.ServerKey) != length { - cfg.ServerKey = tool.GenerateCipher(cfg.ServerKey, length) - } - } - config, _ = json.Marshal(cfg) - serverInfo.Config = string(config) - } - - err = l.svcCtx.ServerModel.Insert(l.ctx, &serverInfo) + err := l.svcCtx.NodeModel.InsertNode(l.ctx, &data) if err != nil { l.Errorw("[CreateNode] Insert Database Error: ", logger.Field("error", err.Error())) - return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseInsertError), "create server error: %v", err) - } - - if req.City == "" || req.Country == "" { - // Marshal the task payload - payload, err := json.Marshal(queue.GetNodeCountry{ - Protocol: serverInfo.Protocol, - ServerAddr: serverInfo.ServerAddr, - }) - if err != nil { - l.Errorw("[GetNodeCountry]: Marshal Error", logger.Field("error", err.Error())) - return errors.Wrap(xerr.NewErrCode(xerr.ERROR), "Failed to marshal task payload") - } - // Create a queue task - task := asynq.NewTask(queue.ForthwithGetCountry, payload) - // Enqueue the task - taskInfo, err := l.svcCtx.Queue.Enqueue(task) - if err != nil { - l.Errorw("[GetNodeCountry]: Enqueue Error", logger.Field("error", err.Error()), logger.Field("payload", string(payload))) - return errors.Wrap(xerr.NewErrCode(xerr.ERROR), "Failed to enqueue task") - } - l.Infow("[GetNodeCountry]: Enqueue Success", logger.Field("taskID", taskInfo.ID), logger.Field("payload", string(payload))) + return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseInsertError), "[CreateNode] Insert Database Error") } return nil diff --git a/internal/logic/admin/server/createServerLogic.go b/internal/logic/admin/server/createServerLogic.go new file mode 100644 index 0000000..cc2da85 --- /dev/null +++ b/internal/logic/admin/server/createServerLogic.go @@ -0,0 +1,111 @@ +package server + +import ( + "context" + "strings" + + "github.com/perfect-panel/server/internal/model/node" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/ip" + "github.com/perfect-panel/server/pkg/logger" + "github.com/perfect-panel/server/pkg/tool" + "github.com/perfect-panel/server/pkg/xerr" + "github.com/pkg/errors" +) + +type CreateServerLogic struct { + logger.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +// NewCreateServerLogic Create Server +func NewCreateServerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateServerLogic { + return &CreateServerLogic{ + Logger: logger.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *CreateServerLogic) CreateServer(req *types.CreateServerRequest) error { + data := node.Server{ + Name: req.Name, + Country: req.Country, + City: req.City, + Ratio: req.Ratio, + Address: req.Address, + Sort: req.Sort, + Protocols: "", + } + protocols := make([]node.Protocol, 0) + for _, item := range req.Protocols { + if item.Type == "" { + return errors.Wrapf(xerr.NewErrCodeMsg(xerr.InvalidParams, "protocols type is empty"), "protocols type is empty") + } + var protocol node.Protocol + tool.DeepCopy(&protocol, item) + + // VLESS Reality Key Generation + if protocol.Type == "vless" { + if protocol.Security == "reality" { + if protocol.RealityPublicKey == "" { + public, private, err := tool.Curve25519Genkey(false, "") + if err != nil { + l.Errorf("[CreateServer] Generate Reality Key Error: %v", err.Error()) + return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "generate reality key error: %v", err) + } + protocol.RealityPublicKey = public + protocol.RealityPrivateKey = private + protocol.RealityShortId = tool.GenerateShortID(private) + } + if protocol.RealityServerAddr == "" { + protocol.RealityServerAddr = protocol.SNI + } + if protocol.RealityServerPort == 0 { + protocol.RealityServerPort = 443 + } + } + + } + // ShadowSocks 2022 Key Generation + if protocol.Type == "shadowsocks" { + if strings.Contains(protocol.Cipher, "2022") { + var length int + switch protocol.Cipher { + case "2022-blake3-aes-128-gcm": + length = 16 + default: + length = 32 + } + if len(protocol.ServerKey) != length { + protocol.ServerKey = tool.GenerateCipher(protocol.ServerKey, length) + } + } + } + protocols = append(protocols, protocol) + } + + err := data.MarshalProtocols(protocols) + if err != nil { + l.Errorf("[CreateServer] Marshal Protocols Error: %v", err.Error()) + return errors.Wrapf(xerr.NewErrCodeMsg(xerr.InvalidParams, "protocols marshal error"), "protocols marshal error: %v", err) + } + if data.City == "" && data.Country == "" { + // query server ip location + result, err := ip.GetRegionByIp(req.Address) + if err != nil { + l.Errorf("[CreateServer] GetRegionByIp Error: %v", err.Error()) + } else { + data.City = result.City + data.Country = result.Country + } + } + err = l.svcCtx.NodeModel.InsertServer(l.ctx, &data) + if err != nil { + l.Errorf("[CreateServer] Insert Server error: %v", err.Error()) + return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseInsertError), "insert server error: %v", err) + } + return nil +} diff --git a/internal/logic/admin/server/deleteNodeLogic.go b/internal/logic/admin/server/deleteNodeLogic.go index cd4e4c6..a8839be 100644 --- a/internal/logic/admin/server/deleteNodeLogic.go +++ b/internal/logic/admin/server/deleteNodeLogic.go @@ -2,14 +2,12 @@ package server import ( "context" - "github.com/perfect-panel/server/pkg/tool" "github.com/perfect-panel/server/internal/svc" "github.com/perfect-panel/server/internal/types" "github.com/perfect-panel/server/pkg/logger" "github.com/perfect-panel/server/pkg/xerr" "github.com/pkg/errors" - "gorm.io/gorm" ) type DeleteNodeLogic struct { @@ -18,6 +16,7 @@ type DeleteNodeLogic struct { svcCtx *svc.ServiceContext } +// NewDeleteNodeLogic Delete Node func NewDeleteNodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteNodeLogic { return &DeleteNodeLogic{ Logger: logger.WithContext(ctx), @@ -27,33 +26,10 @@ func NewDeleteNodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Delete } func (l *DeleteNodeLogic) DeleteNode(req *types.DeleteNodeRequest) error { - err := l.svcCtx.DB.Transaction(func(tx *gorm.DB) error { - // Delete server - err := l.svcCtx.ServerModel.Delete(l.ctx, req.Id, tx) - if err != nil { - return err - } - // Delete server to subscribe - subs, err := l.svcCtx.SubscribeModel.QuerySubscribeIdsByServerIdAndServerGroupId(l.ctx, req.Id, 0) - if err != nil { - l.Logger.Errorf("[DeleteNode] QuerySubscribeIdsByServerIdAndServerGroupId error: %v", err.Error()) - return err - } - - for _, sub := range subs { - servers := tool.StringToInt64Slice(sub.Server) - newServers := tool.RemoveElementBySlice(servers, req.Id) - sub.Server = tool.Int64SliceToString(newServers) - if err = l.svcCtx.SubscribeModel.Update(l.ctx, sub, tx); err != nil { - l.Logger.Errorf("[DeleteNode] UpdateSubscribe error: %v", err.Error()) - return err - } - } - return nil - }) + err := l.svcCtx.NodeModel.DeleteNode(l.ctx, req.Id) if err != nil { l.Errorw("[DeleteNode] Delete Database Error: ", logger.Field("error", err.Error())) - return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseDeletedError), "delete server error: %v", err) + return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseDeletedError), "[DeleteNode] Delete Database Error") } return nil } diff --git a/internal/logic/admin/server/deleteServerLogic.go b/internal/logic/admin/server/deleteServerLogic.go new file mode 100644 index 0000000..f801429 --- /dev/null +++ b/internal/logic/admin/server/deleteServerLogic.go @@ -0,0 +1,30 @@ +package server + +import ( + "context" + + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/logger" +) + +type DeleteServerLogic struct { + logger.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +// NewDeleteServerLogic Delete Server +func NewDeleteServerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteServerLogic { + return &DeleteServerLogic{ + Logger: logger.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *DeleteServerLogic) DeleteServer(req *types.DeleteServerRequest) error { + // todo: add your logic here and delete this line + + return nil +} diff --git a/internal/logic/admin/server/filterNodeListLogic.go b/internal/logic/admin/server/filterNodeListLogic.go new file mode 100644 index 0000000..308e9d6 --- /dev/null +++ b/internal/logic/admin/server/filterNodeListLogic.go @@ -0,0 +1,49 @@ +package server + +import ( + "context" + + "github.com/perfect-panel/server/internal/model/node" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/logger" + "github.com/perfect-panel/server/pkg/tool" + "github.com/perfect-panel/server/pkg/xerr" + "github.com/pkg/errors" +) + +type FilterNodeListLogic struct { + logger.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +// NewFilterNodeListLogic Filter Node List +func NewFilterNodeListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FilterNodeListLogic { + return &FilterNodeListLogic{ + Logger: logger.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *FilterNodeListLogic) FilterNodeList(req *types.FilterNodeListRequest) (resp *types.FilterNodeListResponse, err error) { + total, data, err := l.svcCtx.NodeModel.FilterNodeList(l.ctx, &node.FilterParams{ + Page: req.Page, + Size: req.Size, + Search: req.Search, + }) + + if err != nil { + l.Errorw("[FilterNodeList] Query Database Error: ", logger.Field("error", err.Error())) + return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "[FilterNodeList] Query Database Error") + } + + list := make([]types.Node, 0) + tool.DeepCopy(&list, data) + + return &types.FilterNodeListResponse{ + List: list, + Total: total, + }, nil +} diff --git a/internal/logic/admin/server/filterServerListLogic.go b/internal/logic/admin/server/filterServerListLogic.go new file mode 100644 index 0000000..c6b11e4 --- /dev/null +++ b/internal/logic/admin/server/filterServerListLogic.go @@ -0,0 +1,119 @@ +package server + +import ( + "context" + + "github.com/perfect-panel/server/internal/model/node" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/logger" + "github.com/perfect-panel/server/pkg/tool" + "github.com/perfect-panel/server/pkg/xerr" + "github.com/pkg/errors" +) + +type FilterServerListLogic struct { + logger.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +// NewFilterServerListLogic Filter Server List +func NewFilterServerListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FilterServerListLogic { + return &FilterServerListLogic{ + Logger: logger.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *FilterServerListLogic) FilterServerList(req *types.FilterServerListRequest) (resp *types.FilterServerListResponse, err error) { + total, data, err := l.svcCtx.NodeModel.FilterServerList(l.ctx, &node.FilterParams{ + Page: req.Page, + Size: req.Size, + Search: req.Search, + }) + if err != nil { + l.Errorw("[FilterServerList] Query Database Error: ", logger.Field("error", err.Error())) + return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "[FilterServerList] Query Database Error") + } + + list := make([]types.Server, 0) + + for _, datum := range data { + var server types.Server + tool.DeepCopy(&server, datum) + + // handler protocols + var protocols []types.Protocol + dst, err := datum.UnmarshalProtocols() + if err != nil { + l.Errorf("[FilterServerList] UnmarshalProtocols Error: %s", err.Error()) + continue + } + tool.DeepCopy(&protocols, dst) + server.Protocols = protocols + // handler status + server.Status = l.handlerServerStatus(datum.Id) + list = append(list, server) + } + + return &types.FilterServerListResponse{ + List: list, + Total: total, + }, nil +} + +func (l *FilterServerListLogic) handlerServerStatus(id int64) types.ServerStatus { + var result types.ServerStatus + nodeStatus, err := l.svcCtx.NodeCache.GetNodeStatus(l.ctx, id) + if err != nil { + l.Errorw("[handlerServerStatus] GetNodeStatus Error: ", logger.Field("error", err.Error()), logger.Field("node_id", id)) + return result + } + result = types.ServerStatus{ + Mem: nodeStatus.Mem, + Cpu: nodeStatus.Cpu, + Disk: nodeStatus.Disk, + Online: make([]types.ServerOnlineUser, 0), + } + + // parse online users + onlineUser, err := l.svcCtx.NodeCache.GetNodeOnlineUser(l.ctx, id) + if err != nil { + l.Errorw("[handlerServerStatus] GetNodeOnlineUser Error: ", logger.Field("error", err.Error()), logger.Field("node_id", id)) + return result + } + + var onlineList []types.ServerOnlineUser + var onlineMap = make(map[int64]types.ServerOnlineUser) + // group by user_id + for subId, info := range onlineUser { + data, err := l.svcCtx.UserModel.FindOneUserSubscribe(l.ctx, subId) + if err != nil { + l.Errorw("[handlerServerStatus] FindOneSubscribe Error: ", logger.Field("error", err.Error())) + continue + } + if online, exist := onlineMap[data.UserId]; !exist { + onlineMap[data.UserId] = types.ServerOnlineUser{ + IP: info, + UserId: data.UserId, + Subscribe: data.Subscribe.Name, + SubscribeId: data.SubscribeId, + Traffic: data.Traffic, + ExpiredAt: data.ExpireTime.UnixMilli(), + } + } else { + online.IP = append(online.IP, info...) + onlineMap[data.UserId] = online + } + } + + for _, online := range onlineMap { + onlineList = append(onlineList, online) + } + + result.Online = onlineList + + return result +} diff --git a/internal/logic/admin/server/getServerProtocolsLogic.go b/internal/logic/admin/server/getServerProtocolsLogic.go new file mode 100644 index 0000000..79b4007 --- /dev/null +++ b/internal/logic/admin/server/getServerProtocolsLogic.go @@ -0,0 +1,30 @@ +package server + +import ( + "context" + + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/logger" +) + +type GetServerProtocolsLogic struct { + logger.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +// Get Server Protocols +func NewGetServerProtocolsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetServerProtocolsLogic { + return &GetServerProtocolsLogic{ + Logger: logger.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *GetServerProtocolsLogic) GetServerProtocols(req *types.GetServerProtocolsRequest) (resp *types.GetServerProtocolsResponse, err error) { + // todo: add your logic here and delete this line + + return +} diff --git a/internal/logic/admin/server/toggleNodeStatusLogic.go b/internal/logic/admin/server/toggleNodeStatusLogic.go new file mode 100644 index 0000000..04e7244 --- /dev/null +++ b/internal/logic/admin/server/toggleNodeStatusLogic.go @@ -0,0 +1,43 @@ +package server + +import ( + "context" + + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/logger" + "github.com/perfect-panel/server/pkg/xerr" + "github.com/pkg/errors" +) + +type ToggleNodeStatusLogic struct { + logger.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +// NewToggleNodeStatusLogic Toggle Node Status +func NewToggleNodeStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ToggleNodeStatusLogic { + return &ToggleNodeStatusLogic{ + Logger: logger.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *ToggleNodeStatusLogic) ToggleNodeStatus(req *types.ToggleNodeStatusRequest) error { + data, err := l.svcCtx.NodeModel.FindOneNode(l.ctx, req.Id) + if err != nil { + l.Errorw("[ToggleNodeStatus] Query Database Error: ", logger.Field("error", err.Error())) + return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "[ToggleNodeStatus] Query Database Error") + } + data.Enabled = req.Enable + + err = l.svcCtx.NodeModel.UpdateNode(l.ctx, data) + if err != nil { + l.Errorw("[ToggleNodeStatus] Update Database Error: ", logger.Field("error", err.Error())) + return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseUpdateError), "[ToggleNodeStatus] Update Database Error") + } + + return nil +} diff --git a/internal/logic/admin/server/updateNodeLogic.go b/internal/logic/admin/server/updateNodeLogic.go index 3582f12..112ac52 100644 --- a/internal/logic/admin/server/updateNodeLogic.go +++ b/internal/logic/admin/server/updateNodeLogic.go @@ -2,12 +2,6 @@ package server import ( "context" - "encoding/json" - "strings" - - "github.com/hibiken/asynq" - "github.com/perfect-panel/server/pkg/device" - queue "github.com/perfect-panel/server/queue/types" "github.com/perfect-panel/server/internal/svc" "github.com/perfect-panel/server/internal/types" @@ -23,6 +17,7 @@ type UpdateNodeLogic struct { svcCtx *svc.ServiceContext } +// NewUpdateNodeLogic Update Node func NewUpdateNodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateNodeLogic { return &UpdateNodeLogic{ Logger: logger.WithContext(ctx), @@ -32,108 +27,20 @@ func NewUpdateNodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Update } func (l *UpdateNodeLogic) UpdateNode(req *types.UpdateNodeRequest) error { - // Check server exist - nodeInfo, err := l.svcCtx.ServerModel.FindOne(l.ctx, req.Id) + data, err := l.svcCtx.NodeModel.FindOneNode(l.ctx, req.Id) if err != nil { - return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "find server error: %v", err) + l.Errorw("[UpdateNode] Query Database Error: ", logger.Field("error", err.Error())) + return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseUpdateError), "[UpdateNode] Query Database Error") } - tool.DeepCopy(nodeInfo, req, tool.CopyWithIgnoreEmpty(false)) - config, err := json.Marshal(req.Config) - if err != nil { - return err - } - - nodeInfo.Config = string(config) - nodeRelay, err := json.Marshal(req.RelayNode) - if err != nil { - l.Errorw("[UpdateNode] Marshal RelayNode Error: ", logger.Field("error", err.Error())) - return err - } - - // 处理Tags字段 - switch { - case len(req.Tags) > 0: - // 有Tags,进行连接 - nodeInfo.Tags = strings.Join(req.Tags, ",") - default: - // 空数组,清空Tags - nodeInfo.Tags = "" - } - - nodeInfo.City = req.City - nodeInfo.Country = req.Country - - nodeInfo.RelayNode = string(nodeRelay) - if req.Protocol == "vless" { - var cfg types.Vless - if err := json.Unmarshal(config, &cfg); err != nil { - return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "json.Unmarshal error: %v", err.Error()) - } - if cfg.Security == "reality" && cfg.SecurityConfig.RealityPublicKey == "" { - public, private, err := tool.Curve25519Genkey(false, "") - if err != nil { - return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "generate curve25519 key error") - } - cfg.SecurityConfig.RealityPublicKey = public - cfg.SecurityConfig.RealityPrivateKey = private - cfg.SecurityConfig.RealityShortId = tool.GenerateShortID(private) - } - if cfg.SecurityConfig.RealityServerAddr == "" { - cfg.SecurityConfig.RealityServerAddr = cfg.SecurityConfig.SNI - } - if cfg.SecurityConfig.RealityServerPort == 0 { - cfg.SecurityConfig.RealityServerPort = 443 - } - config, _ = json.Marshal(cfg) - nodeInfo.Config = string(config) - } else if req.Protocol == "shadowsocks" { - var cfg types.Shadowsocks - if err = json.Unmarshal(config, &cfg); err != nil { - l.Errorf("[CreateNode] Unmarshal Shadowsocks Config Error: %v", err.Error()) - return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "json.Unmarshal error: %v", err.Error()) - } - if strings.Contains(cfg.Method, "2022") { - var length int - switch cfg.Method { - case "2022-blake3-aes-128-gcm": - length = 16 - default: - length = 32 - } - if len(cfg.ServerKey) != length { - cfg.ServerKey = tool.GenerateCipher(cfg.ServerKey, length) - } - } - config, _ = json.Marshal(cfg) - nodeInfo.Config = string(config) - } - err = l.svcCtx.ServerModel.Update(l.ctx, nodeInfo) + data.Name = req.Name + data.Tags = tool.StringSliceToString(req.Tags) + data.Port = req.Port + data.Address = req.Address + data.Protocol = req.Protocol + err = l.svcCtx.NodeModel.UpdateNode(l.ctx, data) if err != nil { l.Errorw("[UpdateNode] Update Database Error: ", logger.Field("error", err.Error())) - return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseInsertError), "create server error: %v", err) + return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseUpdateError), "[UpdateNode] Update Database Error") } - - if req.City == "" || req.Country == "" { - // Marshal the task payload - payload, err := json.Marshal(queue.GetNodeCountry{ - Protocol: nodeInfo.Protocol, - ServerAddr: nodeInfo.ServerAddr, - }) - if err != nil { - l.Errorw("[GetNodeCountry]: Marshal Error", logger.Field("error", err.Error())) - return errors.Wrap(xerr.NewErrCode(xerr.ERROR), "Failed to marshal task payload") - } - // Create a queue task - task := asynq.NewTask(queue.ForthwithGetCountry, payload) - // Enqueue the task - taskInfo, err := l.svcCtx.Queue.Enqueue(task) - if err != nil { - l.Errorw("[GetNodeCountry]: Enqueue Error", logger.Field("error", err.Error()), logger.Field("payload", string(payload))) - return errors.Wrap(xerr.NewErrCode(xerr.ERROR), "Failed to enqueue task") - } - l.Infow("[GetNodeCountry]: Enqueue Success", logger.Field("taskID", taskInfo.ID), logger.Field("payload", string(payload))) - } - - l.svcCtx.DeviceManager.Broadcast(device.SubscribeUpdate) return nil } diff --git a/internal/logic/admin/server/updateServerLogic.go b/internal/logic/admin/server/updateServerLogic.go new file mode 100644 index 0000000..2554f85 --- /dev/null +++ b/internal/logic/admin/server/updateServerLogic.go @@ -0,0 +1,115 @@ +package server + +import ( + "context" + "strings" + + "github.com/perfect-panel/server/internal/model/node" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/ip" + "github.com/perfect-panel/server/pkg/logger" + "github.com/perfect-panel/server/pkg/tool" + "github.com/perfect-panel/server/pkg/xerr" + "github.com/pkg/errors" +) + +type UpdateServerLogic struct { + logger.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +// NewUpdateServerLogic Update Server +func NewUpdateServerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateServerLogic { + return &UpdateServerLogic{ + Logger: logger.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *UpdateServerLogic) UpdateServer(req *types.UpdateServerRequest) error { + data, err := l.svcCtx.NodeModel.FindOneServer(l.ctx, req.Id) + if err != nil { + l.Errorf("[UpdateServer] FindOneServer Error: %v", err.Error()) + return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "find server error: %v", err.Error()) + } + data.Name = req.Name + data.Country = req.Country + data.City = req.City + data.Ratio = req.Ratio + // only update address when it's different + if req.Address != data.Address { + // query server ip location + result, err := ip.GetRegionByIp(req.Address) + if err != nil { + l.Errorf("[UpdateServer] GetRegionByIp Error: %v", err.Error()) + } else { + data.City = result.City + data.Country = result.Country + } + // update address + data.Address = req.Address + } + protocols := make([]node.Protocol, 0) + for _, item := range req.Protocols { + if item.Type == "" { + return errors.Wrapf(xerr.NewErrCodeMsg(xerr.InvalidParams, "protocols type is empty"), "protocols type is empty") + } + var protocol node.Protocol + tool.DeepCopy(&protocol, item) + + // VLESS Reality Key Generation + if protocol.Type == "vless" { + if protocol.Security == "reality" { + if protocol.RealityPublicKey == "" { + public, private, err := tool.Curve25519Genkey(false, "") + if err != nil { + l.Errorf("[CreateServer] Generate Reality Key Error: %v", err.Error()) + return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "generate reality key error: %v", err) + } + protocol.RealityPublicKey = public + protocol.RealityPrivateKey = private + protocol.RealityShortId = tool.GenerateShortID(private) + } + if protocol.RealityServerAddr == "" { + protocol.RealityServerAddr = protocol.SNI + } + if protocol.RealityServerPort == 0 { + protocol.RealityServerPort = 443 + } + } + + } + // ShadowSocks 2022 Key Generation + if protocol.Type == "shadowsocks" { + if strings.Contains(protocol.Cipher, "2022") { + var length int + switch protocol.Cipher { + case "2022-blake3-aes-128-gcm": + length = 16 + default: + length = 32 + } + if len(protocol.ServerKey) != length { + protocol.ServerKey = tool.GenerateCipher(protocol.ServerKey, length) + } + } + } + protocols = append(protocols, protocol) + } + err = data.MarshalProtocols(protocols) + if err != nil { + l.Errorf("[UpdateServer] Marshal Protocols Error: %v", err.Error()) + return errors.Wrapf(xerr.NewErrCodeMsg(xerr.InvalidParams, "protocols marshal error"), "protocols marshal error: %v", err) + } + + err = l.svcCtx.NodeModel.UpdateServer(l.ctx, data) + if err != nil { + l.Errorf("[UpdateServer] UpdateServer Error: %v", err.Error()) + return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseUpdateError), "update server error: %v", err.Error()) + } + + return nil +} diff --git a/internal/logic/admin/server/batchDeleteNodeGroupLogic.go b/internal/logic/admin/server_bak/batchDeleteNodeGroupLogic.go similarity index 98% rename from internal/logic/admin/server/batchDeleteNodeGroupLogic.go rename to internal/logic/admin/server_bak/batchDeleteNodeGroupLogic.go index 5353475..b64e343 100644 --- a/internal/logic/admin/server/batchDeleteNodeGroupLogic.go +++ b/internal/logic/admin/server_bak/batchDeleteNodeGroupLogic.go @@ -1,4 +1,4 @@ -package server +package server_bak import ( "context" diff --git a/internal/logic/admin/server/batchDeleteNodeLogic.go b/internal/logic/admin/server_bak/batchDeleteNodeLogic.go similarity index 98% rename from internal/logic/admin/server/batchDeleteNodeLogic.go rename to internal/logic/admin/server_bak/batchDeleteNodeLogic.go index 6b03806..86d535e 100644 --- a/internal/logic/admin/server/batchDeleteNodeLogic.go +++ b/internal/logic/admin/server_bak/batchDeleteNodeLogic.go @@ -1,4 +1,4 @@ -package server +package server_bak import ( "context" diff --git a/internal/logic/admin/server/createNodeGroupLogic.go b/internal/logic/admin/server_bak/createNodeGroupLogic.go similarity index 98% rename from internal/logic/admin/server/createNodeGroupLogic.go rename to internal/logic/admin/server_bak/createNodeGroupLogic.go index b194a24..cf6c51d 100644 --- a/internal/logic/admin/server/createNodeGroupLogic.go +++ b/internal/logic/admin/server_bak/createNodeGroupLogic.go @@ -1,4 +1,4 @@ -package server +package server_bak import ( "context" diff --git a/internal/logic/admin/server_bak/createNodeLogic.go b/internal/logic/admin/server_bak/createNodeLogic.go new file mode 100644 index 0000000..81ded62 --- /dev/null +++ b/internal/logic/admin/server_bak/createNodeLogic.go @@ -0,0 +1,129 @@ +package server_bak + +import ( + "context" + "encoding/json" + "strings" + "time" + + "github.com/hibiken/asynq" + "github.com/perfect-panel/server/internal/model/server" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/logger" + "github.com/perfect-panel/server/pkg/tool" + "github.com/perfect-panel/server/pkg/xerr" + queue "github.com/perfect-panel/server/queue/types" + "github.com/pkg/errors" +) + +type CreateNodeLogic struct { + logger.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewCreateNodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateNodeLogic { + return &CreateNodeLogic{ + Logger: logger.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *CreateNodeLogic) CreateNode(req *types.CreateNodeRequest) error { + config, err := json.Marshal(req.Config) + if err != nil { + return err + } + var serverInfo server.Server + tool.DeepCopy(&serverInfo, req) + serverInfo.Config = string(config) + nodeRelay, err := json.Marshal(req.RelayNode) + if err != nil { + l.Errorw("[UpdateNode] Marshal RelayNode Error: ", logger.Field("error", err.Error())) + return err + } + if len(req.Tags) > 0 { + serverInfo.Tags = strings.Join(req.Tags, ",") + } + + serverInfo.LastReportedAt = time.UnixMicro(1218124800) + + serverInfo.City = req.City + serverInfo.Country = req.Country + + serverInfo.RelayNode = string(nodeRelay) + if req.Protocol == "vless" { + var cfg types.Vless + if err = json.Unmarshal(config, &cfg); err != nil { + return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "json.Unmarshal error: %v", err.Error()) + } + if cfg.Security == "reality" && cfg.SecurityConfig.RealityPublicKey == "" { + public, private, err := tool.Curve25519Genkey(false, "") + if err != nil { + return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "generate curve25519 key error") + } + cfg.SecurityConfig.RealityPublicKey = public + cfg.SecurityConfig.RealityPrivateKey = private + cfg.SecurityConfig.RealityShortId = tool.GenerateShortID(private) + } + if cfg.SecurityConfig.RealityServerAddr == "" { + cfg.SecurityConfig.RealityServerAddr = cfg.SecurityConfig.SNI + } + if cfg.SecurityConfig.RealityServerPort == 0 { + cfg.SecurityConfig.RealityServerPort = 443 + } + config, _ = json.Marshal(cfg) + serverInfo.Config = string(config) + } else if req.Protocol == "shadowsocks" { + var cfg types.Shadowsocks + if err = json.Unmarshal(config, &cfg); err != nil { + l.Errorf("[CreateNode] Unmarshal Shadowsocks Config Error: %v", err.Error()) + return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "json.Unmarshal error: %v", err.Error()) + } + if strings.Contains(cfg.Method, "2022") { + var length int + switch cfg.Method { + case "2022-blake3-aes-128-gcm": + length = 16 + default: + length = 32 + } + if len(cfg.ServerKey) != length { + cfg.ServerKey = tool.GenerateCipher(cfg.ServerKey, length) + } + } + config, _ = json.Marshal(cfg) + serverInfo.Config = string(config) + } + + err = l.svcCtx.ServerModel.Insert(l.ctx, &serverInfo) + if err != nil { + l.Errorw("[CreateNode] Insert Database Error: ", logger.Field("error", err.Error())) + return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseInsertError), "create server error: %v", err) + } + + if req.City == "" || req.Country == "" { + // Marshal the task payload + payload, err := json.Marshal(queue.GetNodeCountry{ + Protocol: serverInfo.Protocol, + ServerAddr: serverInfo.ServerAddr, + }) + if err != nil { + l.Errorw("[GetNodeCountry]: Marshal Error", logger.Field("error", err.Error())) + return errors.Wrap(xerr.NewErrCode(xerr.ERROR), "Failed to marshal task payload") + } + // Create a queue task + task := asynq.NewTask(queue.ForthwithGetCountry, payload) + // Enqueue the task + taskInfo, err := l.svcCtx.Queue.Enqueue(task) + if err != nil { + l.Errorw("[GetNodeCountry]: Enqueue Error", logger.Field("error", err.Error()), logger.Field("payload", string(payload))) + return errors.Wrap(xerr.NewErrCode(xerr.ERROR), "Failed to enqueue task") + } + l.Infow("[GetNodeCountry]: Enqueue Success", logger.Field("taskID", taskInfo.ID), logger.Field("payload", string(payload))) + } + + return nil +} diff --git a/internal/logic/admin/server/createRuleGroupLogic.go b/internal/logic/admin/server_bak/createRuleGroupLogic.go similarity index 99% rename from internal/logic/admin/server/createRuleGroupLogic.go rename to internal/logic/admin/server_bak/createRuleGroupLogic.go index 619fbe7..69a39bc 100644 --- a/internal/logic/admin/server/createRuleGroupLogic.go +++ b/internal/logic/admin/server_bak/createRuleGroupLogic.go @@ -1,4 +1,4 @@ -package server +package server_bak import ( "context" diff --git a/internal/logic/admin/server/deleteNodeGroupLogic.go b/internal/logic/admin/server_bak/deleteNodeGroupLogic.go similarity index 98% rename from internal/logic/admin/server/deleteNodeGroupLogic.go rename to internal/logic/admin/server_bak/deleteNodeGroupLogic.go index 04d420e..402fa07 100644 --- a/internal/logic/admin/server/deleteNodeGroupLogic.go +++ b/internal/logic/admin/server_bak/deleteNodeGroupLogic.go @@ -1,4 +1,4 @@ -package server +package server_bak import ( "context" diff --git a/internal/logic/admin/server_bak/deleteNodeLogic.go b/internal/logic/admin/server_bak/deleteNodeLogic.go new file mode 100644 index 0000000..4ea21e8 --- /dev/null +++ b/internal/logic/admin/server_bak/deleteNodeLogic.go @@ -0,0 +1,59 @@ +package server_bak + +import ( + "context" + "github.com/perfect-panel/server/pkg/tool" + + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/logger" + "github.com/perfect-panel/server/pkg/xerr" + "github.com/pkg/errors" + "gorm.io/gorm" +) + +type DeleteNodeLogic struct { + logger.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewDeleteNodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteNodeLogic { + return &DeleteNodeLogic{ + Logger: logger.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *DeleteNodeLogic) DeleteNode(req *types.DeleteNodeRequest) error { + err := l.svcCtx.DB.Transaction(func(tx *gorm.DB) error { + // Delete server + err := l.svcCtx.ServerModel.Delete(l.ctx, req.Id, tx) + if err != nil { + return err + } + // Delete server to subscribe + subs, err := l.svcCtx.SubscribeModel.QuerySubscribeIdsByServerIdAndServerGroupId(l.ctx, req.Id, 0) + if err != nil { + l.Logger.Errorf("[DeleteNode] QuerySubscribeIdsByServerIdAndServerGroupId error: %v", err.Error()) + return err + } + + for _, sub := range subs { + servers := tool.StringToInt64Slice(sub.Server) + newServers := tool.RemoveElementBySlice(servers, req.Id) + sub.Server = tool.Int64SliceToString(newServers) + if err = l.svcCtx.SubscribeModel.Update(l.ctx, sub, tx); err != nil { + l.Logger.Errorf("[DeleteNode] UpdateSubscribe error: %v", err.Error()) + return err + } + } + return nil + }) + if err != nil { + l.Errorw("[DeleteNode] Delete Database Error: ", logger.Field("error", err.Error())) + return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseDeletedError), "delete server error: %v", err) + } + return nil +} diff --git a/internal/logic/admin/server/deleteRuleGroupLogic.go b/internal/logic/admin/server_bak/deleteRuleGroupLogic.go similarity index 98% rename from internal/logic/admin/server/deleteRuleGroupLogic.go rename to internal/logic/admin/server_bak/deleteRuleGroupLogic.go index f0e1110..491457c 100644 --- a/internal/logic/admin/server/deleteRuleGroupLogic.go +++ b/internal/logic/admin/server_bak/deleteRuleGroupLogic.go @@ -1,4 +1,4 @@ -package server +package server_bak import ( "context" diff --git a/internal/logic/admin/server/getNodeDetailLogic.go b/internal/logic/admin/server_bak/getNodeDetailLogic.go similarity index 98% rename from internal/logic/admin/server/getNodeDetailLogic.go rename to internal/logic/admin/server_bak/getNodeDetailLogic.go index 2feb8fe..e8a47e2 100644 --- a/internal/logic/admin/server/getNodeDetailLogic.go +++ b/internal/logic/admin/server_bak/getNodeDetailLogic.go @@ -1,4 +1,4 @@ -package server +package server_bak import ( "context" diff --git a/internal/logic/admin/server/getNodeGroupListLogic.go b/internal/logic/admin/server_bak/getNodeGroupListLogic.go similarity index 98% rename from internal/logic/admin/server/getNodeGroupListLogic.go rename to internal/logic/admin/server_bak/getNodeGroupListLogic.go index 6ab53c8..5f64a1e 100644 --- a/internal/logic/admin/server/getNodeGroupListLogic.go +++ b/internal/logic/admin/server_bak/getNodeGroupListLogic.go @@ -1,4 +1,4 @@ -package server +package server_bak import ( "context" diff --git a/internal/logic/admin/server/getNodeListLogic.go b/internal/logic/admin/server_bak/getNodeListLogic.go similarity index 99% rename from internal/logic/admin/server/getNodeListLogic.go rename to internal/logic/admin/server_bak/getNodeListLogic.go index 544c85d..637b161 100644 --- a/internal/logic/admin/server/getNodeListLogic.go +++ b/internal/logic/admin/server_bak/getNodeListLogic.go @@ -1,4 +1,4 @@ -package server +package server_bak import ( "context" diff --git a/internal/logic/admin/server/getNodeTagListLogic.go b/internal/logic/admin/server_bak/getNodeTagListLogic.go similarity index 97% rename from internal/logic/admin/server/getNodeTagListLogic.go rename to internal/logic/admin/server_bak/getNodeTagListLogic.go index 36eb9e7..78b159d 100644 --- a/internal/logic/admin/server/getNodeTagListLogic.go +++ b/internal/logic/admin/server_bak/getNodeTagListLogic.go @@ -1,4 +1,4 @@ -package server +package server_bak import ( "context" diff --git a/internal/logic/admin/server/getRuleGroupListLogic.go b/internal/logic/admin/server_bak/getRuleGroupListLogic.go similarity index 98% rename from internal/logic/admin/server/getRuleGroupListLogic.go rename to internal/logic/admin/server_bak/getRuleGroupListLogic.go index c3e1a3f..2c1c603 100644 --- a/internal/logic/admin/server/getRuleGroupListLogic.go +++ b/internal/logic/admin/server_bak/getRuleGroupListLogic.go @@ -1,4 +1,4 @@ -package server +package server_bak import ( "context" diff --git a/internal/logic/admin/server/nodeSortLogic.go b/internal/logic/admin/server_bak/nodeSortLogic.go similarity index 99% rename from internal/logic/admin/server/nodeSortLogic.go rename to internal/logic/admin/server_bak/nodeSortLogic.go index 1141ca9..837a3fa 100644 --- a/internal/logic/admin/server/nodeSortLogic.go +++ b/internal/logic/admin/server_bak/nodeSortLogic.go @@ -1,4 +1,4 @@ -package server +package server_bak import ( "context" diff --git a/internal/logic/admin/server/updateNodeGroupLogic.go b/internal/logic/admin/server_bak/updateNodeGroupLogic.go similarity index 98% rename from internal/logic/admin/server/updateNodeGroupLogic.go rename to internal/logic/admin/server_bak/updateNodeGroupLogic.go index 5df203b..4440c99 100644 --- a/internal/logic/admin/server/updateNodeGroupLogic.go +++ b/internal/logic/admin/server_bak/updateNodeGroupLogic.go @@ -1,4 +1,4 @@ -package server +package server_bak import ( "context" diff --git a/internal/logic/admin/server_bak/updateNodeLogic.go b/internal/logic/admin/server_bak/updateNodeLogic.go new file mode 100644 index 0000000..8142ad4 --- /dev/null +++ b/internal/logic/admin/server_bak/updateNodeLogic.go @@ -0,0 +1,139 @@ +package server_bak + +import ( + "context" + "encoding/json" + "strings" + + "github.com/hibiken/asynq" + "github.com/perfect-panel/server/pkg/device" + queue "github.com/perfect-panel/server/queue/types" + + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/logger" + "github.com/perfect-panel/server/pkg/tool" + "github.com/perfect-panel/server/pkg/xerr" + "github.com/pkg/errors" +) + +type UpdateNodeLogic struct { + logger.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewUpdateNodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateNodeLogic { + return &UpdateNodeLogic{ + Logger: logger.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *UpdateNodeLogic) UpdateNode(req *types.UpdateNodeRequest) error { + // Check server exist + nodeInfo, err := l.svcCtx.ServerModel.FindOne(l.ctx, req.Id) + if err != nil { + return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "find server error: %v", err) + } + tool.DeepCopy(nodeInfo, req, tool.CopyWithIgnoreEmpty(false)) + config, err := json.Marshal(req.Config) + if err != nil { + return err + } + + nodeInfo.Config = string(config) + nodeRelay, err := json.Marshal(req.RelayNode) + if err != nil { + l.Errorw("[UpdateNode] Marshal RelayNode Error: ", logger.Field("error", err.Error())) + return err + } + + // 处理Tags字段 + switch { + case len(req.Tags) > 0: + // 有Tags,进行连接 + nodeInfo.Tags = strings.Join(req.Tags, ",") + default: + // 空数组,清空Tags + nodeInfo.Tags = "" + } + + nodeInfo.City = req.City + nodeInfo.Country = req.Country + + nodeInfo.RelayNode = string(nodeRelay) + if req.Protocol == "vless" { + var cfg types.Vless + if err := json.Unmarshal(config, &cfg); err != nil { + return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "json.Unmarshal error: %v", err.Error()) + } + if cfg.Security == "reality" && cfg.SecurityConfig.RealityPublicKey == "" { + public, private, err := tool.Curve25519Genkey(false, "") + if err != nil { + return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "generate curve25519 key error") + } + cfg.SecurityConfig.RealityPublicKey = public + cfg.SecurityConfig.RealityPrivateKey = private + cfg.SecurityConfig.RealityShortId = tool.GenerateShortID(private) + } + if cfg.SecurityConfig.RealityServerAddr == "" { + cfg.SecurityConfig.RealityServerAddr = cfg.SecurityConfig.SNI + } + if cfg.SecurityConfig.RealityServerPort == 0 { + cfg.SecurityConfig.RealityServerPort = 443 + } + config, _ = json.Marshal(cfg) + nodeInfo.Config = string(config) + } else if req.Protocol == "shadowsocks" { + var cfg types.Shadowsocks + if err = json.Unmarshal(config, &cfg); err != nil { + l.Errorf("[CreateNode] Unmarshal Shadowsocks Config Error: %v", err.Error()) + return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "json.Unmarshal error: %v", err.Error()) + } + if strings.Contains(cfg.Method, "2022") { + var length int + switch cfg.Method { + case "2022-blake3-aes-128-gcm": + length = 16 + default: + length = 32 + } + if len(cfg.ServerKey) != length { + cfg.ServerKey = tool.GenerateCipher(cfg.ServerKey, length) + } + } + config, _ = json.Marshal(cfg) + nodeInfo.Config = string(config) + } + err = l.svcCtx.ServerModel.Update(l.ctx, nodeInfo) + if err != nil { + l.Errorw("[UpdateNode] Update Database Error: ", logger.Field("error", err.Error())) + return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseInsertError), "create server error: %v", err) + } + + if req.City == "" || req.Country == "" { + // Marshal the task payload + payload, err := json.Marshal(queue.GetNodeCountry{ + Protocol: nodeInfo.Protocol, + ServerAddr: nodeInfo.ServerAddr, + }) + if err != nil { + l.Errorw("[GetNodeCountry]: Marshal Error", logger.Field("error", err.Error())) + return errors.Wrap(xerr.NewErrCode(xerr.ERROR), "Failed to marshal task payload") + } + // Create a queue task + task := asynq.NewTask(queue.ForthwithGetCountry, payload) + // Enqueue the task + taskInfo, err := l.svcCtx.Queue.Enqueue(task) + if err != nil { + l.Errorw("[GetNodeCountry]: Enqueue Error", logger.Field("error", err.Error()), logger.Field("payload", string(payload))) + return errors.Wrap(xerr.NewErrCode(xerr.ERROR), "Failed to enqueue task") + } + l.Infow("[GetNodeCountry]: Enqueue Success", logger.Field("taskID", taskInfo.ID), logger.Field("payload", string(payload))) + } + + l.svcCtx.DeviceManager.Broadcast(device.SubscribeUpdate) + return nil +} diff --git a/internal/logic/admin/server/updateRuleGroupLogic.go b/internal/logic/admin/server_bak/updateRuleGroupLogic.go similarity index 98% rename from internal/logic/admin/server/updateRuleGroupLogic.go rename to internal/logic/admin/server_bak/updateRuleGroupLogic.go index 500ba02..2fd35a2 100644 --- a/internal/logic/admin/server/updateRuleGroupLogic.go +++ b/internal/logic/admin/server_bak/updateRuleGroupLogic.go @@ -1,4 +1,4 @@ -package server +package server_bak import ( "context" diff --git a/internal/logic/auth/oauth/oAuthLoginGetTokenLogic.go b/internal/logic/auth/oauth/oAuthLoginGetTokenLogic.go index 4107065..ded2724 100644 --- a/internal/logic/auth/oauth/oAuthLoginGetTokenLogic.go +++ b/internal/logic/auth/oauth/oAuthLoginGetTokenLogic.go @@ -65,7 +65,7 @@ func (l *OAuthLoginGetTokenLogic) OAuthLoginGetToken(req *types.OAuthLoginGetTok ) defer func() { - l.recordLoginStatus(loginStatus, userInfo, ip, userAgent, requestID) + l.recordLoginStatus(loginStatus, userInfo, ip, userAgent, requestID, req.Method) }() userInfo, err = l.handleOAuthProvider(req, requestID) @@ -487,10 +487,11 @@ func (l *OAuthLoginGetTokenLogic) createAuthMethod(db *gorm.DB, userID int64, au return nil } -func (l *OAuthLoginGetTokenLogic) recordLoginStatus(loginStatus bool, userInfo *user.User, ip, userAgent, requestID string) { +func (l *OAuthLoginGetTokenLogic) recordLoginStatus(loginStatus bool, userInfo *user.User, ip, userAgent, requestID, authType string) { if userInfo != nil && userInfo.Id != 0 { loginLog := log.Login{ + Method: authType, LoginIP: ip, UserAgent: userAgent, Success: loginStatus, diff --git a/internal/logic/auth/resetPasswordLogic.go b/internal/logic/auth/resetPasswordLogic.go index ab6b5b4..d13089f 100644 --- a/internal/logic/auth/resetPasswordLogic.go +++ b/internal/logic/auth/resetPasswordLogic.go @@ -45,6 +45,7 @@ func (l *ResetPasswordLogic) ResetPassword(req *types.ResetPasswordRequest) (res defer func() { if userInfo.Id != 0 && loginStatus { loginLog := log.Login{ + Method: "email", LoginIP: req.IP, UserAgent: req.UserAgent, Success: loginStatus, diff --git a/internal/logic/auth/telephoneLoginLogic.go b/internal/logic/auth/telephoneLoginLogic.go index b240e58..39b7a5e 100644 --- a/internal/logic/auth/telephoneLoginLogic.go +++ b/internal/logic/auth/telephoneLoginLogic.go @@ -53,6 +53,7 @@ func (l *TelephoneLoginLogic) TelephoneLogin(req *types.TelephoneLoginRequest, r defer func(svcCtx *svc.ServiceContext) { if userInfo.Id != 0 { loginLog := log.Login{ + Method: "mobile", LoginIP: ip, UserAgent: r.UserAgent(), Success: loginStatus, diff --git a/internal/logic/auth/telephoneResetPasswordLogic.go b/internal/logic/auth/telephoneResetPasswordLogic.go index 21f0062..a2408d1 100644 --- a/internal/logic/auth/telephoneResetPasswordLogic.go +++ b/internal/logic/auth/telephoneResetPasswordLogic.go @@ -104,6 +104,7 @@ func (l *TelephoneResetPasswordLogic) TelephoneResetPassword(req *types.Telephon defer func() { if token != "" && userInfo.Id != 0 { loginLog := log.Login{ + Method: "mobile", LoginIP: req.IP, UserAgent: req.UserAgent, Success: token != "", diff --git a/internal/logic/auth/telephoneUserRegisterLogic.go b/internal/logic/auth/telephoneUserRegisterLogic.go index d0e22f4..341d6d3 100644 --- a/internal/logic/auth/telephoneUserRegisterLogic.go +++ b/internal/logic/auth/telephoneUserRegisterLogic.go @@ -159,6 +159,7 @@ func (l *TelephoneUserRegisterLogic) TelephoneUserRegister(req *types.TelephoneR defer func() { if token != "" && userInfo.Id != 0 { loginLog := log.Login{ + Method: "mobile", LoginIP: req.IP, UserAgent: req.UserAgent, Success: token != "", diff --git a/internal/logic/auth/userLoginLogic.go b/internal/logic/auth/userLoginLogic.go index 81be1ae..9063188 100644 --- a/internal/logic/auth/userLoginLogic.go +++ b/internal/logic/auth/userLoginLogic.go @@ -43,6 +43,7 @@ func (l *UserLoginLogic) UserLogin(req *types.UserLoginRequest) (resp *types.Log defer func(svcCtx *svc.ServiceContext) { if userInfo.Id != 0 { loginLog := log.Login{ + Method: "email", LoginIP: req.IP, UserAgent: req.UserAgent, Success: loginStatus, diff --git a/internal/logic/auth/userRegisterLogic.go b/internal/logic/auth/userRegisterLogic.go index a3d7fc2..6871a3a 100644 --- a/internal/logic/auth/userRegisterLogic.go +++ b/internal/logic/auth/userRegisterLogic.go @@ -147,6 +147,7 @@ func (l *UserRegisterLogic) UserRegister(req *types.UserRegisterRequest) (resp * defer func() { if token != "" && userInfo.Id != 0 { loginLog := log.Login{ + Method: "email", LoginIP: req.IP, UserAgent: req.UserAgent, Success: loginStatus, diff --git a/internal/logic/public/user/getSubscribeLogLogic.go b/internal/logic/public/user/getSubscribeLogLogic.go index f18f511..689eca9 100644 --- a/internal/logic/public/user/getSubscribeLogLogic.go +++ b/internal/logic/public/user/getSubscribeLogLogic.go @@ -20,7 +20,7 @@ type GetSubscribeLogLogic struct { svcCtx *svc.ServiceContext } -// Get Subscribe Log +// NewGetSubscribeLogLogic Get Subscribe Log func NewGetSubscribeLogLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSubscribeLogLogic { return &GetSubscribeLogLogic{ Logger: logger.WithContext(ctx), @@ -39,7 +39,7 @@ func (l *GetSubscribeLogLogic) GetSubscribeLog(req *types.GetSubscribeLogRequest Page: req.Page, Size: req.Size, Type: log.TypeSubscribe.Uint8(), - ObjectID: u.Id, + ObjectID: u.Id, // filter by current user id }) if err != nil { l.Errorw("[GetUserSubscribeLogs] Get User Subscribe Logs Error:", logger.Field("err", err.Error())) @@ -56,7 +56,7 @@ func (l *GetSubscribeLogLogic) GetSubscribeLog(req *types.GetSubscribeLogRequest list = append(list, types.UserSubscribeLog{ Id: item.Id, UserId: item.ObjectID, - UserSubscribeId: content.SubscribeId, + UserSubscribeId: content.UserSubscribeId, Token: content.Token, IP: content.ClientIP, UserAgent: content.UserAgent, diff --git a/internal/logic/public/user/queryUserBalanceLogLogic.go b/internal/logic/public/user/queryUserBalanceLogLogic.go index 6a030f9..c9175cc 100644 --- a/internal/logic/public/user/queryUserBalanceLogLogic.go +++ b/internal/logic/public/user/queryUserBalanceLogLogic.go @@ -20,7 +20,7 @@ type QueryUserBalanceLogLogic struct { svcCtx *svc.ServiceContext } -// Query User Balance Log +// NewQueryUserBalanceLogLogic Query User Balance Log func NewQueryUserBalanceLogLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryUserBalanceLogLogic { return &QueryUserBalanceLogLogic{ Logger: logger.WithContext(ctx), @@ -51,21 +51,20 @@ func (l *QueryUserBalanceLogLogic) QueryUserBalanceLog() (resp *types.QueryUserB Total: total, } - list := make([]types.UserBalanceLog, 0) + list := make([]types.BalanceLog, 0) for _, datum := range data { var content log.Balance if err = content.Unmarshal([]byte(datum.Content)); err != nil { l.Errorf("[QueryUserBalanceLog] unmarshal balance log content failed: %v", err.Error()) continue } - list = append(list, types.UserBalanceLog{ - Id: datum.Id, + list = append(list, types.BalanceLog{ UserId: datum.ObjectID, Amount: content.Amount, Type: content.Type, OrderId: content.OrderId, Balance: content.Balance, - CreatedAt: datum.CreatedAt.UnixMilli(), + Timestamp: content.Timestamp, }) } diff --git a/internal/logic/public/user/queryUserCommissionLogLogic.go b/internal/logic/public/user/queryUserCommissionLogLogic.go index b7a5f28..23ecb69 100644 --- a/internal/logic/public/user/queryUserCommissionLogLogic.go +++ b/internal/logic/public/user/queryUserCommissionLogLogic.go @@ -54,10 +54,10 @@ func (l *QueryUserCommissionLogLogic) QueryUserCommissionLog(req *types.QueryUse continue } list = append(list, types.CommissionLog{ - Id: datum.Id, UserId: datum.ObjectID, - OrderNo: content.OrderNo, + Type: content.Type, Amount: content.Amount, + OrderNo: content.OrderNo, CreatedAt: content.CreatedAt, }) } diff --git a/internal/logic/subscribe/subscribeLogic.go b/internal/logic/subscribe/subscribeLogic.go index 3ce99e7..9f8ee2c 100644 --- a/internal/logic/subscribe/subscribeLogic.go +++ b/internal/logic/subscribe/subscribeLogic.go @@ -177,16 +177,17 @@ func (l *SubscribeLogic) logSubscribeActivity(subscribeStatus bool, userSub *use } subscribeLog := log.Subscribe{ - Token: req.Token, - UserAgent: req.UA, - ClientIP: l.ctx.ClientIP(), + Token: req.Token, + UserAgent: req.UA, + ClientIP: l.ctx.ClientIP(), + UserSubscribeId: userSub.Id, } content, _ := subscribeLog.Marshal() err := l.svc.LogModel.Insert(l.ctx.Request.Context(), &log.SystemLog{ Type: log.TypeSubscribe.Uint8(), - ObjectID: userSub.Id, + ObjectID: userSub.UserId, // log user id Date: time.Now().Format(time.DateOnly), Content: string(content), }) diff --git a/internal/model/cache/constant.go b/internal/model/cache/constant.go index 5ae995c..ece753b 100644 --- a/internal/model/cache/constant.go +++ b/internal/model/cache/constant.go @@ -39,14 +39,6 @@ const ( AllNodeUploadTrafficCacheKey = "node:all_node_upload_traffic" // YesterdayTotalTrafficRank 昨日节点总流量排行榜 YesterdayNodeTotalTrafficRank = "node:yesterday_total_traffic_rank" - // YesterdayUploadTrafficRank 昨日节点上传流量排行榜 - YesterdayNodeUploadTrafficRank = "node:yesterday_upload_traffic_rank" - // YesterdayDownloadTrafficRank 昨日节点下载流量排行榜 - YesterdayNodeDownloadTrafficRank = "node:yesterday_download_traffic_rank" // YesterdayUserTotalTrafficRank 昨日用户总流量排行榜 YesterdayUserTotalTrafficRank = "node:yesterday_user_total_traffic_rank" - // YesterdayUserUploadTrafficRank 昨日用户上传流量排行榜 - YesterdayUserUploadTrafficRank = "node:yesterday_user_upload_traffic_rank" - // YesterdayUserDownloadTrafficRank 昨日用户下载流量排行榜 - YesterdayUserDownloadTrafficRank = "node:yesterday_user_download_traffic_rank" ) diff --git a/internal/model/log/log.go b/internal/model/log/log.go index 463332e..a142c0d 100644 --- a/internal/model/log/log.go +++ b/internal/model/log/log.go @@ -7,20 +7,44 @@ import ( type Type uint8 +/* + +Log Types: + 1X Message Logs + 2X Subscription Logs + 3X User Logs + 4X Traffic Ranking Logs +*/ + const ( - TypeEmailMessage Type = iota + 1 // Message log - TypeMobileMessage // Mobile message log - TypeSubscribe // Subscription log - TypeSubscribeTraffic // Subscription traffic log - TypeServerTraffic // Server traffic log - TypeLogin // Login log - TypeRegister // Registration log - TypeBalance // Balance log - TypeCommission // Commission log - TypeResetSubscribe // Reset subscription log - TypeGift // Gift log - TypeUserTrafficRank // Top 10 User traffic rank log - TypeServerTrafficRank // Top 10 Server traffic rank log + TypeEmailMessage Type = 10 // Message log + TypeMobileMessage Type = 11 // Mobile message log + TypeSubscribe Type = 20 // Subscription log + TypeSubscribeTraffic Type = 21 // Subscription traffic log + TypeServerTraffic Type = 22 // Server traffic log + TypeResetSubscribe Type = 23 // Reset subscription log + TypeLogin Type = 30 // Login log + TypeRegister Type = 31 // Registration log + TypeBalance Type = 32 // Balance log + TypeCommission Type = 33 // Commission log + TypeGift Type = 34 // Gift log + TypeUserTrafficRank Type = 40 // Top 10 User traffic rank log + TypeServerTrafficRank Type = 41 // Top 10 Server traffic rank log +) +const ( + ResetSubscribeTypeAuto uint16 = 231 // Auto reset + ResetSubscribeTypeAdvance uint16 = 232 // Advance reset + ResetSubscribeTypePaid uint16 = 233 // Paid reset + BalanceTypeRecharge uint16 = 321 // Recharge + BalanceTypeWithdraw uint16 = 322 // Withdraw + BalanceTypePayment uint16 = 323 // Payment + BalanceTypeRefund uint16 = 324 // Refund + BalanceTypeReward uint16 = 325 // Reward + CommissionTypePurchase uint16 = 331 // Purchase + CommissionTypeRenewal uint16 = 332 // Renewal + CommissionTypeRefund uint16 = 333 // Gift + GiftTypeIncrease uint16 = 341 // Increase + GiftTypeReduce uint16 = 342 // Reduce ) // Uint8 converts Type to uint8. @@ -95,6 +119,7 @@ func (s *Traffic) Unmarshal(data []byte) error { // Login represents a login log entry. type Login struct { + Method string `json:"method"` LoginIP string `json:"login_ip"` UserAgent string `json:"user_agent"` Success bool `json:"success"` @@ -147,10 +172,10 @@ func (r *Register) Unmarshal(data []byte) error { // Subscribe represents a subscription log entry. type Subscribe struct { - Token string `json:"token"` - UserAgent string `json:"user_agent"` - ClientIP string `json:"client_ip"` - SubscribeId int64 `json:"subscribe_id"` + Token string `json:"token"` + UserAgent string `json:"user_agent"` + ClientIP string `json:"client_ip"` + UserSubscribeId int64 `json:"user_subscribe_id"` } // Marshal implements the json.Marshaler interface for Subscribe. @@ -170,15 +195,10 @@ func (s *Subscribe) Unmarshal(data []byte) error { return json.Unmarshal(data, aux) } -const ( - ResetSubscribeTypeAuto uint8 = 1 - ResetSubscribeTypeAdvance uint8 = 2 - ResetSubscribeTypePaid uint8 = 3 -) - // ResetSubscribe represents a reset subscription log entry. type ResetSubscribe struct { - Type uint8 `json:"type"` + Type uint16 `json:"type"` + UserId int64 `json:"user_id"` OrderNo string `json:"order_no,omitempty"` ResetAt int64 `json:"reset_at"` } @@ -200,22 +220,13 @@ func (r *ResetSubscribe) Unmarshal(data []byte) error { return json.Unmarshal(data, aux) } -const ( - BalanceTypeRecharge uint8 = 1 // Recharge - BalanceTypeWithdraw uint8 = 2 // Withdraw - BalanceTypePayment uint8 = 3 // Payment - BalanceTypeRefund uint8 = 4 // Refund - BalanceTypeReward uint8 = 5 // Reward -) - // Balance represents a balance log entry. type Balance struct { - Id int64 `json:"id"` - Type uint8 `json:"type"` - Amount int64 `json:"amount"` - OrderId int64 `json:"order_id,omitempty"` - Balance int64 `json:"balance"` - Timestamp int64 `json:"timestamp"` + Type uint16 `json:"type"` + Amount int64 `json:"amount"` + OrderId int64 `json:"order_id,omitempty"` + Balance int64 `json:"balance"` + Timestamp int64 `json:"timestamp"` } // Marshal implements the json.Marshaler interface for Balance. @@ -235,15 +246,9 @@ func (b *Balance) Unmarshal(data []byte) error { return json.Unmarshal(data, aux) } -const ( - CommissionTypePurchase uint8 = 1 // Purchase - CommissionTypeRenewal uint8 = 2 // Renewal - CommissionTypeRefund uint8 = 3 // Gift -) - // Commission represents a commission log entry. type Commission struct { - Type uint8 `json:"type"` + Type uint16 `json:"type"` Amount int64 `json:"amount"` OrderNo string `json:"order_no"` CreatedAt int64 `json:"created_at"` @@ -266,14 +271,9 @@ func (c *Commission) Unmarshal(data []byte) error { return json.Unmarshal(data, aux) } -const ( - GiftTypeIncrease uint8 = 1 // Increase - GiftTypeReduce uint8 = 2 // Reduce -) - // Gift represents a gift log entry. type Gift struct { - Type uint8 `json:"type"` + Type uint16 `json:"type"` OrderNo string `json:"order_no"` SubscribeId int64 `json:"subscribe_id"` Amount int64 `json:"amount"` diff --git a/internal/model/log/model.go b/internal/model/log/model.go index c523f40..7e60aba 100644 --- a/internal/model/log/model.go +++ b/internal/model/log/model.go @@ -16,6 +16,7 @@ type FilterParams struct { Page int Size int Type uint8 + Data string Search string ObjectID int64 } @@ -43,6 +44,11 @@ func (m *customSystemLogModel) FilterSystemLog(ctx context.Context, filter *Filt if filter.Type != 0 { tx = tx.Where("`type` = ?", filter.Type) } + + if filter.Data != "" { + tx = tx.Where("`data` = ?", filter.Data) + } + if filter.ObjectID != 0 { tx = tx.Where("`object_id` = ?", filter.ObjectID) } diff --git a/internal/model/node/default.go b/internal/model/node/default.go new file mode 100644 index 0000000..f0bac90 --- /dev/null +++ b/internal/model/node/default.go @@ -0,0 +1,127 @@ +package node + +import ( + "context" + + "gorm.io/gorm" +) + +var _ Model = (*customServerModel)(nil) + +//goland:noinspection GoNameStartsWithPackageName +type ( + Model interface { + serverModel + NodeModel + customServerLogicModel + } + serverModel interface { + InsertServer(ctx context.Context, data *Server, tx ...*gorm.DB) error + FindOneServer(ctx context.Context, id int64) (*Server, error) + UpdateServer(ctx context.Context, data *Server, tx ...*gorm.DB) error + DeleteServer(ctx context.Context, id int64, tx ...*gorm.DB) error + Transaction(ctx context.Context, fn func(db *gorm.DB) error) error + } + + NodeModel interface { + InsertNode(ctx context.Context, data *Node, tx ...*gorm.DB) error + FindOneNode(ctx context.Context, id int64) (*Node, error) + UpdateNode(ctx context.Context, data *Node, tx ...*gorm.DB) error + DeleteNode(ctx context.Context, id int64, tx ...*gorm.DB) error + } + + customServerModel struct { + *defaultServerModel + } + defaultServerModel struct { + *gorm.DB + } +) + +func newServerModel(db *gorm.DB) *defaultServerModel { + return &defaultServerModel{ + DB: db, + } +} + +// NewModel returns a model for the database table. +func NewModel(conn *gorm.DB) Model { + return &customServerModel{ + defaultServerModel: newServerModel(conn), + } +} + +func (m *defaultServerModel) InsertServer(ctx context.Context, data *Server, tx ...*gorm.DB) error { + db := m.DB + if len(tx) > 0 { + db = tx[0] + } + return db.WithContext(ctx).Create(data).Error +} + +func (m *defaultServerModel) FindOneServer(ctx context.Context, id int64) (*Server, error) { + var server Server + err := m.WithContext(ctx).Model(&Server{}).Where("id = ?", id).First(&server).Error + return &server, err +} + +func (m *defaultServerModel) UpdateServer(ctx context.Context, data *Server, tx ...*gorm.DB) error { + _, err := m.FindOneServer(ctx, data.Id) + if err != nil { + return err + } + + db := m.DB + if len(tx) > 0 { + db = tx[0] + } + return db.WithContext(ctx).Save(data).Error + +} + +func (m *defaultServerModel) DeleteServer(ctx context.Context, id int64, tx ...*gorm.DB) error { + db := m.DB + if len(tx) > 0 { + db = tx[0] + } + return db.WithContext(ctx).Where("`id` = ?", id).Delete(&Server{}).Error +} + +func (m *defaultServerModel) InsertNode(ctx context.Context, data *Node, tx ...*gorm.DB) error { + db := m.DB + if len(tx) > 0 { + db = tx[0] + } + return db.WithContext(ctx).Create(data).Error +} + +func (m *defaultServerModel) FindOneNode(ctx context.Context, id int64) (*Node, error) { + var node Node + err := m.WithContext(ctx).Model(&Node{}).Where("id = ?", id).First(&node).Error + return &node, err +} + +func (m *defaultServerModel) UpdateNode(ctx context.Context, data *Node, tx ...*gorm.DB) error { + _, err := m.FindOneNode(ctx, data.Id) + if err != nil { + return err + } + + db := m.DB + if len(tx) > 0 { + db = tx[0] + } + return db.WithContext(ctx).Save(data).Error +} + +func (m *defaultServerModel) DeleteNode(ctx context.Context, id int64, tx ...*gorm.DB) error { + db := m.DB + if len(tx) > 0 { + db = tx[0] + } + return db.WithContext(ctx).Where("`id` = ?", id).Delete(&Node{}).Error +} + +func (m *defaultServerModel) Transaction(ctx context.Context, fn func(db *gorm.DB) error) error { + return m.WithContext(ctx).Transaction(fn) +} diff --git a/internal/model/node/model.go b/internal/model/node/model.go new file mode 100644 index 0000000..1aefb05 --- /dev/null +++ b/internal/model/node/model.go @@ -0,0 +1,53 @@ +package node + +import "context" + +type customServerLogicModel interface { + FilterServerList(ctx context.Context, params *FilterParams) (int64, []*Server, error) + FilterNodeList(ctx context.Context, params *FilterParams) (int64, []*Node, error) +} + +// FilterParams Filter Server Params +type FilterParams struct { + Page int + Size int + Search string +} + +// FilterServerList Filter Server List +func (m *customServerModel) FilterServerList(ctx context.Context, params *FilterParams) (int64, []*Server, error) { + var servers []*Server + var total int64 + query := m.WithContext(ctx).Model(&Server{}) + if params == nil { + params = &FilterParams{ + Page: 1, + Size: 10, + } + } + if params.Search != "" { + s := "%" + params.Search + "%" + query = query.Where("`name` LIKE ? OR `address` LIKE ?", s, s) + } + err := query.Count(&total).Limit(params.Size).Offset((params.Page - 1) * params.Size).Find(&servers).Error + return total, servers, err +} + +// FilterNodeList Filter Node List +func (m *customServerModel) FilterNodeList(ctx context.Context, params *FilterParams) (int64, []*Node, error) { + var nodes []*Node + var total int64 + query := m.WithContext(ctx).Model(&Node{}) + if params == nil { + params = &FilterParams{ + Page: 1, + Size: 10, + } + } + if params.Search != "" { + s := "%" + params.Search + "%" + query = query.Where("`name` LIKE ? OR `address` LIKE ? OR `tags` LIKE ? OR `port` LIKE ? ", s, s, s, s) + } + err := query.Count(&total).Limit(params.Size).Offset((params.Page - 1) * params.Size).Find(&nodes).Error + return total, nodes, err +} diff --git a/internal/model/node/node.go b/internal/model/node/node.go new file mode 100644 index 0000000..6e5c35b --- /dev/null +++ b/internal/model/node/node.go @@ -0,0 +1,20 @@ +package node + +import "time" + +type Node struct { + Id int64 `gorm:"primary_key"` + Name string `gorm:"type:varchar(100);not null;default:'';comment:Node Name"` + Tags string `gorm:"type:varchar(255);not null;default:'';comment:Tags"` + Port uint16 `gorm:"not null;default:0;comment:Connect Port"` + Address string `gorm:"type:varchar(255);not null;default:'';comment:Connect Address"` + ServerId int64 `gorm:"not null;default:0;comment:Server ID"` + Protocol string `gorm:"type:varchar(100);not null;default:'';comment:Protocol"` + Enabled *bool `gorm:"type:boolean;not null;default:true;comment:Enabled"` + CreatedAt time.Time `gorm:"<-:create;comment:Creation Time"` + UpdatedAt time.Time `gorm:"comment:Update Time"` +} + +func (Node) TableName() string { + return "nodes" +} diff --git a/internal/model/node/server.go b/internal/model/node/server.go new file mode 100644 index 0000000..e5d59c6 --- /dev/null +++ b/internal/model/node/server.go @@ -0,0 +1,108 @@ +package node + +import ( + "encoding/json" + "time" + + "github.com/pkg/errors" +) + +type Server struct { + Id int64 `gorm:"primary_key"` + Name string `gorm:"type:varchar(100);not null;default:'';comment:Server Name"` + Country string `gorm:"type:varchar(128);not null;default:'';comment:Country"` + City string `gorm:"type:varchar(128);not null;default:'';comment:City"` + Ratio float32 `gorm:"type:DECIMAL(4,2);not null;default:0;comment:Traffic Ratio"` + Address string `gorm:"type:varchar(100);not null;default:'';comment:Server Address"` + Sort int `gorm:"type:int;not null;default:0;comment:Sort"` + Protocols string `gorm:"type:text;default:null;comment:Protocol"` + LastReportedAt time.Time `gorm:"comment:Last Reported Time"` + CreatedAt time.Time `gorm:"<-:create;comment:Creation Time"` + UpdatedAt time.Time `gorm:"comment:Update Time"` +} + +func (*Server) TableName() string { + return "servers" +} + +// MarshalProtocols Marshal server protocols to json +func (m *Server) MarshalProtocols(list []Protocol) error { + var validate = make(map[string]bool) + for _, protocol := range list { + if protocol.Type == "" { + return errors.New("protocol type is required") + } + if _, exists := validate[protocol.Type]; exists { + return errors.New("duplicate protocol type: " + protocol.Type) + } + validate[protocol.Type] = true + } + data, err := json.Marshal(list) + if err != nil { + return err + } + m.Protocols = string(data) + return nil +} + +// UnmarshalProtocols Unmarshal server protocols from json +func (m *Server) UnmarshalProtocols() ([]Protocol, error) { + var list []Protocol + if m.Protocols == "" { + return list, nil + } + err := json.Unmarshal([]byte(m.Protocols), &list) + if err != nil { + return nil, err + } + return list, nil +} + +type Protocol struct { + Type string `json:"type"` + Port uint16 `json:"port"` + Security string `json:"security,omitempty"` + SNI string `json:"sni,omitempty"` + AllowInsecure bool `json:"allow_insecure,omitempty"` + Fingerprint string `json:"fingerprint,omitempty"` + RealityServerAddr string `json:"reality_server_addr,omitempty"` + RealityServerPort int `json:"reality_server_port,omitempty"` + RealityPrivateKey string `json:"reality_private_key,omitempty"` + RealityPublicKey string `json:"reality_public_key,omitempty"` + RealityShortId string `json:"reality_short_id,omitempty"` + Transport string `json:"transport,omitempty"` + Host string `json:"host,omitempty"` + Path string `json:"path,omitempty"` + ServiceName string `json:"service_name,omitempty"` + Cipher string `json:"cipher,omitempty"` + ServerKey string `json:"server_key,omitempty"` + Flow string `json:"flow,omitempty"` + HopPorts string `json:"hop_ports,omitempty"` + HopInterval int `json:"hop_interval,omitempty"` + ObfsPassword string `json:"obfs_password,omitempty"` + DisableSNI bool `json:"disable_sni,omitempty"` + ReduceRtt bool `json:"reduce_rtt,omitempty"` + UDPRelayMode string `json:"udp_relay_mode,omitempty"` + CongestionController string `json:"congestion_controller,omitempty"` +} + +// Marshal protocol to json +func (m *Protocol) Marshal() ([]byte, error) { + type Alias Protocol + return json.Marshal(&struct { + *Alias + }{ + Alias: (*Alias)(m), + }) +} + +// Unmarshal json to protocol +func (m *Protocol) Unmarshal(data []byte) error { + type Alias Protocol + aux := &struct { + *Alias + }{ + Alias: (*Alias)(m), + } + return json.Unmarshal(data, &aux) +} diff --git a/internal/svc/serviceContext.go b/internal/svc/serviceContext.go index 01d80c9..d701590 100644 --- a/internal/svc/serviceContext.go +++ b/internal/svc/serviceContext.go @@ -4,6 +4,7 @@ import ( "context" "github.com/perfect-panel/server/internal/model/client" + "github.com/perfect-panel/server/internal/model/node" "github.com/perfect-panel/server/pkg/device" "github.com/perfect-panel/server/internal/model/ads" @@ -43,6 +44,7 @@ type ServiceContext struct { AuthModel auth.Model AdsModel ads.Model LogModel log.Model + NodeModel node.Model UserModel user.Model OrderModel order.Model ClientModel client.Model @@ -93,6 +95,7 @@ func NewServiceContext(c config.Config) *ServiceContext { AuthLimiter: authLimiter, AdsModel: ads.NewModel(db, rds), LogModel: log.NewModel(db), + NodeModel: node.NewModel(db), AuthModel: auth.NewModel(db, rds), UserModel: user.NewModel(db, rds), OrderModel: order.NewModel(db, rds), diff --git a/internal/types/types.go b/internal/types/types.go index f173018..73125fe 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -128,6 +128,15 @@ type AuthMethodConfig struct { Enabled bool `json:"enabled"` } +type BalanceLog struct { + Type uint16 `json:"type"` + UserId int64 `json:"user_id"` + Amount int64 `json:"amount"` + OrderId int64 `json:"order_id,omitempty"` + Balance int64 `json:"balance"` + Timestamp int64 `json:"timestamp"` +} + type BatchDeleteCouponRequest struct { Ids []int64 `json:"ids" validate:"required"` } @@ -136,14 +145,6 @@ type BatchDeleteDocumentRequest struct { Ids []int64 `json:"ids" validate:"required"` } -type BatchDeleteNodeGroupRequest struct { - Ids []int64 `json:"ids" validate:"required"` -} - -type BatchDeleteNodeRequest struct { - Ids []int64 `json:"ids" validate:"required"` -} - type BatchDeleteSubscribeGroupRequest struct { Ids []int64 `json:"ids" validate:"required"` } @@ -230,10 +231,10 @@ type CloseOrderRequest struct { } type CommissionLog struct { - Id int64 `json:"id"` + Type uint16 `json:"type"` UserId int64 `json:"user_id"` - OrderNo string `json:"order_no"` Amount int64 `json:"amount"` + OrderNo string `json:"order_no"` CreatedAt int64 `json:"created_at"` } @@ -303,26 +304,14 @@ type CreateDocumentRequest struct { Show *bool `json:"show"` } -type CreateNodeGroupRequest struct { - Name string `json:"name" validate:"required"` - Description string `json:"description"` -} - type CreateNodeRequest struct { - Name string `json:"name" validate:"required"` - Tags []string `json:"tags"` - Country string `json:"country"` - City string `json:"city"` - ServerAddr string `json:"server_addr" validate:"required"` - RelayMode string `json:"relay_mode"` - RelayNode []NodeRelay `json:"relay_node"` - SpeedLimit int `json:"speed_limit"` - TrafficRatio float32 `json:"traffic_ratio"` - GroupId int64 `json:"group_id"` - Protocol string `json:"protocol" validate:"required"` - Config interface{} `json:"config" validate:"required"` - Enable *bool `json:"enable"` - Sort int64 `json:"sort"` + Name string `json:"name"` + Tags []string `json:"tags,omitempty"` + Port uint16 `json:"port"` + Address string `json:"address"` + ServerId int64 `json:"server_id"` + Protocol string `json:"protocol"` + Enabled *bool `json:"enabled"` } type CreateOrderRequest struct { @@ -355,14 +344,14 @@ type CreatePaymentMethodRequest struct { Enable *bool `json:"enable" validate:"required"` } -type CreateRuleGroupRequest struct { - Name string `json:"name" validate:"required"` - Icon string `json:"icon"` - Type string `json:"type"` - Tags []string `json:"tags"` - Rules string `json:"rules"` - Default bool `json:"default"` - Enable bool `json:"enable"` +type CreateServerRequest struct { + Name string `json:"name"` + Country string `json:"country,omitempty"` + City string `json:"city,omitempty"` + Ratio float32 `json:"ratio"` + Address string `json:"address"` + Sort int `json:"sort,omitempty"` + Protocols []Protocol `json:"protocols"` } type CreateSubscribeApplicationRequest struct { @@ -479,20 +468,16 @@ type DeleteDocumentRequest struct { Id int64 `json:"id" validate:"required"` } -type DeleteNodeGroupRequest struct { - Id int64 `json:"id" validate:"required"` -} - type DeleteNodeRequest struct { - Id int64 `json:"id" validate:"required"` + Id int64 `json:"id"` } type DeletePaymentMethodRequest struct { Id int64 `json:"id" validate:"required"` } -type DeleteRuleGroupRequest struct { - Id int64 `json:"id" validate:"required"` +type DeleteServerRequest struct { + Id int64 `json:"id"` } type DeleteSubscribeApplicationRequest struct { @@ -559,6 +544,136 @@ type EmailAuthticateConfig struct { DomainSuffixList string `json:"domain_suffix_list"` } +type FilterBalanceLogRequest struct { + FilterLogParams + UserId int64 `form:"user_id,optional"` +} + +type FilterBalanceLogResponse struct { + Total int64 `json:"total"` + List []BalanceLog `json:"list"` +} + +type FilterCommissionLogRequest struct { + FilterLogParams + UserId int64 `form:"user_id,optional"` +} + +type FilterCommissionLogResponse struct { + Total int64 `json:"total"` + List []CommissionLog `json:"list"` +} + +type FilterEmailLogResponse struct { + Total int64 `json:"total"` + List []MessageLog `json:"list"` +} + +type FilterGiftLogRequest struct { + FilterLogParams + UserId int64 `form:"user_id,optional"` +} + +type FilterGiftLogResponse struct { + Total int64 `json:"total"` + List []GiftLog `json:"list"` +} + +type FilterLogParams struct { + Page int `form:"page"` + Size int `form:"size"` + Date string `form:"date,optional"` + Search string `form:"search,optional"` +} + +type FilterLoginLogRequest struct { + FilterLogParams + UserId int64 `form:"user_id,optional"` +} + +type FilterLoginLogResponse struct { + Total int64 `json:"total"` + List []LoginLog `json:"list"` +} + +type FilterMobileLogResponse struct { + Total int64 `json:"total"` + List []MessageLog `json:"list"` +} + +type FilterNodeListRequest struct { + Page int `form:"page"` + Size int `form:"size"` + Search string `form:"search,omitempty"` +} + +type FilterNodeListResponse struct { + Total int64 `json:"total"` + List []Node `json:"list"` +} + +type FilterRegisterLogRequest struct { + FilterLogParams + UserId int64 `form:"user_id,optional"` +} + +type FilterRegisterLogResponse struct { + Total int64 `json:"total"` + List []RegisterLog `json:"list"` +} + +type FilterResetSubscribeLogRequest struct { + FilterLogParams + UserSubscribeId int64 `form:"user_subscribe_id,optional"` +} + +type FilterResetSubscribeLogResponse struct { + Total int64 `json:"total"` + List []ResetSubscribeLog `json:"list"` +} + +type FilterServerListRequest struct { + Page int `form:"page"` + Size int `form:"size"` + Search string `form:"search,omitempty"` +} + +type FilterServerListResponse struct { + Total int64 `json:"total"` + List []Server `json:"list"` +} + +type FilterServerTrafficLogRequest struct { + FilterLogParams + ServerId int64 `form:"server_id,optional"` +} + +type FilterServerTrafficLogResponse struct { + Total int64 `json:"total"` + List []ServerTrafficLog `json:"list"` +} + +type FilterSubscribeLogRequest struct { + FilterLogParams + UserId int64 `form:"user_id,optional"` +} + +type FilterSubscribeLogResponse struct { + Total int64 `json:"total"` + List []SubscribeLog `json:"list"` +} + +type FilterSubscribeTrafficRequest struct { + FilterLogParams + UserId int64 `form:"user_id,optional"` + UserSubscribeId int64 `form:"user_subscribe_id,optional"` +} + +type FilterSubscribeTrafficResponse struct { + Total int64 `json:"total"` + List []UserSubscribeTrafficLog `json:"list"` +} + type Follow struct { Id int64 `json:"id"` TicketId int64 `json:"ticket_id"` @@ -712,36 +827,10 @@ type GetMessageLogListResponse struct { List []MessageLog `json:"list"` } -type GetNodeDetailRequest struct { - Id int64 `form:"id" validate:"required"` -} - -type GetNodeGroupListResponse struct { - Total int64 `json:"total"` - List []ServerGroup `json:"list"` -} - type GetNodeMultiplierResponse struct { Periods []TimePeriod `json:"periods"` } -type GetNodeServerListRequest struct { - Page int `form:"page" validate:"required"` - Size int `form:"size" validate:"required"` - Tags string `form:"tags,omitempty"` - GroupId int64 `form:"group_id,omitempty"` - Search string `form:"search,omitempty"` -} - -type GetNodeServerListResponse struct { - Total int64 `json:"total"` - List []Server `json:"list"` -} - -type GetNodeTagListResponse struct { - Tags []string `json:"tags"` -} - type GetOAuthMethodsResponse struct { Methods []UserAuthMethod `json:"methods"` } @@ -783,11 +872,6 @@ type GetPreSendEmailCountResponse struct { Count int64 `json:"count"` } -type GetRuleGroupResponse struct { - Total int64 `json:"total"` - List []ServerRuleGroup `json:"list"` -} - type GetServerConfigRequest struct { ServerCommon } @@ -798,6 +882,14 @@ type GetServerConfigResponse struct { Config interface{} `json:"config"` } +type GetServerProtocolsRequest struct { + Id int64 `form:"id"` +} + +type GetServerProtocolsResponse struct { + Protocols []Protocol `json:"protocols"` +} + type GetServerUserListRequest struct { ServerCommon } @@ -997,6 +1089,17 @@ type GetUserTicketListResponse struct { List []Ticket `json:"list"` } +type GiftLog struct { + Type uint16 `json:"type"` + UserId int64 `json:"user_id"` + OrderNo string `json:"order_no"` + SubscribeId int64 `json:"subscribe_id"` + Amount int64 `json:"amount"` + Balance int64 `json:"balance"` + Remark string `json:"remark,omitempty"` + CreatedAt int64 `json:"created_at"` +} + type GoogleLoginCallbackRequest struct { Code string `form:"code"` State string `form:"state"` @@ -1024,6 +1127,15 @@ type LogResponse struct { List interface{} `json:"list"` } +type LoginLog struct { + UserId int64 `json:"user_id"` + Method string `json:"method"` + LoginIP string `json:"login_ip"` + UserAgent string `json:"user_agent"` + Success bool `json:"success"` + LoginTime int64 `json:"login_time"` +} + type LoginResponse struct { Token string `json:"token"` } @@ -1045,6 +1157,19 @@ type MobileAuthenticateConfig struct { Whitelist []string `json:"whitelist"` } +type Node struct { + Id int64 `json:"id"` + Name string `json:"name"` + Tags []string `json:"tags"` + Port uint16 `json:"port"` + Address string `json:"address"` + ServerId int64 `json:"server_id"` + Protocol string `json:"protocol"` + Enabled *bool `json:"enabled"` + CreatedAt int64 `json:"created_at"` + UpdatedAt int64 `json:"updated_at"` +} + type NodeConfig struct { NodeSecret string `json:"node_secret"` NodePullInterval int64 `json:"node_pull_interval"` @@ -1057,18 +1182,6 @@ type NodeRelay struct { Prefix string `json:"prefix"` } -type NodeSortRequest struct { - Sort []SortItem `json:"sort"` -} - -type NodeStatus struct { - Online interface{} `json:"online"` - Cpu float64 `json:"cpu"` - Mem float64 `json:"mem"` - Disk float64 `json:"disk"` - UpdatedAt int64 `json:"updated_at"` -} - type OAthLoginRequest struct { Method string `json:"method" validate:"required"` // google, facebook, apple, telegram, github etc. Redirect string `json:"redirect"` @@ -1263,6 +1376,34 @@ type PrivacyPolicyConfig struct { PrivacyPolicy string `json:"privacy_policy"` } +type Protocol struct { + Type string `json:"type"` + Port uint16 `json:"port"` + Security string `json:"security,omitempty"` + SNI string `json:"sni,omitempty"` + AllowInsecure bool `json:"allow_insecure,omitempty"` + Fingerprint string `json:"fingerprint,omitempty"` + RealityServerAddr string `json:"reality_server_addr,omitempty"` + RealityServerPort int `json:"reality_server_port,omitempty"` + RealityPrivateKey string `json:"reality_private_key,omitempty"` + RealityPublicKey string `json:"reality_public_key,omitempty"` + RealityShortId string `json:"reality_short_id,omitempty"` + Transport string `json:"transport,omitempty"` + Host string `json:"host,omitempty"` + Path string `json:"path,omitempty"` + ServiceName string `json:"service_name,omitempty"` + Cipher string `json:"cipher,omitempty"` + ServerKey string `json:"server_key,omitempty"` + Flow string `json:"flow,omitempty"` + HopPorts string `json:"hop_ports,omitempty"` + HopInterval int `json:"hop_interval,omitempty"` + ObfsPassword string `json:"obfs_password,omitempty"` + DisableSNI bool `json:"disable_sni,omitempty"` + ReduceRtt bool `json:"reduce_rtt,omitempty"` + UDPRelayMode string `json:"udp_relay_mode,omitempty"` + CongestionController string `json:"congestion_controller,omitempty"` +} + type PubilcRegisterConfig struct { StopRegister bool `json:"stop_register"` EnableIpRegisterLimit bool `json:"enable_ip_register_limit"` @@ -1368,8 +1509,8 @@ type QueryUserAffiliateListResponse struct { } type QueryUserBalanceLogListResponse struct { - List []UserBalanceLog `json:"list"` - Total int64 `json:"total"` + List []BalanceLog `json:"list"` + Total int64 `json:"total"` } type QueryUserCommissionLogListRequest struct { @@ -1407,6 +1548,15 @@ type RegisterConfig struct { IpRegisterLimitDuration int64 `json:"ip_register_limit_duration"` } +type RegisterLog struct { + UserId int64 `json:"user_id"` + AuthMethod string `json:"auth_method"` + Identifier string `json:"identifier"` + RegisterIP string `json:"register_ip"` + UserAgent string `json:"user_agent"` + RegisterTime int64 `json:"register_time"` +} + type RenewalOrderRequest struct { UserSubscribeID int64 `json:"user_subscribe_id"` Quantity int64 `json:"quantity"` @@ -1427,9 +1577,17 @@ type ResetPasswordRequest struct { CfToken string `json:"cf_token,optional"` } +type ResetSubscribeLog struct { + Type uint16 `json:"type"` + UserId int64 `json:"user_id"` + UserSubscribeId int64 `json:"user_subscribe_id"` + OrderNo string `json:"order_no,omitempty"` + ResetAt int64 `json:"reset_at"` +} + type ResetSubscribeTrafficLog struct { Id int64 `json:"id"` - Type uint8 `json:"type"` + Type uint16 `json:"type"` UserSubscribeId int64 `json:"user_subscribe_id"` OrderNo string `json:"order_no,omitempty"` ResetAt int64 `json:"reset_at"` @@ -1482,24 +1640,18 @@ type SendSmsCodeRequest struct { } type Server struct { - Id int64 `json:"id"` - Tags []string `json:"tags"` - Country string `json:"country"` - City string `json:"city"` - Name string `json:"name"` - ServerAddr string `json:"server_addr"` - RelayMode string `json:"relay_mode"` - RelayNode []NodeRelay `json:"relay_node"` - SpeedLimit int `json:"speed_limit"` - TrafficRatio float32 `json:"traffic_ratio"` - GroupId int64 `json:"group_id"` - Protocol string `json:"protocol"` - Config interface{} `json:"config"` - Enable *bool `json:"enable"` - CreatedAt int64 `json:"created_at"` - UpdatedAt int64 `json:"updated_at"` - Status *NodeStatus `json:"status"` - Sort int64 `json:"sort"` + Id int64 `json:"id"` + Name string `json:"name"` + Country string `json:"country"` + City string `json:"city"` + Ratio float32 `json:"ratio"` + Address string `json:"address"` + Sort int `json:"sort"` + Protocols []Protocol `json:"protocols"` + LastReportedAt int64 `json:"last_reported_at"` + Status ServerStatus `json:"status"` + CreatedAt int64 `json:"created_at"` + UpdatedAt int64 `json:"updated_at"` } type ServerBasic struct { @@ -1521,6 +1673,15 @@ type ServerGroup struct { UpdatedAt int64 `json:"updated_at"` } +type ServerOnlineUser struct { + IP []string `json:"ip"` + UserId int64 `json:"user_id"` + Subscribe string `json:"subscribe"` + SubscribeId int64 `json:"subscribe_id"` + Traffic int64 `json:"traffic"` + ExpiredAt int64 `json:"expired_at"` +} + type ServerPushStatusRequest struct { ServerCommon Cpu float64 `json:"cpu"` @@ -1547,6 +1708,13 @@ type ServerRuleGroup struct { UpdatedAt int64 `json:"updated_at"` } +type ServerStatus struct { + Online []ServerOnlineUser `json:"online"` + Cpu float64 `json:"cpu"` + Mem float64 `json:"mem"` + Disk float64 `json:"disk"` +} + type ServerTotalDataResponse struct { OnlineUserIPs int64 `json:"online_user_ips"` OnlineServers int64 `json:"online_servers"` @@ -1569,6 +1737,14 @@ type ServerTrafficData struct { Download int64 `json:"download"` } +type ServerTrafficLog struct { + ServerId int64 `json:"server_id"` // Server ID + Upload int64 `json:"upload"` // Upload traffic in bytes + Download int64 `json:"download"` // Download traffic in bytes + Total int64 `json:"total"` // Total traffic in bytes (Upload + Download) + Date string `json:"date"` // Date in YYYY-MM-DD format +} + type ServerUser struct { Id int64 `json:"id"` UUID string `json:"uuid"` @@ -1701,6 +1877,15 @@ type SubscribeItem struct { Sold int64 `json:"sold"` } +type SubscribeLog struct { + UserId int64 `json:"user_id"` + Token string `json:"token"` + UserAgent string `json:"user_agent"` + ClientIP string `json:"client_ip"` + UserSubscribeId int64 `json:"user_subscribe_id"` + SubscribedAt int64 `json:"subscribed_at"` +} + type SubscribeSortRequest struct { Sort []SortItem `json:"sort"` } @@ -1786,6 +1971,11 @@ type TimePeriod struct { Multiplier float32 `json:"multiplier"` } +type ToggleNodeStatusRequest struct { + Id int64 `json:"id"` + Enable *bool `json:"enable"` +} + type TosConfig struct { TosContent string `json:"tos_content"` } @@ -1906,28 +2096,15 @@ type UpdateDocumentRequest struct { Show *bool `json:"show"` } -type UpdateNodeGroupRequest struct { - Id int64 `json:"id" validate:"required"` - Name string `json:"name" validate:"required"` - Description string `json:"description"` -} - type UpdateNodeRequest struct { - Id int64 `json:"id" validate:"required"` - Tags []string `json:"tags"` - Country string `json:"country"` - City string `json:"city"` - Name string `json:"name" validate:"required"` - ServerAddr string `json:"server_addr" validate:"required"` - RelayMode string `json:"relay_mode"` - RelayNode []NodeRelay `json:"relay_node"` - SpeedLimit int `json:"speed_limit"` - TrafficRatio float32 `json:"traffic_ratio"` - GroupId int64 `json:"group_id"` - Protocol string `json:"protocol" validate:"required"` - Config interface{} `json:"config" validate:"required"` - Enable *bool `json:"enable"` - Sort int64 `json:"sort"` + Id int64 `json:"id"` + Name string `json:"name"` + Tags []string `json:"tags,omitempty"` + Port uint16 `json:"port"` + Address string `json:"address"` + ServerId int64 `json:"server_id"` + Protocol string `json:"protocol"` + Enabled *bool `json:"enabled"` } type UpdateOrderStatusRequest struct { @@ -1951,15 +2128,15 @@ type UpdatePaymentMethodRequest struct { Enable *bool `json:"enable" validate:"required"` } -type UpdateRuleGroupRequest struct { - Id int64 `json:"id" validate:"required"` - Icon string `json:"icon"` - Type string `json:"type"` - Name string `json:"name" validate:"required"` - Tags []string `json:"tags"` - Rules string `json:"rules"` - Default bool `json:"default"` - Enable bool `json:"enable"` +type UpdateServerRequest struct { + Id int64 `json:"id"` + Name string `json:"name"` + Country string `json:"country,omitempty"` + City string `json:"city,omitempty"` + Ratio float32 `json:"ratio"` + Address string `json:"address"` + Sort int `json:"sort,omitempty"` + Protocols []Protocol `json:"protocols"` } type UpdateSubscribeApplicationRequest struct { @@ -2100,16 +2277,6 @@ type UserAuthMethod struct { Verified bool `json:"verified"` } -type UserBalanceLog struct { - Id int64 `json:"id"` - UserId int64 `json:"user_id"` - Amount int64 `json:"amount"` - Type uint8 `json:"type"` - OrderId int64 `json:"order_id"` - Balance int64 `json:"balance"` - CreatedAt int64 `json:"created_at"` -} - type UserDevice struct { Id int64 `json:"id"` Ip string `json:"ip"` @@ -2210,6 +2377,15 @@ type UserSubscribeLog struct { CreatedAt int64 `json:"created_at"` } +type UserSubscribeTrafficLog struct { + SubscribeId int64 `json:"subscribe_id"` // Subscribe ID + UserId int64 `json:"user_id"` // User ID + Upload int64 `json:"upload"` // Upload traffic in bytes + Download int64 `json:"download"` // Download traffic in bytes + Total int64 `json:"total"` // Total traffic in bytes (Upload + Download) + Date string `json:"date"` // Date in YYYY-MM-DD format +} + type UserTraffic struct { SID int64 `json:"uid"` Upload int64 `json:"upload"` diff --git a/pkg/orm/tool_test.go b/pkg/orm/tool_test.go index f73e62b..e63e078 100644 --- a/pkg/orm/tool_test.go +++ b/pkg/orm/tool_test.go @@ -3,7 +3,7 @@ package orm import ( "testing" - "github.com/perfect-panel/server/internal/model/log" + "github.com/perfect-panel/server/internal/model/node" "gorm.io/driver/mysql" "gorm.io/gorm" @@ -31,7 +31,7 @@ func TestMysql(t *testing.T) { if err != nil { t.Fatalf("Failed to connect to MySQL: %v", err) } - err = db.Migrator().AutoMigrate(&log.SystemLog{}) + err = db.Migrator().AutoMigrate(&node.Server{}) if err != nil { t.Fatalf("Failed to auto migrate: %v", err) return diff --git a/queue/logic/order/activateOrderLogic.go b/queue/logic/order/activateOrderLogic.go index 5f77930..bb02e4c 100644 --- a/queue/logic/order/activateOrderLogic.go +++ b/queue/logic/order/activateOrderLogic.go @@ -376,7 +376,7 @@ func (l *ActivateOrderLogic) handleCommission(ctx context.Context, userInfo *use return err } - var commissionType uint8 + var commissionType uint16 switch orderInfo.Type { case OrderTypeSubscribe: commissionType = log.CommissionTypePurchase @@ -593,6 +593,7 @@ func (l *ActivateOrderLogic) ResetTraffic(ctx context.Context, orderInfo *order. // insert reset traffic log resetLog := &log.ResetSubscribe{ Type: log.ResetSubscribeTypePaid, + UserId: userInfo.Id, OrderNo: orderInfo.OrderNo, ResetAt: time.Now().UnixMilli(), } diff --git a/queue/logic/traffic/resetTrafficLogic.go b/queue/logic/traffic/resetTrafficLogic.go index db33be7..ef1956c 100644 --- a/queue/logic/traffic/resetTrafficLogic.go +++ b/queue/logic/traffic/resetTrafficLogic.go @@ -594,19 +594,20 @@ func (l *ResetTrafficLogic) clearCache(ctx context.Context, list []*user.Subscri } } // Insert traffic reset log - l.insertLog(ctx, sub.Id) + l.insertLog(ctx, sub.Id, sub.UserId) } } } // insertLog inserts a reset traffic log entry -func (l *ResetTrafficLogic) insertLog(ctx context.Context, subId int64) { +func (l *ResetTrafficLogic) insertLog(ctx context.Context, subId, userId int64) { trafficLog := log.ResetSubscribe{ Type: log.ResetSubscribeTypeAuto, + UserId: userId, ResetAt: time.Now().UnixMilli(), } content, _ := trafficLog.Marshal() - if err := l.svc.DB.Model(&log.SystemLog{}).Create(&log.SystemLog{ + if err := l.svc.DB.WithContext(ctx).Model(&log.SystemLog{}).Create(&log.SystemLog{ Type: log.TypeResetSubscribe.Uint8(), ObjectID: subId, Date: time.Now().Format(time.DateOnly), diff --git a/scheduler/scheduler.go b/scheduler/scheduler.go index 7c691ef..377dca3 100644 --- a/scheduler/scheduler.go +++ b/scheduler/scheduler.go @@ -29,11 +29,11 @@ func (m *Service) Start() { if _, err := m.server.Register("@every 60s", checkTask); err != nil { logger.Errorf("register check subscription task failed: %s", err.Error()) } - // schedule total server data task: every 5 minutes - totalServerDataTask := asynq.NewTask(types.SchedulerTotalServerData, nil) - if _, err := m.server.Register("@every 180s", totalServerDataTask); err != nil { - logger.Errorf("register total server data task failed: %s", err.Error()) - } + //// schedule total server data task: every 5 minutes + //totalServerDataTask := asynq.NewTask(types.SchedulerTotalServerData, nil) + //if _, err := m.server.Register("@every 180s", totalServerDataTask); err != nil { + // logger.Errorf("register total server data task failed: %s", err.Error()) + //} // schedule reset traffic task: every day at 00:30 resetTrafficTask := asynq.NewTask(types.SchedulerResetTraffic, nil) if _, err := m.server.Register("30 0 * * *", resetTrafficTask); err != nil {