Files
waf-platform/EdgeUser/internal/web/actions/default/servers/index.go
2026-02-04 20:27:13 +08:00

236 lines
6.5 KiB
Go

package servers
import (
"encoding/json"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeUser/internal/configloaders"
"github.com/TeaOSLab/EdgeUser/internal/utils"
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/maps"
timeutil "github.com/iwind/TeaGo/utils/time"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "", "index")
}
func (this *IndexAction) RunGet(params struct {
Keyword string
GroupId int64
}) {
// 检查用户状态
this.CheckUserStatus()
// 更新用户可用状态
stateResp, err := this.RPC().UserRPC().RenewUserServersState(this.UserContext(), &pb.RenewUserServersStateRequest{})
if err != nil {
this.ErrorPage(err)
return
}
this.Data["serversIsEnabled"] = stateResp.IsEnabled
// 提醒有逾期未支付的账单
this.Data["countUnpaidBills"] = 0
priceConfig, _ := configloaders.LoadCacheableUserPriceConfig()
if priceConfig != nil && priceConfig.IsOn && priceConfig.UnpaidBillPolicy.IsOn && (priceConfig.UnpaidBillPolicy.MinDailyBillDays > 0 || priceConfig.UnpaidBillPolicy.MinMonthlyBillDays > 0) {
countResp, err := this.RPC().UserBillRPC().CountAllUserBills(this.UserContext(), &pb.CountAllUserBillsRequest{
PaidFlag: 0,
UserId: this.UserId(),
Month: "",
TrafficRelated: true,
MinDailyBillDays: priceConfig.UnpaidBillPolicy.MinDailyBillDays,
MinMonthlyBillDays: priceConfig.UnpaidBillPolicy.MinMonthlyBillDays,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Data["countUnpaidBills"] = countResp.Count
}
this.Data["keyword"] = params.Keyword
this.Data["groupId"] = params.GroupId
// 分组
groupsResp, err := this.RPC().ServerGroupRPC().FindAllEnabledServerGroups(this.UserContext(), &pb.FindAllEnabledServerGroupsRequest{})
if err != nil {
this.ErrorPage(err)
return
}
var groupMaps = []maps.Map{}
for _, group := range groupsResp.ServerGroups {
groupMaps = append(groupMaps, maps.Map{
"id": group.Id,
"name": group.Name,
})
}
this.Data["groups"] = groupMaps
// 分页
countResp, err := this.RPC().ServerRPC().CountAllEnabledServersMatch(this.UserContext(), &pb.CountAllEnabledServersMatchRequest{
ServerGroupId: params.GroupId,
Keyword: params.Keyword,
UserId: this.UserId(),
ProtocolFamily: "http",
})
if err != nil {
this.ErrorPage(err)
return
}
var count = countResp.Count
var page = this.NewPage(count)
this.Data["page"] = page.AsHTML()
// 列表
serversResp, err := this.RPC().ServerRPC().ListEnabledServersMatch(this.UserContext(), &pb.ListEnabledServersMatchRequest{
Offset: page.Offset,
Size: page.Size,
ServerGroupId: params.GroupId,
Keyword: params.Keyword,
ProtocolFamily: "http",
UserId: this.UserId(),
IgnoreServerNames: true,
IgnoreSSLCerts: true,
})
if err != nil {
this.ErrorPage(err)
return
}
var serverMaps = []maps.Map{}
for _, server := range serversResp.Servers {
// 域名列表
var serverNames = []*serverconfigs.ServerNameConfig{}
if server.IsAuditing || (server.AuditingResult != nil && !server.AuditingResult.IsOk) {
server.ServerNamesJSON = server.AuditingServerNamesJSON
}
if len(server.ServerNamesJSON) > 0 {
err = json.Unmarshal(server.ServerNamesJSON, &serverNames)
if err != nil {
this.ErrorPage(err)
return
}
}
// 域名和限流状态
var trafficLimitStatus *serverconfigs.TrafficLimitStatus
if len(server.Config) > 0 {
var serverConfig = &serverconfigs.ServerConfig{}
err = json.Unmarshal(server.Config, serverConfig)
if err == nil {
if len(serverNames) == 0 {
serverNames = serverConfig.ServerNames
}
if serverConfig.TrafficLimitStatus != nil && serverConfig.TrafficLimitStatus.IsValid() {
trafficLimitStatus = serverConfig.TrafficLimitStatus
}
}
}
// CNAME
var cname = ""
if server.NodeCluster != nil {
clusterId := server.NodeCluster.Id
if clusterId > 0 {
dnsInfoResp, err := this.RPC().NodeClusterRPC().FindEnabledNodeClusterDNS(this.UserContext(), &pb.FindEnabledNodeClusterDNSRequest{NodeClusterId: clusterId})
if err != nil {
this.ErrorPage(err)
return
}
if dnsInfoResp.Domain != nil {
cname = server.DnsName + "." + dnsInfoResp.Domain.Name + "."
}
}
}
// HTTP
var httpIsOn = false
if len(server.HttpJSON) > 0 {
httpConfig, err := serverconfigs.NewHTTPProtocolConfigFromJSON(server.HttpJSON)
if err != nil {
this.ErrorPage(err)
return
}
httpIsOn = httpConfig.IsOn && len(httpConfig.Listen) > 0
}
// HTTPS
var httpsIsOn = false
if len(server.HttpsJSON) > 0 {
httpsConfig, err := serverconfigs.NewHTTPSProtocolConfigFromJSON(server.HttpsJSON)
if err != nil {
this.ErrorPage(err)
return
}
httpsIsOn = httpsConfig.IsOn && len(httpsConfig.Listen) > 0
}
// 套餐
var userPlanMap = maps.Map{"id": 0}
if server.UserPlanId > 0 {
userPlanResp, err := this.RPC().UserPlanRPC().FindEnabledUserPlan(this.UserContext(), &pb.FindEnabledUserPlanRequest{
UserPlanId: server.UserPlanId,
})
if err != nil {
if !utils.IsNotFound(err) {
this.ErrorPage(err)
return
}
} else {
var userPlan = userPlanResp.UserPlan
if userPlan != nil && userPlan.Plan != nil {
if len(userPlan.Name) == 0 {
userPlan.Name = userPlan.Plan.Name
}
userPlanMap = maps.Map{
"id": userPlan.Id,
"name": userPlan.Name,
"dayTo": userPlan.DayTo,
"isExpired": userPlan.DayTo <= timeutil.Format("Y-m-d"),
}
}
}
}
// 分组
var serverGroupMaps = []maps.Map{}
for _, group := range server.ServerGroups {
if group.UserId != this.UserId() {
continue
}
serverGroupMaps = append(serverGroupMaps, maps.Map{
"id": group.Id,
"name": group.Name,
})
}
serverMaps = append(serverMaps, maps.Map{
"id": server.Id,
"name": server.Name,
"serverNames": serverNames,
"countServerNames": server.CountServerNames,
"isAuditing": false,
"cname": cname,
"httpIsOn": httpIsOn,
"httpsIsOn": httpsIsOn,
"status": maps.Map{
"isOk": false,
"message": "",
"type": "",
},
"isOn": server.IsOn,
"userPlan": userPlanMap,
"groups": serverGroupMaps,
"trafficLimitStatus": trafficLimitStatus,
})
}
this.Data["servers"] = serverMaps
this.Show()
}