fix(api): 仅信任配置的反向代理转发客户端 IP (#39)
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
@@ -17,6 +17,7 @@ import (
|
||||
"github.com/baicai2026-baicai/goods/api/internal/adminstore"
|
||||
"github.com/baicai2026-baicai/goods/api/internal/adminweb"
|
||||
"github.com/baicai2026-baicai/goods/api/internal/auth"
|
||||
"github.com/baicai2026-baicai/goods/api/internal/clientip"
|
||||
"github.com/baicai2026-baicai/goods/api/internal/ratelimit"
|
||||
)
|
||||
|
||||
@@ -32,6 +33,10 @@ func main() {
|
||||
dbURL := getenv("OPENGOODS_DATABASE_URL", "postgres://opengoods:opengoods@localhost:5432/opengoods?sslmode=disable")
|
||||
basePath := getenv("GOODS_ADMIN_BASE_PATH", "/ping")
|
||||
username := getenv("GOODS_ADMIN_USER", "admin")
|
||||
clientIPs, err := clientip.Parse(getenv("OPENGOODS_TRUSTED_PROXIES", ""))
|
||||
if err != nil {
|
||||
log.Fatalf("invalid OPENGOODS_TRUSTED_PROXIES: %v", err)
|
||||
}
|
||||
|
||||
// Password: prefer a bcrypt hash; otherwise hash a plaintext password.
|
||||
var passwordHash []byte
|
||||
@@ -72,7 +77,8 @@ func main() {
|
||||
authn := auth.New(username, passwordHash, secret, 12*time.Hour)
|
||||
usage := ratelimit.New(getenv("OPENGOODS_REDIS_URL", "redis://localhost:6379/0"))
|
||||
h := adminhandler.New(adminstore.New(pool), authn, basePath, adminweb.Dist()).
|
||||
WithUsage(usage)
|
||||
WithUsage(usage).
|
||||
WithClientIP(clientIPs)
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: addr,
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"github.com/baicai2026-baicai/goods/api/internal/cache"
|
||||
"github.com/baicai2026-baicai/goods/api/internal/clientip"
|
||||
"github.com/baicai2026-baicai/goods/api/internal/config"
|
||||
"github.com/baicai2026-baicai/goods/api/internal/handler"
|
||||
"github.com/baicai2026-baicai/goods/api/internal/publicweb"
|
||||
@@ -19,6 +20,10 @@ import (
|
||||
|
||||
func main() {
|
||||
cfg := config.Load()
|
||||
clientIPs, err := clientip.Parse(cfg.TrustedProxies)
|
||||
if err != nil {
|
||||
log.Fatalf("invalid OPENGOODS_TRUSTED_PROXIES: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
pool, err := pgxpool.New(ctx, cfg.DatabaseURL)
|
||||
@@ -44,7 +49,8 @@ func main() {
|
||||
}
|
||||
h := handler.New(store.New(pool).WithCache(readCache), publicweb.Dist()).
|
||||
WithRateLimit(limiter, cfg.AnonRateLimitPerMin).
|
||||
WithQuotas(cfg.AnonTotalQuota, cfg.RegisteredRateLimitPerMin, cfg.RegisteredQuotaTotal)
|
||||
WithQuotas(cfg.AnonTotalQuota, cfg.RegisteredRateLimitPerMin, cfg.RegisteredQuotaTotal).
|
||||
WithClientIP(clientIPs)
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: cfg.Addr,
|
||||
|
||||
Reference in New Issue
Block a user