mirror of
https://github.com/yhl452493373/frps-panel.git
synced 2026-04-04 14:27:00 +08:00
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c5e90c9e6c | ||
|
|
c8c6d17c9c | ||
|
|
dd87729455 | ||
|
|
e4d1caa4b9 | ||
|
|
1fdc0fc63a | ||
|
|
d09fc2fe91 | ||
|
|
70915e6e0e | ||
|
|
012a18ceb2 | ||
|
|
6ca0044dc3 | ||
|
|
9096585a34 | ||
|
|
11855f0917 |
6
Makefile
6
Makefile
@@ -1,8 +1,12 @@
|
||||
export GO111MODULE=on
|
||||
export CGO_ENABLED=0
|
||||
export GOOS=linux
|
||||
export GOARCH=amd64
|
||||
|
||||
build: frps-multiuser
|
||||
cp ./config/frps-multiuser.ini ./bin/frps-multiuser.ini
|
||||
cp -r ./assets/ ./bin/assets/
|
||||
|
||||
frps-multiuser:
|
||||
go build -o ./bin/frps-multiuser ./cmd/fp-multiuser
|
||||
rm -rf ./bin
|
||||
go build -o ./bin/frps-multiuser ./cmd/frps-multiuser
|
||||
|
||||
@@ -9,16 +9,16 @@ copy: build
|
||||
cp -r ./assets/ ./release/assets/
|
||||
|
||||
build:
|
||||
env CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o ./release/frps-multiuser-darwin-amd64 ./cmd/fp-multiuser
|
||||
env CGO_ENABLED=0 GOOS=freebsd GOARCH=386 go build -ldflags "$(LDFLAGS)" -o ./release/frps-multiuser-freebsd-386 ./cmd/fp-multiuser
|
||||
env CGO_ENABLED=0 GOOS=freebsd GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o ./release/frps-multiuser-freebsd-amd64 ./cmd/fp-multiuser
|
||||
env CGO_ENABLED=0 GOOS=linux GOARCH=386 go build -ldflags "$(LDFLAGS)" -o ./release/frps-multiuser-linux-386 ./cmd/fp-multiuser
|
||||
env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o ./release/frps-multiuser-linux-amd64 ./cmd/fp-multiuser
|
||||
env CGO_ENABLED=0 GOOS=linux GOARCH=arm go build -ldflags "$(LDFLAGS)" -o ./release/frps-multiuser-linux-arm ./cmd/fp-multiuser
|
||||
env CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags "$(LDFLAGS)" -o ./release/frps-multiuser-linux-arm64 ./cmd/fp-multiuser
|
||||
env CGO_ENABLED=0 GOOS=windows GOARCH=386 go build -ldflags "$(LDFLAGS)" -o ./release/frps-multiuser-windows-386.exe ./cmd/fp-multiuser
|
||||
env CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o ./release/frps-multiuser-windows-amd64.exe ./cmd/fp-multiuser
|
||||
env CGO_ENABLED=0 GOOS=linux GOARCH=mips64 go build -ldflags "$(LDFLAGS)" -o ./release/frps-multiuser-linux-mips64 ./cmd/fp-multiuser
|
||||
env CGO_ENABLED=0 GOOS=linux GOARCH=mips64le go build -ldflags "$(LDFLAGS)" -o ./release/frps-multiuser-linux-mips64le ./cmd/fp-multiuser
|
||||
env CGO_ENABLED=0 GOOS=linux GOARCH=mips GOMIPS=softfloat go build -ldflags "$(LDFLAGS)" -o ./release/frps-multiuser-linux-mips ./cmd/fp-multiuser
|
||||
env CGO_ENABLED=0 GOOS=linux GOARCH=mipsle GOMIPS=softfloat go build -ldflags "$(LDFLAGS)" -o ./release/frps-multiuser-linux-mipsle ./cmd/fp-multiuser
|
||||
env CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o ./release/frps-multiuser-darwin-amd64 ./cmd/frps-multiuser
|
||||
env CGO_ENABLED=0 GOOS=freebsd GOARCH=386 go build -ldflags "$(LDFLAGS)" -o ./release/frps-multiuser-freebsd-386 ./cmd/frps-multiuser
|
||||
env CGO_ENABLED=0 GOOS=freebsd GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o ./release/frps-multiuser-freebsd-amd64 ./cmd/frps-multiuser
|
||||
env CGO_ENABLED=0 GOOS=linux GOARCH=386 go build -ldflags "$(LDFLAGS)" -o ./release/frps-multiuser-linux-386 ./cmd/frps-multiuser
|
||||
env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o ./release/frps-multiuser-linux-amd64 ./cmd/frps-multiuser
|
||||
env CGO_ENABLED=0 GOOS=linux GOARCH=arm go build -ldflags "$(LDFLAGS)" -o ./release/frps-multiuser-linux-arm ./cmd/frps-multiuser
|
||||
env CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags "$(LDFLAGS)" -o ./release/frps-multiuser-linux-arm64 ./cmd/frps-multiuser
|
||||
env CGO_ENABLED=0 GOOS=windows GOARCH=386 go build -ldflags "$(LDFLAGS)" -o ./release/frps-multiuser-windows-386.exe ./cmd/frps-multiuser
|
||||
env CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o ./release/frps-multiuser-windows-amd64.exe ./cmd/frps-multiuser
|
||||
env CGO_ENABLED=0 GOOS=linux GOARCH=mips64 go build -ldflags "$(LDFLAGS)" -o ./release/frps-multiuser-linux-mips64 ./cmd/frps-multiuser
|
||||
env CGO_ENABLED=0 GOOS=linux GOARCH=mips64le go build -ldflags "$(LDFLAGS)" -o ./release/frps-multiuser-linux-mips64le ./cmd/frps-multiuser
|
||||
env CGO_ENABLED=0 GOOS=linux GOARCH=mips GOMIPS=softfloat go build -ldflags "$(LDFLAGS)" -o ./release/frps-multiuser-linux-mips ./cmd/frps-multiuser
|
||||
env CGO_ENABLED=0 GOOS=linux GOARCH=mipsle GOMIPS=softfloat go build -ldflags "$(LDFLAGS)" -o ./release/frps-multiuser-linux-mipsle ./cmd/frps-multiuser
|
||||
11
README.md
11
README.md
@@ -1,5 +1,7 @@
|
||||
# frps-multiuser
|
||||
|
||||
[README](README.md) | [中文文档](README_zh.md)
|
||||
|
||||
frp server plugin to support multiple users for [frp](https://github.com/fatedier/frp).
|
||||
|
||||
frps-multiuser will run as one single process and accept HTTP requests from frps.
|
||||
@@ -23,11 +25,11 @@ frps-multiuser will run as one single process and accept HTTP requests from frps
|
||||
***when a user is dynamic been `remove` or `disable`,it will take some time to be effective***
|
||||
***the limit of `ports`、`domains`、`subdomains` only effective at `NewProxy`***
|
||||
|
||||
[README](README.md) | [中文文档](README_zh.md)
|
||||
|
||||
### Features
|
||||
|
||||
* Support multiple user authentication by tokens saved in file.
|
||||
* Support dynamic `add`,`remove`,`disable` or `enable` user
|
||||
* Limit `ports`,`domains` and `subdomains` for each user
|
||||
|
||||
### Download
|
||||
|
||||
@@ -130,10 +132,13 @@ remote_port = 6000
|
||||
|
||||
## Issues & Ideas
|
||||
|
||||
___If you want visit mange ui from internet, you should change `plugin_addr` to `0.0.0.0`___
|
||||
|
||||
If you have any issues or ideas, put it on [issues](https://github.com/yhl452493373/frps-multiuser/issues). I will try my best to achieve it.
|
||||
|
||||
## Credits
|
||||
|
||||
+ [frp](https://github.com/fatedier/frp)
|
||||
+ [fp-multiuser](https://github.com/gofrp/fp-multiuser)
|
||||
+ [layui](https://github.com/layui/layui)
|
||||
+ [layui](https://github.com/layui/layui)
|
||||
+ [layui-theme-dark](https://github.com/Sight-wcg/layui-theme-dark)
|
||||
@@ -28,6 +28,8 @@ frps-multiuser 会以一个单独的进程运行,并接收 frps 发送过来
|
||||
### 功能
|
||||
|
||||
* 通过配置文件配置所有支持的用户名和 Token,只允许匹配的 frpc 客户端登录。
|
||||
* 动态`添加`、`删除`、`禁用`、`启用`用户
|
||||
* 对每个用户进行`端口`、`域名`、`二级域名`限制
|
||||
|
||||
### 下载
|
||||
|
||||
@@ -130,10 +132,13 @@ remote_port = 6000
|
||||
|
||||
## 使用
|
||||
|
||||
___如果要从外网访问管理界面, 需要把配置中的 `plugin_addr` 改为 `0.0.0.0`___
|
||||
|
||||
如果使用中有问题或者有其他想法,在[issues](https://github.com/yhl452493373/frps-multiuser/issues)上提出来。 如果我能搞定的话,我尽量搞。
|
||||
|
||||
## 致谢
|
||||
|
||||
+ [frp](https://github.com/fatedier/frp)
|
||||
+ [fp-multiuser](https://github.com/gofrp/fp-multiuser)
|
||||
+ [layui](https://github.com/layui/layui)
|
||||
+ [layui](https://github.com/layui/layui)
|
||||
+ [layui-theme-dark](https://github.com/Sight-wcg/layui-theme-dark)
|
||||
@@ -29,7 +29,8 @@
|
||||
"Other error": "Other error",
|
||||
"Param error": "Param error",
|
||||
"User exist": "User exist",
|
||||
"Token cannot be empty": "Token cannot be empty",
|
||||
"User format error": "User cannot be empty or include space char. It only allowed alphanumeric and underline.",
|
||||
"Token format error": "Token cannot be empty or include space char. It allow include those special char: _!@#$%^&*()",
|
||||
"Please check at least one user": "Please Check at least one user",
|
||||
"Operation confirm": "Operation confirm",
|
||||
"Empty data": "Empty data",
|
||||
|
||||
@@ -29,7 +29,8 @@
|
||||
"Other error": "其他异常",
|
||||
"Param error": "参数异常",
|
||||
"User exist": "用户已经存在",
|
||||
"Token cannot be empty": "Token 不能为空",
|
||||
"User format error": "用户不能为空或包含空格。只允许英文数字、字母、下划线",
|
||||
"Token format error": "Token不能为空或包含空格。允许的特殊符号:_!@#$%^&*()",
|
||||
"Please check at least one user": "请选中需要操作的用户",
|
||||
"Operation confirm": "操作确认",
|
||||
"Empty data": "无数据",
|
||||
@@ -38,7 +39,7 @@
|
||||
"Allowed domains": "允许域名",
|
||||
"Please input allowed domains": "请输入允许使用的域名,如:web01.domain.com,web02.domain.com",
|
||||
"Allowed subdomains": "允许子域名",
|
||||
"Please input allowed subdomains": "请输入允许使用的端口,如:web01,web02",
|
||||
"Please input allowed subdomains": "请输入允许使用的子域名,如:web01,web02",
|
||||
"Ports is invalid": "端口不正确",
|
||||
"Domains is invalid": "域名不正确",
|
||||
"Subdomains is invalid": "子域名不正确",
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -6,6 +6,36 @@ $(function () {
|
||||
Disable: 3
|
||||
}
|
||||
|
||||
/**
|
||||
* verify user value
|
||||
* @param username
|
||||
*/
|
||||
function verifyUser(username) {
|
||||
var valid = true;
|
||||
if (username.trim() === '' || !/^\w+$/.test(username)) {
|
||||
valid = false;
|
||||
}
|
||||
return {
|
||||
valid: valid,
|
||||
trim: username
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* verify token value
|
||||
* @param token
|
||||
*/
|
||||
function verifyToken(token) {
|
||||
var valid = true;
|
||||
if (token.trim() === '' || !/^[\w!@#$%^&*()]+$/.test(token)) {
|
||||
valid = false;
|
||||
}
|
||||
return {
|
||||
valid: valid,
|
||||
trim: token.trim()
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* verify comment is valid
|
||||
* @param comment
|
||||
@@ -120,15 +150,43 @@ $(function () {
|
||||
function langLoaded(lang) {
|
||||
//set verify rules
|
||||
var verifyRules = {
|
||||
user: function (value, item) {
|
||||
var result = verifyUser(value);
|
||||
if (!result.valid) {
|
||||
return lang['UserFormatError'];
|
||||
}
|
||||
if (item != null) {
|
||||
if (typeof item === "function") {
|
||||
item && item(result.trim);
|
||||
} else {
|
||||
$(item).val(result.trim);
|
||||
}
|
||||
}
|
||||
},
|
||||
token: function (value, item) {
|
||||
var result = verifyToken(value);
|
||||
if (!result.valid) {
|
||||
return lang['TokenFormatError'];
|
||||
}
|
||||
if (item != null) {
|
||||
if (typeof item === "function") {
|
||||
item && item(result.trim);
|
||||
} else {
|
||||
$(item).val(result.trim);
|
||||
}
|
||||
}
|
||||
},
|
||||
comment: function (value, item) {
|
||||
var result = verifyComment(value);
|
||||
if (!result.valid) {
|
||||
return lang['CommentInvalid'];
|
||||
}
|
||||
if (typeof item === "function") {
|
||||
item && item(result.trim);
|
||||
} else {
|
||||
$(item).val(result.trim);
|
||||
if (item != null) {
|
||||
if (typeof item === "function") {
|
||||
item && item(result.trim);
|
||||
} else {
|
||||
$(item).val(result.trim);
|
||||
}
|
||||
}
|
||||
},
|
||||
ports: function (value, item) {
|
||||
@@ -136,10 +194,12 @@ $(function () {
|
||||
if (!result.valid) {
|
||||
return lang['PortsInvalid'];
|
||||
}
|
||||
if (typeof item === "function") {
|
||||
item && item(result.trim);
|
||||
} else {
|
||||
$(item).val(result.trim);
|
||||
if (item != null) {
|
||||
if (typeof item === "function") {
|
||||
item && item(result.trim);
|
||||
} else {
|
||||
$(item).val(result.trim);
|
||||
}
|
||||
}
|
||||
},
|
||||
domains: function (value, item) {
|
||||
@@ -147,10 +207,12 @@ $(function () {
|
||||
if (!result.valid) {
|
||||
return lang['DomainsInvalid'];
|
||||
}
|
||||
if (typeof item === "function") {
|
||||
item && item(result.trim);
|
||||
} else {
|
||||
$(item).val(result.trim);
|
||||
if (item != null) {
|
||||
if (typeof item === "function") {
|
||||
item && item(result.trim);
|
||||
} else {
|
||||
$(item).val(result.trim);
|
||||
}
|
||||
}
|
||||
},
|
||||
subdomains: function (value, item) {
|
||||
@@ -158,14 +220,20 @@ $(function () {
|
||||
if (!result.valid) {
|
||||
return lang['SubdomainsInvalid'];
|
||||
}
|
||||
if (typeof item === "function") {
|
||||
item && item(result.trim);
|
||||
} else {
|
||||
$(item).val(result.trim);
|
||||
if (item != null) {
|
||||
if (typeof item === "function") {
|
||||
item && item(result.trim);
|
||||
} else {
|
||||
$(item).val(result.trim);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
layui.form.verify(verifyRules);
|
||||
|
||||
layui.form.set({
|
||||
verIncludeRequired: true,
|
||||
verify: verifyRules
|
||||
});
|
||||
|
||||
layui.table.render({
|
||||
elem: '#tokenTable',
|
||||
@@ -217,8 +285,11 @@ $(function () {
|
||||
var after = $.extend(true, {}, obj.data);
|
||||
var verifyMsg = false;
|
||||
if (field === 'token') {
|
||||
if (value.trim() === '') {
|
||||
layui.layer.msg(lang['TokenEmpty'])
|
||||
verifyMsg = verifyRules.token(value, function (trim) {
|
||||
updateTableField(obj, field, trim)
|
||||
});
|
||||
if (verifyMsg) {
|
||||
layui.layer.msg(verifyMsg);
|
||||
return obj.reedit();
|
||||
}
|
||||
|
||||
@@ -318,7 +389,7 @@ $(function () {
|
||||
btn: [lang['Confirm'], lang['Cancel']],
|
||||
btn1: function (index) {
|
||||
if (layui.form.validate('#addUserForm')) {
|
||||
add(layui.form.val('addUserForm'), index)
|
||||
add(layui.form.val('addUserForm'), index);
|
||||
}
|
||||
},
|
||||
btn2: function (index) {
|
||||
@@ -540,6 +611,12 @@ $(function () {
|
||||
reason = lang['ParamError'];
|
||||
else if (result.code === 2)
|
||||
reason = lang['UserExist'];
|
||||
else if (result.code === 3)
|
||||
reason = lang['ParamError'];
|
||||
else if (result.code === 4)
|
||||
reason = lang['UserFormatError'];
|
||||
else if (result.code === 5)
|
||||
reason = lang['TokenFormatError'];
|
||||
layui.layer.msg(lang['OperateFailed'] + ',' + reason)
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
<div class="layui-input-prefix">
|
||||
<i class="layui-icon layui-icon-username"></i>
|
||||
</div>
|
||||
<input type="text" name="user" value="" placeholder="${ .User }" class="layui-input" autocomplete="off"
|
||||
<input type="text" name="user" placeholder="${ .User }" class="layui-input" autocomplete="off"
|
||||
lay-affix="clear">
|
||||
</div>
|
||||
</div>
|
||||
@@ -73,21 +73,19 @@
|
||||
</div>
|
||||
</script>
|
||||
<script type="text/html" id="addTemplate">
|
||||
<div class="layui-form" id="addUserForm" lay-filter="addUserForm">
|
||||
<form class="layui-form" id="addUserForm" lay-filter="addUserForm">
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label">${ .User }</label>
|
||||
<div class="layui-input-block">
|
||||
<input type="text" name="user" placeholder="${ .PleaseInputUserAccount }" autocomplete="off"
|
||||
class="layui-input"
|
||||
lay-verify="required">
|
||||
<input type="text" name="user" lay-verify="user" placeholder="${ .PleaseInputUserAccount }"
|
||||
autocomplete="off" class="layui-input"/>
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label">${ .Token }</label>
|
||||
<div class="layui-input-block">
|
||||
<input type="text" name="token" lay-verify="required" placeholder="${ .PleaseInputUserToken }"
|
||||
class="layui-input"
|
||||
autocomplete="off">
|
||||
<input type="text" name="token" lay-verify="token" placeholder="${ .PleaseInputUserToken }"
|
||||
autocomplete="off" class="layui-input"/>
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-form-item layui-form-text">
|
||||
@@ -118,7 +116,7 @@
|
||||
autocomplete="off" class="layui-textarea"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"io/fs"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@@ -33,20 +34,29 @@ var rootCmd = &cobra.Command{
|
||||
log.Println(version)
|
||||
return nil
|
||||
}
|
||||
executable, err := os.Executable()
|
||||
if err != nil {
|
||||
log.Printf("error get program path: %v", err)
|
||||
return err
|
||||
}
|
||||
rootDir := filepath.Dir(executable)
|
||||
|
||||
common, tokens, ports, domains, subdomains, iniFile, err := ParseConfigFile(configFile)
|
||||
if err != nil {
|
||||
log.Printf("fail to start frps-multiuser : %v", err)
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
s, err := server.New(controller.HandleController{
|
||||
CommonInfo: common,
|
||||
Tokens: tokens,
|
||||
Ports: ports,
|
||||
Domains: domains,
|
||||
Subdomains: subdomains,
|
||||
ConfigFile: configFile,
|
||||
IniFile: iniFile,
|
||||
})
|
||||
s, err := server.New(
|
||||
rootDir,
|
||||
controller.HandleController{
|
||||
CommonInfo: common,
|
||||
Tokens: tokens,
|
||||
Ports: ports,
|
||||
Domains: domains,
|
||||
Subdomains: subdomains,
|
||||
ConfigFile: configFile,
|
||||
IniFile: iniFile,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -15,12 +15,16 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
Success = 0
|
||||
ParamError = 1
|
||||
UserExist = 2
|
||||
SaveError = 3
|
||||
Success = 0
|
||||
ParamError = 1
|
||||
UserExist = 2
|
||||
SaveError = 3
|
||||
UserFormatError = 4
|
||||
TokenFormatError = 5
|
||||
)
|
||||
|
||||
var UserFormatReg = regexp.MustCompile("^\\w+$")
|
||||
var TokenFormatReg = regexp.MustCompile("^[\\w!@#$%^&*()]+$")
|
||||
var TrimAllSpaceReg = regexp.MustCompile("[\\n\\t\\r\\s]")
|
||||
var TrimBreakLineReg = regexp.MustCompile("[\\n\\t\\r]")
|
||||
|
||||
@@ -210,7 +214,8 @@ func (c *HandleController) MakeLangFunc() func(context *gin.Context) {
|
||||
"OperateError": ginI18n.MustGetMessage(context, "Operate error"),
|
||||
"OperateFailed": ginI18n.MustGetMessage(context, "Operate failed"),
|
||||
"UserExist": ginI18n.MustGetMessage(context, "User exist"),
|
||||
"TokenEmpty": ginI18n.MustGetMessage(context, "Token cannot be empty"),
|
||||
"UserFormatError": ginI18n.MustGetMessage(context, "User format error"),
|
||||
"TokenFormatError": ginI18n.MustGetMessage(context, "Token format error"),
|
||||
"ShouldCheckUser": ginI18n.MustGetMessage(context, "Please check at least one user"),
|
||||
"OperationConfirm": ginI18n.MustGetMessage(context, "Operation confirm"),
|
||||
"EmptyData": ginI18n.MustGetMessage(context, "Empty data"),
|
||||
@@ -221,6 +226,7 @@ func (c *HandleController) MakeLangFunc() func(context *gin.Context) {
|
||||
"DomainsInvalid": ginI18n.MustGetMessage(context, "Domains is invalid"),
|
||||
"SubdomainsInvalid": ginI18n.MustGetMessage(context, "Subdomains is invalid"),
|
||||
"CommentInvalid": ginI18n.MustGetMessage(context, "Comment is invalid"),
|
||||
"ParamError": ginI18n.MustGetMessage(context, "Param error"),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -313,6 +319,14 @@ func (c *HandleController) MakeAddTokenFunc() func(context *gin.Context) {
|
||||
context.JSON(http.StatusOK, &response)
|
||||
return
|
||||
}
|
||||
if !UserFormatReg.MatchString(info.User) {
|
||||
log.Printf("user add failed, user format error")
|
||||
response.Success = false
|
||||
response.Code = UserFormatError
|
||||
response.Message = fmt.Sprintf("user add failed, user format error")
|
||||
context.JSON(http.StatusOK, &response)
|
||||
return
|
||||
}
|
||||
if _, exist := c.Tokens[info.User]; exist {
|
||||
log.Printf("user add failed, user [%v] exist", info.User)
|
||||
response.Success = false
|
||||
@@ -321,6 +335,16 @@ func (c *HandleController) MakeAddTokenFunc() func(context *gin.Context) {
|
||||
context.JSON(http.StatusOK, &response)
|
||||
return
|
||||
}
|
||||
if !TokenFormatReg.MatchString(info.Token) {
|
||||
log.Printf("user add failed, token format error")
|
||||
response.Success = false
|
||||
response.Code = TokenFormatError
|
||||
response.Message = fmt.Sprintf("user add failed, token format error")
|
||||
context.JSON(http.StatusOK, &response)
|
||||
return
|
||||
}
|
||||
replaceSpaceToken := TrimAllSpaceReg.ReplaceAllString(info.Token, "")
|
||||
info.Token = replaceSpaceToken
|
||||
c.Tokens[info.User] = info
|
||||
|
||||
usersSection, _ := c.IniFile.GetSection("users")
|
||||
@@ -388,7 +412,18 @@ func (c *HandleController) MakeUpdateTokensFunc() func(context *gin.Context) {
|
||||
comment := TrimBreakLineReg.ReplaceAllString(after.Comment, "")
|
||||
after.Comment = comment
|
||||
key.Comment = comment
|
||||
key.SetValue(after.Token)
|
||||
|
||||
if !TokenFormatReg.MatchString(after.Token) {
|
||||
log.Printf("update failed, token format error")
|
||||
response.Success = false
|
||||
response.Code = TokenFormatError
|
||||
response.Message = "user update failed, token format error "
|
||||
context.JSON(http.StatusOK, &response)
|
||||
return
|
||||
}
|
||||
replaceSpaceToken := TrimAllSpaceReg.ReplaceAllString(after.Token, "")
|
||||
after.Token = replaceSpaceToken
|
||||
key.SetValue(replaceSpaceToken)
|
||||
|
||||
if before.Ports != after.Ports {
|
||||
portsSection, _ := c.IniFile.GetSection("ports")
|
||||
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"gopkg.in/ini.v1"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@@ -24,9 +26,15 @@ func NewHandleController(config *HandleController) *HandleController {
|
||||
return config
|
||||
}
|
||||
|
||||
func (c *HandleController) Register(engine *gin.Engine) {
|
||||
func (c *HandleController) Register(rootDir string, engine *gin.Engine) {
|
||||
assets := filepath.Join(rootDir, "assets")
|
||||
_, err := os.Stat(assets)
|
||||
if err != nil && !os.IsExist(err) {
|
||||
assets = "./assets"
|
||||
}
|
||||
|
||||
engine.Delims("${", "}")
|
||||
engine.LoadHTMLGlob("./assets/templates/*")
|
||||
engine.LoadHTMLGlob(filepath.Join(assets, "templates/*"))
|
||||
engine.POST("/handler", c.MakeHandlerFunc())
|
||||
|
||||
var group *gin.RouterGroup
|
||||
@@ -37,7 +45,7 @@ func (c *HandleController) Register(engine *gin.Engine) {
|
||||
} else {
|
||||
group = engine.Group("/")
|
||||
}
|
||||
group.Static("/static", "./assets/static")
|
||||
group.Static("/static", filepath.Join(assets, "static"))
|
||||
group.GET("/", c.MakeManagerFunc())
|
||||
group.GET("/lang", c.MakeLangFunc())
|
||||
group.GET("/tokens", c.MakeQueryTokensFunc())
|
||||
|
||||
@@ -13,21 +13,24 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
cfg controller.HandleController
|
||||
s *http.Server
|
||||
done chan struct{}
|
||||
cfg controller.HandleController
|
||||
s *http.Server
|
||||
done chan struct{}
|
||||
rootDir string
|
||||
}
|
||||
|
||||
func New(cfg controller.HandleController) (*Server, error) {
|
||||
func New(rootDir string, cfg controller.HandleController) (*Server, error) {
|
||||
s := &Server{
|
||||
cfg: cfg,
|
||||
done: make(chan struct{}),
|
||||
cfg: cfg,
|
||||
done: make(chan struct{}),
|
||||
rootDir: rootDir,
|
||||
}
|
||||
if err := s.init(); err != nil {
|
||||
return nil, err
|
||||
@@ -74,14 +77,10 @@ func LoadSupportLanguage(dir string) ([]language.Tag, error) {
|
||||
var tags []language.Tag
|
||||
|
||||
files, err := os.Open(dir)
|
||||
if err != nil {
|
||||
log.Printf("error opening directory: %v", err)
|
||||
return tags, err
|
||||
}
|
||||
|
||||
fileList, err := files.Readdir(-1)
|
||||
if err != nil {
|
||||
log.Printf("error reading directory: %v", err)
|
||||
log.Printf("error read lang directory: %v", err)
|
||||
return tags, err
|
||||
}
|
||||
|
||||
@@ -103,15 +102,21 @@ func LoadSupportLanguage(dir string) ([]language.Tag, error) {
|
||||
return tags, nil
|
||||
}
|
||||
|
||||
func GinI18nLocalize() gin.HandlerFunc {
|
||||
dir := "./assets/lang"
|
||||
tags, err := LoadSupportLanguage(dir)
|
||||
func GinI18nLocalize(rootDir string) gin.HandlerFunc {
|
||||
assets := filepath.Join(rootDir, "assets")
|
||||
_, err := os.Stat(assets)
|
||||
if err != nil && !os.IsExist(err) {
|
||||
assets = "./assets"
|
||||
}
|
||||
lang := filepath.Join(assets, "lang")
|
||||
tags, err := LoadSupportLanguage(lang)
|
||||
if err != nil {
|
||||
log.Panicf("language file is not found: %v", err)
|
||||
}
|
||||
|
||||
return ginI18n.Localize(
|
||||
ginI18n.WithBundle(&ginI18n.BundleCfg{
|
||||
RootPath: dir,
|
||||
RootPath: lang,
|
||||
AcceptLanguage: tags,
|
||||
DefaultLanguage: language.Chinese,
|
||||
FormatBundleFile: "json",
|
||||
@@ -133,10 +138,10 @@ func GinI18nLocalize() gin.HandlerFunc {
|
||||
func (s *Server) initHTTPServer() error {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
engine := gin.New()
|
||||
engine.Use(GinI18nLocalize())
|
||||
engine.Use(GinI18nLocalize(s.rootDir))
|
||||
s.s = &http.Server{
|
||||
Handler: engine,
|
||||
}
|
||||
controller.NewHandleController(&s.cfg).Register(engine)
|
||||
controller.NewHandleController(&s.cfg).Register(s.rootDir, engine)
|
||||
return nil
|
||||
}
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 327 KiB After Width: | Height: | Size: 331 KiB |
Reference in New Issue
Block a user