package lb import ( "encoding/json" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/userconfigs" "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" "strings" ) type IndexAction struct { actionutils.ParentAction } func (this *IndexAction) Init() { this.Nav("", "", "index") } func (this *IndexAction) RunGet(params struct{}) { // 更新用户可用状态 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 } var supportTCP = this.ValidateFeature(userconfigs.UserFeatureCodeServerTCP, 0) var supportUDP = this.ValidateFeature(userconfigs.UserFeatureCodeServerUDP, 0) if !supportTCP && !supportUDP { return } this.Data["supportTCP"] = supportTCP this.Data["supportUDP"] = supportUDP var protocols = []string{} if supportTCP { protocols = append(protocols, "tcp") } if supportUDP { protocols = append(protocols, "udp") } countResp, err := this.RPC().ServerRPC().CountAllEnabledServersMatch(this.UserContext(), &pb.CountAllEnabledServersMatchRequest{ ServerGroupId: 0, Keyword: "", UserId: this.UserId(), ProtocolFamily: strings.Join(protocols, ","), }) 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: 0, Keyword: "", ProtocolFamily: strings.Join(protocols, ","), UserId: this.UserId(), IgnoreSSLCerts: true, }) if err != nil { this.ErrorPage(err) return } var serverMaps = []maps.Map{} for _, server := range serversResp.Servers { // 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 + "." } } } // TCP var tcpPorts = []string{} if len(server.TcpJSON) > 0 { config, err := serverconfigs.NewTCPProtocolConfigFromJSON(server.TcpJSON) if err != nil { this.ErrorPage(err) return } if config.IsOn { for _, listen := range config.Listen { tcpPorts = append(tcpPorts, listen.PortRange) } } } // TLS var tlsPorts = []string{} if len(server.TlsJSON) > 0 { config, err := serverconfigs.NewTLSProtocolConfigFromJSON(server.TlsJSON) if err != nil { this.ErrorPage(err) return } if config.IsOn { for _, listen := range config.Listen { tlsPorts = append(tlsPorts, listen.PortRange) } } } // UDP var udpPorts = []string{} if len(server.UdpJSON) > 0 { config, err := serverconfigs.NewUDPProtocolConfigFromJSON(server.UdpJSON) if err != nil { this.ErrorPage(err) return } if config.IsOn { for _, listen := range config.Listen { udpPorts = append(udpPorts, listen.PortRange) } } } // 套餐 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 trafficLimitStatus *serverconfigs.TrafficLimitStatus if len(server.Config) > 0 { var serverConfig = &serverconfigs.ServerConfig{} err = json.Unmarshal(server.Config, serverConfig) if err == nil { if serverConfig.TrafficLimitStatus != nil && serverConfig.TrafficLimitStatus.IsValid() { trafficLimitStatus = serverConfig.TrafficLimitStatus } } } serverMaps = append(serverMaps, maps.Map{ "id": server.Id, "name": server.Name, "cname": cname, "tcpPorts": tcpPorts, "tlsPorts": tlsPorts, "udpPorts": udpPorts, "isOn": server.IsOn, "userPlan": userPlanMap, "trafficLimitStatus": trafficLimitStatus, }) } this.Data["servers"] = serverMaps this.Show() }