feat: fix config

This commit is contained in:
2025-05-08 10:08:36 +08:00
parent 4a9afa8784
commit c69c5b5cd7
7 changed files with 122 additions and 70 deletions

15
config/default.yaml Normal file
View File

@ -0,0 +1,15 @@
server:
port: "25555"
web_path: "/torii"
rule_path: "/www/server_torii/config/rules"
error_page: "/www/server_torii/config/error_page"
log_path: "/www/server_torii/log/"
node_name: "Server Torii"
connecting_host_headers:
- "Torii-Real-Host"
connecting_ip_headers:
- "Torii-Real-IP"
connecting_uri_headers:
- "Torii-Original-URI"
connecting_captcha_status_headers:
- "Torii-Captcha-Status"

16
go.mod
View File

@ -5,8 +5,22 @@ go 1.23.5
require ( require (
github.com/cespare/xxhash/v2 v2.3.0 github.com/cespare/xxhash/v2 v2.3.0
github.com/mssola/useragent v1.0.0 github.com/mssola/useragent v1.0.0
github.com/spf13/pflag v1.0.6
github.com/spf13/viper v1.20.1
go.uber.org/zap v1.27.0 go.uber.org/zap v1.27.0
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1
) )
require go.uber.org/multierr v1.10.0 // indirect require (
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/sagikazarmark/locafero v0.7.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.12.0 // indirect
github.com/spf13/cast v1.7.1 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/sys v0.29.0 // indirect
golang.org/x/text v0.21.0 // indirect
)

View File

@ -2,51 +2,73 @@ package config
import ( import (
"bufio" "bufio"
"bytes"
"fmt"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"net" "net"
"os" "os"
"path/filepath"
"regexp" "regexp"
"server_torii/internal/dataType" "server_torii/internal/dataType"
"server_torii/internal/utils" "server_torii/internal/utils"
"strings" "strings"
) )
type MainConfig struct { type ServerConfig struct {
Port string `yaml:"port"` Port string `mapstructure:"port"`
WebPath string `yaml:"web_path"` WebPath string `mapstructure:"web_path"`
RulePath string `yaml:"rule_path"` RulePath string `mapstructure:"rule_path"`
ErrorPage string `yaml:"error_page"` ErrorPage string `mapstructure:"error_page"`
LogPath string `yaml:"log_path"` LogPath string `mapstructure:"log_path"`
NodeName string `yaml:"node_name"` NodeName string `mapstructure:"node_name"`
ConnectingHostHeaders []string `yaml:"connecting_host_headers"` ConnectingHostHeaders []string `mapstructure:"connecting_host_headers"`
ConnectingIPHeaders []string `yaml:"connecting_ip_headers"` ConnectingIPHeaders []string `mapstructure:"connecting_ip_headers"`
ConnectingURIHeaders []string `yaml:"connecting_uri_headers"` ConnectingURIHeaders []string `mapstructure:"connecting_uri_headers"`
ConnectingCaptchaStatusHeaders []string `yaml:"connecting_captcha_status_headers"` ConnectingCaptchaStatusHeaders []string `mapstructure:"connecting_captcha_status_headers"`
} }
// LoadMainConfig Read the configuration file and return the configuration object type AppConfig struct {
func LoadMainConfig(basePath string) (*MainConfig, error) { Server ServerConfig `mapstructure:"server"`
exePath, err := os.Executable() }
if err != nil {
return nil, err
}
if basePath == "" {
basePath = filepath.Dir(exePath)
}
configPath := filepath.Join(basePath, "config", "torii.yml")
data, err := os.ReadFile(configPath) var Cfg AppConfig
if err != nil {
return nil, err func InitConfig(defaultConfigContent []byte) {
// 1. 处理命令行参数
var configFile string
pflag.StringVar(&configFile, "config", "", "Path to custom config file")
pflag.Parse()
v := viper.New()
// 2. 加载嵌入的默认配置文件
if len(defaultConfigContent) > 0 {
v.SetConfigType("yml")
if err := v.ReadConfig(bytes.NewBuffer(defaultConfigContent)); err != nil {
fmt.Printf("加载默认配置失败: %v\n", err)
os.Exit(1)
}
} }
var cfg MainConfig // 3. 加载外部配置文件(如果存在)
if err := yaml.Unmarshal(data, &cfg); err != nil { if configFile != "" {
return nil, err if _, err := os.Stat(configFile); err == nil {
v.SetConfigFile(configFile)
if err := v.MergeInConfig(); err != nil {
fmt.Printf("加载外部配置失败: %v (路径: %s)\n", err, configFile)
os.Exit(1)
}
} else {
fmt.Printf("警告: 外部配置文件不存在,使用默认配置 (路径: %s)\n", configFile)
}
} }
return &cfg, nil // 4. 映射到结构体
if err := v.Unmarshal(&Cfg); err != nil {
fmt.Println("解析配置失败:", err)
os.Exit(1)
}
} }
// RuleSet stores all rules // RuleSet stores all rules

