Start fixing ratelimit + revers proxy error

main
Leonid Maslakov 7 months ago
parent bec7ac0c85
commit 1c4fe2136d

@ -20,6 +20,7 @@ package logger
import (
"fmt"
"git.lcomrade.su/root/lenpaste/internal/netshare"
"net/http"
"time"
)
@ -40,10 +41,10 @@ func (config Config) Error(err error) {
func (config Config) HttpRequest(req *http.Request) {
now := time.Now()
fmt.Println(now.Format(config.TimeFormat), "[REQUEST]", req.RemoteAddr, req.Method, req.URL.Path)
fmt.Println(now.Format(config.TimeFormat), "[REQUEST]", netshare.GetClientAddr(req), req.Method, req.URL.Path)
}
func (config Config) HttpError(req *http.Request, err error) {
now := time.Now()
fmt.Println(now.Format(config.TimeFormat), "[ERROR]", req.RemoteAddr, req.Method, req.URL.Path, "Error:", err.Error())
fmt.Println(now.Format(config.TimeFormat), "[ERROR]", netshare.GetClientAddr(req), req.Method, req.URL.Path, "Error:", err.Error())
}

@ -20,6 +20,7 @@ package netshare
import (
"net/http"
"strings"
)
func GetHost(req *http.Request) string {
@ -45,3 +46,16 @@ func GetProtocol(header http.Header) string {
return "http"
}
func GetClientAddr(req *http.Request) string {
// Read header
xFor := req.Header.Get("X-Forwarded-For")
xFor = strings.Split(xFor, ",")[0]
// Check
if xFor != "" {
return xFor
}
return req.RemoteAddr
}

@ -31,7 +31,7 @@ import (
func PasteAddFromForm(req *http.Request, db storage.DB, rateLimit RateLimit, titleMaxLen int, bodyMaxLen int, maxLifeTime int64, lexerNames []string) (string, int64, int64, error) {
// Check rate limit
cleanIP, _, err := net.SplitHostPort(req.RemoteAddr)
cleanIP, _, err := net.SplitHostPort(GetClientAddr(req))
if err != nil {
return "", 0, 0, err
}

Loading…
Cancel
Save