添加客户端到期时间功能
This commit is contained in:
@@ -35,6 +35,7 @@
|
||||
- 新增:
|
||||
- `nps -server` 管理脚本欢迎语显示当前版本号,更新版本时比对版本,已是最新则直接退出,避免无谓覆盖 [#328](https://github.com/yisier/nps/issues/328)
|
||||
- `npc` 管理脚本欢迎语显示当前版本号,新增 `[5] 更新客户端` 选项,更新并替换 npc 二进制
|
||||
- 客户端新增到期时间字段,创建/修改客户端时可选填写,到期后自动暂停客户端 [#322](https://github.com/yisier/nps/issues/322)
|
||||
- 修复:
|
||||
- Dashboard IO 速率采集阻塞请求 500ms,改为后台采集缓存,消除 Sleep 等待
|
||||
- ServerStatus 切片并发读写不安全,添加 RWMutex 保护
|
||||
|
||||
@@ -45,6 +45,7 @@ POST /client/add/
|
||||
| flow\_limit | 流量限制 单位M 空则为不限制 |
|
||||
| max\_conn | 客户端最大连接数量 空则为不限制 |
|
||||
| max\_tunnel | 客户端最大隧道数量 空则为不限制 |
|
||||
| expire\_time | 到期时间,留空表示永不过期,到期后自动暂停。支持格式:`2006-01-02 15:04:05`、`2006-01-02 15:04`、`2006-01-02T15:04:05`、`2006-01-02T15:04`、`2006-01-02` |
|
||||
|
||||
***
|
||||
修改客户端
|
||||
@@ -67,6 +68,7 @@ POST /client/edit/
|
||||
| flow\_limit | 流量限制 单位M 空则为不限制 |
|
||||
| max\_conn | 客户端最大连接数量 空则为不限制 |
|
||||
| max\_tunnel | 客户端最大隧道数量 空则为不限制 |
|
||||
| expire\_time | 到期时间,留空表示永不过期,到期后自动暂停。格式同新增接口 |
|
||||
| id | 要修改的客户端id |
|
||||
|
||||
***
|
||||
|
||||
@@ -57,6 +57,7 @@ type Client struct {
|
||||
IpWhite bool // 是否启用ip白名单
|
||||
IpWhitePass string // ip授权密码
|
||||
IpWhiteList []string // ip白名单
|
||||
ExpireTime string // 到期时间,留空表示永不过期,格式 2006-01-02 15:04:05
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
|
||||
@@ -106,6 +106,7 @@ func StartNewServer(bridgePort int, cnf *file.Tunnel, bridgeType string, bridgeD
|
||||
}
|
||||
go DealBridgeTask()
|
||||
go dealClientFlow()
|
||||
go dealClientExpire()
|
||||
if svr := NewMode(Bridge, cnf); svr != nil {
|
||||
if err := svr.Start(); err != nil {
|
||||
logs.Error(err)
|
||||
@@ -128,6 +129,50 @@ func dealClientFlow() {
|
||||
}
|
||||
}
|
||||
|
||||
// dealClientExpire 周期性扫描客户端到期时间,过期则自动暂停
|
||||
func dealClientExpire() {
|
||||
// 启动时立即检查一次,避免重启后到期客户端最多 1 分钟内才被暂停
|
||||
checkClientExpire()
|
||||
ticker := time.NewTicker(time.Minute)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
checkClientExpire()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// checkClientExpire 遍历所有客户端,若 ExpireTime 已过则将 Status 置为 false 并断开连接
|
||||
func checkClientExpire() {
|
||||
now := time.Now()
|
||||
changed := false
|
||||
file.GetDb().JsonDb.Clients.Range(func(key, value interface{}) bool {
|
||||
v, ok := value.(*file.Client)
|
||||
if !ok || v == nil {
|
||||
return true
|
||||
}
|
||||
if v.ExpireTime == "" || !v.Status {
|
||||
return true
|
||||
}
|
||||
t, err := time.ParseInLocation("2006-01-02 15:04:05", v.ExpireTime, time.Local)
|
||||
if err != nil {
|
||||
return true
|
||||
}
|
||||
if now.Before(t) {
|
||||
return true
|
||||
}
|
||||
v.Status = false
|
||||
changed = true
|
||||
logs.Info("client id %d (remark: %s) expired at %s, auto paused", v.Id, v.Remark, v.ExpireTime)
|
||||
DelClientConnect(v.Id)
|
||||
return true
|
||||
})
|
||||
if changed {
|
||||
file.GetDb().JsonDb.StoreClientsToJsonFile()
|
||||
}
|
||||
}
|
||||
|
||||
// new a server by mode name
|
||||
func NewMode(Bridge *bridge.Bridge, c *file.Tunnel) proxy.Service {
|
||||
var service proxy.Service
|
||||
|
||||
@@ -73,6 +73,7 @@ func (s *ClientController) Add() {
|
||||
IpWhite: s.GetBoolNoErr("ipwhite"),
|
||||
IpWhitePass: s.getEscapeString("ipwhitepass"),
|
||||
IpWhiteList: RemoveRepeatedElement(strings.Split(s.getEscapeString("ipwhitelist"), "\r\n")),
|
||||
ExpireTime: normalizeExpireTime(s.getEscapeString("expire_time")),
|
||||
CreateTime: time.Now().Format("2006-01-02 15:04:05"),
|
||||
}
|
||||
if err := file.GetDb().NewClient(t); err != nil {
|
||||
@@ -159,6 +160,7 @@ func (s *ClientController) Edit() {
|
||||
}
|
||||
|
||||
c.BlackIpList = RemoveRepeatedElement(strings.Split(s.getEscapeString("blackiplist"), "\r\n"))
|
||||
c.ExpireTime = normalizeExpireTime(s.getEscapeString("expire_time"))
|
||||
file.GetDb().JsonDb.StoreClientsToJsonFile()
|
||||
}
|
||||
s.AjaxOk("save success")
|
||||
@@ -186,6 +188,39 @@ func RemoveRepeatedElement(arr []string) (newArr []string) {
|
||||
return
|
||||
}
|
||||
|
||||
// expireTimeFormats 支持的到期时间输入格式
|
||||
var expireTimeFormats = []string{
|
||||
"2006-01-02 15:04:05",
|
||||
"2006-01-02 15:04",
|
||||
"2006-01-02T15:04:05",
|
||||
"2006-01-02T15:04",
|
||||
"2006-01-02",
|
||||
}
|
||||
|
||||
// ParseExpireTime 将多种格式的到期时间字符串解析为 time.Time
|
||||
// 留空或无法解析返回 ok=false
|
||||
func ParseExpireTime(s string) (time.Time, bool) {
|
||||
s = strings.TrimSpace(s)
|
||||
if s == "" {
|
||||
return time.Time{}, false
|
||||
}
|
||||
for _, f := range expireTimeFormats {
|
||||
if t, err := time.ParseInLocation(f, s, time.Local); err == nil {
|
||||
return t, true
|
||||
}
|
||||
}
|
||||
return time.Time{}, false
|
||||
}
|
||||
|
||||
// normalizeExpireTime 将用户提交的到期时间统一为 "2006-01-02 15:04:05"
|
||||
// 无法解析则返回空串表示不限制
|
||||
func normalizeExpireTime(s string) string {
|
||||
if t, ok := ParseExpireTime(s); ok {
|
||||
return t.Format("2006-01-02 15:04:05")
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 更改状态
|
||||
func (s *ClientController) ChangeStatus() {
|
||||
id := s.GetIntNoErr("id")
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
(function (global, factory) {
|
||||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
||||
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
||||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.zh = {}));
|
||||
}(this, (function (exports) { 'use strict';
|
||||
|
||||
var fp = typeof window !== "undefined" && window.flatpickr !== undefined
|
||||
? window.flatpickr
|
||||
: {
|
||||
l10ns: {},
|
||||
};
|
||||
var Mandarin = {
|
||||
weekdays: {
|
||||
shorthand: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
|
||||
longhand: [
|
||||
"星期日",
|
||||
"星期一",
|
||||
"星期二",
|
||||
"星期三",
|
||||
"星期四",
|
||||
"星期五",
|
||||
"星期六",
|
||||
],
|
||||
},
|
||||
months: {
|
||||
shorthand: [
|
||||
"一月",
|
||||
"二月",
|
||||
"三月",
|
||||
"四月",
|
||||
"五月",
|
||||
"六月",
|
||||
"七月",
|
||||
"八月",
|
||||
"九月",
|
||||
"十月",
|
||||
"十一月",
|
||||
"十二月",
|
||||
],
|
||||
longhand: [
|
||||
"一月",
|
||||
"二月",
|
||||
"三月",
|
||||
"四月",
|
||||
"五月",
|
||||
"六月",
|
||||
"七月",
|
||||
"八月",
|
||||
"九月",
|
||||
"十月",
|
||||
"十一月",
|
||||
"十二月",
|
||||
],
|
||||
},
|
||||
rangeSeparator: " 至 ",
|
||||
weekAbbreviation: "周",
|
||||
scrollTitle: "滚动切换",
|
||||
toggleTitle: "点击切换 12/24 小时时制",
|
||||
};
|
||||
fp.l10ns.zh = Mandarin;
|
||||
var zh = fp.l10ns;
|
||||
|
||||
exports.Mandarin = Mandarin;
|
||||
exports.default = zh;
|
||||
|
||||
Object.defineProperty(exports, '__esModule', { value: true });
|
||||
|
||||
})));
|
||||
Vendored
+2
File diff suppressed because one or more lines are too long
@@ -792,6 +792,16 @@
|
||||
<en-US>Last Online Time</en-US>
|
||||
</lang>
|
||||
|
||||
<lang id="word-expiretime">
|
||||
<zh-CN>到期时间</zh-CN>
|
||||
<en-US>Expire Time</en-US>
|
||||
</lang>
|
||||
|
||||
<lang id="info-expiretime">
|
||||
<zh-CN>留空表示永不过期,到期后将自动暂停客户端</zh-CN>
|
||||
<en-US>Leave blank for no expiration. The client will be paused automatically when expired.</en-US>
|
||||
</lang>
|
||||
|
||||
<lang id="word-ipwhite">
|
||||
<zh-CN>启用IP白名单</zh-CN>
|
||||
<en-US> Enable IP White </en-US>
|
||||
|
||||
@@ -142,6 +142,14 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group" id="expire_time">
|
||||
<label class="control-label font-bold" langtag="word-expiretime"></label>
|
||||
<div class="col-sm-10">
|
||||
<input class="form-control flatpickr-expire" type="text" name="expire_time" placeholder="2026-12-31 23:59:59" autocomplete="off">
|
||||
<span class="help-block m-b-none" langtag="info-expiretime"></span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="hr-line-dashed"></div>
|
||||
<div class="form-group">
|
||||
<div class="col-sm-4 col-sm-offset-2">
|
||||
@@ -170,5 +178,18 @@ $(document).ready(function() {
|
||||
$('#black_ip_list').show();
|
||||
}
|
||||
});
|
||||
|
||||
// 到期时间选择器
|
||||
if (typeof flatpickr !== 'undefined') {
|
||||
flatpickr('.flatpickr-expire', {
|
||||
enableTime: true,
|
||||
enableSeconds: true,
|
||||
time_24hr: true,
|
||||
dateFormat: 'Y-m-d H:i:S',
|
||||
minDate: 'today',
|
||||
allowInput: true,
|
||||
locale: (flatpickr.l10ns && flatpickr.l10ns.zh) ? 'zh' : 'default'
|
||||
});
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
@@ -166,6 +166,15 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group" id="expire_time">
|
||||
<label class="control-label font-bold" langtag="word-expiretime"></label>
|
||||
<div class="col-sm-10">
|
||||
<input class="form-control flatpickr-expire" value="{{.c.ExpireTime}}" type="text" name="expire_time"
|
||||
placeholder="2026-12-31 23:59:59" autocomplete="off">
|
||||
<span class="help-block m-b-none" langtag="info-expiretime"></span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="hr-line-dashed"></div>
|
||||
<div class="form-group">
|
||||
<div class="col-sm-4 col-sm-offset-2">
|
||||
@@ -199,5 +208,17 @@
|
||||
$(document).ready(function () {
|
||||
// 页面加载时初始化显示状态
|
||||
changeIpWhite();
|
||||
|
||||
// 到期时间选择器
|
||||
if (typeof flatpickr !== 'undefined') {
|
||||
flatpickr('.flatpickr-expire', {
|
||||
enableTime: true,
|
||||
enableSeconds: true,
|
||||
time_24hr: true,
|
||||
dateFormat: 'Y-m-d H:i:S',
|
||||
allowInput: true,
|
||||
locale: (flatpickr.l10ns && flatpickr.l10ns.zh) ? 'zh' : 'default'
|
||||
});
|
||||
}
|
||||
});
|
||||
</script>
|
||||
@@ -73,7 +73,8 @@
|
||||
+ '<b langtag="word-ipwhitelist"></b>: ' + row.IpWhiteList + ' <br/>'
|
||||
+ '<b langtag="word-blackip"></b>: ' + row.BlackIpList + ' <br/><br/>'
|
||||
+ '<b langtag="word-createtime"></b>: ' + row.CreateTime + ' <br/>'
|
||||
+ '<b langtag="word-lastonlinetime"></b>: ' + row.LastOnlineTime + ' <br/><br/>'
|
||||
+ '<b langtag="word-lastonlinetime"></b>: ' + row.LastOnlineTime + ' <br/>'
|
||||
+ '<b langtag="word-expiretime"></b>: ' + (row.ExpireTime || '<span langtag="info-unrestricted"></span>') + ' <br/><br/>'
|
||||
+ '<b langtag="word-quicklycommand"></b>: <span>' + encodeToBase64(row.Remark +'|'+'{{.ip}}:{{.p}}|' + row.VerifyKey + '|false') + '</span> <button class="copy btn btn-info btn-xs" onclick="copyCommand(this)" data-clipboard-text="">复制</button><br/>'
|
||||
+ '<b langtag="word-tlsquicklycommand"></b>: <span>' + encodeToBase64(row.Remark +'|'+'{{.ip}}:{{.tls_p}}|' + row.VerifyKey + '|true') + '</span> <button class="copy btn btn-info btn-xs" onclick="copyCommand(this)" data-clipboard-text="">复制</button><br/>'
|
||||
+ '<b langtag="word-commandclient"></b>: ' + "<code>{{.win}} -server={{.ip}}:{{.p}} -vkey=" + row.VerifyKey + " -type=" +{{.bridgeType}} +"</code><button class=\"copy btn btn-info btn-xs\" onclick=\"copyCommand(this)\" data-clipboard-text=\"\">复制</button><br/>"
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
<link href="{{.web_base_url}}/static/css/bootstrap.min.css?v={{.version}}" rel="stylesheet">
|
||||
<link href="{{.web_base_url}}/static/css/bootstrap-table.min.css?v={{.version}}" rel="stylesheet">
|
||||
<link href="{{.web_base_url}}/static/css/style.css?v={{.version}}" rel="stylesheet">
|
||||
<link href="{{.web_base_url}}/static/css/flatpickr.min.css?v={{.version}}" rel="stylesheet">
|
||||
|
||||
<!-- Latest compiled and minified JavaScript -->
|
||||
<script src="{{.web_base_url}}/static/js/jquery-3.4.1.min.js?v={{.version}}"></script>
|
||||
@@ -28,6 +29,8 @@
|
||||
<script src="{{.web_base_url}}/static/js/bootstrap-table-locale-all.min.js?v={{.version}}"></script>
|
||||
<script src="{{.web_base_url}}/static/js/echarts.min.js?v={{.version}}"></script>
|
||||
<script src="{{.web_base_url}}/static/js/inspinia.js?v={{.version}}"></script>
|
||||
<script src="{{.web_base_url}}/static/js/flatpickr.min.js?v={{.version}}"></script>
|
||||
<script src="{{.web_base_url}}/static/js/flatpickr-l10n-zh.js?v={{.version}}"></script>
|
||||
<!-- Latest compiled and minified Locales -->
|
||||
<script src="{{.web_base_url}}/static/js/language.js?v={{.version}}" type="text/javascript"></script>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user