richie.wiki
RICHIE'S NOTES

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

  • 主要参考Ubuntu系统下Clash代理工具的安装与配置指南这篇文章

  • 运行环境

    推荐使用腾讯云轻量应用服务器,选择新加坡锐驰型最便宜一档
    image.png
    操作系统选择ubuntu-24.04LTS,记得把用户最大进程数调高一些

    ulimit -u 65535
    
  • 安装mihomo

    release页面: https://github.com/MetaCubeX/mihomo/releases

    # 下载最新版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最好添加验证账户密码,防止被偷跑流量

    dns:
      enable: falseauthentication:
      - "myuser:mypassword"  # 这是你的用户和密码
    

    如果没有网页管理mihomo的需求,就把external-controller也注释掉

    dns也可以顺手关掉

    dns:
      enable: false
    
  • 启动mihomo

    方法一:直接启动

    mihomo -d /root/.config/clash
    

    方法二:或者使用Systemd服务, service文件在/usr/lib/systemd/system/mihomo.service
    Systemd服务启动命令

    # 重载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
    比如腾讯云轻量应用服务器,在详情页可以看到设置入口,添加这个规则即可
    image.png

  • 配置代理

    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 原因