mirror of
https://github.com/Rayzggz/server_torii.git
synced 2025-06-23 07:21:32 +08:00
revert: Let Current Waiting room become External Migration
This commit is contained in:
@ -15,7 +15,8 @@ VerifyBot:
|
|||||||
verify_yandex_bot: true
|
verify_yandex_bot: true
|
||||||
verify_sogou_bot: true
|
verify_sogou_bot: true
|
||||||
verify_apple_bot: true
|
verify_apple_bot: true
|
||||||
WaitingRoom:
|
ExternalMigration:
|
||||||
enabled: false
|
enabled: false
|
||||||
max_concurrent_user: 1000
|
redirect_url: "https://example.com/migration"
|
||||||
|
secret_key: "0378b0f84c4310279918d71a5647ba5d"
|
||||||
session_timeout: 1800
|
session_timeout: 1800
|
21
internal/check/ExternalMigration.go
Normal file
21
internal/check/ExternalMigration.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package check
|
||||||
|
|
||||||
|
import (
|
||||||
|
"server_torii/internal/action"
|
||||||
|
"server_torii/internal/config"
|
||||||
|
"server_torii/internal/dataType"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ExternalMigration(reqData dataType.UserRequest, ruleSet *config.RuleSet, decision *action.Decision, sharedMem *dataType.SharedMemory) {
|
||||||
|
if !ruleSet.ExternalMigrationRule.Enabled {
|
||||||
|
decision.Set(action.Continue)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !verifyClearanceCookie(reqData, *ruleSet) {
|
||||||
|
decision.SetResponse(action.Done, []byte("EXTERNAL"), genSessionID(reqData, *ruleSet))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
decision.Set(action.Continue)
|
||||||
|
}
|
@ -1,22 +0,0 @@
|
|||||||
package check
|
|
||||||
|
|
||||||
import (
|
|
||||||
"server_torii/internal/action"
|
|
||||||
"server_torii/internal/config"
|
|
||||||
"server_torii/internal/dataType"
|
|
||||||
)
|
|
||||||
|
|
||||||
func WaitingRoom(reqData dataType.UserRequest, ruleSet *config.RuleSet, decision *action.Decision, sharedMem *dataType.SharedMemory) {
|
|
||||||
if !ruleSet.WaitingRoomRule.Enabled {
|
|
||||||
decision.Set(action.Continue)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if !VerifyWaitingRoomClearance(reqData, *ruleSet) {
|
|
||||||
decision.SetResponse(action.Done, []byte("WAITING-ROOM"), genSessionID(reqData, *ruleSet))
|
|
||||||
return
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
decision.Set(action.Continue)
|
|
||||||
}
|
|
@ -73,7 +73,7 @@ type RuleSet struct {
|
|||||||
CAPTCHARule *dataType.CaptchaRule
|
CAPTCHARule *dataType.CaptchaRule
|
||||||
VerifyBotRule *dataType.VerifyBotRule
|
VerifyBotRule *dataType.VerifyBotRule
|
||||||
HTTPFloodRule *dataType.HTTPFloodRule
|
HTTPFloodRule *dataType.HTTPFloodRule
|
||||||
WaitingRoomRule *dataType.WaitingRoomRule
|
ExternalMigrationRule *dataType.ExternalMigrationRule
|
||||||
}
|
}
|
||||||
|
|
||||||
// ruleSetWrapper
|
// ruleSetWrapper
|
||||||
@ -81,7 +81,7 @@ type ruleSetWrapper struct {
|
|||||||
CAPTCHARule *dataType.CaptchaRule `yaml:"CAPTCHA"`
|
CAPTCHARule *dataType.CaptchaRule `yaml:"CAPTCHA"`
|
||||||
VerifyBotRule *dataType.VerifyBotRule `yaml:"VerifyBot"`
|
VerifyBotRule *dataType.VerifyBotRule `yaml:"VerifyBot"`
|
||||||
HTTPFloodRule httpFloodRuleWrapper `yaml:"HTTPFlood"`
|
HTTPFloodRule httpFloodRuleWrapper `yaml:"HTTPFlood"`
|
||||||
WaitingRoomRule *dataType.WaitingRoomRule `yaml:"WaitingRoom"`
|
ExternalMigrationRule *dataType.ExternalMigrationRule `yaml:"ExternalMigration"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type httpFloodRuleWrapper struct {
|
type httpFloodRuleWrapper struct {
|
||||||
@ -99,7 +99,7 @@ func LoadRules(rulePath string) (*RuleSet, error) {
|
|||||||
CAPTCHARule: &dataType.CaptchaRule{},
|
CAPTCHARule: &dataType.CaptchaRule{},
|
||||||
VerifyBotRule: &dataType.VerifyBotRule{},
|
VerifyBotRule: &dataType.VerifyBotRule{},
|
||||||
HTTPFloodRule: &dataType.HTTPFloodRule{},
|
HTTPFloodRule: &dataType.HTTPFloodRule{},
|
||||||
WaitingRoomRule: &dataType.WaitingRoomRule{},
|
ExternalMigrationRule: &dataType.ExternalMigrationRule{},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load IP Allow List
|
// Load IP Allow List
|
||||||
@ -152,8 +152,8 @@ func loadServerRules(YAMLFile string, rs RuleSet) (*RuleSet, error) {
|
|||||||
|
|
||||||
*rs.CAPTCHARule = *wrapper.CAPTCHARule
|
*rs.CAPTCHARule = *wrapper.CAPTCHARule
|
||||||
*rs.VerifyBotRule = *wrapper.VerifyBotRule
|
*rs.VerifyBotRule = *wrapper.VerifyBotRule
|
||||||
if wrapper.WaitingRoomRule != nil {
|
if wrapper.ExternalMigrationRule != nil {
|
||||||
*rs.WaitingRoomRule = *wrapper.WaitingRoomRule
|
*rs.ExternalMigrationRule = *wrapper.ExternalMigrationRule
|
||||||
}
|
}
|
||||||
|
|
||||||
rs.HTTPFloodRule.HTTPFloodSpeedLimit = make(map[int64]int64)
|
rs.HTTPFloodRule.HTTPFloodSpeedLimit = make(map[int64]int64)
|
||||||
|
@ -31,14 +31,14 @@ type HTTPFloodRule struct {
|
|||||||
HTTPFloodSameURILimit map[int64]int64
|
HTTPFloodSameURILimit map[int64]int64
|
||||||
}
|
}
|
||||||
|
|
||||||
type WaitingRoomRule struct {
|
type ExternalMigrationRule struct {
|
||||||
Enabled bool `yaml:"enabled"`
|
Enabled bool `yaml:"enabled"`
|
||||||
MaxConcurrentUser int64 `yaml:"max_concurrent_user"`
|
RedirectUrl string `yaml:"redirect_url"`
|
||||||
|
SecretKey string `yaml:"secret_key"`
|
||||||
SessionTimeout int64 `yaml:"session_timeout"`
|
SessionTimeout int64 `yaml:"session_timeout"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type SharedMemory struct {
|
type SharedMemory struct {
|
||||||
HTTPFloodSpeedLimitCounter *Counter
|
HTTPFloodSpeedLimitCounter *Counter
|
||||||
HTTPFloodSameURILimitCounter *Counter
|
HTTPFloodSameURILimitCounter *Counter
|
||||||
WaitingRoom *WaitingRoom
|
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,7 @@ func CheckMain(w http.ResponseWriter, userRequestData dataType.UserRequest, rule
|
|||||||
checkFuncs = append(checkFuncs, check.URLBlockList)
|
checkFuncs = append(checkFuncs, check.URLBlockList)
|
||||||
checkFuncs = append(checkFuncs, check.VerifyBot)
|
checkFuncs = append(checkFuncs, check.VerifyBot)
|
||||||
checkFuncs = append(checkFuncs, check.HTTPFlood)
|
checkFuncs = append(checkFuncs, check.HTTPFlood)
|
||||||
checkFuncs = append(checkFuncs, check.WaitingRoom)
|
checkFuncs = append(checkFuncs, check.ExternalMigration)
|
||||||
checkFuncs = append(checkFuncs, check.Captcha)
|
checkFuncs = append(checkFuncs, check.Captcha)
|
||||||
|
|
||||||
for _, checkFunc := range checkFuncs {
|
for _, checkFunc := range checkFuncs {
|
||||||
@ -107,35 +107,6 @@ func CheckMain(w http.ResponseWriter, userRequestData dataType.UserRequest, rule
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if bytes.Compare(decision.HTTPCode, []byte("WAITING_ROOM")) == 0 {
|
|
||||||
tpl, err := template.ParseFiles(cfg.ErrorPage + "/waiting_room.html")
|
|
||||||
if err != nil {
|
|
||||||
utils.LogError(userRequestData, fmt.Sprintf("Error parsing template: %v", err), "CheckMain")
|
|
||||||
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
sessionID := string(decision.ResponseData)
|
|
||||||
|
|
||||||
data := struct {
|
|
||||||
EdgeTag string
|
|
||||||
ConnectIP string
|
|
||||||
Date string
|
|
||||||
}{
|
|
||||||
EdgeTag: cfg.NodeName,
|
|
||||||
ConnectIP: userRequestData.RemoteIP,
|
|
||||||
Date: time.Now().Format("2006-01-02 15:04:05"),
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Header().Set("Set-Cookie", "__torii_session_id="+sessionID+"; Path=/; Max-Age=86400; Priority=High; HttpOnly;")
|
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
||||||
w.WriteHeader(http.StatusServiceUnavailable)
|
|
||||||
if err = tpl.Execute(w, data); err != nil {
|
|
||||||
utils.LogError(userRequestData, fmt.Sprintf("Error executing template: %v", err), "CheckMain")
|
|
||||||
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
//should never happen
|
//should never happen
|
||||||
utils.LogError(userRequestData, fmt.Sprintf("Error access in wrong state: %v", decision), "CheckMain")
|
utils.LogError(userRequestData, fmt.Sprintf("Error access in wrong state: %v", decision), "CheckMain")
|
||||||
|
@ -2,7 +2,6 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
"server_torii/internal/action"
|
"server_torii/internal/action"
|
||||||
@ -21,12 +20,6 @@ func CheckTorii(w http.ResponseWriter, r *http.Request, reqData dataType.UserReq
|
|||||||
check.CheckCaptcha(r, reqData, ruleSet, decision)
|
check.CheckCaptcha(r, reqData, ruleSet, decision)
|
||||||
} else if reqData.Uri == cfg.WebPath+"/health_check" {
|
} else if reqData.Uri == cfg.WebPath+"/health_check" {
|
||||||
decision.SetResponse(action.Done, []byte("200"), []byte("ok"))
|
decision.SetResponse(action.Done, []byte("200"), []byte("ok"))
|
||||||
} else if reqData.Uri == cfg.WebPath+"/waiting_room/status" {
|
|
||||||
handleWaitingRoomStatus(w, r, reqData, ruleSet, sharedMem)
|
|
||||||
return
|
|
||||||
} else if reqData.Uri == cfg.WebPath+"/waiting_room/join" {
|
|
||||||
handleWaitingRoomJoin(w, r, reqData, ruleSet, sharedMem)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if bytes.Compare(decision.HTTPCode, []byte("200")) == 0 {
|
if bytes.Compare(decision.HTTPCode, []byte("200")) == 0 {
|
||||||
if bytes.Compare(decision.ResponseData, []byte("ok")) == 0 {
|
if bytes.Compare(decision.ResponseData, []byte("ok")) == 0 {
|
||||||
@ -95,114 +88,3 @@ func CheckTorii(w http.ResponseWriter, r *http.Request, reqData dataType.UserReq
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleWaitingRoomStatus(w http.ResponseWriter, r *http.Request, reqData dataType.UserRequest, ruleSet *config.RuleSet, sharedMem *dataType.SharedMemory) {
|
|
||||||
if !ruleSet.WaitingRoomRule.Enabled {
|
|
||||||
w.WriteHeader(http.StatusNotFound)
|
|
||||||
err := json.NewEncoder(w).Encode(map[string]interface{}{
|
|
||||||
"error": "Waiting room not enabled",
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
sessionID := reqData.ToriiSessionID
|
|
||||||
clearance := reqData.ToriiClearance
|
|
||||||
userKey := check.GenerateUserKey(reqData)
|
|
||||||
secretKey := ruleSet.CAPTCHARule.SecretKey
|
|
||||||
|
|
||||||
// Check queue status using new core logic
|
|
||||||
inQueue, position, totalQueue, canEnter := sharedMem.WaitingRoom.GetQueueStatus(sessionID, clearance, userKey, secretKey)
|
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
err := json.NewEncoder(w).Encode(map[string]interface{}{
|
|
||||||
"inQueue": inQueue,
|
|
||||||
"position": position,
|
|
||||||
"totalQueue": totalQueue,
|
|
||||||
"canEnter": canEnter,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleWaitingRoomJoin(w http.ResponseWriter, r *http.Request, reqData dataType.UserRequest, ruleSet *config.RuleSet, sharedMem *dataType.SharedMemory) {
|
|
||||||
if !ruleSet.WaitingRoomRule.Enabled {
|
|
||||||
w.WriteHeader(http.StatusNotFound)
|
|
||||||
err := json.NewEncoder(w).Encode(map[string]interface{}{
|
|
||||||
"error": "Waiting room not enabled",
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if r.Method != http.MethodPost {
|
|
||||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
||||||
err := json.NewEncoder(w).Encode(map[string]interface{}{
|
|
||||||
"error": "Method not allowed",
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
sessionID := reqData.ToriiSessionID
|
|
||||||
userKey := check.GenerateUserKey(reqData)
|
|
||||||
secretKey := ruleSet.CAPTCHARule.SecretKey
|
|
||||||
|
|
||||||
// Join queue using new core logic
|
|
||||||
success, positiveID, clearance, canEnter := sharedMem.WaitingRoom.JoinQueue(sessionID, userKey, secretKey)
|
|
||||||
|
|
||||||
if !success {
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
err := json.NewEncoder(w).Encode(map[string]interface{}{
|
|
||||||
"success": false,
|
|
||||||
"message": "Invalid session or unable to join queue",
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if canEnter {
|
|
||||||
// User can enter directly
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
err := json.NewEncoder(w).Encode(map[string]interface{}{
|
|
||||||
"success": true,
|
|
||||||
"canEnter": true,
|
|
||||||
"message": "可以进入网站",
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// User is in queue, set clearance cookie and return position info
|
|
||||||
inQueue, position, totalQueue, _ := sharedMem.WaitingRoom.GetQueueStatus(sessionID, clearance, userKey, secretKey)
|
|
||||||
|
|
||||||
w.Header().Set("Set-Cookie", "__torii_clearance="+clearance+"; Path=/; Max-Age=86400; Priority=High; HttpOnly;")
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
err := json.NewEncoder(w).Encode(map[string]interface{}{
|
|
||||||
"success": true,
|
|
||||||
"canEnter": false,
|
|
||||||
"inQueue": inQueue,
|
|
||||||
"position": position,
|
|
||||||
"totalQueue": totalQueue,
|
|
||||||
"positiveID": positiveID,
|
|
||||||
"message": "已加入排队",
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
1
main.go
1
main.go
@ -58,7 +58,6 @@ func main() {
|
|||||||
sharedMem := &dataType.SharedMemory{
|
sharedMem := &dataType.SharedMemory{
|
||||||
HTTPFloodSpeedLimitCounter: dataType.NewCounter(max(runtime.NumCPU()*8, 16), utils.FindMaxRateTime(ruleSet.HTTPFloodRule.HTTPFloodSpeedLimit)),
|
HTTPFloodSpeedLimitCounter: dataType.NewCounter(max(runtime.NumCPU()*8, 16), utils.FindMaxRateTime(ruleSet.HTTPFloodRule.HTTPFloodSpeedLimit)),
|
||||||
HTTPFloodSameURILimitCounter: dataType.NewCounter(max(runtime.NumCPU()*8, 16), utils.FindMaxRateTime(ruleSet.HTTPFloodRule.HTTPFloodSameURILimit)),
|
HTTPFloodSameURILimitCounter: dataType.NewCounter(max(runtime.NumCPU()*8, 16), utils.FindMaxRateTime(ruleSet.HTTPFloodRule.HTTPFloodSameURILimit)),
|
||||||
WaitingRoom: dataType.NewWaitingRoom(ruleSet.WaitingRoomRule.MaxConcurrentUser, ruleSet.WaitingRoomRule.SessionTimeout),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//GC
|
//GC
|
||||||
|
Reference in New Issue
Block a user