mirror of
https://github.com/Rayzggz/server_torii.git
synced 2025-06-17 04:31:22 +08:00
fix: Obtain User request URI
This commit is contained in:
@ -1,4 +1,6 @@
|
|||||||
port: "25555"
|
port: "25555"
|
||||||
rule_path: "/www/dev/server_torii/config/rules"
|
rule_path: "/www/dev/server_torii/config/rules"
|
||||||
connecting_ip_headers:
|
connecting_ip_headers:
|
||||||
- "X-Real-IP"
|
- "X-Real-IP"
|
||||||
|
connecting_uri_headers:
|
||||||
|
- "X-Original-URI"
|
@ -12,9 +12,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type MainConfig struct {
|
type MainConfig struct {
|
||||||
Port string `yaml:"port"`
|
Port string `yaml:"port"`
|
||||||
RulePath string `yaml:"rule_path"`
|
RulePath string `yaml:"rule_path"`
|
||||||
ConnectingIPHeaders []string `yaml:"connecting_ip_headers"`
|
ConnectingIPHeaders []string `yaml:"connecting_ip_headers"`
|
||||||
|
ConnectingURIHeaders []string `yaml:"connecting_uri_headers"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadMainConfig Read the configuration file and return the configuration object
|
// LoadMainConfig Read the configuration file and return the configuration object
|
||||||
|
@ -10,45 +10,30 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// StartServer starts the HTTP server
|
type userRequest struct {
|
||||||
func StartServer(port string, ruleSet *config.RuleSet, ipHeaders []string) error {
|
remoteIP string
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
uri string
|
||||||
var clientIP string
|
}
|
||||||
for _, headerName := range ipHeaders {
|
|
||||||
if ipVal := r.Header.Get(headerName); ipVal != "" {
|
|
||||||
if strings.Contains(clientIP, ",") {
|
|
||||||
parts := strings.Split(ipVal, ",")
|
|
||||||
clientIP = strings.TrimSpace(parts[0])
|
|
||||||
}
|
|
||||||
clientIP = ipVal
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if clientIP == "" {
|
// StartServer starts the HTTP server
|
||||||
remoteAddr := r.RemoteAddr
|
func StartServer(cfg *config.MainConfig, ruleSet *config.RuleSet) error {
|
||||||
ipStr, _, err := net.SplitHostPort(remoteAddr)
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
if err != nil {
|
|
||||||
//TODO: log error
|
userRequestData := processRequestData(cfg, r)
|
||||||
clientIP = remoteAddr
|
|
||||||
} else {
|
|
||||||
clientIP = ipStr
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
decision := action.NewDecision()
|
decision := action.NewDecision()
|
||||||
|
|
||||||
// run main check logic
|
// run main check logic
|
||||||
checkIPAllow(clientIP, ruleSet.IPAllowTrie, decision)
|
checkIPAllow(userRequestData.remoteIP, ruleSet.IPAllowTrie, decision)
|
||||||
checkIPBlock(clientIP, ruleSet.IPBlockTrie, decision)
|
checkIPBlock(userRequestData.remoteIP, ruleSet.IPBlockTrie, decision)
|
||||||
checkURLAllow(r.RequestURI, ruleSet.URLAllowList, decision)
|
checkURLAllow(userRequestData.uri, ruleSet.URLAllowList, decision)
|
||||||
checkURLBlock(r.RequestURI, ruleSet.URLBlockList, decision)
|
checkURLBlock(userRequestData.uri, ruleSet.URLBlockList, decision)
|
||||||
|
|
||||||
// if still undecided, allow
|
// if still undecided, allow
|
||||||
if decision.Get() == action.Undecided {
|
if decision.Get() == action.Undecided {
|
||||||
decision.Set(action.Allow)
|
decision.Set(action.Allow)
|
||||||
}
|
}
|
||||||
|
log.Printf("clientIP: %s, decision: %s, Headers: %v", userRequestData.remoteIP, decision.Get(), r.Header)
|
||||||
// return response
|
// return response
|
||||||
if decision.Get() == action.Allow {
|
if decision.Get() == action.Allow {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
@ -62,8 +47,51 @@ func StartServer(port string, ruleSet *config.RuleSet, ipHeaders []string) error
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
log.Printf("HTTP Server listening on :%s ...", port)
|
log.Printf("HTTP Server listening on :%s ...", cfg.Port)
|
||||||
return http.ListenAndServe(":"+port, nil)
|
return http.ListenAndServe(":"+cfg.Port, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func processRequestData(cfg *config.MainConfig, r *http.Request) userRequest {
|
||||||
|
|
||||||
|
var clientIP string
|
||||||
|
for _, headerName := range cfg.ConnectingIPHeaders {
|
||||||
|
if ipVal := r.Header.Get(headerName); ipVal != "" {
|
||||||
|
if strings.Contains(clientIP, ",") {
|
||||||
|
parts := strings.Split(ipVal, ",")
|
||||||
|
clientIP = strings.TrimSpace(parts[0])
|
||||||
|
}
|
||||||
|
clientIP = ipVal
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if clientIP == "" {
|
||||||
|
remoteAddr := r.RemoteAddr
|
||||||
|
ipStr, _, err := net.SplitHostPort(remoteAddr)
|
||||||
|
if err != nil {
|
||||||
|
//TODO: log error
|
||||||
|
clientIP = remoteAddr
|
||||||
|
} else {
|
||||||
|
clientIP = ipStr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var clientURI string
|
||||||
|
for _, headerName := range cfg.ConnectingURIHeaders {
|
||||||
|
if uriVal := r.Header.Get(headerName); uriVal != "" {
|
||||||
|
clientURI = uriVal
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if clientURI == "" {
|
||||||
|
clientURI = r.RequestURI
|
||||||
|
}
|
||||||
|
|
||||||
|
userRequest := userRequest{
|
||||||
|
remoteIP: clientIP,
|
||||||
|
uri: clientURI,
|
||||||
|
}
|
||||||
|
return userRequest
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkIPAllow(remoteIP string, trie *dataType.TrieNode, decision *action.Decision) {
|
func checkIPAllow(remoteIP string, trie *dataType.TrieNode, decision *action.Decision) {
|
||||||
|
Reference in New Issue
Block a user