Skip to content

Commit

Permalink
Fixed Duplicate Code
Browse files Browse the repository at this point in the history
  • Loading branch information
41Baloo committed Feb 21, 2023
1 parent 3650819 commit 4dd385f
Showing 1 changed file with 6 additions and 59 deletions.
65 changes: 6 additions & 59 deletions core/server/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ func Middleware(writer http.ResponseWriter, request *http.Request) {
botFp = firewall.BotFingerprints[tlsFp]
}

firewall.Mutex.Lock()
domainData = domains.DomainsData[domainName]
domainData.TotalRequests++
domains.DomainsData[domainName] = domainData
firewall.Mutex.Unlock()

writer.Header().Set("baloo-Proxy", "1.2")

//Start the suspicious level where the stage currently is
Expand All @@ -83,25 +89,13 @@ func Middleware(writer http.ResponseWriter, request *http.Request) {
if ipCountCookie > proxy.FailChallengeRatelimit {
writer.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(writer, "Blocked by BalooProxy.\nYou have been ratelimited. (R1)")

firewall.Mutex.Lock()
domainData = domains.DomainsData[domainName]
domainData.TotalRequests++
domains.DomainsData[domainName] = domainData
firewall.Mutex.Unlock()
return
}

//Ratelimit spamming Ips (feel free to play around with the threshhold)
if ipCount > proxy.IPRatelimit {
writer.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(writer, "Blocked by BalooProxy.\nYou have been ratelimited. (R2)")

firewall.Mutex.Lock()
domainData = domains.DomainsData[domainName]
domainData.TotalRequests++
domains.DomainsData[domainName] = domainData
firewall.Mutex.Unlock()
return
}

Expand All @@ -110,12 +104,6 @@ func Middleware(writer http.ResponseWriter, request *http.Request) {
if fpCount > proxy.FPRatelimit {
writer.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(writer, "Blocked by BalooProxy.\nYou have been ratelimited. (R3)")

firewall.Mutex.Lock()
domainData = domains.DomainsData[domainName]
domainData.TotalRequests++
domains.DomainsData[domainName] = domainData
firewall.Mutex.Unlock()
return
}

Expand All @@ -129,12 +117,6 @@ func Middleware(writer http.ResponseWriter, request *http.Request) {
if forbiddenFp != "" {
writer.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(writer, "Blocked by BalooProxy.\nYour browser %s is not allowed.", forbiddenFp)

firewall.Mutex.Lock()
domainData = domains.DomainsData[domainName]
domainData.TotalRequests++
domains.DomainsData[domainName] = domainData
firewall.Mutex.Unlock()
return
}

Expand Down Expand Up @@ -199,12 +181,6 @@ func Middleware(writer http.ResponseWriter, request *http.Request) {
default:
writer.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(writer, "Blocked by BalooProxy.\nSuspicious request of level %d (base %d)", susLv, domainData.Stage)

firewall.Mutex.Lock()
domainData = domains.DomainsData[domainName]
domainData.TotalRequests++
domains.DomainsData[domainName] = domainData
firewall.Mutex.Unlock()
return
}
firewall.Mutex.Lock()
Expand All @@ -226,22 +202,10 @@ func Middleware(writer http.ResponseWriter, request *http.Request) {
case 1:
writer.Header().Set("Set-Cookie", "_1__bProxy_v="+encryptedIP+"; SameSite=None; path=/; Secure")
http.Redirect(writer, request, request.URL.RequestURI(), http.StatusTemporaryRedirect)

firewall.Mutex.Lock()
domainData = domains.DomainsData[domainName]
domainData.TotalRequests++
domains.DomainsData[domainName] = domainData
firewall.Mutex.Unlock()
return
case 2:
writer.Header().Set("Content-Type", "text/html")
fmt.Fprintf(writer, `<script>document.cookie = '_2__bProxy_v=%s; SameSite=None; path=/; Secure';window.location.reload();</script>`, encryptedIP)

firewall.Mutex.Lock()
domainData = domains.DomainsData[domainName]
domainData.TotalRequests++
domains.DomainsData[domainName] = domainData
firewall.Mutex.Unlock()
return
case 3:
secretPart := encryptedIP[:6]
Expand All @@ -268,12 +232,6 @@ func Middleware(writer http.ResponseWriter, request *http.Request) {
var buf bytes.Buffer
if err := png.Encode(&buf, captchaImg); err != nil {
fmt.Fprintf(writer, `BalooProxy Error: Failed to encode captcha: %s`, err)

firewall.Mutex.Lock()
domainData = domains.DomainsData[domainName]
domainData.TotalRequests++
domains.DomainsData[domainName] = domainData
firewall.Mutex.Unlock()
return
}
data := buf.Bytes()
Expand Down Expand Up @@ -511,20 +469,10 @@ func Middleware(writer http.ResponseWriter, request *http.Request) {
}
</script>
`, captchaData, ip, publicPart)
firewall.Mutex.Lock()
domainData = domains.DomainsData[domainName]
domainData.TotalRequests++
domains.DomainsData[domainName] = domainData
firewall.Mutex.Unlock()
return
default:
writer.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(writer, "Blocked by BalooProxy.\nSuspicious request of level %d (base %d)", susLv, domainData.Stage)
firewall.Mutex.Lock()
domainData = domains.DomainsData[domainName]
domainData.TotalRequests++
domains.DomainsData[domainName] = domainData
firewall.Mutex.Unlock()
return
}
}
Expand All @@ -551,7 +499,6 @@ func Middleware(writer http.ResponseWriter, request *http.Request) {

firewall.Mutex.Lock()
domainData = domains.DomainsData[domainName]
domainData.TotalRequests++
domainData.BypassedRequests++
domains.DomainsData[domainName] = domainData
firewall.Mutex.Unlock()
Expand Down

0 comments on commit 4dd385f

Please sign in to comment.