yikart/AiToEarn · error · Error

代理地址不合法

Error message

代理地址不合法

What it means

requestNet validates any proxy string passed in its options. The proxy string is parsed with parseProxyString and the resulting ipAndPort must match the ipv4Regular IPv4 regex; if parsing fails (returns false) or the address is not a valid IPv4:port, this Error is thrown. The library only supports IPv4 proxy addresses, not hostnames or IPv6.

Source

Thrown at project/aitoearn-electron/electron/plat/requestNet.ts:46

  headers,
  body,
  method,
  url,
  isFile,
  formData,
  isReqFile,
  proxy,
}: IRequestNetParams): Promise<IRequestNetResult<T>> => {
  let customSession: Session;
  let proxyInfo: ProxyInfo | false;
  return new Promise(async (resolve, reject) => {
    try {
      // 如果传入了代理配置,动态设置代理
      if (proxy) {
        // 解析代理信息
        proxyInfo = parseProxyString(proxy);
        if (proxyInfo === false || !ipv4Regular.test(proxyInfo.ipAndPort))
          throw new Error('代理地址不合法');
        customSession = session.fromPartition(
          `persist:proxy-session-${Date.now()}`,
        );
        const proxyUrl = `${proxyInfo.protocol}://${proxyInfo.ipAndPort}`;
        const proxyRules = `http=${proxyUrl};https=${proxyUrl}`;
        console.log(proxyRules);
        await customSession.setProxy({
          proxyRules,
        });

        headers = {
          ...(headers ? headers : {}),
          ...(proxy
            ? {
                'x-forwarded-for': proxyInfo.ipAndPort,
              }
            : {}),
        };

View on GitHub (pinned to d3aa8bea5b)

Solutions

  1. Use a numeric IPv4 address with port, e.g. '127.0.0.1:7890' or '192.168.1.10:8080'.
  2. Check the string format parseProxyString expects (protocol + ip:port, optionally user:pass) and match it exactly.
  3. Remove the proxy option entirely if no proxy is needed — validation only runs when proxy is truthy.
  4. Pre-validate the proxy with the ipv4Regular regex before passing it to requestNet.

Example fix

// before
await requestNet({ url, proxy: 'http://proxy.corp.local:8080' });
// after
const proxy = 'http://10.0.0.8:8080'; // must be IPv4 ip:port
if (!ipv4Regular.test(proxy)) throw new Error('proxy must be ipv4:port');
await requestNet({ url, proxy });
Defensive patterns

Strategy: validation

Validate before calling

import { ipv4Regular } from './commont/regular';
function isProxyValid(proxy?: string): boolean {
  if (!proxy) return true; // no proxy is fine
  const m = proxy.match(/^(\w+:\/\/)?([^/?]+)$/);
  return !!m && ipv4Regular.test(m[2]);
}
if (!isProxyValid(proxy)) throw new Error('proxy must be ipv4:port');

Type guard

function parseProxySafe(proxy: string): ProxyInfo | null {
  const info = parseProxyString(proxy);
  return info === false ? null : info;
}

Try / catch

try {
  await requestNet({ url, proxy });
} catch (e) {
  if (e.message === '代理地址不合法') {
    // prompt user to enter a valid ipv4:port proxy
  }
}

Prevention

When it happens

Trigger: Calling requestNet (or any platform service that forwards a proxy option, e.g. Douyin/Shipinhao publish flows) with proxy set to a malformed string like 'http=proxy.com:8080' (hostname), an IPv6 address, a missing port, or an unparseable format.

Common situations: Users entering a proxy hostname (e.g. proxy.example.com:8080) instead of an IP, copying a proxy URL with scheme/path into the ip:port field, IPv6 proxies, or empty/garbage values in proxy config UI.

Related errors


AI-assisted analysis of yikart/AiToEarn@d3aa8bea5b (2026-08-31). Data as JSON: /api/errors/a4d8cb0561f0a85d. Report an issue: GitHub.