Files
new-api/common/proxy_url.go
T
CaIon 14a9acd368 feat(custom): CNY native billing, daily check-in button, subscription plans page, usage docs and promo banner
Based on QuantumNous/new-api v1.0.0-rc.36 (shallow history squashed).

- CNY quota display mode: RMB-denominated top-up and crediting across Epay/Waffo channels
- New CheckinButton in both authenticated and public headers (Turnstile supported)
- New /subscribe page with public plan snapshot, Alipay/balance payment, auto group upgrade/reset
- New /docs 12-section beginner guide with anchor navigation
- New home PromoBanner linking to /docs; i18n synced (en/zh/zh-TW/fr/ja/ru/vi)
- README: document custom features, live demo https://ai.88531.cn
2026-09-23 16:28:52 +08:00

78 lines
2.3 KiB
Go

package common
import (
"fmt"
"net"
"net/url"
"strconv"
"strings"
)
// ParseProxyURLStrict validates and normalizes a proxy URL for persistence.
func ParseProxyURLStrict(rawProxyURL string) (*url.URL, error) {
parsedURL, _, err := parseProxyURL(rawProxyURL, false)
return parsedURL, err
}
// ParseProxyURLRuntime validates and normalizes a proxy URL for runtime use.
// The boolean result reports whether a legacy path, query, or fragment was removed.
func ParseProxyURLRuntime(rawProxyURL string) (*url.URL, bool, error) {
return parseProxyURL(rawProxyURL, true)
}
func parseProxyURL(rawProxyURL string, allowLegacySuffix bool) (*url.URL, bool, error) {
trimmedProxyURL := strings.TrimSpace(rawProxyURL)
if trimmedProxyURL == "" {
return nil, false, nil
}
parsedURL, err := url.Parse(trimmedProxyURL)
if err != nil {
return nil, false, fmt.Errorf("invalid proxy URL")
}
parsedURL.Scheme = strings.ToLower(parsedURL.Scheme)
switch parsedURL.Scheme {
case "http", "https", "socks5", "socks5h":
default:
return nil, false, fmt.Errorf("proxy URL must use http, https, socks5, or socks5h")
}
if parsedURL.Hostname() == "" {
return nil, false, fmt.Errorf("proxy URL must include a host")
}
if portText := parsedURL.Port(); portText != "" {
port, err := strconv.Atoi(portText)
if err != nil || port < 1 || port > 65535 {
return nil, false, fmt.Errorf("proxy URL must include a valid port")
}
}
hasQuery := parsedURL.RawQuery != "" || parsedURL.ForceQuery
hasFragment := strings.Contains(trimmedProxyURL, "#")
escapedPath := parsedURL.EscapedPath()
hasNonRootPath := escapedPath != "" && escapedPath != "/"
legacySuffixStripped := hasQuery || hasFragment || hasNonRootPath
if !allowLegacySuffix {
switch {
case hasQuery:
return nil, false, fmt.Errorf("proxy URL must not include a query")
case hasFragment:
return nil, false, fmt.Errorf("proxy URL must not include a fragment")
case hasNonRootPath:
return nil, false, fmt.Errorf("proxy URL must not include a path")
}
}
parsedURL.Path = ""
parsedURL.RawPath = ""
parsedURL.RawQuery = ""
parsedURL.ForceQuery = false
parsedURL.Fragment = ""
parsedURL.RawFragment = ""
if (parsedURL.Scheme == "socks5" || parsedURL.Scheme == "socks5h") && parsedURL.Port() == "" {
parsedURL.Host = net.JoinHostPort(parsedURL.Hostname(), "1080")
}
return parsedURL, legacySuffixStripped, nil
}