本文将介绍如何在服务器中命令行配置Clash代理,并成功访问外网和ChatGPT API
前言
因为后续服务器需要调用ChatGPT API,所以要配置好系统代理。
我本来尝试用clash-verge配置带图形界面的代理应用,但不知道为啥,我在macos下能打开ubuntu服务器的xclock图形应用,死活打不开calsh-verge图形应用,遂放弃。只能纯命令行部署系统代理。
尝试在服务器上下载clash安装包,由于网络问题下载不了,被迫下载到本地然后sftp上传到服务器。
clash配置好后,命令行下修改vpn节点的方式很奇怪,需要通过curl命令发送http请求实现的,而且你发送的请求里必须要带特定emoji😂。
下载安装文件
从 https://github.com/DustinWin/clash_singbox-tools/releases/tag/Clash-Premium 选择 https://github.com/DustinWin/clash_singbox-tools/releases/download/Clash-Premium/clashpremium-release-linux-amd64-v3.tar.gz 进行下载。
下载到本地后,通过FileZilla上传到服务器。
安装clash
tar -zxvf clashpremium-release-linux-amd64-v3.tar.gz #解压安装包
sudo mv clash /usr/local/bin/clash #复制clash二进制文件到系统相应目录
sudo chmod +x /usr/local/bin/clash #开启执行权限
通过订阅链接下载配置文件
curl -o ~/.config/clash/config.yaml https://your_subscription_link
运行clash
clash & #后台运行clash
配置Country.mmdb文件
遇到了点小问题:
(base) dachuang@master:~/tmp$ clash
20:37:47 WRN [MMDB] db invalid, remove and download path=/home/dachuang/.config/clash/Country.mmdb
^C
(base) dachuang@master:~/tmp$
大意是说Country.mmdb这个文件已损坏,需要你删了重新下。
于是我被迫去 https://www.maxmind.com/en/geolite2/thank-you 这个网站注册了账号。
注册完账号申请了一个 License Key,然后通过相应的License Key下载了一个Country.mmdb文件到相应目录:
wget "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&license_key=<Your License Key>&suffix=tar.gz" -O GeoLite2-Country.tar.gz
tar -xzf GeoLite2-Country.tar.gz
cd GeoLite2-Country_20240920
cp GeoLite2-Country.mmdb ../Country.mmdb
再次启动clash
(base) dachuang@master:~/.config/clash$ clash &
(base) dachuang@master:~/.config/clash$ 21:08:56 INF [Config] initial compatible provider name=🔰 选择节点
21:08:56 INF [Config] initial compatible provider name=🎮 Steam 商店/社区
21:08:56 INF [Config] initial compatible provider name=🌏 爱奇艺&哔哩哔哩
21:08:56 INF [Config] initial compatible provider name=🛑 拦截广告
21:08:56 INF [Config] initial compatible provider name=🌩️ Cloudflare
21:08:56 INF [Config] initial compatible provider name=📺 动画疯
21:08:56 INF [Config] initial compatible provider name=🎮 Steam 登录/下载
21:08:56 INF [Config] initial compatible provider name=🐟 漏网之鱼
21:08:56 INF [Config] initial compatible provider name=🇨🇳 国内网站
21:08:56 INF [Config] initial compatible provider name=☁️ OneDrive
21:08:56 INF inbound create success inbound=http addr=127.0.0.1:7890 network=tcp
21:08:56 INF inbound create success inbound=socks addr=127.0.0.1:7891 network=tcp
21:08:56 INF inbound create success inbound=socks addr=127.0.0.1:7891 network=udp
21:08:56 INF [API] listening addr=127.0.0.1:9090
(base) dachuang@master:~/.config/clash$
(base) dachuang@master:~/.config/clash$
(base) dachuang@master:~/.config/clash$ curl -I https://www.google.com --proxy http://127.0.0.1:7890
HTTP/1.1 200 Connection established
21:09:12 INF [TCP] connected lAddr=127.0.0.1:56432 rAddr=www.google.com:443 mode=rule rule=DomainKeyword(google) proxy=🔰 选择节点[🇭🇰 香港W01]
可以注意到默认使用的节点是香港W01
测试代理效果
(base) dachuang@master:~/.config/clash$ curl -I https://www.google.com --proxy http://127.0.0.1:7890
HTTP/1.1 200 Connection established
21:09:12 INF [TCP] connected lAddr=127.0.0.1:56432 rAddr=www.google.com:443 mode=rule rule=DomainKeyword(google) proxy=🔰 选择节点[🇭🇰 香港W01]
HTTP/2 200
content-type: text/html; charset=ISO-8859-1
content-security-policy-report-only: object-src 'none';base-uri 'self';script-src 'nonce-OqXM-V19ZsNI0PH2pJVMtA' 'strict-dynamic' 'report-sample' 'unsafe-eval' 'unsafe-inline' https: http:;report-uri https://csp.withgoogle.com/csp/gws/other-hp
accept-ch: Sec-CH-Prefers-Color-Scheme
p3p: CP="This is not a P3P policy! See g.co/p3phelp for more info."
date: Sun, 22 Sep 2024 13:09:13 GMT
server: gws
x-xss-protection: 0
x-frame-options: SAMEORIGIN
expires: Sun, 22 Sep 2024 13:09:13 GMT
cache-control: private
set-cookie: AEC=AVYB7cphf8tVuRSPh76qPiyJZjusY7D7z2w1PPJSI4E0EhPnl-tP1PpGOX4; expires=Fri, 21-Mar-2025 13:09:13 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=lax
set-cookie: NID=517=SKBAUnGIIo3LT2p_e7AQ8gxPRDqT3dNwVjYsLDRsJ1ivBXpv7sNRdIvTcGkn3GvTqAlw8AyBl8B77CsiI8X6WxTFZfunSo9KrnSoLCLBJAAMCTvOhE_62zYnHeSh-Yte6cjLgW3RhkdcE4XlBSHebsy80JGowswxglBAHLzu7y-lthO-lcvEdcZ-OSKWOPQ; expires=Mon, 24-Mar-2025 13:09:13 GMT; path=/; domain=.google.com; HttpOnly
alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
可以确定已经能够成功连接到google。
更换节点并重新测试代理效果
(base) dachuang@master:~/.config/clash$ curl -X PUT "http://127.0.0.1:9090/proxies/🔰%20选择节点" -H "Content-Type: application/json" -d '{"name":"🇺🇲 美国W02"}'
(base) dachuang@master:~/.config/clash$ curl -I https://www.google.com --proxy http://127.0.0.1:7890
HTTP/1.1 200 Connection established
21:12:14 INF [TCP] connected lAddr=127.0.0.1:60904 rAddr=www.google.com:443 mode=rule rule=DomainKeyword(google) proxy=🔰 选择节点[🇺🇲 美国W02]
HTTP/2 200
content-type: text/html; charset=ISO-8859-1
content-security-policy-report-only: object-src 'none';base-uri 'self';script-src 'nonce-BFDdjrkzxroVGCVAl9R8Ug' 'strict-dynamic' 'report-sample' 'unsafe-eval' 'unsafe-inline' https: http:;report-uri https://csp.withgoogle.com/csp/gws/other-hp
accept-ch: Sec-CH-Prefers-Color-Scheme
p3p: CP="This is not a P3P policy! See g.co/p3phelp for more info."
date: Sun, 22 Sep 2024 13:12:16 GMT
server: gws
x-xss-protection: 0
x-frame-options: SAMEORIGIN
expires: Sun, 22 Sep 2024 13:12:16 GMT
cache-control: private
set-cookie: AEC=AVYB7cpzT75tvh-ACi5Fo_M8EVwxw14iDUSwF5v6D7Uu0KqzTY9-BV1Tjw; expires=Fri, 21-Mar-2025 13:12:16 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=lax
set-cookie: NID=517=tM3bbg4p0Rxl-eeffqYyk4uivflGZFEXFAtCl6cWiTMidWBFrPlpxSVI2AvwwpHONxMfd93qf0v5xu-Kz-3UdtmgYyMMs3AMmGxeY1qezUwKxyv397jMi2s1v0ZwUfyp9qzlimkEaArvZezxaz5CI6id2PSX7sU2JR9hrQhY-RULEc1BrwYaT4Yxh312gkKr; expires=Mon, 24-Mar-2025 13:12:16 GMT; path=/; domain=.google.com; HttpOnly
alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
可以从输出的提示中确认节点已经成功切换。
请将切换节点的命令备份好,因为你很难打出这两个emoji😂:🔰🇺🇲:
curl -X PUT "http://127.0.0.1:9090/proxies/🔰%20选择节点" -H "Content-Type: application/json" -d '{"name":"🇺🇲 美国W02"}'
测试ChatGPT API能否正常调用
在python交互式环境中运行如下命令:
import os
os.environ["OPENAI_API_KEY"] = "YOUR-API-KEY"
os.environ["http_proxy"] = "http://127.0.0.1:7890"
os.environ["https_proxy"] = "http://127.0.0.1:7890"
from langchain_openai import ChatOpenAI
model = ChatOpenAI(model="gpt-3.5-turbo")
from langchain_core.messages import HumanMessage
model.invoke([HumanMessage(content="写一个快速排序")]).content
注意,由于没有配置环境变量,所以需要在Python中专门配置环境变量
临时配置服务器环境变量
export https_proxy=http://127.0.0.1:7890
export http_proxy=http://127.0.0.1:7890
将环境变量写入bash配置文件,使之默认生效
(base) vim ~/.bashrc
# 加入如下内容
export HTTPS_PROXY=http://127.0.0.1:7890
export HTTP_PROXY=http://127.0.0.1:7890
(base) source ~/.bashrc #重新加载配置文件
再次测试代理效果
curl -I www.google.com
curl -I www.huggingface.co
目前存在的几个问题
- 只有服务器能正常使用Clash代理,Docker容器仍无法使用。
- 修改代理节点只能使用curl命令修改,命令中需要带有emoji符号,有的shell终端无法正常输入相应符号。