Skip to content

Commit cb11c31

Browse files
committed
cors-proxy: get ip from header
1 parent e5ffd6d commit cb11c31

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

cmd/cors-proxy/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ MAX_RESPONSE_SIZE=1048576 # 1MB in bytes (1024^2)
3535
ALLOWED_HOSTS=github.com,raw.githubusercontent.com,gitlab.com,codeberg.org
3636
RATE_TOKENS=40
3737
RATE_INTERVAL=1m
38+
RATE_IP_SOURCE_HEADER="" # useful when behind a reverse-proxy
3839
```
3940

4041
## Usage

cmd/cors-proxy/utils.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,14 @@ func rateLimit(next http.Handler) http.Handler {
5959
panic(err)
6060
}
6161

62-
middleware, err := httplimit.NewMiddleware(store, httplimit.IPKeyFunc())
62+
var limitFunc httplimit.KeyFunc
63+
if cfg.IpSourceHeader != "" {
64+
limitFunc = httplimit.IPKeyFunc(cfg.IpSourceHeader)
65+
} else {
66+
limitFunc = httplimit.IPKeyFunc()
67+
}
68+
69+
middleware, err := httplimit.NewMiddleware(store, limitFunc)
6370
if err != nil {
6471
panic(err)
6572
}
@@ -165,6 +172,7 @@ type config struct {
165172
AllowedHosts []string `env:"ALLOWED_HOSTS,default=github.com,raw.githubusercontent.com,gitlab.com,codeberg.org"`
166173
RateTokens uint64 `env:"RATE_TOKENS,default=40"` // 40 req/min should be ok for legit usage
167174
RateInterval time.Duration `env:"RATE_INTERVAL,default=1m"`
175+
IpSourceHeader string `env:"RATE_IP_SOURCE_HEADER"` // for reverse proxy etc.
168176
}
169177

170178
func loadConfig() {

0 commit comments

Comments
 (0)