mirror of
https://github.com/gotoeasy/glogcenter.git
synced 2025-09-15 12:58:34 +08:00
增加黑白名单
This commit is contained in:
parent
19c486d1a5
commit
2d4ffde19a
@ -49,6 +49,8 @@ var pageSize int
|
||||
var mulitLineSearch bool
|
||||
var testMode bool
|
||||
var tokenSalt string
|
||||
var aryWhite []string
|
||||
var aryBlack []string
|
||||
|
||||
func init() {
|
||||
UpdateConfigByEnv()
|
||||
@ -74,6 +76,8 @@ func UpdateConfigByEnv() {
|
||||
username = cmn.GetEnvStr("GLC_USERNAME", "glc") // 登录用户名,默认“glc”
|
||||
password = cmn.GetEnvStr("GLC_PASSWORD", "GLogCenter100%666") // 登录密码,默认“GLogCenter100%666”
|
||||
tokenSalt = cmn.GetEnvStr("GLC_TOKEN_SALT", "") // 令牌盐,默认“”
|
||||
aryWhite = cmn.Split(cmn.GetEnvStr("GLC_WHITE_LIST", ""), ",") // IP或区域白名单,逗号分隔,默认“”
|
||||
aryBlack = cmn.Split(cmn.GetEnvStr("GLC_BLACK_LIST", ""), ",") // IP或区域黑名单,逗号分隔,单个*代表全部,内网地址不受限制,默认“”
|
||||
clusterMode = cmn.GetEnvBool("GLC_CLUSTER_MODE", false) // 是否开启集群模式,默认false
|
||||
splitUrls(cmn.GetEnvStr("GLC_CLUSTER_URLS", "")) // 从服务器地址,多个时逗号分开,默认“”
|
||||
enableBackup = cmn.GetEnvBool("GLC_ENABLE_BACKUP", false) // 是否开启备份,默认false
|
||||
@ -90,6 +94,16 @@ func UpdateConfigByEnv() {
|
||||
testMode = cmn.GetEnvBool("GLC_TEST_MODE", false) // 是否测试模式,默认false
|
||||
}
|
||||
|
||||
// 取配置: 白名单,可通过环境变量“GLC_WHITE_LIST”设定,默认“”
|
||||
func GetWhiteList() []string {
|
||||
return aryWhite
|
||||
}
|
||||
|
||||
// 取配置: 黑名单,可通过环境变量“GLC_BLACK_LIST”设定,默认“”
|
||||
func GetBlackList() []string {
|
||||
return aryBlack
|
||||
}
|
||||
|
||||
// 取配置: 令牌盐,可通过环境变量“GLC_TOKEN_SALT”设定,默认“”
|
||||
func GetTokenSalt() string {
|
||||
return tokenSalt
|
||||
|
||||
@ -11,6 +11,9 @@ import (
|
||||
// 日志检索(表单提交方式)
|
||||
func LogSearchController(req *gweb.HttpRequest) *gweb.HttpResult {
|
||||
|
||||
if !InWhiteList(req) && InBlackList(req) {
|
||||
return gweb.Error403() // 黑名单,访问受限
|
||||
}
|
||||
if conf.IsEnableLogin() && req.GetFormParameter("token") != GetSessionid() {
|
||||
return gweb.Error403() // 登录检查
|
||||
}
|
||||
|
||||
@ -28,6 +28,11 @@ func init() {
|
||||
}
|
||||
|
||||
func LoginController(req *gweb.HttpRequest) *gweb.HttpResult {
|
||||
|
||||
if !InWhiteList(req) && InBlackList(req) {
|
||||
return gweb.Error403() // 黑名单,访问受限
|
||||
}
|
||||
|
||||
username := req.GetFormParameter("username")
|
||||
password := req.GetFormParameter("password")
|
||||
key := getClientHash(req)
|
||||
@ -86,3 +91,45 @@ func getClientHash(req *gweb.HttpRequest) string {
|
||||
ary = append(ary, req.GinCtx.ClientIP())
|
||||
return cmn.HashString(cmn.Join(ary, ","))
|
||||
}
|
||||
|
||||
// 客户端IP是否在白名单中(内网地址总是在白名单中)
|
||||
func InWhiteList(req *gweb.HttpRequest) bool {
|
||||
cityIp := cmn.GetCityIp(req.GinCtx.ClientIP())
|
||||
if cmn.Contains(cityIp, "内网") {
|
||||
return true
|
||||
}
|
||||
for i := 0; i < len(conf.GetWhiteList()); i++ {
|
||||
item := conf.GetWhiteList()[i]
|
||||
if item == "" {
|
||||
continue
|
||||
}
|
||||
if cmn.Endwiths(item, ".*") {
|
||||
item = cmn.ReplaceAll(item, "*", "") // 支持IP的最后一段使用通配符*
|
||||
}
|
||||
if cmn.Contains(cityIp, item) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// 客户端IP是否在黑名单中(内网地址总是在白名单中)
|
||||
func InBlackList(req *gweb.HttpRequest) bool {
|
||||
cityIp := cmn.GetCityIp(req.GinCtx.ClientIP())
|
||||
for i := 0; i < len(conf.GetBlackList()); i++ {
|
||||
item := conf.GetBlackList()[i]
|
||||
if item == "" {
|
||||
continue
|
||||
}
|
||||
if item == "*" {
|
||||
return true
|
||||
}
|
||||
if cmn.Endwiths(item, ".*") {
|
||||
item = cmn.ReplaceAll(item, "*", "") // 支持IP的最后一段使用通配符*
|
||||
}
|
||||
if cmn.Contains(cityIp, item) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@ -28,6 +28,9 @@ func VersionController(req *gweb.HttpRequest) *gweb.HttpResult {
|
||||
|
||||
// 查询日志仓名称列表
|
||||
func StorageNamesController(req *gweb.HttpRequest) *gweb.HttpResult {
|
||||
if !InWhiteList(req) && InBlackList(req) {
|
||||
return gweb.Error403() // 黑名单,访问受限
|
||||
}
|
||||
if conf.IsEnableLogin() && req.GetFormParameter("token") != GetSessionid() {
|
||||
return gweb.Error403() // 登录检查
|
||||
}
|
||||
@ -38,6 +41,9 @@ func StorageNamesController(req *gweb.HttpRequest) *gweb.HttpResult {
|
||||
|
||||
// 查询日志仓信息列表
|
||||
func StorageListController(req *gweb.HttpRequest) *gweb.HttpResult {
|
||||
if !InWhiteList(req) && InBlackList(req) {
|
||||
return gweb.Error403() // 黑名单,访问受限
|
||||
}
|
||||
if conf.IsEnableLogin() && req.GetFormParameter("token") != GetSessionid() {
|
||||
return gweb.Error403() // 登录检查
|
||||
}
|
||||
@ -48,6 +54,9 @@ func StorageListController(req *gweb.HttpRequest) *gweb.HttpResult {
|
||||
|
||||
// 删除指定日志仓
|
||||
func StorageDeleteController(req *gweb.HttpRequest) *gweb.HttpResult {
|
||||
if !InWhiteList(req) && InBlackList(req) {
|
||||
return gweb.Error403() // 黑名单,访问受限
|
||||
}
|
||||
if conf.IsEnableLogin() && req.GetFormParameter("token") != GetSessionid() {
|
||||
return gweb.Error403() // 登录检查
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user