Skip to content

Commit

Permalink
ADD updatesubscribe to WebUI
Browse files Browse the repository at this point in the history
  • Loading branch information
iotames committed Apr 9, 2024
1 parent e043c9f commit cc276ec
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 24 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
## 升级日志

### v1.3.3

1. 升级v2ray核心兼容至v5.14.1
2. 修复v2ray核心文件v5.14.1版本读取配置出现的BUG
3. 新增Web面板的更新订阅功能

### v1.3.2

升级内置v2ray核心文件版本至v5.14.1

### v1.3.0

优化Web面板,修复若干BUG
Expand Down
52 changes: 40 additions & 12 deletions main/resource/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ <h2 style="padding: 16px;text-align: center;">
V2rayPool控制面板
</h2>

<table class="layui-hide" id="test" lay-filter="test"></table>
<table class="layui-hide" id="nodeslist" lay-filter="nodeslist"></table>

</div>

Expand All @@ -58,6 +58,7 @@ <h2 style="padding: 16px;text-align: center;">
<button class="layui-btn layui-btn-sm layui-bg-blue" lay-on="routingrules">路由规则</button>
<button class="layui-btn layui-btn-sm" lay-on="v2raylist">v2ray</button>
<button class="layui-btn layui-btn-sm layui-bg-purple" lay-event="startNodes" id="startproxynodes">启动</button>
<button class="layui-btn layui-btn-sm" lay-on="subscribe">订阅</button>
<!-- <i class="itable-top layui-icon layui-icon-set" style="color: #fc4706e8;" lay-on="settinglayer" id="setting"></i> -->
</div>

Expand Down Expand Up @@ -107,7 +108,7 @@ <h2 style="padding: 16px;text-align: center;">
// var elem = data.elem; // 获得 select 原始 DOM 对象
// var othis = data.othis; // 获得 select 元素被替换后的 jQuery 对象
// layer.msg(this.innerHTML + ' 的 value: ' + value); // this 为当前选中 <option> 元素对象
table.reloadData('test', { where: { domain: data.value } })
table.reloadData('nodeslist', { where: { domain: data.value } })
});

// 事件
Expand Down Expand Up @@ -174,7 +175,7 @@ <h2 style="padding: 16px;text-align: center;">
postjson("/api/v2ray/routing-rules/update", field, function (dt) {
layer.close(index)
layer.msg(dt.msg);
// table.reload('test')
// table.reload('nodeslist')
}, function (dt) {
console.log(field)
layer.alert(dt.msg, { icon: 2, title: dt.code + "错误" });
Expand All @@ -183,7 +184,30 @@ <h2 style="padding: 16px;text-align: center;">
}
})
},
// 'settinglayer': function () { }
'subscribe': function () {
layer.open({
title: "更新订阅",
type: 1,
area: ['60%', '50%'],
content: $('#subscribelayer'),
success: (layero, index, that) => {
form.render();
form.on('submit(updatesubscribe)', function (data) {
submitbtn = document.getElementById("updatesubscribe")
var field = data.field;
console.log(field)
postjson("/api/nodes/subscribe", field, function (dt) {
layer.close(index)
layer.msg(dt.msg);
table.reload('nodeslist')
}, function (dt) {
console.log(field)
layer.alert(dt.msg, { icon: 2, title: dt.code + "错误" });
}, submitbtn)
})
}
})
}
})

// 创建渲染实例
Expand Down Expand Up @@ -285,7 +309,7 @@ <h2 style="padding: 16px;text-align: center;">

