Skip to content

Commit

Permalink
ADD feature: DeleteNode
Browse files Browse the repository at this point in the history
  • Loading branch information
iotames committed Jun 6, 2024
1 parent 1198b2e commit b4b7107
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 11 deletions.
22 changes: 18 additions & 4 deletions main/resource/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ <h2 style="padding: 22px;text-align: center;">
<a class="layui-btn layui-btn-xs {{= d.is_active ? 'layui-bg-purple' : 'layui-bg-blue' }}" lay-event="active">
{{= d.is_active ? '已启用' : '启用' }}
</a>
<!-- layui-icon layui-icon-delete -->
<a class="layui-btn layui-btn-xs layui-bg-red" lay-event="delete">删除</a>
</div>
</script>

Expand Down Expand Up @@ -272,9 +274,9 @@ <h2 style="padding: 22px;text-align: center;">
{ field: 'speed', title: '速度/秒', templet: '<span class="layui-badge {{= d.test_at != "0001-01-01 00:00" ? "layui-bg-green" : ""}}" onclick="showTips(\'{{= d.test_url }}\', this)">{{= d.speed >= 5 ? 100 : d.speed }} s</span>', width: 100, sort: true },
{ field: 'title', title: '标题', width: 200 },
{ field: 'remote_addr', title: '节点地址', width: 180 },
{ field: 'is_running', title: '状态', templet: '<span>{{= d.is_running ? "运行中" : "已停止"}}</span>', width: 60 },
{ field: 'is_running', title: '状态', templet: '<span class="layui-badge {{= d.is_running ? "layui-bg-green" : ""}}">{{= d.is_running ? "运行中" : "已停止"}}</span>', width: 60 },
{ field: 'test_at', title: '测速时间', width: 180 },
{ fixed: 'right', title: '操作', width: 80, minWidth: 80, toolbar: '#barDemo' }
{ fixed: 'right', title: '操作', width: 80, minWidth: 115, toolbar: '#barDemo' }
]],
error: function (res, msg) {
console.log(res, msg)
Expand Down Expand Up @@ -428,7 +430,6 @@ <h2 style="padding: 22px;text-align: center;">
});

} else {

layer.open({
title: hintmsg,
type: 1,
Expand Down Expand Up @@ -469,10 +470,23 @@ <h2 style="padding: 22px;text-align: center;">
});
}
});

}

break;
case 'delete':
var posturl = "/api/node/delete"

layer.confirm("确定删除节点【" + data.title + "】?", function (index) {
postjson(posturl, {index: data.index}, function (dt) {
layer.msg(dt.msg);
}, function (dt) {
layer.alert(dt.msg, { icon: 2, title: dt.code + "错误" });
})
table.reload('nodeslist')
console.log(data)
layer.close(index);
});
break
}
});
});
Expand Down
1 change: 1 addition & 0 deletions proxy_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type ProxyNode struct {
TestAt time.Time
V2rayNode V2rayNode
Status int
IsDelete bool
}

func NewProxyNodeByV2ray(vnd V2rayNode) *ProxyNode {
Expand Down
61 changes: 54 additions & 7 deletions v2raypool.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,21 @@ func (p *ProxyPool) RemoveNode(n ProxyNode) {
func (p *ProxyPool) GetNodes(domain string) ProxyNodes {
p.lock.Lock()
defer p.lock.Unlock()
var nds ProxyNodes
if domain == "" {
return p.nodes
for _, nd := range p.nodes {
if !nd.IsDelete {
nds = append(nds, nd)
}
}
return nds
}
return p.getSpeedNodes(domain)
for _, nd := range p.getSpeedNodes(domain) {
if !nd.IsDelete {
nds = append(nds, nd)
}
}
return nds
}

func (p *ProxyPool) UpdateNode(n ProxyNode) error {
Expand Down Expand Up @@ -630,15 +641,20 @@ func (p *ProxyPool) StartAll() error {
if c.Dial() == nil {
defer c.Close()
}
for _, n := range p.nodes {
for i, n := range p.nodes {
if !n.IsRunning() {
err = n.AddToPool(c)
if err != nil {
logger := getConf().GetLogger()
logger.Errorf("------StartAll--err--addr(%s)--AddToPool(%v)", n.RemoteAddr, err)
break
}
p.UpdateNode(n)
p.nodes[i].Status = 1
}
}
for k, v := range p.speedMap {
for j := range v {
p.speedMap[k][j].Status = 1
}
}
p.IsLock = false
Expand Down Expand Up @@ -683,9 +699,39 @@ func (p *ProxyPool) StopAll() error {
return err
}

// func (p *ProxyPool) Delete() error {
// return nil
// }
func (p *ProxyPool) Delete(index int) error {
var err error
n := p.nodes[index]
if n.IsRunning() {
c := NewV2rayApiClientV5(p.getGrpcAddr())
err = c.Dial()
if err == nil {
defer c.Close()
err = n.Remove(c, getProxyNodeTag(index))
if err != nil {
return err
}
}
}

for i, pnd := range p.nodes {
if pnd.Index == index {
p.nodes[i].IsDelete = true
p.nodes[i].Status = 0
}
}

for k, vvv := range p.speedMap {
for j, vv := range vvv {
if vv.Index == index {
p.speedMap[k][j].IsDelete = true
p.speedMap[k][j].Status = 0
}
}
}

return err
}

func (p *ProxyPool) KillAllNodes() (total, runport, kill, fail int) {
p.IsLock = true
Expand All @@ -702,6 +748,7 @@ func (p *ProxyPool) KillAllNodes() (total, runport, kill, fail int) {
p.poolCmd = nil
return
}

func (p *ProxyPool) UnActiveNode(n ProxyNode) error {
// activePort := p.localPortStart - 1
err := p.killActiveNode()
Expand Down
12 changes: 12 additions & 0 deletions webserver/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ func ActiveNode(remoteAddr string, globalProxy bool) []byte {
return result.Bytes()
}

func DeleteNode(index int) []byte {
pp := vp.GetProxyPool()
result := BaseResult{}
err := pp.Delete(index)
if err != nil {
result.Fail(err.Error(), 500)
return result.Bytes()
}
result.Success("删除成功")
return result.Bytes()
}

func UpdateSubscribe(httpProxy string) []byte {
pp := vp.GetProxyPool()
total, add := pp.UpdateSubscribe(httpProxy)
Expand Down
12 changes: 12 additions & 0 deletions webserver/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ func setRouter(s *web.EasyServer) {
ctx.Writer.Write(ActiveNode(dt.RemoteAddr, dt.GlobalProxy))
})

s.AddHandler("POST", "/api/node/delete", func(ctx web.Context) {
dt := RequestNode{}
err := getPostJson(ctx, &dt)
if err != nil {
return
}
ctx.Writer.Write(DeleteNode(dt.Index))
})

s.AddHandler("POST", "/api/v2ray/run", func(ctx web.Context) {
dt := V2rayServerData{}
err := getPostJson(ctx, &dt)
Expand Down Expand Up @@ -134,6 +143,9 @@ type HomePageData struct {
TestedDomainList []string
}

type RequestNode struct {
Index int
}
type RequestActiveNode struct {
RemoteAddr string `json:"remote_addr"`
GlobalProxy bool `json:"global_proxy"`
Expand Down

0 comments on commit b4b7107

Please sign in to comment.