IPBox把IP加入黑名单可以选择过期时间/可以从已经添加的名单中删除/已经添加的名单中显示过期时间

This commit is contained in:
GoEdgeLab
2021-12-02 17:11:44 +08:00
parent 54eda21f7d
commit 889417beda
7 changed files with 145 additions and 18 deletions

View File

@@ -14,19 +14,30 @@ type AddIPAction struct {
} }
func (this *AddIPAction) RunPost(params struct { func (this *AddIPAction) RunPost(params struct {
ListId int64 ListId int64
Ip string Ip string
ExpiredAt int64
}) { }) {
var itemId int64 = 0
defer func() {
this.CreateLogInfo("在名单 %d 中创建IP %d", params.ListId, itemId)
}()
var ipType = "ipv4" var ipType = "ipv4"
if strings.Contains(params.Ip, ":") { if strings.Contains(params.Ip, ":") {
ipType = "ipv6" ipType = "ipv6"
} }
_, err := this.RPC().IPItemRPC().CreateIPItem(this.AdminContext(), &pb.CreateIPItemRequest{ if params.ExpiredAt <= 0 {
params.ExpiredAt = time.Now().Unix() + 86400
}
createResp, err := this.RPC().IPItemRPC().CreateIPItem(this.AdminContext(), &pb.CreateIPItemRequest{
IpListId: params.ListId, IpListId: params.ListId,
IpFrom: params.Ip, IpFrom: params.Ip,
IpTo: "", IpTo: "",
ExpiredAt: time.Now().Unix() + 86400, // TODO 可以自定义时间 ExpiredAt: params.ExpiredAt,
Reason: "从IPBox中加入名单", Reason: "从IPBox中加入名单",
Type: ipType, Type: ipType,
EventLevel: "critical", EventLevel: "critical",
@@ -36,5 +47,7 @@ func (this *AddIPAction) RunPost(params struct {
return return
} }
itemId = createResp.IpItemId
this.Success() this.Success()
} }

View File

@@ -0,0 +1,27 @@
// 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"
)
type DeleteFromListAction struct {
actionutils.ParentAction
}
func (this *DeleteFromListAction) RunPost(params struct {
ListId int64
ItemId int64
}) {
defer this.CreateLogInfo("从IP名单 %d 中删除IP %d", params.ListId, params.ItemId)
_, err := this.RPC().IPItemRPC().DeleteIPItem(this.AdminContext(), &pb.DeleteIPItemRequest{IpItemId: params.ItemId})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -37,17 +37,44 @@ func (this *IndexAction) RunGet(params struct {
this.Data["isp"] = regionResp.IpRegion.Isp this.Data["isp"] = regionResp.IpRegion.Isp
// IP列表 // IP列表
ipListResp, err := this.RPC().IPListRPC().FindEnabledIPListContainsIP(this.AdminContext(), &pb.FindEnabledIPListContainsIPRequest{Ip: params.Ip}) ipListResp, err := this.RPC().IPListRPC().FindEnabledIPListContainsIP(this.AdminContext(), &pb.FindEnabledIPListContainsIPRequest{
Ip: params.Ip,
})
if err != nil { if err != nil {
this.ErrorPage(err) this.ErrorPage(err)
return return
} }
var ipListMaps = []maps.Map{} var ipListMaps = []maps.Map{}
for _, ipList := range ipListResp.IpLists { for _, ipList := range ipListResp.IpLists {
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)
}
ipListMaps = append(ipListMaps, maps.Map{ ipListMaps = append(ipListMaps, maps.Map{
"id": ipList.Id, "id": ipList.Id,
"name": ipList.Name, "name": ipList.Name,
"type": ipList.Type, "type": ipList.Type,
"itemExpiredTime": expiredTime,
"itemId": item.Id,
}) })
} }
this.Data["ipLists"] = ipListMaps this.Data["ipLists"] = ipListMaps
@@ -58,7 +85,7 @@ func (this *IndexAction) RunGet(params struct {
IsPublic: true, IsPublic: true,
Keyword: "", Keyword: "",
Offset: 0, Offset: 0,
Size: 10, // TODO 将来考虑到支持更多的黑名单 Size: 20, // TODO 将来考虑到支持更多的黑名单
}) })
if err != nil { if err != nil {
this.ErrorPage(err) this.ErrorPage(err)

View File

@@ -15,6 +15,7 @@ func init() {
Prefix("/servers/ipbox"). Prefix("/servers/ipbox").
Get("", new(IndexAction)). Get("", new(IndexAction)).
Post("/addIP", new(AddIPAction)). Post("/addIP", new(AddIPAction)).
Post("/deleteFromList", new(DeleteFromListAction)).
EndAll() EndAll()
}) })
} }

View File

