2021-07-21 08:07:46 +08:00
|
|
|
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
|
|
|
|
|
|
|
|
package ipbox
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
2021-08-15 15:42:05 +08:00
|
|
|
"github.com/iwind/TeaGo/maps"
|
2021-07-21 08:07:46 +08:00
|
|
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
2022-08-21 20:47:29 +08:00
|
|
|
"strings"
|
2021-07-21 08:07:46 +08:00
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type IndexAction struct {
|
|
|
|
|
actionutils.ParentAction
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *IndexAction) Init() {
|
|
|
|
|
this.Nav("", "", "")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *IndexAction) RunGet(params struct {
|
|
|
|
|
Ip string
|
|
|
|
|
}) {
|
|
|
|
|
this.Data["ip"] = params.Ip
|
|
|
|
|
|
2021-08-15 15:42:05 +08:00
|
|
|
// IP信息
|
|
|
|
|
regionResp, err := this.RPC().IPLibraryRPC().LookupIPRegion(this.AdminContext(), &pb.LookupIPRegionRequest{Ip: params.Ip})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if regionResp.IpRegion != nil {
|
2022-08-21 20:47:29 +08:00
|
|
|
var regionName = regionResp.IpRegion.Summary
|
|
|
|
|
|
|
|
|
|
// remove isp from regionName
|
|
|
|
|
var index = strings.LastIndex(regionName, "|")
|
|
|
|
|
if index > 0 {
|
|
|
|
|
regionName = regionName[:index]
|
|
|
|
|
}
|
|
|
|
|
this.Data["regions"] = regionName
|
2021-08-15 15:42:05 +08:00
|
|
|
} else {
|
|
|
|
|
this.Data["regions"] = ""
|
|
|
|
|
}
|
|
|
|
|
this.Data["isp"] = regionResp.IpRegion.Isp
|
|
|
|
|
|
|
|
|
|
// IP列表
|
2021-12-02 17:11:44 +08:00
|
|
|
ipListResp, err := this.RPC().IPListRPC().FindEnabledIPListContainsIP(this.AdminContext(), &pb.FindEnabledIPListContainsIPRequest{
|
|
|
|
|
Ip: params.Ip,
|
|
|
|
|
})
|
2021-08-15 15:42:05 +08:00
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
var ipListMaps = []maps.Map{}
|
|
|
|
|
for _, ipList := range ipListResp.IpLists {
|
2021-12-02 17:11:44 +08:00
|
|
|
itemsResp, err := this.RPC().IPItemRPC().ListIPItemsWithListId(this.AdminContext(), &pb.ListIPItemsWithListIdRequest{
|
|
|
|
|
IpListId: ipList.Id,
|
|
|
|
|
Keyword: "",
|
|
|
|
|
IpFrom: params.Ip,
|
|
|
|
|
IpTo: "",
|
|
|
|
|
Offset: 0,
|
|
|
|
|
Size: 1,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
var items = itemsResp.IpItems
|
|
|
|
|
if len(items) == 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
var item = items[0]
|
|
|
|
|
|
|
|
|
|
var expiredTime = ""
|
|
|
|
|
if item.ExpiredAt > 0 {
|
|
|
|
|
expiredTime = timeutil.FormatTime("Y-m-d H:i:s", item.ExpiredAt)
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-15 15:42:05 +08:00
|
|
|
ipListMaps = append(ipListMaps, maps.Map{
|
2021-12-02 17:11:44 +08:00
|
|
|
"id": ipList.Id,
|
|
|
|
|
"name": ipList.Name,
|
|
|
|
|
"type": ipList.Type,
|
|
|
|
|
"itemExpiredTime": expiredTime,
|
|
|
|
|
"itemId": item.Id,
|
2021-08-15 15:42:05 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
this.Data["ipLists"] = ipListMaps
|
|
|
|
|
|
|
|
|
|
// 所有公用的IP列表
|
|
|
|
|
publicBlackIPListResp, err := this.RPC().IPListRPC().ListEnabledIPLists(this.AdminContext(), &pb.ListEnabledIPListsRequest{
|
|
|
|
|
Type: "black",
|
|
|
|
|
IsPublic: true,
|
|
|
|
|
Keyword: "",
|
|
|
|
|
Offset: 0,
|
2021-12-02 17:11:44 +08:00
|
|
|
Size: 20, // TODO 将来考虑到支持更多的黑名单
|
2021-08-15 15:42:05 +08:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
var publicBlackIPListMaps = []maps.Map{}
|
|
|
|
|
for _, ipList := range publicBlackIPListResp.IpLists {
|
|
|
|
|
publicBlackIPListMaps = append(publicBlackIPListMaps, maps.Map{
|
|
|
|
|
"id": ipList.Id,
|
|
|
|
|
"name": ipList.Name,
|
|
|
|
|
"type": ipList.Type,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
this.Data["publicBlackIPLists"] = publicBlackIPListMaps
|
|
|
|
|
|
|
|
|
|
// 访问日志
|
2022-04-17 16:23:52 +08:00
|
|
|
var hasAccessLogs = false
|
2022-04-22 09:15:08 +08:00
|
|
|
Loop:
|
2022-04-17 16:23:52 +08:00
|
|
|
for _, day := range []string{timeutil.Format("Ymd"), timeutil.Format("Ymd", time.Now().AddDate(0, 0, -1))} {
|
|
|
|
|
partitionsResp, err := this.RPC().HTTPAccessLogRPC().FindHTTPAccessLogPartitions(this.AdminContext(), &pb.FindHTTPAccessLogPartitionsRequest{Day: day})
|
2021-07-21 08:07:46 +08:00
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2022-04-17 16:23:52 +08:00
|
|
|
for _, partition := range partitionsResp.ReversePartitions {
|
|
|
|
|
accessLogsResp, err := this.RPC().HTTPAccessLogRPC().ListHTTPAccessLogs(this.AdminContext(), &pb.ListHTTPAccessLogsRequest{
|
|
|
|
|
Partition: partition,
|
|
|
|
|
Day: day,
|
|
|
|
|
Ip: params.Ip,
|
|
|
|
|
Size: 20,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-07-21 08:07:46 +08:00
|
|
|
|
2022-04-17 16:23:52 +08:00
|
|
|
var accessLogs = accessLogsResp.HttpAccessLogs
|
|
|
|
|
if len(accessLogs) > 0 {
|
|
|
|
|
this.Data["accessLogs"] = accessLogs
|
|
|
|
|
hasAccessLogs = true
|
2022-04-22 09:15:08 +08:00
|
|
|
break Loop
|
2022-04-17 16:23:52 +08:00
|
|
|
}
|
2021-07-21 08:07:46 +08:00
|
|
|
}
|
|
|
|
|
}
|
2022-04-17 16:23:52 +08:00
|
|
|
|
|
|
|
|
if !hasAccessLogs {
|
|
|
|
|
this.Data["accessLogs"] = []interface{}{}
|
|
|
|
|
}
|
2021-07-21 08:07:46 +08:00
|
|
|
|
|
|
|
|
this.Show()
|
|
|
|
|
}
|