// 创建渲染实例
table.render({
elem: '#test',
elem: '#nodeslist',
url: '/api/nodes', // 此处为静态模拟数据,实际使用时需换成真实接口
toolbar: '#toolbarDemo',
defaultToolbar: ['filter', {
Expand Down Expand Up @@ -323,7 +347,7 @@ <h2 style="padding: 16px;text-align: center;">
});

// 工具栏事件
table.on('toolbar(test)', function (obj) {
table.on('toolbar(nodeslist)', function (obj) {
var id = obj.config.id;
var checkStatus = table.checkStatus(id);
var othis = lay(this);
Expand All @@ -342,7 +366,7 @@ <h2 style="padding: 16px;text-align: center;">
interval = setInterval(function () {
console.log("-----setInterval--times:", times)
if (times > 0) {
table.reload('test')
table.reload('nodeslist')
btn.disabled = true;
btn.innerText = ((times < 10 ? "0" + times : times) + "s测速中");
times--;
Expand All @@ -369,7 +393,7 @@ <h2 style="padding: 16px;text-align: center;">
interval = setInterval(function () {
console.log("-----setInterval--times:", times)
if (times > 0) {
table.reloadData('test')
table.reloadData('nodeslist')
btn.disabled = true;
// btn.innerText = ((times < 10 ? "0" + times : times) + "s启动中");
times--;
Expand All @@ -383,7 +407,7 @@ <h2 style="padding: 16px;text-align: center;">
layer.alert(dt.msg, { icon: 2, title: dt.code + "错误" });
}, btn)
break;
// case "refreshData": table.reload('test')
// case "refreshData": table.reload('nodeslist')
// break;
case 'settinglayer':
layer.open({
Expand Down Expand Up @@ -423,7 +447,7 @@ <h2 style="padding: 16px;text-align: center;">
});

// 触发单元格工具事件
table.on('tool(test)', function (obj) { // 双击 toolDouble
table.on('tool(nodeslist)', function (obj) { // 双击 toolDouble
var data = obj.data; // 获得当前行数据
// console.log(obj)
switch (obj.event) {
Expand All @@ -445,7 +469,7 @@ <h2 style="padding: 16px;text-align: center;">
layer.confirm(hintmsg, function (index) {
postjson(posturl, { remote_addr: data.remote_addr }, function (dt) {
layer.msg(dt.msg);
table.reload('test')
table.reload('nodeslist')
}, function (dt) {
layer.alert(dt.msg, { icon: 2, title: dt.code + "错误" });
})
Expand Down Expand Up @@ -481,7 +505,7 @@ <h2 style="padding: 16px;text-align: center;">
layer.msg(dt.msg);
window.setTimeout(function () {
// window.location.reload();
table.reloadData('test')
table.reloadData('nodeslist')
}, 500)

}, function (dt) {
Expand Down Expand Up @@ -531,6 +555,10 @@ <h2 style="padding: 16px;text-align: center;">
<{% tplinclude "routing_rules_form.html" . | println %}>
</div>

<div id="subscribelayer" style="display: none;">
<{% tplinclude "subscribe.html" . | println %}>
</div>

</body>

</html>
17 changes: 17 additions & 0 deletions main/resource/subscribe.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div class="layui-form layui-form-pane" lay-filter="updatesubscribe" style="margin: 16px;">

<div class="layui-form-item">
<label class="layui-form-label">订阅地址</label>
<div class="layui-input-block">
<input type="text" disabled name="subscribe_url" value="<{% .SubscribeUrl %}>" class="layui-input">
</div>
</div>

<div class="layui-form-item">
<button id="updatesubscribe" class="layui-btn" style="float: right;" lay-submit lay-filter="updatesubscribe">更新订阅</button>
</div>

</div>



38 changes: 26 additions & 12 deletions v2raypool.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,28 +327,42 @@ func (p *ProxyPool) UpdateSubscribe() (total, add int) {
var dt string
var err error
var srawdata string
for _, n := range p.nodes {
if n.IsRunning() {
localAddr := p.GetLocalAddr(n)
dt, srawdata, err = parseSubscribeByUrl(p.subscribeUrl, localAddr)
fmt.Printf("---UpdateSubscribe--UseProxy(%s)Title(%s)--Err(%v)--ParseV2rayNodes(%s)---\n", localAddr, n.Title, err, dt)
if err == nil {
break

if p.subscribeUrl == "" {
fmt.Printf("---WARNING--subscribeUrl is empty----\n")
return
}

dt, srawdata, err = parseSubscribeByUrl(p.subscribeUrl, "")
if err != nil {
fmt.Printf("---UpdateSubscribe-parseSubscribeByUrl-err(%v)--RetryByProxy-\n", err)
for _, n := range p.nodes {
if n.IsRunning() {
localAddr := p.GetLocalAddr(n)
dt, srawdata, err = parseSubscribeByUrl(p.subscribeUrl, localAddr)
fmt.Printf("---UpdateSubscribe--UseProxy(%s)Title(%s)--Err(%v)--ParseV2rayNodes(%s)---\n", localAddr, n.Title, err, dt)
if err == nil {
fmt.Printf("---SUCCESS--UpdateSubscribe--parseSubscribeByUrl----\n")
break
}
}
}
}

vnds := ParseV2rayNodes(dt)
total = len(vnds)
if total == 0 {
fmt.Printf("---WARNING--proxy nodes count empty----\n")
return
}

if srawdata != "" {
err = conf.GetConf().UpdateSubscribeData(srawdata)
if err != nil {
panic(err)
}
}

vnds := ParseV2rayNodes(dt)
total = len(vnds)
if total == 0 {
return
}
oldLen := len(p.nodes)
oldNodesMap := make(map[string]ProxyNode, oldLen)
for _, oldn := range p.nodes {
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 UpdateSubscribe() []byte {
pp := vp.GetProxyPool()
total, add := pp.UpdateSubscribe()
result := BaseResult{}
if total == 0 {
result.Fail("更新订阅失败。节点数(0)", 500)
return result.Bytes()
}
result.Success(fmt.Sprintf("更新订阅成功。节点数(%d),新增节点(%d)", total, add))
return result.Bytes()
}

func StartNodes() []byte {
result := BaseResult{}
pp := vp.GetProxyPool()
Expand Down
3 changes: 3 additions & 0 deletions webserver/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func setRouter(s *web.EasyServer) {
s.AddHandler("POST", "/api/nodes/start", func(ctx web.Context) {
ctx.Writer.Write(StartNodes())
})
s.AddHandler("POST", "/api/nodes/subscribe", func(ctx web.Context) {
ctx.Writer.Write(UpdateSubscribe())
})
s.AddHandler("POST", "/api/node/active", func(ctx web.Context) {
dt := RequestActiveNode{}
err := getPostJson(ctx, &dt)
Expand Down

0 comments on commit cc276ec

Please sign in to comment.