2020-11-24 17:36:42 +08:00
|
|
|
|
package certs
|
2020-10-01 16:01:04 +08:00
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"encoding/json"
|
2023-03-25 20:50:27 +08:00
|
|
|
|
"errors"
|
2020-12-04 09:25:35 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/utils/numberutils"
|
2020-10-01 16:01:04 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
|
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
2023-03-25 20:50:27 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
2020-10-01 16:01:04 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/sslconfigs"
|
2020-12-04 09:25:35 +08:00
|
|
|
|
"github.com/iwind/TeaGo/lists"
|
2020-10-01 16:01:04 +08:00
|
|
|
|
"github.com/iwind/TeaGo/maps"
|
|
|
|
|
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
2020-12-04 09:25:35 +08:00
|
|
|
|
"strings"
|
2020-10-01 16:01:04 +08:00
|
|
|
|
"time"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2021-05-23 20:54:17 +08:00
|
|
|
|
// SelectPopupAction 选择证书
|
2020-10-01 16:01:04 +08:00
|
|
|
|
type SelectPopupAction struct {
|
|
|
|
|
|
actionutils.ParentAction
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *SelectPopupAction) Init() {
|
|
|
|
|
|
this.Nav("", "", "")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-15 16:41:32 +08:00
|
|
|
|
func (this *SelectPopupAction) RunGet(params struct {
|
2023-03-25 20:50:27 +08:00
|
|
|
|
ServerId int64 // 搜索的服务
|
|
|
|
|
|
UserId int64 // 搜索的用户名
|
|
|
|
|
|
SearchingDomains string // 搜索的域名
|
|
|
|
|
|
SearchingType string // 搜索类型:match|all
|
|
|
|
|
|
|
2020-12-04 09:25:35 +08:00
|
|
|
|
ViewSize string
|
|
|
|
|
|
SelectedCertIds string
|
2021-05-23 20:54:17 +08:00
|
|
|
|
Keyword string
|
2020-10-15 16:41:32 +08:00
|
|
|
|
}) {
|
2023-03-25 20:50:27 +08:00
|
|
|
|
// 服务相关
|
|
|
|
|
|
if params.ServerId > 0 {
|
|
|
|
|
|
serverResp, err := this.RPC().ServerRPC().FindEnabledUserServerBasic(this.AdminContext(), &pb.FindEnabledUserServerBasicRequest{ServerId: params.ServerId})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
var server = serverResp.Server
|
|
|
|
|
|
if server != nil {
|
|
|
|
|
|
if server.UserId > 0 {
|
|
|
|
|
|
params.UserId = server.UserId
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 读取所有ServerNames
|
|
|
|
|
|
serverNamesResp, err := this.RPC().ServerRPC().FindServerNames(this.AdminContext(), &pb.FindServerNamesRequest{ServerId: params.ServerId})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(serverNamesResp.ServerNamesJSON) > 0 {
|
|
|
|
|
|
var serverNames = []*serverconfigs.ServerNameConfig{}
|
|
|
|
|
|
err = json.Unmarshal(serverNamesResp.ServerNamesJSON, &serverNames)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
params.SearchingDomains = strings.Join(serverconfigs.PlainServerNames(serverNames), ",")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 用户相关
|
|
|
|
|
|
this.Data["userId"] = params.UserId
|
|
|
|
|
|
|
|
|
|
|
|
// 域名搜索相关
|
|
|
|
|
|
var url = this.Request.URL.Path
|
|
|
|
|
|
var query = this.Request.URL.Query()
|
|
|
|
|
|
query.Del("searchingType")
|
|
|
|
|
|
this.Data["baseURL"] = url + "?" + query.Encode()
|
|
|
|
|
|
|
|
|
|
|
|
var searchingDomains = []string{}
|
|
|
|
|
|
if len(params.SearchingDomains) > 0 {
|
|
|
|
|
|
searchingDomains = strings.Split(params.SearchingDomains, ",")
|
|
|
|
|
|
}
|
|
|
|
|
|
const maxDomains = 2_000 // 限制搜索的域名数量
|
|
|
|
|
|
if len(searchingDomains) > maxDomains {
|
|
|
|
|
|
searchingDomains = searchingDomains[:maxDomains]
|
|
|
|
|
|
}
|
|
|
|
|
|
this.Data["searchingDomains"] = searchingDomains
|
2020-12-04 09:25:35 +08:00
|
|
|
|
|
2021-05-23 20:54:17 +08:00
|
|
|
|
this.Data["keyword"] = params.Keyword
|
2022-10-30 20:06:38 +08:00
|
|
|
|
this.Data["selectedCertIds"] = params.SelectedCertIds
|
2021-05-23 20:54:17 +08:00
|
|
|
|
|
2023-03-25 20:50:27 +08:00
|
|
|
|
var searchingType = params.SearchingType
|
|
|
|
|
|
if len(searchingType) == 0 {
|
|
|
|
|
|
if len(params.SearchingDomains) == 0 {
|
|
|
|
|
|
searchingType = "all"
|
|
|
|
|
|
} else {
|
|
|
|
|
|
searchingType = "match"
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if searchingType != "all" && searchingType != "match" {
|
|
|
|
|
|
this.ErrorPage(errors.New("invalid searching type '" + searchingType + "'"))
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
this.Data["searchingType"] = searchingType
|
|
|
|
|
|
|
2020-12-04 09:25:35 +08:00
|
|
|
|
// 已经选择的证书
|
2022-10-30 20:06:38 +08:00
|
|
|
|
var selectedCertIds = []string{}
|
2020-12-04 09:25:35 +08:00
|
|
|
|
if len(params.SelectedCertIds) > 0 {
|
|
|
|
|
|
selectedCertIds = strings.Split(params.SelectedCertIds, ",")
|
|
|
|
|
|
}
|
2020-10-01 16:01:04 +08:00
|
|
|
|
|
2020-10-15 16:41:32 +08:00
|
|
|
|
if len(params.ViewSize) == 0 {
|
|
|
|
|
|
params.ViewSize = "normal"
|
|
|
|
|
|
}
|
|
|
|
|
|
this.Data["viewSize"] = params.ViewSize
|
|
|
|
|
|
|
2023-03-25 20:50:27 +08:00
|
|
|
|
// 全部证书数量
|
|
|
|
|
|
countAllResp, err := this.RPC().SSLCertRPC().CountSSLCerts(this.AdminContext(), &pb.CountSSLCertRequest{
|
|
|
|
|
|
UserId: params.UserId,
|
2021-05-23 20:54:17 +08:00
|
|
|
|
Keyword: params.Keyword,
|
|
|
|
|
|
})
|
2020-10-01 16:01:04 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2023-03-25 20:50:27 +08:00
|
|
|
|
var totalAll = countAllResp.Count
|
|
|
|
|
|
this.Data["totalAll"] = totalAll
|
2020-10-01 16:01:04 +08:00
|
|
|
|
|
2023-03-25 20:50:27 +08:00
|
|
|
|
// 已匹配证书数量
|
|
|
|
|
|
var totalMatch int64 = 0
|
|
|
|
|
|
if len(searchingDomains) > 0 {
|
|
|
|
|
|
countMatchResp, err := this.RPC().SSLCertRPC().CountSSLCerts(this.AdminContext(), &pb.CountSSLCertRequest{
|
|
|
|
|
|
UserId: params.UserId,
|
|
|
|
|
|
Keyword: params.Keyword,
|
|
|
|
|
|
Domains: searchingDomains,
|
|
|
|
|
|
})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
totalMatch = countMatchResp.Count
|
|
|
|
|
|
}
|
|
|
|
|
|
this.Data["totalMatch"] = totalMatch
|
|
|
|
|
|
|
|
|
|
|
|
var totalCerts int64
|
|
|
|
|
|
if searchingType == "all" {
|
|
|
|
|
|
totalCerts = totalAll
|
|
|
|
|
|
} else if searchingType == "match" {
|
|
|
|
|
|
totalCerts = totalMatch
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var page = this.NewPage(totalCerts)
|
2020-10-01 16:01:04 +08:00
|
|
|
|
this.Data["page"] = page.AsHTML()
|
|
|
|
|
|
|
2023-03-25 20:50:27 +08:00
|
|
|
|
var listResp *pb.ListSSLCertsResponse
|
|
|
|
|
|
if searchingType == "all" {
|
|
|
|
|
|
listResp, err = this.RPC().SSLCertRPC().ListSSLCerts(this.AdminContext(), &pb.ListSSLCertsRequest{
|
|
|
|
|
|
UserId: params.UserId,
|
|
|
|
|
|
Keyword: params.Keyword,
|
|
|
|
|
|
Offset: page.Offset,
|
|
|
|
|
|
Size: page.Size,
|
|
|
|
|
|
})
|
|
|
|
|
|
} else if searchingType == "match" {
|
|
|
|
|
|
listResp, err = this.RPC().SSLCertRPC().ListSSLCerts(this.AdminContext(), &pb.ListSSLCertsRequest{
|
|
|
|
|
|
UserId: params.UserId,
|
|
|
|
|
|
Keyword: params.Keyword,
|
|
|
|
|
|
Domains: searchingDomains,
|
|
|
|
|
|
Offset: page.Offset,
|
|
|
|
|
|
Size: page.Size,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if listResp == nil {
|
|
|
|
|
|
this.ErrorPage(errors.New("'listResp' should not be nil"))
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2020-10-01 16:01:04 +08:00
|
|
|
|
|
2023-03-25 20:50:27 +08:00
|
|
|
|
var certConfigs = []*sslconfigs.SSLCertConfig{}
|
2020-12-18 21:18:35 +08:00
|
|
|
|
err = json.Unmarshal(listResp.SslCertsJSON, &certConfigs)
|
2020-10-01 16:01:04 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
this.Data["certs"] = certConfigs
|
|
|
|
|
|
|
2023-03-25 20:50:27 +08:00
|
|
|
|
var certMaps = []maps.Map{}
|
|
|
|
|
|
var nowTime = time.Now().Unix()
|
2020-10-01 16:01:04 +08:00
|
|
|
|
for _, certConfig := range certConfigs {
|
2020-12-18 21:18:35 +08:00
|
|
|
|
countServersResp, err := this.RPC().ServerRPC().CountAllEnabledServersWithSSLCertId(this.AdminContext(), &pb.CountAllEnabledServersWithSSLCertIdRequest{SslCertId: certConfig.Id})
|
2020-10-01 16:01:04 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
certMaps = append(certMaps, maps.Map{
|
|
|
|
|
|
"beginDay": timeutil.FormatTime("Y-m-d", certConfig.TimeBeginAt),
|
|
|
|
|
|
"endDay": timeutil.FormatTime("Y-m-d", certConfig.TimeEndAt),
|
|
|
|
|
|
"isExpired": nowTime > certConfig.TimeEndAt,
|
|
|
|
|
|
"isAvailable": nowTime <= certConfig.TimeEndAt,
|
|
|
|
|
|
"countServers": countServersResp.Count,
|
2020-12-04 09:25:35 +08:00
|
|
|
|
"isSelected": lists.ContainsString(selectedCertIds, numberutils.FormatInt64(certConfig.Id)),
|
2020-10-01 16:01:04 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
this.Data["certInfos"] = certMaps
|
|
|
|
|
|
|
|
|
|
|
|
this.Show()
|
|
|
|
|
|
}
|