mirror of
https://github.com/Rayzggz/server_torii.git
synced 2025-06-17 20:51:22 +08:00
fix: Obtain User request URI
This commit is contained in:
@ -2,3 +2,5 @@ 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"
|
@ -15,6 +15,7 @@ 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,11 +10,51 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type userRequest struct {
|
||||||
|
remoteIP string
|
||||||
|
uri string
|
||||||
|
}
|
||||||
|
|
||||||
// StartServer starts the HTTP server
|
// StartServer starts the HTTP server
|
||||||
func StartServer(port string, ruleSet *config.RuleSet, ipHeaders []string) error {
|
func StartServer(cfg *config.MainConfig, ruleSet *config.RuleSet) error {
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
userRequestData := processRequestData(cfg, r)
|
||||||
|
|
||||||
|
decision := action.NewDecision()
|
||||||
|
|
||||||
|
// run main check logic
|
||||||
|
checkIPAllow(userRequestData.remoteIP, ruleSet.IPAllowTrie, decision)
|
||||||
|
checkIPBlock(userRequestData.remoteIP, ruleSet.IPBlockTrie, decision)
|
||||||
|
checkURLAllow(userRequestData.uri, ruleSet.URLAllowList, decision)
|
||||||
|
checkURLBlock(userRequestData.uri, ruleSet.URLBlockList, decision)
|
||||||
|
|
||||||
|
// if still undecided, allow
|
||||||
|
if decision.Get() == action.Undecided {
|
||||||
|
decision.Set(action.Allow)
|
||||||
|
}
|
||||||
|
log.Printf("clientIP: %s, decision: %s, Headers: %v", userRequestData.remoteIP, decision.Get(), r.Header)
|
||||||
|
// return response
|
||||||
|
if decision.Get() == action.Allow {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write([]byte("Allowed"))
|
||||||
|
} else if decision.Get() == action.Block {
|
||||||
|
w.WriteHeader(http.StatusForbidden)
|
||||||
|
w.Write([]byte("Blocked"))
|
||||||
|
} else {
|
||||||
|
// should not reach here
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
log.Printf("HTTP Server listening on :%s ...", cfg.Port)
|
||||||
|
return http.ListenAndServe(":"+cfg.Port, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func processRequestData(cfg *config.MainConfig, r *http.Request) userRequest {
|
||||||
|
|
||||||
var clientIP string
|
var clientIP string
|
||||||
for _, headerName := range ipHeaders {
|
for _, headerName := range cfg.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, ",")
|
||||||
@ -36,34 +76,22 @@ func StartServer(port string, ruleSet *config.RuleSet, ipHeaders []string) error
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
decision := action.NewDecision()
|
var clientURI string
|
||||||
|
for _, headerName := range cfg.ConnectingURIHeaders {
|
||||||
// run main check logic
|
if uriVal := r.Header.Get(headerName); uriVal != "" {
|
||||||
checkIPAllow(clientIP, ruleSet.IPAllowTrie, decision)
|
clientURI = uriVal
|
||||||
checkIPBlock(clientIP, ruleSet.IPBlockTrie, decision)
|
break
|
||||||
checkURLAllow(r.RequestURI, ruleSet.URLAllowList, decision)
|
}
|
||||||
checkURLBlock(r.RequestURI, ruleSet.URLBlockList, decision)
|
}
|
||||||
|
if clientURI == "" {
|
||||||
// if still undecided, allow
|
clientURI = r.RequestURI
|
||||||
if decision.Get() == action.Undecided {
|
|
||||||
decision.Set(action.Allow)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// return response
|
userRequest := userRequest{
|
||||||
if decision.Get() == action.Allow {
|
remoteIP: clientIP,
|
||||||
w.WriteHeader(http.StatusOK)
|
uri: clientURI,
|
||||||
w.Write([]byte("Allowed"))
|
|
||||||
} else if decision.Get() == action.Block {
|
|
||||||
w.WriteHeader(http.StatusForbidden)
|
|
||||||
w.Write([]byte("Blocked"))
|
|
||||||
} else {
|
|
||||||
// should not reach here
|
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
}
|
}
|
||||||
})
|
return userRequest
|
||||||
|
|
||||||
log.Printf("HTTP Server listening on :%s ...", port)
|
|
||||||
return http.ListenAndServe(":"+port, nil)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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