mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-09-13 07:40:56 +00:00
feat(task): replace built-in task adaptors with a sandboxed JS plugin system (#7076)
This commit is contained in:
@@ -1,51 +1,20 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/QuantumNous/new-api/common"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var defaultTrustedProxyCIDRs = []string{
|
||||
"127.0.0.0/8",
|
||||
"::1",
|
||||
"10.0.0.0/8",
|
||||
"172.16.0.0/12",
|
||||
"192.168.0.0/16",
|
||||
"fc00::/7",
|
||||
}
|
||||
|
||||
func ConfigureTrustedProxies(engine *gin.Engine) error {
|
||||
rawTrustedProxies := strings.TrimSpace(os.Getenv("TRUSTED_PROXIES"))
|
||||
if rawTrustedProxies == "" {
|
||||
trustedProxies, usedDefaults, err := common.ResolveTrustedProxies(os.Getenv("TRUSTED_PROXIES"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if usedDefaults {
|
||||
log.Print("WARNING: TRUSTED_PROXIES is unset or blank; trusting loopback, RFC 1918, and IPv6 ULA proxy addresses for compatibility. Set TRUSTED_PROXIES=none to trust no proxies, or configure explicit proxy IPs/CIDRs to replace these defaults.")
|
||||
return engine.SetTrustedProxies(defaultTrustedProxyCIDRs)
|
||||
}
|
||||
if strings.EqualFold(rawTrustedProxies, "none") {
|
||||
return engine.SetTrustedProxies(nil)
|
||||
}
|
||||
|
||||
parts := strings.Split(rawTrustedProxies, ",")
|
||||
trustedProxies := make([]string, 0, len(parts))
|
||||
for _, part := range parts {
|
||||
trustedProxy := strings.TrimSpace(part)
|
||||
if trustedProxy == "" {
|
||||
continue
|
||||
}
|
||||
if strings.EqualFold(trustedProxy, "none") {
|
||||
return errors.New("TRUSTED_PROXIES=none must be used alone")
|
||||
}
|
||||
trustedProxies = append(trustedProxies, trustedProxy)
|
||||
}
|
||||
if len(trustedProxies) == 0 {
|
||||
return errors.New("TRUSTED_PROXIES does not contain an IP address or CIDR")
|
||||
}
|
||||
if err := engine.SetTrustedProxies(trustedProxies); err != nil {
|
||||
return fmt.Errorf("invalid TRUSTED_PROXIES: %w", err)
|
||||
}
|
||||
return nil
|
||||
return common.ConfigureTrustedProxies(engine, trustedProxies)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user