Mac电脑上给Openclaw配置代理

2026/03/01 AI 共 2179 字,约 7 分钟

Mac电脑上给Openclaw配置代理


1)配置 openclaw.json

进入目录,备份一下

cd /Users/ab/.openclaw     
cp openclaw.json openclaw.json.bak.$(date +%Y%m%d-%H%M%S)

假设代理端口是7890,则在 openclaw.json 中加入

  "channels": {
    "telegram": {
      "network": {
        "autoSelectFamily": false
      },
      "proxy": "http://127.0.0.1:7890"
    }
  }

关掉 autoSelectFamily(避免优先 IPv6/自动切换导致失败)

配置 proxy 代理

2)新建 /Users/ab/.openclaw/undici-proxy.mjs 文件如下

import {
  ProxyAgent,
  setGlobalDispatcher,
  getGlobalDispatcher,
} from "/Users/ab/.npm-global/lib/node_modules/openclaw/node_modules/undici/index.js";

const proxy =
  process.env.HTTPS_PROXY ||
  process.env.HTTP_PROXY ||
  process.env.https_proxy ||
  process.env.http_proxy;

if (!proxy) {
  console.log("[undici] proxy not set");
} else {
  const agent = new ProxyAgent(proxy);

  const ensure = () => {
    const cur = getGlobalDispatcher();
    const name = cur?.constructor?.name || "unknown";
    if (name !== "ProxyAgent") {
      setGlobalDispatcher(agent);
      return "reset->ProxyAgent";
    }
    return "ok";
  };

  // 启动时先确保一次
  console.log("[undici] proxy enabled:", proxy, "ensure:", ensure());

  // 之后防止被覆盖:每 500ms 检查一次(很轻量)
  setInterval(() => {
    try {
      ensure();
    } catch {}
  }, 500).unref();
}

3)配置 ai.openclaw.gateway.plist

进入目录,备份一下

cd /Users/ab/Library/LaunchAgents/
cp ai.openclaw.gateway.plist ai.openclaw.gateway.plist.bak.$(date +%Y%m%d-%H%M%S)

在 EnvironmentVariables 下新增

    <key>HTTP_PROXY</key>
    <string>http://127.0.0.1:7890</string>
    <key>HTTPS_PROXY</key>
    <string>http://127.0.0.1:7890</string>
    <key>http_proxy</key>
    <string>http://127.0.0.1:7890</string>
    <key>https_proxy</key>
    <string>http://127.0.0.1:7890</string>
    <key>NO_PROXY</key>
    <string>localhost,127.0.0.1,*.local,10.*,192.168.*</string>
    <key>no_proxy</key>
    <string>localhost,127.0.0.1,*.local,10.*,192.168.*</string>
    <key>NODE_OPTIONS</key>
    <string>--import /Users/ab/.openclaw/undici-proxy.mjs</string>

重新加载

launchctl bootout gui/$(id -u) /Users/ab/Library/LaunchAgents/ai.openclaw.gateway.plist
launchctl bootstrap gui/$(id -u) /Users/ab/Library/LaunchAgents/ai.openclaw.gateway.plist
launchctl kickstart -k gui/$(id -u)/ai.openclaw.gateway

4)其他命令:

如果不确定自己是不是7890端口,可以使用代理命令测试telegream是否可通

curl --proxy http://127.0.0.1:7890 https://api.telegram.org/bot<token>/getMe

文档信息

Search

    Table of Contents