mirror of
https://github.com/Rayzggz/server_torii.git
synced 2025-06-17 04:31:22 +08:00
fix: clearance failure caused by UA changes
This commit is contained in:
5
go.mod
5
go.mod
@ -2,4 +2,7 @@ module server_torii
|
|||||||
|
|
||||||
go 1.23.5
|
go 1.23.5
|
||||||
|
|
||||||
require gopkg.in/yaml.v3 v3.0.1
|
require (
|
||||||
|
github.com/mssola/useragent v1.0.0
|
||||||
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
|
)
|
||||||
|
@ -12,6 +12,7 @@ import (
|
|||||||
"server_torii/internal/action"
|
"server_torii/internal/action"
|
||||||
"server_torii/internal/config"
|
"server_torii/internal/config"
|
||||||
"server_torii/internal/dataType"
|
"server_torii/internal/dataType"
|
||||||
|
"server_torii/internal/utils"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -96,7 +97,7 @@ func CheckCaptcha(r *http.Request, reqData dataType.UserRequest, ruleSet *config
|
|||||||
func GenClearance(reqData dataType.UserRequest, ruleSet config.RuleSet) []byte {
|
func GenClearance(reqData dataType.UserRequest, ruleSet config.RuleSet) []byte {
|
||||||
timeNow := time.Now().Unix()
|
timeNow := time.Now().Unix()
|
||||||
mac := hmac.New(sha512.New, []byte(ruleSet.CAPTCHARule.SecretKey))
|
mac := hmac.New(sha512.New, []byte(ruleSet.CAPTCHARule.SecretKey))
|
||||||
mac.Write([]byte(fmt.Sprintf("%d%s%sCAPTCHA-CLEARANCE", timeNow, reqData.Host, reqData.UserAgent)))
|
mac.Write([]byte(fmt.Sprintf("%d%s%sCAPTCHA-CLEARANCE", timeNow, reqData.Host, utils.GetClearanceUserAgent(reqData.UserAgent))))
|
||||||
return []byte(fmt.Sprintf("%s:%s", fmt.Sprintf("%d", time.Now().Unix()), fmt.Sprintf("%x", mac.Sum(nil))))
|
return []byte(fmt.Sprintf("%s:%s", fmt.Sprintf("%d", time.Now().Unix()), fmt.Sprintf("%x", mac.Sum(nil))))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,7 +124,7 @@ func verifyClearanceCookie(reqData dataType.UserRequest, ruleSet config.RuleSet)
|
|||||||
}
|
}
|
||||||
|
|
||||||
mac := hmac.New(sha512.New, []byte(ruleSet.CAPTCHARule.SecretKey))
|
mac := hmac.New(sha512.New, []byte(ruleSet.CAPTCHARule.SecretKey))
|
||||||
mac.Write([]byte(fmt.Sprintf("%d%s%sCAPTCHA-CLEARANCE", parsedTimestamp, reqData.Host, reqData.UserAgent)))
|
mac.Write([]byte(fmt.Sprintf("%d%s%sCAPTCHA-CLEARANCE", parsedTimestamp, reqData.Host, utils.GetClearanceUserAgent(reqData.UserAgent))))
|
||||||
computedHash := fmt.Sprintf("%x", mac.Sum(nil))
|
computedHash := fmt.Sprintf("%x", mac.Sum(nil))
|
||||||
|
|
||||||
return hmac.Equal([]byte(computedHash), []byte(expectedHash))
|
return hmac.Equal([]byte(computedHash), []byte(expectedHash))
|
||||||
|
21
internal/utils/UAParser.go
Normal file
21
internal/utils/UAParser.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/mssola/useragent"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetClearanceUserAgent(inputUA string) string {
|
||||||
|
if len(inputUA) < 8 || inputUA[:8] != "Mozilla/" {
|
||||||
|
return inputUA
|
||||||
|
}
|
||||||
|
|
||||||
|
ua := useragent.New(inputUA)
|
||||||
|
|
||||||
|
engin, enginVersion := ua.Engine()
|
||||||
|
browser, browserVersion := ua.Browser()
|
||||||
|
|
||||||
|
ret := fmt.Sprintf("Mozilla:%v,Module:%v,Platform:%v,OS:%v,Engine:%v,EngineVersion:%v,Browser:%v,BrowserVersion:%v", ua.Mozilla(), ua.Model(), ua.Platform(), ua.OS(), engin, enginVersion, browser, browserVersion)
|
||||||
|
return ret
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user