IPBox增加地域、ISP、添加到黑名单等功能

This commit is contained in:
GoEdgeLab
2021-08-15 15:42:05 +08:00
parent 806a6bf8a7
commit 537c3da8c3
11 changed files with 192 additions and 11 deletions

View File

@@ -0,0 +1,40 @@
// 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"
"strings"
"time"
)
type AddIPAction struct {
actionutils.ParentAction
}
func (this *AddIPAction) RunPost(params struct {
ListId int64
Ip string
}) {
var ipType = "ipv4"
if strings.Contains(params.Ip, ":") {
ipType = "ipv6"
}
_, err := this.RPC().IPItemRPC().CreateIPItem(this.AdminContext(), &pb.CreateIPItemRequest{
IpListId: params.ListId,
IpFrom: params.Ip,
IpTo: "",
ExpiredAt: time.Now().Unix() + 86400, // TODO 可以自定义时间
Reason: "从IPBox中加入名单",
Type: ipType,
EventLevel: "critical",
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -5,6 +5,7 @@ package ipbox
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/maps"
timeutil "github.com/iwind/TeaGo/utils/time"
"time"
)
@@ -22,10 +23,62 @@ func (this *IndexAction) RunGet(params struct {
}) {
this.Data["ip"] = params.Ip
// 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 {
this.Data["regions"] = regionResp.IpRegion.Summary
} else {
this.Data["regions"] = ""
}
this.Data["isp"] = regionResp.IpRegion.Isp
// IP列表
ipListResp, err := this.RPC().IPListRPC().FindEnabledIPListContainsIP(this.AdminContext(), &pb.FindEnabledIPListContainsIPRequest{Ip: params.Ip})
if err != nil {
this.ErrorPage(err)
return
}
var ipListMaps = []maps.Map{}
for _, ipList := range ipListResp.IpLists {
ipListMaps = append(ipListMaps, maps.Map{
"id": ipList.Id,
"name": ipList.Name,
"type": ipList.Type,
})
}
this.Data["ipLists"] = ipListMaps
// 所有公用的IP列表
publicBlackIPListResp, err := this.RPC().IPListRPC().ListEnabledIPLists(this.AdminContext(), &pb.ListEnabledIPListsRequest{
Type: "black",
IsPublic: true,
Keyword: "",
Offset: 0,
Size: 10, // TODO 将来考虑到支持更多的黑名单
})
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
// 访问日志
accessLogsResp, err := this.RPC().HTTPAccessLogRPC().ListHTTPAccessLogs(this.AdminContext(), &pb.ListHTTPAccessLogsRequest{
Day: timeutil.Format("Ymd"),
Keyword: "ip:" + params.Ip,
Size: 10,
Day: timeutil.Format("Ymd"),
Ip: params.Ip,
Size: 20,
})
if err != nil {
this.ErrorPage(err)
@@ -35,9 +88,9 @@ func (this *IndexAction) RunGet(params struct {
if len(accessLogs) == 0 {
// 查询昨天
accessLogsResp, err = this.RPC().HTTPAccessLogRPC().ListHTTPAccessLogs(this.AdminContext(), &pb.ListHTTPAccessLogsRequest{
Day: timeutil.Format("Ymd", time.Now().AddDate(0, 0, -1)),
Keyword: "ip:" + params.Ip,
Size: 10,
Day: timeutil.Format("Ymd", time.Now().AddDate(0, 0, -1)),
Ip: params.Ip,
Size: 20,
})
if err != nil {
this.ErrorPage(err)

View File

@@ -14,6 +14,7 @@ func init() {
Helper(helpers.NewUserMustAuth(configloaders.AdminModuleCodeServer)).
Prefix("/servers/ipbox").
Get("", new(IndexAction)).
Post("/addIP", new(AddIPAction)).
EndAll()
})
}
}