mirror of
https://github.com/tobychui/zoraxy.git
synced 2025-06-04 22:57:20 +02:00
clean up implementation
This commit is contained in:
parent
9545343151
commit
bb1b161ae2
@ -73,11 +73,12 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Rate Limit Check
|
// Rate Limit Check
|
||||||
// if sep.RequireBasicAuth {
|
if sep.RequireRateLimit {
|
||||||
if err := handleRateLimit(w, r, sep); err != nil {
|
err := h.handleRateLimitRouting(w, r, sep)
|
||||||
return
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
//Validate basic auth
|
//Validate basic auth
|
||||||
if sep.RequireBasicAuth {
|
if sep.RequireBasicAuth {
|
||||||
|
@ -50,6 +50,14 @@ func (t *IpTable) Clear() {
|
|||||||
|
|
||||||
var ipTable = IpTable{}
|
var ipTable = IpTable{}
|
||||||
|
|
||||||
|
func (h *ProxyHandler) handleRateLimitRouting(w http.ResponseWriter, r *http.Request, pe *ProxyEndpoint) error {
|
||||||
|
err := handleRateLimit(w, r, pe)
|
||||||
|
if err != nil {
|
||||||
|
h.logRequest(r, false, 429, "ratelimit", pe.Domain)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func handleRateLimit(w http.ResponseWriter, r *http.Request, pe *ProxyEndpoint) error {
|
func handleRateLimit(w http.ResponseWriter, r *http.Request, pe *ProxyEndpoint) error {
|
||||||
ip, _, err := net.SplitHostPort(r.RemoteAddr)
|
ip, _, err := net.SplitHostPort(r.RemoteAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -60,7 +68,7 @@ func handleRateLimit(w http.ResponseWriter, r *http.Request, pe *ProxyEndpoint)
|
|||||||
|
|
||||||
ipTable.Increment(ip)
|
ipTable.Increment(ip)
|
||||||
|
|
||||||
if ipTable.Exceeded(ip, 10) {
|
if ipTable.Exceeded(ip, int64(pe.RateLimit)) {
|
||||||
w.WriteHeader(429)
|
w.WriteHeader(429)
|
||||||
return errors.New("rate limit exceeded")
|
return errors.New("rate limit exceeded")
|
||||||
}
|
}
|
||||||
|
@ -125,8 +125,8 @@ type ProxyEndpoint struct {
|
|||||||
BasicAuthExceptionRules []*BasicAuthExceptionRule //Path to exclude in a basic auth enabled proxy target
|
BasicAuthExceptionRules []*BasicAuthExceptionRule //Path to exclude in a basic auth enabled proxy target
|
||||||
|
|
||||||
// Rate Limiting
|
// Rate Limiting
|
||||||
EnableRateLimiting bool
|
RequireRateLimit bool
|
||||||
RateLimiting int // Rate limit in requests per second
|
RateLimit int64 // Rate limit in requests per second
|
||||||
|
|
||||||
//Access Control
|
//Access Control
|
||||||
AccessFilterUUID string //Access filter ID
|
AccessFilterUUID string //Access filter ID
|
||||||
|
Loading…
x
Reference in New Issue
Block a user