View File

@ -15,7 +15,7 @@ import (
type CheckFunc func(dataType.UserRequest, *config.RuleSet, *action.Decision, *dataType.SharedMemory) type CheckFunc func(dataType.UserRequest, *config.RuleSet, *action.Decision, *dataType.SharedMemory)
func CheckMain(w http.ResponseWriter, userRequestData dataType.UserRequest, ruleSet *config.RuleSet, cfg *config.MainConfig, sharedMem *dataType.SharedMemory) { func CheckMain(w http.ResponseWriter, userRequestData dataType.UserRequest, ruleSet *config.RuleSet, sharedMem *dataType.SharedMemory) {
decision := action.NewDecision() decision := action.NewDecision()
checkFuncs := make([]CheckFunc, 0) checkFuncs := make([]CheckFunc, 0)
@ -42,7 +42,7 @@ func CheckMain(w http.ResponseWriter, userRequestData dataType.UserRequest, rule
return return
} }
} else if bytes.Compare(decision.HTTPCode, []byte("403")) == 0 { } else if bytes.Compare(decision.HTTPCode, []byte("403")) == 0 {
tpl, err := template.ParseFiles(cfg.ErrorPage + "/403.html") tpl, err := template.ParseFiles(config.Cfg.Server.ErrorPage + "/403.html")
if err != nil { if err != nil {
utils.LogError(userRequestData, fmt.Sprintf("Error parsing template: %v", err), "CheckMain") utils.LogError(userRequestData, fmt.Sprintf("Error parsing template: %v", err), "CheckMain")
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError) http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
@ -54,7 +54,7 @@ func CheckMain(w http.ResponseWriter, userRequestData dataType.UserRequest, rule
ConnectIP string ConnectIP string
Date string Date string
}{ }{
EdgeTag: cfg.NodeName, EdgeTag: config.Cfg.Server.NodeName,
ConnectIP: userRequestData.RemoteIP, ConnectIP: userRequestData.RemoteIP,
Date: time.Now().Format("2006-01-02 15:04:05"), Date: time.Now().Format("2006-01-02 15:04:05"),
} }
@ -67,7 +67,7 @@ func CheckMain(w http.ResponseWriter, userRequestData dataType.UserRequest, rule
} }
} else if bytes.Compare(decision.HTTPCode, []byte("CAPTCHA")) == 0 { } else if bytes.Compare(decision.HTTPCode, []byte("CAPTCHA")) == 0 {
tpl, err := template.ParseFiles(cfg.ErrorPage + "/CAPTCHA.html") tpl, err := template.ParseFiles(config.Cfg.Server.ErrorPage + "/CAPTCHA.html")
if err != nil { if err != nil {
utils.LogError(userRequestData, fmt.Sprintf("Error parsing template: %v", err), "CheckMain") utils.LogError(userRequestData, fmt.Sprintf("Error parsing template: %v", err), "CheckMain")
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError) http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
@ -83,7 +83,7 @@ func CheckMain(w http.ResponseWriter, userRequestData dataType.UserRequest, rule
} }
} else if bytes.Compare(decision.HTTPCode, []byte("429")) == 0 { } else if bytes.Compare(decision.HTTPCode, []byte("429")) == 0 {
tpl, err := template.ParseFiles(cfg.ErrorPage + "/429.html") tpl, err := template.ParseFiles(config.Cfg.Server.ErrorPage + "/429.html")
if err != nil { if err != nil {
utils.LogError(userRequestData, fmt.Sprintf("Error parsing template: %v", err), "CheckMain") utils.LogError(userRequestData, fmt.Sprintf("Error parsing template: %v", err), "CheckMain")
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError) http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
@ -94,7 +94,7 @@ func CheckMain(w http.ResponseWriter, userRequestData dataType.UserRequest, rule
ConnectIP string ConnectIP string
Date string Date string
}{ }{
EdgeTag: cfg.NodeName, EdgeTag: config.Cfg.Server.NodeName,
ConnectIP: userRequestData.RemoteIP, ConnectIP: userRequestData.RemoteIP,
Date: time.Now().Format("2006-01-02 15:04:05"), Date: time.Now().Format("2006-01-02 15:04:05"),
} }

View File

@ -10,33 +10,33 @@ import (
) )
// StartServer starts the HTTP server // StartServer starts the HTTP server
func StartServer(cfg *config.MainConfig, ruleSet *config.RuleSet, sharedMem *dataType.SharedMemory) error { func StartServer(ruleSet *config.RuleSet, sharedMem *dataType.SharedMemory) error {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
userRequestData := processRequestData(cfg, r) userRequestData := processRequestData(r)
if strings.HasPrefix(userRequestData.Uri, cfg.WebPath) { if strings.HasPrefix(userRequestData.Uri, config.Cfg.Server.WebPath) {
CheckTorii(w, r, userRequestData, ruleSet, cfg, sharedMem) CheckTorii(w, r, userRequestData, ruleSet, sharedMem)
} else { } else {
CheckMain(w, userRequestData, ruleSet, cfg, sharedMem) CheckMain(w, userRequestData, ruleSet, sharedMem)
} }
}) })
log.Printf("HTTP Server listening on :%s ...", cfg.Port) log.Printf("HTTP Server listening on :%s ...", config.Cfg.Server.Port)
return http.ListenAndServe(":"+cfg.Port, nil) return http.ListenAndServe(":"+config.Cfg.Server.Port, nil)
} }
func processRequestData(cfg *config.MainConfig, r *http.Request) dataType.UserRequest { func processRequestData(r *http.Request) dataType.UserRequest {
userRequest := dataType.UserRequest{ userRequest := dataType.UserRequest{
RemoteIP: getClientIP(cfg, r), RemoteIP: getClientIP(r),
Uri: getReqURI(cfg, r), Uri: getReqURI(r),
Captcha: getCaptchaStatus(cfg, r), Captcha: getCaptchaStatus(r),
ToriiClearance: getHeader(r, "__torii_clearance"), ToriiClearance: getHeader(r, "__torii_clearance"),
ToriiSessionID: getHeader(r, "__torii_session_id"), ToriiSessionID: getHeader(r, "__torii_session_id"),
UserAgent: r.UserAgent(), UserAgent: r.UserAgent(),
Host: getReqHost(cfg, r), Host: getReqHost(r),
} }
return userRequest return userRequest
} }
@ -49,9 +49,9 @@ func getHeader(r *http.Request, headerName string) string {
return cookie.Value return cookie.Value
} }
func getCaptchaStatus(cfg *config.MainConfig, r *http.Request) bool { func getCaptchaStatus(r *http.Request) bool {
captchaStatus := false captchaStatus := false
for _, headerName := range cfg.ConnectingCaptchaStatusHeaders { for _, headerName := range config.Cfg.Server.ConnectingCaptchaStatusHeaders {
if captchaVal := r.Header.Get(headerName); captchaVal != "" { if captchaVal := r.Header.Get(headerName); captchaVal != "" {
if captchaVal == "on" { if captchaVal == "on" {
captchaStatus = true captchaStatus = true
@ -63,9 +63,9 @@ func getCaptchaStatus(cfg *config.MainConfig, r *http.Request) bool {
} }
func getReqURI(cfg *config.MainConfig, r *http.Request) string { func getReqURI(r *http.Request) string {
var clientURI string var clientURI string
for _, headerName := range cfg.ConnectingURIHeaders { for _, headerName := range config.Cfg.Server.ConnectingURIHeaders {
if uriVal := r.Header.Get(headerName); uriVal != "" { if uriVal := r.Header.Get(headerName); uriVal != "" {
clientURI = uriVal clientURI = uriVal
break break
@ -77,9 +77,9 @@ func getReqURI(cfg *config.MainConfig, r *http.Request) string {
return clientURI return clientURI
} }
func getClientIP(cfg *config.MainConfig, r *http.Request) string { func getClientIP(r *http.Request) string {
var clientIP string var clientIP string
for _, headerName := range cfg.ConnectingIPHeaders { for _, headerName := range config.Cfg.Server.ConnectingIPHeaders {
if ipVal := r.Header.Get(headerName); ipVal != "" { if ipVal := r.Header.Get(headerName); ipVal != "" {
if strings.Contains(clientIP, ",") { if strings.Contains(clientIP, ",") {
parts := strings.Split(ipVal, ",") parts := strings.Split(ipVal, ",")
@ -102,9 +102,9 @@ func getClientIP(cfg *config.MainConfig, r *http.Request) string {
return clientIP return clientIP
} }
func getReqHost(cfg *config.MainConfig, r *http.Request) string { func getReqHost(r *http.Request) string {
var clientHost = "" var clientHost = ""
for _, headerName := range cfg.ConnectingHostHeaders { for _, headerName := range config.Cfg.Server.ConnectingHostHeaders {
if hostVal := r.Header.Get(headerName); hostVal != "" { if hostVal := r.Header.Get(headerName); hostVal != "" {
clientHost = hostVal clientHost = hostVal
break break

View File

@ -12,13 +12,13 @@ import (
"time" "time"
) )
func CheckTorii(w http.ResponseWriter, r *http.Request, reqData dataType.UserRequest, ruleSet *config.RuleSet, cfg *config.MainConfig, sharedMem *dataType.SharedMemory) { func CheckTorii(w http.ResponseWriter, r *http.Request, reqData dataType.UserRequest, ruleSet *config.RuleSet, sharedMem *dataType.SharedMemory) {
decision := action.NewDecision() decision := action.NewDecision()
decision.SetCode(action.Continue, []byte("403")) decision.SetCode(action.Continue, []byte("403"))
if reqData.Uri == cfg.WebPath+"/captcha" { if reqData.Uri == config.Cfg.Server.WebPath+"/captcha" {
check.CheckCaptcha(r, reqData, ruleSet, decision) check.CheckCaptcha(r, reqData, ruleSet, decision)
} else if reqData.Uri == cfg.WebPath+"/health_check" { } else if reqData.Uri == config.Cfg.Server.WebPath+"/health_check" {
decision.SetResponse(action.Done, []byte("200"), []byte("ok")) decision.SetResponse(action.Done, []byte("200"), []byte("ok"))
} }
if bytes.Compare(decision.HTTPCode, []byte("200")) == 0 { if bytes.Compare(decision.HTTPCode, []byte("200")) == 0 {
@ -65,7 +65,7 @@ func CheckTorii(w http.ResponseWriter, r *http.Request, reqData dataType.UserReq
} }
} }
} else { } else {
tpl, err := template.ParseFiles(cfg.ErrorPage + "/403.html") tpl, err := template.ParseFiles(config.Cfg.Server.ErrorPage + "/403.html")
if err != nil { if err != nil {
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError) http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
return return
@ -76,7 +76,7 @@ func CheckTorii(w http.ResponseWriter, r *http.Request, reqData dataType.UserReq
ConnectIP string ConnectIP string
Date string Date string
}{ }{
EdgeTag: cfg.NodeName, EdgeTag: config.Cfg.Server.NodeName,
ConnectIP: reqData.RemoteIP, ConnectIP: reqData.RemoteIP,
Date: time.Now().Format("2006-01-02 15:04:05"), Date: time.Now().Format("2006-01-02 15:04:05"),
} }

19
main.go
View File

@ -1,6 +1,7 @@
package main package main
import ( import (
_ "embed"
"flag" "flag"
"log" "log"
"os" "os"
@ -15,6 +16,9 @@ import (
"time" "time"
) )
//go:embed config/default.yaml
var defaultConfigContent []byte
func main() { func main() {
var basePath string var basePath string
flag.StringVar(&basePath, "prefix", "", "Config file base path") flag.StringVar(&basePath, "prefix", "", "Config file base path")
@ -25,21 +29,18 @@ func main() {
} }
// Load MainConfig // Load MainConfig
cfg, err := config.LoadMainConfig(basePath) config.InitConfig(defaultConfigContent)
if err != nil {
log.Fatalf("Load config failed: %v", err)
}
// Load rules // Load rules
ruleSet, err := config.LoadRules(cfg.RulePath) ruleSet, err := config.LoadRules(config.Cfg.Server.RulePath)
if err != nil { if err != nil {
log.Fatalf("Load rules failed: %v", err) log.Fatalf("Load rules failed: %v", err)
} }
log.Printf("Ready to start server on port %s", cfg.Port) log.Printf("Ready to start server on port %s", config.Cfg.Server.Port)
//set log file //set log file
defaultLogPath := filepath.Join(cfg.LogPath + "server_torii.log") defaultLogPath := filepath.Join(config.Cfg.Server.LogPath + "server_torii.log")
logFile, err := os.OpenFile(defaultLogPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) logFile, err := os.OpenFile(defaultLogPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil { if err != nil {
log.Fatalf("Failed to open log file: %v", err) log.Fatalf("Failed to open log file: %v", err)
@ -52,7 +53,7 @@ func main() {
}(logFile) }(logFile)
log.SetOutput(logFile) log.SetOutput(logFile)
utils.InitLogx(cfg.LogPath) utils.InitLogx(config.Cfg.Server.LogPath)
//allocate shared memory //allocate shared memory
sharedMem := &dataType.SharedMemory{ sharedMem := &dataType.SharedMemory{
@ -71,7 +72,7 @@ func main() {
serverErr := make(chan error, 1) serverErr := make(chan error, 1)
go func() { go func() {
serverErr <- server.StartServer(cfg, ruleSet, sharedMem) serverErr <- server.StartServer(ruleSet, sharedMem)
}() }()
select { select {