fish2018/pansou · error

解析代理URL失败

Error message

解析代理URL失败: %w

What it means

In dy4k's createProxyTransport, url.Parse failed on the configured HTTP (non-SOCKS5) proxy URL, so createProxyTransport aborts with this wrapped error. url.Parse is lenient but rejects control characters, invalid percent-escapes, and some malformed schemes. Nothing has connected yet; this is purely a URL-syntax failure in configuration.

Solutions

  1. Log the exact proxyURL value and run url.Parse on it manually to see the parse error.
  2. Normalize the config value: trim spaces/quotes and ensure a scheme prefix like http:// is present.
  3. Pre-validate the proxy setting at startup and fail with a clear configuration message instead of at request time.
  4. Use url.Parse with a scheme default, e.g. prepend "http://" when no scheme is present before parsing.

Example fix

// before
parsedURL, err := url.Parse(proxyURL)
if err != nil {
    return nil, fmt.Errorf("解析代理URL失败: %w", err)
}
// after
proxyURL = strings.TrimSpace(strings.Trim(proxyURL, "\"'"))
parsedURL, err := url.Parse(proxyURL)
if err != nil {
    return nil, fmt.Errorf("解析代理URL失败 %q: %w", proxyURL, err)
}
Defensive patterns

Strategy: validation

Validate before calling

func validateHTTPProxy(proxyURL string) error {
    proxyURL = strings.TrimSpace(proxyURL)
    if !strings.Contains(proxyURL, "://") {
        proxyURL = "http://" + proxyURL
    }
    u, err := url.Parse(proxyURL)
    if err != nil { return err }
    if u.Host == "" { return fmt.Errorf("proxy missing host: %q", proxyURL) }
    return nil
}

Type guard

func isParseableURL(s string) bool {
    _, err := url.Parse(strings.TrimSpace(s))
    return err == nil
}

Prevention

When it happens

Trigger: The proxy string (from config/env) for the HTTP proxy branch is malformed — e.g. contains spaces, stray characters, or invalid escape sequences — so url.Parse(proxyURL) returns a non-nil error.

Common situations: Proxy env var set with surrounding whitespace or quotes pasted from a shell config; missing scheme handled elsewhere but an accidental 'http//:' typo breaks parsing; hostnames containing illegal characters from a template substitution.

Understand the failure class

Background: "Invalid URL" errors: why new URL(), URI.parse, and reqwest::Url reject your string — missing scheme, whitespace, and bad path format — this error's family across 39 libraries.

Related errors


AI-assisted analysis of fish2018/pansou@beaa561337 (2026-09-07). Data as JSON: /api/errors/735480a5ac985332. Report an issue: GitHub.

Appendix: source

Thrown at plugin/dy4k/dy4k.go:161

	}

	if proxyURL == "" {
		return transport, nil
	}

	if strings.HasPrefix(proxyURL, "socks5://") {
		// SOCKS5代理
		dialer, err := proxy.SOCKS5("tcp", strings.TrimPrefix(proxyURL, "socks5://"), nil, proxy.Direct)
		if err != nil {
			return nil, fmt.Errorf("创建SOCKS5代理失败: %w", err)
		}
		transport.Dial = dialer.Dial
		debugPrintf("🔧 [Dy4k DEBUG] 使用SOCKS5代理: %s\n", proxyURL)
	} else {
		// HTTP代理
		parsedURL, err := url.Parse(proxyURL)
		if err != nil {
			return nil, fmt.Errorf("解析代理URL失败: %w", err)
		}
		transport.Proxy = http.ProxyURL(parsedURL)
		debugPrintf("🔧 [Dy4k DEBUG] 使用HTTP代理: %s\n", proxyURL)
	}

	return transport, nil
}

// createOptimizedHTTPClient 创建优化的HTTP客户端(支持代理)
func createOptimizedHTTPClient() *http.Client {
	var selectedProxy string

	if ProxyEnabled {
		// 随机选择代理类型
		proxyTypes := []string{"", DefaultHTTPProxy, DefaultSocks5Proxy}
		selectedProxy = proxyTypes[rand.Intn(len(proxyTypes))]
	} else {
		// 代理未启用,使用直连

View on GitHub (pinned to beaa561337)