mirror of
https://gitee.com/samwaf/SamWaf.git
synced 2026-11-02 19:21:44 +08:00
!8 feat:add host default encoding
Merge pull request !8 from samwaf/feat_IC8E8F
This commit is contained in:
@@ -38,6 +38,7 @@ type Hosts struct {
|
||||
CaptchaJSON string `json:"captcha_json"` //验证码配置 json
|
||||
AntiLeechJSON string `json:"anti_leech_json"` //防盗链配置 json
|
||||
CacheJSON string `json:"cache_json"` //缓存配置 json
|
||||
DefaultEncoding string `json:"default_encoding"` //默认编码 utf-8 或者 gbk auto字符串自动选择
|
||||
}
|
||||
|
||||
type HostsDefense struct {
|
||||
|
||||
@@ -33,6 +33,7 @@ type WafHostAddReq struct {
|
||||
CaptchaJSON string `json:"captcha_json"` //验证码配置 json
|
||||
AntiLeechJSON string `json:"anti_leech_json"` //防盗链配置 json
|
||||
CacheJSON string `json:"cache_json"` //缓存配置 json
|
||||
DefaultEncoding string `json:"default_encoding"` //默认编码 utf-8 或者 gbk auto字符串自动选择
|
||||
}
|
||||
|
||||
type WafHostDelReq struct {
|
||||
@@ -74,6 +75,7 @@ type WafHostEditReq struct {
|
||||
CaptchaJSON string `json:"captcha_json"` //验证码配置 json
|
||||
AntiLeechJSON string `json:"anti_leech_json"` //防盗链配置 json
|
||||
CacheJSON string `json:"cache_json"` //缓存配置 json
|
||||
DefaultEncoding string `json:"default_encoding"` //默认编码 utf-8 或者 gbk auto字符串自动选择
|
||||
}
|
||||
|
||||
type WafHostGuardStatusReq struct {
|
||||
|
||||
@@ -64,6 +64,7 @@ func (receiver *WafHostService) AddApi(wafHostAddReq request.WafHostAddReq) (str
|
||||
CaptchaJSON: wafHostAddReq.CaptchaJSON,
|
||||
AntiLeechJSON: wafHostAddReq.AntiLeechJSON,
|
||||
CacheJSON: wafHostAddReq.CacheJSON,
|
||||
DefaultEncoding: wafHostAddReq.DefaultEncoding,
|
||||
}
|
||||
global.GWAF_LOCAL_DB.Create(wafHost)
|
||||
return wafHost.Code, nil
|
||||
@@ -118,6 +119,7 @@ func (receiver *WafHostService) ModifyApi(wafHostEditReq request.WafHostEditReq)
|
||||
"CaptchaJSON": wafHostEditReq.CaptchaJSON,
|
||||
"AntiLeechJSON": wafHostEditReq.AntiLeechJSON,
|
||||
"CacheJSON": wafHostEditReq.CacheJSON,
|
||||
"DefaultEncoding": wafHostEditReq.DefaultEncoding,
|
||||
}
|
||||
err := global.GWAF_LOCAL_DB.Debug().Model(model.Hosts{}).Where("CODE=?", wafHostEditReq.CODE).Updates(hostMap).Error
|
||||
|
||||
|
||||
+7
-1
@@ -89,7 +89,13 @@ func pathCoreSql(db *gorm.DB) {
|
||||
} else {
|
||||
zlog.Info("db", "anti_ccs :ip_mode init successfully")
|
||||
}
|
||||
|
||||
//20250519 默认编码设置
|
||||
err = db.Exec("UPDATE hosts SET default_encoding='auto' WHERE default_encoding IS NULL").Error
|
||||
if err != nil {
|
||||
panic("failed to hosts: default_encoding " + err.Error())
|
||||
} else {
|
||||
zlog.Info("db", "hosts: default_encoding init successfully")
|
||||
}
|
||||
// 记录结束时间并计算耗时
|
||||
duration := time.Since(startTime)
|
||||
zlog.Info("create core default value completely", "duration", duration.String())
|
||||
|
||||
@@ -76,7 +76,7 @@ func (waf *WafEngine) compressContent(res *http.Response, isStaticAssist bool, i
|
||||
}
|
||||
|
||||
// 获取原始内容
|
||||
func (waf *WafEngine) getOrgContent(resp *http.Response, isStaticAssist bool) (cntBytes []byte, encodeType string, err error) {
|
||||
func (waf *WafEngine) getOrgContent(resp *http.Response, isStaticAssist bool, defaultEncoding string) (cntBytes []byte, encodeType string, err error) {
|
||||
bodyBytes, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return bodyBytes, "", fmt.Errorf("读取原始响应体失败: %v", err)
|
||||
@@ -116,7 +116,19 @@ func (waf *WafEngine) getOrgContent(resp *http.Response, isStaticAssist bool) (c
|
||||
contentType := resp.Header.Get("Content-Type")
|
||||
var currentEncoding encoding.Encoding
|
||||
|
||||
if strings.Contains(contentType, "charset=") {
|
||||
if defaultEncoding != "" {
|
||||
switch strings.ToLower(defaultEncoding) {
|
||||
case "utf-8", "utf8":
|
||||
currentEncoding = unicode.UTF8
|
||||
charsetNameFinal = "utf-8"
|
||||
break
|
||||
case "gbk", "gb2312":
|
||||
currentEncoding = simplifiedchinese.GBK
|
||||
charsetNameFinal = "gbk"
|
||||
break
|
||||
default:
|
||||
}
|
||||
} else if strings.Contains(contentType, "charset=") {
|
||||
charsetName := ""
|
||||
parts := strings.Split(contentType, ";")
|
||||
for _, part := range parts {
|
||||
|
||||
@@ -763,7 +763,7 @@ func (waf *WafEngine) modifyResponse() func(*http.Response) error {
|
||||
}
|
||||
}
|
||||
if ldpFlag == true {
|
||||
orgContentBytes, charsetName, responseEncodingError := waf.getOrgContent(resp, isStaticAssist)
|
||||
orgContentBytes, charsetName, responseEncodingError := waf.getOrgContent(resp, isStaticAssist, waf.HostTarget[host].Host.DefaultEncoding)
|
||||
if responseEncodingError == nil {
|
||||
newPayload := []byte("" + utils.DeSenText(string(orgContentBytes)))
|
||||
finalCompressBytes, _ := waf.compressContent(resp, isStaticAssist, newPayload, charsetName)
|
||||
@@ -774,12 +774,12 @@ func (waf *WafEngine) modifyResponse() func(*http.Response) error {
|
||||
resp.Header.Set("Content-Length", strconv.FormatInt(int64(len(finalCompressBytes)), 10))
|
||||
} else {
|
||||
resp.Body = io.NopCloser(bytes.NewBuffer(orgContentBytes))
|
||||
zlog.Warn(fmt.Sprintf("识别响应内容编码失败,隐私防护不可用 %v,请求URL: %s", responseEncodingError, r.URL.String()))
|
||||
zlog.Warn(fmt.Sprintf("识别响应内容编码失败,隐私防护不可用 %v,请求URL: %s ,可以在主机其他设置里面设置默认编码", responseEncodingError, r.URL.String()))
|
||||
}
|
||||
|
||||
}
|
||||
//编码转换,自动检测网页编码 resp *http.Response
|
||||
orgContentBytes, charsetName, responseEncodingError := waf.getOrgContent(resp, isStaticAssist)
|
||||
orgContentBytes, charsetName, responseEncodingError := waf.getOrgContent(resp, isStaticAssist, waf.HostTarget[host].Host.DefaultEncoding)
|
||||
if responseEncodingError == nil {
|
||||
if global.GCONFIG_RECORD_RESP == 1 {
|
||||
if resp.ContentLength < global.GCONFIG_RECORD_MAX_RES_BODY_LENGTH {
|
||||
@@ -821,7 +821,7 @@ func (waf *WafEngine) modifyResponse() func(*http.Response) error {
|
||||
|
||||
} else {
|
||||
resp.Body = io.NopCloser(bytes.NewBuffer(orgContentBytes))
|
||||
zlog.Warn(fmt.Sprintf("识别响应内容编码失败,响应日志,敏感词替换 不可用 %v,请求URL: %s", responseEncodingError, r.URL.String()))
|
||||
zlog.Warn(fmt.Sprintf("识别响应内容编码失败,响应日志,敏感词替换 不可用 %v,请求URL: %s ,可以在主机其他设置里面设置默认编码", responseEncodingError, r.URL.String()))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user