Skip to content

Commit 21fc3f7

Browse files
committed
Add TrustedProxies configuration to support proxy IPs in Gin
1 parent 70a18c2 commit 21fc3f7

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

common/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ const (
7070
type Config struct {
7171
Listen string `yaml:"listen"`
7272

73+
// TrustedProxies is a list of trusted proxy IPs or CIDRs.
74+
// When set, Gin will only trust X-Forwarded-For from these sources.
75+
// When empty (default), proxy headers are not trusted and ClientIP()
76+
// returns the direct remote address.
77+
TrustedProxies []string `yaml:"trusted-proxies"`
78+
7379
Db DbConfig `yaml:"db"`
7480

7581
APIPath string `yaml:"api-path"`

docs/config.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
# The application will listen at this address
22
listen: :8089
33

4+
# Trusted proxy IPs or CIDRs.
5+
# When set, Gin will only trust X-Forwarded-For headers from these sources
6+
# to determine the client IP. This is important for IP-based rate limiting
7+
# and logging to work correctly.
8+
# When empty or not set (default), proxy headers are NOT trusted and
9+
# ClientIP() returns the direct remote address (most secure default).
10+
# If go-drive is running behind a reverse proxy (e.g. Nginx), you MUST
11+
# configure this to include the proxy's IP/CIDR, otherwise the real
12+
# client IP will not be recognized.
13+
#trusted-proxies:
14+
# - 127.0.0.1
15+
# - 10.0.0.0/8
16+
# - 172.16.0.0/12
17+
# - 192.168.0.0/16
18+
419
db:
520
# database type: currently supports sqlite, mysql
621
type: sqlite

server/server.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ func InitServer(config common.Config,
5555

5656
engine := gin.New()
5757

58+
if len(config.TrustedProxies) > 0 {
59+
engine.SetTrustedProxies(config.TrustedProxies)
60+
} else {
61+
engine.SetTrustedProxies(nil)
62+
}
63+
5864
engine.Use(gin.CustomRecovery(handlePanic))
5965

6066
if noLogRequest, _ := os.LookupEnv("NO_LOG_REQUEST"); noLogRequest == "" {

0 commit comments

Comments
 (0)