# 通过clash客户端实现远程http代理

- wiki::
  updated:: [[2025-11-05]]
  created:: [[2025-10-29]]
  created-at:: [[2025-10-29]]
  updated-at:: [[2025-11-01]]
  
- 主要参考[Ubuntu系统下Clash代理工具的安装与配置指南](https://comate.baidu.com/zh/page/8zzmr692bec)这篇文章
- ### 运行环境
  推荐使用腾讯云轻量应用服务器，选择新加坡锐驰型最便宜一档
  ![image.png](../../assets/image_1761799553264_0.png){:height 235, :width 164}
  操作系统选择ubuntu-24.04LTS，记得把用户最大进程数调高一些
  ```shell
  ulimit -u 65535
  ```
- ### 安装mihomo
  release页面: https://github.com/MetaCubeX/mihomo/releases
  ```shell
  # 下载最新版deb文件
  curl -L -O $(curl -s "https://api.github.com/repos/MetaCubeX/mihomo/releases/latest" | jq -r '.assets[] | select(.name | contains("linux-amd64") and endswith(".deb") and ((contains("-v1-") or contains("-v2-") or contains("-v3-")) | not)) | .browser_download_url')
  
  sudo apt install -f mihomo-linux-amd64-*.deb
  ```
- ### 配置文件
  把提供商的yaml文件放到/etc/mihomo，文件名叫config.yaml
  
  下载好要打开config.yaml看一眼，保证配置项`allow-lan: true`
  
  如果接下来启动mihomo时，提醒mmdb下载失败的话，就手动下载`https://cdn.jsdelivr.net/gh/Dreamacro/maxmind-geoip@release/Country.mmdb`到`/etc/mihomo`目录下
  
  config.yaml最好添加验证账户密码，防止被偷跑流量
  ```yaml
  dns:
    enable: falseauthentication:
    - "myuser:mypassword"  # 这是你的用户和密码
  ```
  
  如果没有网页管理mihomo的需求，就把`external-controller`也注释掉
  
  dns也可以顺手关掉
  ```
  dns:
    enable: false
  ```
- ### 启动mihomo
  方法一：直接启动
  ```shell
  mihomo -d /root/.config/clash
  ```
  方法二：或者使用Systemd服务, service文件在`/usr/lib/systemd/system/mihomo.service`
  Systemd服务启动命令
  ```shell
  # 重载systemd配置
  sudo systemctl daemon-reload
  # 开机启动
  sudo systemctl enable mihomo
  # 手工启动
  sudo systemctl start mihomo
  # 查看日志
  sudo journalctl -u mihomo -f
  # 查看服务状态
  sudo systemctl status mihomo
  ```
- ### 记得防火墙放通7890端口
  ![image.png](../../assets/image_1761810366036_0.png){:height 177, :width 577}
  比如腾讯云轻量应用服务器，在详情页可以看到设置入口，添加这个规则即可
  ![image.png](../../assets/image_1761810451190_0.png){:height 78, :width 579}
- ### 配置代理
  proxy.pac文件 （⚠️pac文件不支持需要用户名和密码的代理）
  ```
  function FindProxyForURL(url, host) {
      var proxy = "PROXY xxx.xxx.xxx.xxx:7890";
  
      // OpenAI 相关域名
      if (
          dnsDomainIs(host, "openai.com") ||
          shExpMatch(host, "*.openai.com") ||
          shExpMatch(host, "*.chat.openai.com") ||
          shExpMatch(host, "chat.openai.com") ||
          shExpMatch(host, "platform.openai.com") ||
          shExpMatch(host, "auth0.openai.com")
      ) {
          return proxy;
      }
  
      // Claude (Anthropic) 相关域名
      if (
          dnsDomainIs(host, "claude.ai") ||
          shExpMatch(host, "*.claude.ai") ||
          dnsDomainIs(host, "anthropic.com") ||
          shExpMatch(host, "*.anthropic.com") ||
          shExpMatch(host, "api.anthropic.com") ||
          shExpMatch(host, "console.anthropic.com")
      ) {
          return proxy;
      }
  
      // Cursor 相关域名（包括 API、插件服务等）
      if (
          dnsDomainIs(host, "cursor.sh") ||
          shExpMatch(host, "*.cursor.sh") ||
          shExpMatch(host, "api.cursor.sh") ||
          shExpMatch(host, "registry.cursor.sh") ||
          shExpMatch(host, "cloud.cursor.sh")
      ) {
          return proxy;
      }
  
      // 其他走直连
      return "DIRECT";
  }
  
  ```
  
  macOS 终端设置代理命令：
  ```
  # 本地proxy.pac
  networksetup -setautoproxyurl "Wi-Fi" "file:///path/to/your/proxy.pac"
  
  # 或者是把proxy.pac放到了http server
  networksetup -setautoproxyurl "Wi-Fi" "http://xxx.xxx.xxx.xxx/proxy.pac"
  ```
  
  Windows 设置代理命令（PowerShell）：
  ```
  # 本地proxy.pac
  Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" -Name AutoConfigURL -Value "file:///C:/Users/实际用户名/Documents/proxy.pac"
  
  # 或者是把proxy.pac放到了http server
  Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" -Name AutoConfigURL -Value "http://xxx.xxx.xxx.xxx/proxy.pac"
  ```
  
  配置后可以使用ChatGPT、Claude桌面客户端，解锁cursor被禁用的模型。
- ### 关闭代理
  macOS 终端设置代理命令：
  ```
  networksetup -setautoproxystate "Wi-Fi" off
  ```
  
  Windows 关闭代理命令（PowerShell）：
  ```
  Remove-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" -Name AutoConfigURL
  ```
- ### Claude Code配置代理
  支持在Macos和devcloud上配置：
  ```
  cp ~/.claude/settings.json ~/.claude/settings.json.bak
  echo '{
  "env": {
    "HTTP_PROXY": "http://xxx.xxx.xxx.xxx:7890",
    "HTTPS_PROXY": "http://xxx.xxx.xxx.xxx:7890"
  }
  }' > ~/.claude/settings.json
  ```
  
  如果配置了账号密码：
  ```
  cp ~/.claude/settings.json ~/.claude/settings.json.bak
  echo '{
  "env": {
    "HTTP_PROXY": "http://myuser:mypassword@xxx.xxx.xxx.xxx:7890",
    "HTTPS_PROXY": "http://myuser:mypassword@xxx.xxx.xxx.xxx:7890"
  }
  }' > ~/.claude/settings.json
  ```
- 如果要使用nginx做带有密码的反代比较麻烦，不如直接使用squid [原因](https://claude.ai/share/151d6c33-7f64-4f5d-828d-0246537840b0)
