mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-05 09:30:26 +08:00
40 lines
974 B
Go
40 lines
974 B
Go
|
|
package waf
|
||
|
|
|
||
|
|
import (
|
||
|
|
"github.com/TeaOSLab/EdgeNode/internal/waf/requests"
|
||
|
|
"github.com/iwind/TeaGo/types"
|
||
|
|
stringutil "github.com/iwind/TeaGo/utils/string"
|
||
|
|
"net/http"
|
||
|
|
"net/url"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
var captchaSalt = stringutil.Rand(32)
|
||
|
|
|
||
|
|
const (
|
||
|
|
CaptchaSeconds = 600 // 10 minutes
|
||
|
|
)
|
||
|
|
|
||
|
|
type CaptchaAction struct {
|
||
|
|
}
|
||
|
|
|
||
|
|
func (this *CaptchaAction) Perform(waf *WAF, request *requests.Request, writer http.ResponseWriter) (allow bool) {
|
||
|
|
// TEAWEB_CAPTCHA:
|
||
|
|
cookie, err := request.Cookie("TEAWEB_WAF_CAPTCHA")
|
||
|
|
if err == nil && cookie != nil && len(cookie.Value) > 32 {
|
||
|
|
m := cookie.Value[:32]
|
||
|
|
timestamp := cookie.Value[32:]
|
||
|
|
if stringutil.Md5(captchaSalt+timestamp) == m && time.Now().Unix() < types.Int64(timestamp) { // verify md5
|
||
|
|
return true
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
refURL := request.URL.String()
|
||
|
|
if len(request.Referer()) > 0 {
|
||
|
|
refURL = request.Referer()
|
||
|
|
}
|
||
|
|
http.Redirect(writer, request.Raw(), "/WAFCAPTCHA?url="+url.QueryEscape(refURL), http.StatusTemporaryRedirect)
|
||
|
|
|
||
|
|
return false
|
||
|
|
}
|