From 2d4ffde19ae509ddd50ec9a3a6fe5e926caf8230 Mon Sep 17 00:00:00 2001 From: gotoeasy Date: Sun, 29 Oct 2023 13:32:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=BB=91=E7=99=BD=E5=90=8D?= =?UTF-8?q?=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- glc/conf/config.go | 14 ++++++ glc/www/controller/log_search_controller.go | 3 ++ glc/www/controller/login_controller.go | 47 ++++++++++++++++++++ glc/www/controller/storage_mnt_controller.go | 9 ++++ 4 files changed, 73 insertions(+) diff --git a/glc/conf/config.go b/glc/conf/config.go index 3b12adb..d55aaf8 100644 --- a/glc/conf/config.go +++ b/glc/conf/config.go @@ -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 diff --git a/glc/www/controller/log_search_controller.go b/glc/www/controller/log_search_controller.go index 6d04a9c..e4d4ec4 100644 --- a/glc/www/controller/log_search_controller.go +++ b/glc/www/controller/log_search_controller.go @@ -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() // 登录检查 } diff --git a/glc/www/controller/login_controller.go b/glc/www/controller/login_controller.go index 9bf663d..b3c94fa 100644 --- a/glc/www/controller/login_controller.go +++ b/glc/www/controller/login_controller.go @@ -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 +} diff --git a/glc/www/controller/storage_mnt_controller.go b/glc/www/controller/storage_mnt_controller.go index 93786ed..019dd32 100644 --- a/glc/www/controller/storage_mnt_controller.go +++ b/glc/www/controller/storage_mnt_controller.go @@ -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() // 登录检查 }