204 lines
5.3 KiB
Go
204 lines
5.3 KiB
Go
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
|
|
package clusters
|
|
|
|
import (
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
|
|
"github.com/TeaOSLab/EdgeUser/internal/web/actions/default/ns/nsutils"
|
|
"github.com/iwind/TeaGo/lists"
|
|
"github.com/iwind/TeaGo/maps"
|
|
"github.com/iwind/TeaGo/types"
|
|
)
|
|
|
|
type OptionsAction struct {
|
|
actionutils.ParentAction
|
|
}
|
|
|
|
func (this *OptionsAction) RunPost(params struct {
|
|
ClusterId int64
|
|
DomainId int64
|
|
UserId int64
|
|
}) {
|
|
var routeMaps = []maps.Map{}
|
|
|
|
planConfig, err := nsutils.FindUserPlanConfig(this.UserContext())
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
|
|
// 默认线路
|
|
for _, route := range dnsconfigs.AllDefaultRoutes {
|
|
routeMaps = append(routeMaps, maps.Map{
|
|
"name": route.Name,
|
|
"code": route.Code,
|
|
"type": "default",
|
|
})
|
|
}
|
|
|
|
// 自定义
|
|
routesResp, err := this.RPC().NSRouteRPC().FindAllNSRoutes(this.UserContext(), &pb.FindAllNSRoutesRequest{
|
|
NsClusterId: params.ClusterId,
|
|
NsDomainId: params.DomainId,
|
|
UserId: params.UserId,
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
|
|
for _, route := range routesResp.NsRoutes {
|
|
if len(route.Code) == 0 {
|
|
route.Code = "id:" + types.String(route.Id)
|
|
}
|
|
|
|
if !route.IsOn {
|
|
continue
|
|
}
|
|
|
|
routeMaps = append(routeMaps, maps.Map{
|
|
"name": route.Name,
|
|
"code": route.Code,
|
|
"type": "user",
|
|
})
|
|
}
|
|
|
|
// 运营商
|
|
this.Data["supportISPRoutes"] = false
|
|
if planConfig == nil || planConfig.SupportISPRoutes {
|
|
this.Data["supportISPRoutes"] = true
|
|
for _, route := range dnsconfigs.AllDefaultISPRoutes {
|
|
routeMaps = append(routeMaps, maps.Map{
|
|
"name": route.Name,
|
|
"code": route.Code,
|
|
"type": "isp",
|
|
})
|
|
}
|
|
}
|
|
|
|
// 中国省份
|
|
this.Data["supportChinaProvinceRoutes"] = false
|
|
if planConfig == nil || planConfig.SupportChinaProvinceRoutes {
|
|
this.Data["supportChinaProvinceRoutes"] = true
|
|
for _, route := range dnsconfigs.AllDefaultChinaProvinceRoutes {
|
|
routeMaps = append(routeMaps, maps.Map{
|
|
"name": route.Name,
|
|
"code": route.Code,
|
|
"type": "china",
|
|
})
|
|
}
|
|
}
|
|
|
|
// 全球
|
|
this.Data["supportWorldRegionRoutes"] = false
|
|
if planConfig == nil || planConfig.SupportCountryRoutes {
|
|
this.Data["supportWorldRegionRoutes"] = true
|
|
for _, route := range dnsconfigs.AllDefaultWorldRegionRoutes {
|
|
routeMaps = append(routeMaps, maps.Map{
|
|
"name": route.Name,
|
|
"code": route.Code,
|
|
"type": "world",
|
|
})
|
|
}
|
|
}
|
|
|
|
// 省份
|
|
provincesResp, err := this.RPC().RegionProvinceRPC().FindAllRegionProvinces(this.UserContext(), &pb.FindAllRegionProvincesRequest{})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
var provinceMaps = []maps.Map{}
|
|
for _, province := range provincesResp.RegionProvinces {
|
|
if province.RegionCountry == nil || len(province.RegionCountry.RouteCode) == 0 {
|
|
continue
|
|
}
|
|
provinceMaps = append(provinceMaps, maps.Map{
|
|
"id": province.Id,
|
|
"name": province.Name,
|
|
"code": "region:province:" + types.String(province.Id),
|
|
"type": "province",
|
|
"countryCode": province.RegionCountry.RouteCode,
|
|
})
|
|
}
|
|
this.Data["provinces"] = provinceMaps
|
|
|
|
// 搜索引擎
|
|
this.Data["supportAgentRoutes"] = false
|
|
if planConfig == nil || planConfig.SupportAgentRoutes {
|
|
this.Data["supportAgentRoutes"] = true
|
|
agentsResp, err := this.RPC().NSRouteRPC().FindAllAgentNSRoutes(this.UserContext(), &pb.FindAllAgentNSRoutesRequest{})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
for _, route := range agentsResp.NsRoutes {
|
|
routeMaps = append(routeMaps, maps.Map{
|
|
"name": route.Name,
|
|
"code": route.Code,
|
|
"type": "agent",
|
|
})
|
|
}
|
|
}
|
|
|
|
// 公共线路
|
|
this.Data["publicCategories"] = []maps.Map{}
|
|
this.Data["supportPublicRoutes"] = false
|
|
|
|
publicRoutesResp, err := this.RPC().NSRouteRPC().FindAllPublicNSRoutes(this.UserContext(), &pb.FindAllPublicRoutesRequest{})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
if len(publicRoutesResp.NsRoutes) == 0 {
|
|
this.Data["supportPublicRoutes"] = true
|
|
} else if planConfig == nil || planConfig.SupportPublicRoutes {
|
|
this.Data["supportPublicRoutes"] = true
|
|
|
|
var publicCategoryMaps = []maps.Map{}
|
|
var publicCategoryIds = []int64{}
|
|
for _, route := range publicRoutesResp.NsRoutes {
|
|
var categoryId int64 = 0
|
|
if route.NsRouteCategory != nil {
|
|
categoryId = route.NsRouteCategory.Id
|
|
}
|
|
|
|
routeMaps = append(routeMaps, maps.Map{
|
|
"name": route.Name,
|
|
"code": route.Code,
|
|
"type": "public:category:" + types.String(categoryId),
|
|
})
|
|
|
|
// 未分类
|
|
if route.NsRouteCategory == nil {
|
|
if !lists.ContainsInt64(publicCategoryIds, 0) {
|
|
publicCategoryIds = append(publicCategoryIds, 0)
|
|
publicCategoryMaps = append(publicCategoryMaps, maps.Map{
|
|
"id": 0,
|
|
"name": "官方线路",
|
|
"type": "public:category:" + types.String(0),
|
|
})
|
|
}
|
|
continue
|
|
}
|
|
|
|
// 有分类
|
|
if !lists.ContainsInt64(publicCategoryIds, categoryId) {
|
|
publicCategoryIds = append(publicCategoryIds, route.NsRouteCategory.Id)
|
|
publicCategoryMaps = append(publicCategoryMaps, maps.Map{
|
|
"id": route.NsRouteCategory.Id,
|
|
"name": route.NsRouteCategory.Name,
|
|
"type": "public:category:" + types.String(categoryId),
|
|
})
|
|
}
|
|
}
|
|
this.Data["publicCategories"] = publicCategoryMaps
|
|
}
|
|
|
|
this.Data["routes"] = routeMaps
|
|
|
|
this.Success()
|
|
}
|