mirror of
https://github.com/Rayzggz/server_torii.git
synced 2025-06-24 16:01:30 +08:00
Compare commits
14 Commits
1.0.0
...
feature/wa
Author | SHA1 | Date | |
---|---|---|---|
fff4327007 | |||
6abbb3a323 | |||
50380ebc5c | |||
aa1e760a79 | |||
fcb08478d2 | |||
c203cdf684 | |||
42e9d6502d | |||
1888f10b64 | |||
d622430a6f | |||
2db26b78a0 | |||
6c340966a1 | |||
d47938ba22 | |||
bd4bbb01c1 | |||
87fb76f157 |
@ -1,4 +0,0 @@
|
|||||||
secret_key: "0378b0f84c4310279918d71a5647ba5d"
|
|
||||||
captcha_validate_time: 600
|
|
||||||
captcha_challenge_session_timeout: 120
|
|
||||||
hcaptcha_secret: ""
|
|
@ -1,4 +0,0 @@
|
|||||||
HTTPFloodSpeedLimit:
|
|
||||||
- "150/10s"
|
|
||||||
HTTPFloodSameURILimit:
|
|
||||||
- "50/10s"
|
|
22
config_example/rules/Server.yml
Normal file
22
config_example/rules/Server.yml
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
CAPTCHA:
|
||||||
|
secret_key: "0378b0f84c4310279918d71a5647ba5d"
|
||||||
|
captcha_validate_time: 600
|
||||||
|
captcha_challenge_session_timeout: 120
|
||||||
|
hcaptcha_secret: ""
|
||||||
|
HTTPFlood:
|
||||||
|
HTTPFloodSpeedLimit:
|
||||||
|
- "150/10s"
|
||||||
|
HTTPFloodSameURILimit:
|
||||||
|
- "50/10s"
|
||||||
|
VerifyBot:
|
||||||
|
verify_google_bot: true
|
||||||
|
verify_bing_bot: true
|
||||||
|
verify_baidu_bot: true
|
||||||
|
verify_yandex_bot: true
|
||||||
|
verify_sogou_bot: true
|
||||||
|
verify_apple_bot: true
|
||||||
|
ExternalMigration:
|
||||||
|
enabled: false
|
||||||
|
redirect_url: "https://example.com/migration"
|
||||||
|
secret_key: "0378b0f84c4310279918d71a5647ba5d"
|
||||||
|
session_timeout: 1800
|
@ -1,6 +0,0 @@
|
|||||||
verify_google_bot: true
|
|
||||||
verify_bing_bot: true
|
|
||||||
verify_baidu_bot: true
|
|
||||||
verify_yandex_bot: true
|
|
||||||
verify_sogou_bot: true
|
|
||||||
verify_apple_bot: true
|
|
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)
|
||||||
|
}
|
@ -2,6 +2,7 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"fmt"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
@ -27,6 +28,20 @@ type MainConfig struct {
|
|||||||
|
|
||||||
// LoadMainConfig Read the configuration file and return the configuration object
|
// LoadMainConfig Read the configuration file and return the configuration object
|
||||||
func LoadMainConfig(basePath string) (*MainConfig, error) {
|
func LoadMainConfig(basePath string) (*MainConfig, error) {
|
||||||
|
|
||||||
|
defaultCfg := MainConfig{
|
||||||
|
Port: "25555",
|
||||||
|
WebPath: "/torii",
|
||||||
|
RulePath: "/www/server_torii/config/rules",
|
||||||
|
ErrorPage: "/www/server_torii/config/error_page",
|
||||||
|
LogPath: "/www/server_torii/log/",
|
||||||
|
NodeName: "Server Torii",
|
||||||
|
ConnectingHostHeaders: []string{"Torii-Real-Host"},
|
||||||
|
ConnectingIPHeaders: []string{"Torii-Real-IP"},
|
||||||
|
ConnectingURIHeaders: []string{"Torii-Original-URI"},
|
||||||
|
ConnectingCaptchaStatusHeaders: []string{"Torii-Captcha-Status"},
|
||||||
|
}
|
||||||
|
|
||||||
exePath, err := os.Executable()
|
exePath, err := os.Executable()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -38,12 +53,12 @@ func LoadMainConfig(basePath string) (*MainConfig, error) {
|
|||||||
|
|
||||||
data, err := os.ReadFile(configPath)
|
data, err := os.ReadFile(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return &defaultCfg, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var cfg MainConfig
|
var cfg MainConfig
|
||||||
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
||||||
return nil, err
|
return &defaultCfg, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &cfg, nil
|
return &cfg, nil
|
||||||
@ -51,95 +66,115 @@ func LoadMainConfig(basePath string) (*MainConfig, error) {
|
|||||||
|
|
||||||
// RuleSet stores all rules
|
// RuleSet stores all rules
|
||||||
type RuleSet struct {
|
type RuleSet struct {
|
||||||
IPAllowTrie *dataType.TrieNode
|
IPAllowTrie *dataType.TrieNode
|
||||||
IPBlockTrie *dataType.TrieNode
|
IPBlockTrie *dataType.TrieNode
|
||||||
URLAllowList *dataType.URLRuleList
|
URLAllowList *dataType.URLRuleList
|
||||||
URLBlockList *dataType.URLRuleList
|
URLBlockList *dataType.URLRuleList
|
||||||
CAPTCHARule *dataType.CaptchaRule
|
CAPTCHARule *dataType.CaptchaRule
|
||||||
VerifyBotRule *dataType.VerifyBotRule
|
VerifyBotRule *dataType.VerifyBotRule
|
||||||
HTTPFloodRule *dataType.HTTPFloodRule
|
HTTPFloodRule *dataType.HTTPFloodRule
|
||||||
|
ExternalMigrationRule *dataType.ExternalMigrationRule
|
||||||
|
}
|
||||||
|
|
||||||
|
// ruleSetWrapper
|
||||||
|
type ruleSetWrapper struct {
|
||||||
|
CAPTCHARule *dataType.CaptchaRule `yaml:"CAPTCHA"`
|
||||||
|
VerifyBotRule *dataType.VerifyBotRule `yaml:"VerifyBot"`
|
||||||
|
HTTPFloodRule httpFloodRuleWrapper `yaml:"HTTPFlood"`
|
||||||
|
ExternalMigrationRule *dataType.ExternalMigrationRule `yaml:"ExternalMigration"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type httpFloodRuleWrapper struct {
|
||||||
|
HTTPFloodSpeedLimit []string `yaml:"HTTPFloodSpeedLimit"`
|
||||||
|
HTTPFloodSameURILimit []string `yaml:"HTTPFloodSameURILimit"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadRules Load all rules from the specified path
|
// LoadRules Load all rules from the specified path
|
||||||
func LoadRules(rulePath string) (*RuleSet, error) {
|
func LoadRules(rulePath string) (*RuleSet, error) {
|
||||||
rs := RuleSet{
|
rs := RuleSet{
|
||||||
IPAllowTrie: &dataType.TrieNode{},
|
IPAllowTrie: &dataType.TrieNode{},
|
||||||
IPBlockTrie: &dataType.TrieNode{},
|
IPBlockTrie: &dataType.TrieNode{},
|
||||||
URLAllowList: &dataType.URLRuleList{},
|
URLAllowList: &dataType.URLRuleList{},
|
||||||
URLBlockList: &dataType.URLRuleList{},
|
URLBlockList: &dataType.URLRuleList{},
|
||||||
CAPTCHARule: &dataType.CaptchaRule{},
|
CAPTCHARule: &dataType.CaptchaRule{},
|
||||||
VerifyBotRule: &dataType.VerifyBotRule{},
|
VerifyBotRule: &dataType.VerifyBotRule{},
|
||||||
HTTPFloodRule: &dataType.HTTPFloodRule{},
|
HTTPFloodRule: &dataType.HTTPFloodRule{},
|
||||||
|
ExternalMigrationRule: &dataType.ExternalMigrationRule{},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load IP Allow List
|
// Load IP Allow List
|
||||||
ipAllowFile := rulePath + "/IP_AllowList.conf"
|
ipAllowFile := filepath.Join(rulePath, "/IP_AllowList.conf")
|
||||||
if err := loadIPRules(ipAllowFile, rs.IPAllowTrie); err != nil {
|
if err := loadIPRules(ipAllowFile, rs.IPAllowTrie); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load IP Block List
|
// Load IP Block List
|
||||||
ipBlockFile := rulePath + "/IP_BlockList.conf"
|
ipBlockFile := filepath.Join(rulePath, "/IP_BlockList.conf")
|
||||||
if err := loadIPRules(ipBlockFile, rs.IPBlockTrie); err != nil {
|
if err := loadIPRules(ipBlockFile, rs.IPBlockTrie); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load URL Allow List
|
// Load URL Allow List
|
||||||
urlAllowFile := rulePath + "/URL_AllowList.conf"
|
urlAllowFile := filepath.Join(rulePath, "/URL_AllowList.conf")
|
||||||
if err := loadURLRules(urlAllowFile, rs.URLAllowList); err != nil {
|
if err := loadURLRules(urlAllowFile, rs.URLAllowList); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load URL Block List
|
// Load URL Block List
|
||||||
urlBlockFile := rulePath + "/URL_BlockList.conf"
|
urlBlockFile := filepath.Join(rulePath, "/URL_BlockList.conf")
|
||||||
if err := loadURLRules(urlBlockFile, rs.URLBlockList); err != nil {
|
if err := loadURLRules(urlBlockFile, rs.URLBlockList); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load CAPTCHA Rule
|
YAMLFile := filepath.Join(rulePath, "Server.yml")
|
||||||
captchaFile := rulePath + "/CAPTCHA.yml"
|
set, err := loadServerRules(YAMLFile, rs)
|
||||||
if err := loadCAPTCHARule(captchaFile, rs.CAPTCHARule); err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return set, err
|
||||||
}
|
|
||||||
|
|
||||||
// Load Verify Bot Rule
|
|
||||||
verifyBotFile := rulePath + "/VerifyBot.yml"
|
|
||||||
if err := loadVerifyBotRule(verifyBotFile, rs.VerifyBotRule); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load HTTP Flood Rule
|
|
||||||
httpFloodFile := rulePath + "/HTTPFlood.yml"
|
|
||||||
if err := loadHTTPFloodRule(httpFloodFile, rs.HTTPFloodRule); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &rs, nil
|
return &rs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadCAPTCHARule(file string, rule *dataType.CaptchaRule) error {
|
func loadServerRules(YAMLFile string, rs RuleSet) (*RuleSet, error) {
|
||||||
data, err := os.ReadFile(file)
|
yamlData, err := os.ReadFile(YAMLFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
if os.IsNotExist(err) {
|
||||||
|
return nil, fmt.Errorf("[ERROR] rules file %s does not exist: %w", YAMLFile, err)
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("[ERROR] failed to read rules file %s: %w", YAMLFile, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := yaml.Unmarshal(data, &rule); err != nil {
|
var wrapper ruleSetWrapper
|
||||||
return err
|
if err := yaml.Unmarshal(yamlData, &wrapper); err != nil {
|
||||||
|
return nil, fmt.Errorf("[ERROR] failed to parse rules file %s: %w", YAMLFile, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
*rs.CAPTCHARule = *wrapper.CAPTCHARule
|
||||||
|
*rs.VerifyBotRule = *wrapper.VerifyBotRule
|
||||||
}
|
if wrapper.ExternalMigrationRule != nil {
|
||||||
|
*rs.ExternalMigrationRule = *wrapper.ExternalMigrationRule
|
||||||
func loadVerifyBotRule(file string, rule *dataType.VerifyBotRule) error {
|
|
||||||
data, err := os.ReadFile(file)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
if err := yaml.Unmarshal(data, &rule); err != nil {
|
|
||||||
return err
|
rs.HTTPFloodRule.HTTPFloodSpeedLimit = make(map[int64]int64)
|
||||||
|
rs.HTTPFloodRule.HTTPFloodSameURILimit = make(map[int64]int64)
|
||||||
|
|
||||||
|
for _, s := range wrapper.HTTPFloodRule.HTTPFloodSpeedLimit {
|
||||||
|
limit, seconds, err := utils.ParseRate(s)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
rs.HTTPFloodRule.HTTPFloodSpeedLimit[seconds] = limit
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
|
for _, s := range wrapper.HTTPFloodRule.HTTPFloodSameURILimit {
|
||||||
|
limit, seconds, err := utils.ParseRate(s)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
rs.HTTPFloodRule.HTTPFloodSameURILimit[seconds] = limit
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// loadIPRules read the IP rule file and insert the rules into the trie
|
// loadIPRules read the IP rule file and insert the rules into the trie
|
||||||
@ -217,43 +252,3 @@ func loadURLRules(filePath string, list *dataType.URLRuleList) error {
|
|||||||
|
|
||||||
return scanner.Err()
|
return scanner.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadHTTPFloodRule(file string, rule *dataType.HTTPFloodRule) error {
|
|
||||||
data, err := os.ReadFile(file)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
type httpFloodRuleYAML struct {
|
|
||||||
HTTPFloodSpeedLimit []string `yaml:"HTTPFloodSpeedLimit"`
|
|
||||||
HTTPFloodSameURILimit []string `yaml:"HTTPFloodSameURILimit"`
|
|
||||||
}
|
|
||||||
|
|
||||||
var ymlRule httpFloodRuleYAML
|
|
||||||
err = yaml.Unmarshal(data, &ymlRule)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
rule.HTTPFloodSpeedLimit = make(map[int64]int64)
|
|
||||||
rule.HTTPFloodSameURILimit = make(map[int64]int64)
|
|
||||||
|
|
||||||
for _, s := range ymlRule.HTTPFloodSpeedLimit {
|
|
||||||
limit, seconds, err := utils.ParseRate(s)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
rule.HTTPFloodSpeedLimit[seconds] = limit
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, s := range ymlRule.HTTPFloodSameURILimit {
|
|
||||||
limit, seconds, err := utils.ParseRate(s)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
rule.HTTPFloodSameURILimit[seconds] = limit
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
|
|
||||||
}
|
|
||||||
|
@ -31,6 +31,13 @@ type HTTPFloodRule struct {
|
|||||||
HTTPFloodSameURILimit map[int64]int64
|
HTTPFloodSameURILimit map[int64]int64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ExternalMigrationRule struct {
|
||||||
|
Enabled bool `yaml:"enabled"`
|
||||||
|
RedirectUrl string `yaml:"redirect_url"`
|
||||||
|
SecretKey string `yaml:"secret_key"`
|
||||||
|
SessionTimeout int64 `yaml:"session_timeout"`
|
||||||
|
}
|
||||||
|
|
||||||
type SharedMemory struct {
|
type SharedMemory struct {
|
||||||
HTTPFloodSpeedLimitCounter *Counter
|
HTTPFloodSpeedLimitCounter *Counter
|
||||||
HTTPFloodSameURILimitCounter *Counter
|
HTTPFloodSameURILimitCounter *Counter
|
||||||
|
@ -25,6 +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.ExternalMigration)
|
||||||
checkFuncs = append(checkFuncs, check.Captcha)
|
checkFuncs = append(checkFuncs, check.Captcha)
|
||||||
|
|
||||||
for _, checkFunc := range checkFuncs {
|
for _, checkFunc := range checkFuncs {
|
||||||
|
2
main.go
2
main.go
@ -27,7 +27,7 @@ func main() {
|
|||||||
// Load MainConfig
|
// Load MainConfig
|
||||||
cfg, err := config.LoadMainConfig(basePath)
|
cfg, err := config.LoadMainConfig(basePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Load config failed: %v", err)
|
log.Printf("[ERROR] Load config failed: %v. Using default config.", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load rules
|
// Load rules
|
||||||
|
Reference in New Issue
Block a user