@@ -135,6 +135,7 @@ Vue.component("datetime-input", {
this.timestamp = Math.floor(date.getTime() / 1000) this.timestamp = Math.floor(date.getTime() / 1000)
}, },
leadingZero: function (s, l) { leadingZero: function (s, l) {
s = s.toString()
if (l <= s.length) { if (l <= s.length) {
return s return s
} }
@@ -142,6 +143,18 @@ Vue.component("datetime-input", {
s = "0" + s s = "0" + s
} }
return s return s
},
resultTimestamp: function () {
return this.timestamp
},
nextDays: function (days) {
let date = new Date()
date.setTime(date.getTime() + days * 86400 * 1000)
this.day = date.getFullYear() + "-" + this.leadingZero(date.getMonth() + 1, 2) + "-" + this.leadingZero(date.getDate(), 2)
this.hour = this.leadingZero(date.getHours(), 2)
this.minute = this.leadingZero(date.getMinutes(), 2)
this.second = this.leadingZero(date.getSeconds(), 2)
this.change()
} }
}, },
template: `<div> template: `<div>
@@ -156,5 +169,6 @@ Vue.component("datetime-input", {
<div class="ui field">:</div> <div class="ui field">:</div>
<div class="ui field" :class="{error: hasSecondError}"><input type="text" v-model="second" maxlength="2" style="width:4em" placeholder="秒" @input="change"/></div> <div class="ui field" :class="{error: hasSecondError}"><input type="text" v-model="second" maxlength="2" style="width:4em" placeholder="秒" @input="change"/></div>
</div> </div>
<p class="comment">常用时间:<a href="" @click.prevent="nextDays(1)"> &nbsp;1天&nbsp; </a> <span class="disabled">|</span> <a href="" @click.prevent="nextDays(3)"> &nbsp;3天&nbsp; </a> <span class="disabled">|</span> <a href="" @click.prevent="nextDays(7)"> &nbsp;一周&nbsp; </a> <span class="disabled">|</span> <a href="" @click.prevent="nextDays(30)"> &nbsp;30天&nbsp; </a> </p>
</div>` </div>`
}) })

View File

@@ -23,19 +23,44 @@
<tr v-if="publicBlackIPLists.length > 0"> <tr v-if="publicBlackIPLists.length > 0">
<td>所在IP名单</td> <td>所在IP名单</td>
<td colspan="5"> <td colspan="5">
<div class="ui label basic small" v-for="ipList in ipLists"> <div class="ui label basic small" v-for="ipList in ipLists" style="margin-bottom: 0.5em">
{{ipList.name}} {{ipList.name}}
<!-- 过期时间 -->
<span v-if="ipList.itemExpiredTime.length == 0" class="grey small">(不过期)</span>
<span v-else class="grey small">(有效至:{{ipList.itemExpiredTime}}</span>
<!-- 操作 -->
<a href="" title="删除" @click.prevent="deleteFromList(ipList.id, ipList.itemId)"><i class="icon remove small"></i></a>
</div> </div>
<span v-if="ipLists.length == 0" class="disabled">暂时未在任何名单。</span>
&nbsp; &nbsp; &nbsp; &nbsp;
<a href="" @click.prevent="showBlackLists"><i class="icon angle" :class="{up: blackListsVisible, down: !blackListsVisible}"></i>加入到黑名单</a> <div>
<a href="" @click.prevent="showBlackLists"><i class="icon angle" :class="{up: blackListsVisible, down: !blackListsVisible}"></i>加入到<span v-if="ipLists.length > 0">其他</span>黑名单</a>
</div>
</td> </td>
</tr> </tr>
<tr v-if="publicBlackIPLists.length > 0 && blackListsVisible"> <tr v-if="publicBlackIPLists.length > 0 && blackListsVisible">
<td>加入黑名单</td> <td>加入黑名单</td>
<td colspan="5"> <td colspan="5">
<a class="ui label basic small" v-for="ipList in publicBlackIPLists" @click.prevent="addBlackIP(ipList)"> <form class="ui form">
{{ipList.name}} <table class="ui table">
</a> <tr>
<td class="title">过期时间</td>
<td>
<datetime-input :v-timestamp="defaultItemExpiredAt" ref="itemExpiredTimestamp"></datetime-input>
</td>
</tr>
<tr>
<td>选择名单 *</td>
<td>
<a class="ui label basic small" :class="{blue: selectedListId == ipList.id}" v-for="ipList in publicBlackIPLists" @click.prevent="addBlackIP(ipList)" style="margin-top: 0.3em">
{{ipList.name}}
</a>
</td>
</tr>
</table>
</form>
</td> </td>
</tr> </tr>
</table> </table>

View File

@@ -2,7 +2,11 @@ Tea.context(function () {
this.blackListsVisible = false this.blackListsVisible = false
this.allPublicBlackIPLists = this.publicBlackIPLists.$copy() this.allPublicBlackIPLists = this.publicBlackIPLists.$copy()
this.defaultItemExpiredAt = Math.floor(new Date().getTime() / 1000) + 86400
this.showBlackLists = function () { this.showBlackLists = function () {
this.defaultItemExpiredAt = Math.floor(new Date().getTime() / 1000) + 86400
let that = this let that = this
this.publicBlackIPLists = this.allPublicBlackIPLists.filter(function (allList) { this.publicBlackIPLists = this.allPublicBlackIPLists.filter(function (allList) {
let found = true let found = true
@@ -16,17 +20,33 @@ Tea.context(function () {
this.blackListsVisible = !this.blackListsVisible this.blackListsVisible = !this.blackListsVisible
} }
this.selectedListId = 0
this.addBlackIP = function (list) { this.addBlackIP = function (list) {
this.selectedListId = list.id
let expiredAt = this.$refs.itemExpiredTimestamp.resultTimestamp()
let that = this let that = this
teaweb.confirm("确定要将此IP添加到黑名单吗?", function () { teaweb.confirm("确定要将此IP添加到黑名单'" + list.name + "'吗?", function () {
that.$post(".addIP") that.$post(".addIP")
.params({ .params({
listId: list.id, listId: list.id,
ip: that.ip ip: that.ip,
expiredAt: expiredAt
}) })
.success(function () { .success(function () {
that.ipLists.push(list) teaweb.reload()
that.blackListsVisible = false })
})
}
this.deleteFromList = function (listId, itemId) {
teaweb.confirm("确定要从此名单中删除此IP吗", function () {
this.$post(".deleteFromList")
.params({
listId: listId,
itemId: itemId
})
.success(function () {
teaweb.reload()
}) })
}) })
} }