mirror of
https://github.com/TeaOSLab/EdgeAdmin.git
synced 2026-04-26 15:25:17 +08:00
域名服务增加访问日志
This commit is contained in:
166
internal/web/actions/default/ns/clusters/accessLogs/index.go
Normal file
166
internal/web/actions/default/ns/clusters/accessLogs/index.go
Normal file
@@ -0,0 +1,166 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package logs
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/iwind/TeaGo/lists"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"net"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type IndexAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *IndexAction) Init() {
|
||||
this.Nav("", "", "")
|
||||
}
|
||||
|
||||
func (this *IndexAction) RunGet(params struct {
|
||||
RequestId string
|
||||
Day string
|
||||
}) {
|
||||
day := strings.ReplaceAll(params.Day, "-", "")
|
||||
if !regexp.MustCompile(`^\d{8}$`).MatchString(day) {
|
||||
day = timeutil.Format("Ymd")
|
||||
}
|
||||
|
||||
this.Data["day"] = day[:4] + "-" + day[4:6] + "-" + day[6:]
|
||||
this.Data["path"] = this.Request.URL.Path
|
||||
|
||||
var size = int64(10)
|
||||
|
||||
resp, err := this.RPC().NSAccessLogRPC().ListNSAccessLogs(this.AdminContext(), &pb.ListNSAccessLogsRequest{
|
||||
RequestId: params.RequestId,
|
||||
NsNodeId: 0,
|
||||
NsDomainId: 0,
|
||||
NsRecordId: 0,
|
||||
Size: size,
|
||||
Day: day,
|
||||
Reverse: false,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
ipList := []string{}
|
||||
nodeIds := []int64{}
|
||||
domainIds := []int64{}
|
||||
if len(resp.NsAccessLogs) == 0 {
|
||||
this.Data["accessLogs"] = []interface{}{}
|
||||
} else {
|
||||
this.Data["accessLogs"] = resp.NsAccessLogs
|
||||
for _, accessLog := range resp.NsAccessLogs {
|
||||
// IP
|
||||
if len(accessLog.RemoteAddr) > 0 {
|
||||
// 去掉端口
|
||||
ip, _, err := net.SplitHostPort(accessLog.RemoteAddr)
|
||||
if err == nil {
|
||||
accessLog.RemoteAddr = ip
|
||||
if !lists.ContainsString(ipList, ip) {
|
||||
ipList = append(ipList, ip)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 节点
|
||||
if !lists.ContainsInt64(nodeIds, accessLog.NsNodeId) {
|
||||
nodeIds = append(nodeIds, accessLog.NsNodeId)
|
||||
}
|
||||
|
||||
// 域名
|
||||
if !lists.ContainsInt64(domainIds, accessLog.NsDomainId) {
|
||||
domainIds = append(domainIds, accessLog.NsDomainId)
|
||||
}
|
||||
}
|
||||
}
|
||||
this.Data["hasMore"] = resp.HasMore
|
||||
this.Data["nextRequestId"] = resp.RequestId
|
||||
|
||||
// 上一个requestId
|
||||
this.Data["hasPrev"] = false
|
||||
this.Data["lastRequestId"] = ""
|
||||
if len(params.RequestId) > 0 {
|
||||
this.Data["hasPrev"] = true
|
||||
prevResp, err := this.RPC().NSAccessLogRPC().ListNSAccessLogs(this.AdminContext(), &pb.ListNSAccessLogsRequest{
|
||||
RequestId: params.RequestId,
|
||||
NsNodeId: 0,
|
||||
NsDomainId: 0,
|
||||
NsRecordId: 0,
|
||||
Day: day,
|
||||
Size: size,
|
||||
Reverse: true,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
if int64(len(prevResp.NsAccessLogs)) == size {
|
||||
this.Data["lastRequestId"] = prevResp.RequestId
|
||||
}
|
||||
}
|
||||
|
||||
// 根据IP查询区域
|
||||
regionMap := map[string]string{} // ip => region
|
||||
if len(ipList) > 0 {
|
||||
resp, err := this.RPC().IPLibraryRPC().LookupIPRegions(this.AdminContext(), &pb.LookupIPRegionsRequest{IpList: ipList})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
if resp.IpRegionMap != nil {
|
||||
for ip, region := range resp.IpRegionMap {
|
||||
regionMap[ip] = region.Summary
|
||||
}
|
||||
}
|
||||
}
|
||||
this.Data["regions"] = regionMap
|
||||
|
||||
// 节点信息
|
||||
nodeMap := map[int64]interface{}{} // node id => { ... }
|
||||
for _, nodeId := range nodeIds {
|
||||
nodeResp, err := this.RPC().NSNodeRPC().FindEnabledNSNode(this.AdminContext(), &pb.FindEnabledNSNodeRequest{NsNodeId: nodeId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
node := nodeResp.NsNode
|
||||
if node != nil {
|
||||
nodeMap[node.Id] = maps.Map{
|
||||
"id": node.Id,
|
||||
"name": node.Name,
|
||||
"cluster": maps.Map{
|
||||
"id": node.NsCluster.Id,
|
||||
"name": node.NsCluster.Name,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
this.Data["nodes"] = nodeMap
|
||||
|
||||
// 域名信息
|
||||
domainMap := map[int64]interface{}{} // domain id => { ... }
|
||||
for _, domainId := range domainIds {
|
||||
domainResp, err := this.RPC().NSDomainRPC().FindEnabledNSDomain(this.AdminContext(), &pb.FindEnabledNSDomainRequest{NsDomainId: domainId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
domain := domainResp.NsDomain
|
||||
if domain != nil {
|
||||
domainMap[domain.Id] = maps.Map{
|
||||
"id": domain.Id,
|
||||
"name": domain.Name,
|
||||
}
|
||||
}
|
||||
}
|
||||
this.Data["domains"] = domainMap
|
||||
|
||||
this.Show()
|
||||
}
|
||||
19
internal/web/actions/default/ns/clusters/accessLogs/init.go
Normal file
19
internal/web/actions/default/ns/clusters/accessLogs/init.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package logs
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
|
||||
"github.com/iwind/TeaGo"
|
||||
)
|
||||
|
||||
func init() {
|
||||
TeaGo.BeforeStart(func(server *TeaGo.Server) {
|
||||
server.
|
||||
Helper(helpers.NewUserMustAuth(configloaders.AdminModuleCodeNS)).
|
||||
Data("teaMenu", "ns").
|
||||
Data("teaSubMenu", "accessLog").
|
||||
Prefix("/ns/clusters/accessLogs").
|
||||
Get("", new(IndexAction)).
|
||||
EndAll()
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
)
|
||||
|
||||
type IndexAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *IndexAction) Init() {
|
||||
this.Nav("", "setting", "")
|
||||
this.SecondMenu("accessLog")
|
||||
}
|
||||
|
||||
func (this *IndexAction) RunGet(params struct {
|
||||
ClusterId int64
|
||||
}) {
|
||||
accessLogResp, err := this.RPC().NSClusterRPC().FindNSClusterAccessLog(this.AdminContext(), &pb.FindNSClusterAccessLogRequest{NsClusterId: params.ClusterId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
accessLogRef := &dnsconfigs.AccessLogRef{}
|
||||
if len(accessLogResp.AccessLogJSON) > 0 {
|
||||
err = json.Unmarshal(accessLogResp.AccessLogJSON, accessLogRef)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
this.Data["accessLogRef"] = accessLogRef
|
||||
|
||||
this.Show()
|
||||
}
|
||||
|
||||
func (this *IndexAction) RunPost(params struct {
|
||||
ClusterId int64
|
||||
AccessLogJSON []byte
|
||||
|
||||
Must *actions.Must
|
||||
CSRF *actionutils.CSRF
|
||||
}) {
|
||||
defer this.CreateLogInfo("修改域名服务集群 %d 访问日志配置", params.ClusterId)
|
||||
|
||||
ref := &dnsconfigs.AccessLogRef{}
|
||||
err := json.Unmarshal(params.AccessLogJSON, ref)
|
||||
if err != nil {
|
||||
this.Fail("数据格式错误:" + err.Error())
|
||||
}
|
||||
err = ref.Init()
|
||||
if err != nil {
|
||||
this.Fail("数据格式错误:" + err.Error())
|
||||
}
|
||||
|
||||
_, err = this.RPC().NSClusterRPC().UpdateNSClusterAccessLog(this.AdminContext(), &pb.UpdateNSClusterAccessLogRequest{
|
||||
NsClusterId: params.ClusterId,
|
||||
AccessLogJSON: params.AccessLogJSON,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.Success()
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/ns/clusters/clusterutils"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
|
||||
"github.com/iwind/TeaGo"
|
||||
)
|
||||
|
||||
func init() {
|
||||
TeaGo.BeforeStart(func(server *TeaGo.Server) {
|
||||
server.
|
||||
Helper(helpers.NewUserMustAuth(configloaders.AdminModuleCodeNS)).
|
||||
Helper(new(clusterutils.ClusterHelper)).
|
||||
Data("teaMenu", "ns").
|
||||
Data("teaSubMenu", "cluster").
|
||||
Prefix("/ns/clusters/cluster/settings/accessLog").
|
||||
GetPost("", new(IndexAction)).
|
||||
EndAll()
|
||||
})
|
||||
}
|
||||
@@ -83,5 +83,10 @@ func (this *ClusterHelper) createSettingMenu(cluster *pb.NSCluster, selectedItem
|
||||
"url": "/ns/clusters/cluster/settings?clusterId=" + clusterId,
|
||||
"isActive": selectedItem == "basic",
|
||||
})
|
||||
items = append(items, maps.Map{
|
||||
"name": "访问日志",
|
||||
"url": "/ns/clusters/cluster/settings/accessLog?clusterId=" + clusterId,
|
||||
"isActive": selectedItem == "accessLog",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user