Skip to content

Commit c9cd18d

Browse files
committed
feat: add github proxy
Change-Id: Ibf6b13a2ebd7529e3dd26a730f0b6794721c28ac Co-developed-by: Cursor <[email protected]>
1 parent 2e6f7b8 commit c9cd18d

File tree

7 files changed

+367
-75
lines changed

7 files changed

+367
-75
lines changed

src/code/agent/Dockerfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ RUN --mount=type=cache,target=/root/.cache/pip \
4747
${AGENT_DIR}/venv/bin/pip install -r ${AGENT_DIR}/requirements.txt --no-cache-dir
4848

4949
RUN mkdir -p ~/.pip \
50-
&& cp ${AGENT_DIR}/services/proxy/pip.conf ~/.pip/pip.conf
50+
&& cp ${AGENT_DIR}/services/proxy/pip.conf ~/.pip/pip.conf \
51+
&& cp ${AGENT_DIR}/services/proxy/gitconfig.conf ~/.gitconfig
52+
53+
# 设置 GitHub 代理监控脚本执行权限
54+
RUN chmod +x ${AGENT_DIR}/services/proxy/github-proxy-monitor.sh
5155

5256
RUN chmod +x ${AGENT_DIR}/entrypoint.bash
5357
ENTRYPOINT [ "/bin/bash", "-c", "agent/entrypoint.bash" ]

src/code/agent/entrypoint.bash

Lines changed: 47 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -19,57 +19,55 @@
1919
# --- 20250102-120159
2020
# ---- comfyui
2121
# ---- venv.tar
22-
init_mitmproxy(){
23-
echo 'export NO_PROXY="127.0.0.1,mirrors.aliyun.com,ghfast.top,ghgo.xyz,ghp.ci,ghproxy.com,hf-mirror.com,deb.debian.org,www.modelscope.cn"'>> ~/.bashrc
24-
echo 'export no_proxy="127.0.0.1,mirrors.aliyun.com,ghfast.top,ghgo.xyz,ghp.ci,ghproxy.com,hf-mirror.com,deb.debian.org,www.modelscope.cn"'>> ~/.bashrc
2522

26-
echo 'export HTTP_PROXY="http://127.0.0.1:8080"' >> ~/.bashrc
27-
echo 'export http_proxy="http://127.0.0.1:8080"' >> ~/.bashrc
28-
29-
echo 'export HTTPS_PROXY="http://127.0.0.1:8080"' >> ~/.bashrc
30-
echo 'export https_proxy="http://127.0.0.1:8080"' >> ~/.bashrc
31-
32-
echo 'export HF_ENDPOINT="https://hf-mirror.com"' >> ~/.bashrc
33-
34-
echo 'export REQUESTS_CA_BUNDLE="/etc/ssl/certs/ca-certificates.crt"'>> ~/.bashrc
35-
echo 'export SSL_CERT_FILE="/etc/ssl/certs/ca-certificates.crt"'>> ~/.bashrc
36-
echo 'export CURL_CA_BUNDLE="/etc/ssl/certs/ca-certificates.crt"'>> ~/.bashrc
37-
38-
source ~/.bashrc
39-
40-
# mitmdump -s ${AGENT_DIR}/services/proxy/mirror_proxy.py &
41-
mitmdump -s ${AGENT_DIR}/services/proxy/mirror_proxy.py >> /root/agent/mitmproxy.log 2>> /root/agent/mitmproxy_error.log &
42-
43-
sleep 5
44-
# cp ~/.mitmproxy/mitmproxy-ca-cert.cer /usr/local/share/ca-certificates/
45-
cp ~/.mitmproxy/mitmproxy-ca-cert.pem /usr/local/share/ca-certificates/mitmproxy.crt
46-
update-ca-certificates
47-
git config --global http.sslCAInfo /usr/local/share/ca-certificates/mitmproxy.crt
48-
}
49-
50-
check_and_init_mitmproxy(){
51-
echo "Check and init mitmproxy..."
52-
53-
# 根据AUTO_LAUNCH_SNAPSHOT_NAME是否为空来判断当前函数是否use_api_mode
54-
use_api_mode=false
55-
if [[ -n "${AUTO_LAUNCH_SNAPSHOT_NAME}" ]]; then
56-
use_api_mode=true
57-
fi
58-
59-
# 根据当前REGION判断是否处于国内
60-
domestic_regions=("cn-hangzhou" "cn-shanghai" "cn-shenzhen" "cn-beijing")
61-
region="${REGION}"
62-
is_domestic=false
23+
# 检查是否为国内区域
24+
is_domestic_region() {
25+
local domestic_regions=("cn-hangzhou" "cn-shanghai" "cn-shenzhen" "cn-beijing")
26+
local region="${REGION:-}"
27+
6328
for reg in "${domestic_regions[@]}"; do
64-
if [[ "${region}" == "${reg}" ]]; then
65-
is_domestic=true
66-
break
67-
fi
29+
if [[ "${region}" == "${reg}" ]]; then
30+
return 0
31+
fi
6832
done
33+
return 1
34+
}
6935

70-
if [[ ${use_api_mode} == false ]] && [[ ${is_domestic} == true ]]; then
71-
echo "Init mitmproxy..."
72-
init_mitmproxy
36+
# 设置网络配置
37+
setup_network() {
38+
echo "[INFO] Setting up network configuration..."
39+
40+
# 如果是国内集群,配置加速
41+
if is_domestic_region; then
42+
echo "[INFO] Domestic region detected: ${REGION}"
43+
44+
# 设置 HuggingFace 镜像
45+
export HF_ENDPOINT="https://hf-mirror.com"
46+
47+
# ComfyUI-Manager 使用
48+
# 设置 Github 镜像
49+
export GITHUB_ENDPOINT="https://cap-accor-proxy-qkqnjxeail.ap-southeast-1.fcapp.run/https://github.com/"
50+
51+
# uv 镜像配置
52+
export UV_DEFAULT_INDEX="https://mirrors.aliyun.com/pypi/simple/"
53+
54+
# 使用 copy 模式,避免 hardlink 警告(当缓存和目标在不同文件系统时)
55+
export UV_LINK_MODE="copy"
56+
57+
# 增加超时时间(默认30秒太短)
58+
export UV_HTTP_TIMEOUT="300"
59+
60+
# 启动 GitHub 代理监控守护进程
61+
GITHUB_PROXY_MONITOR="${AGENT_DIR}/services/proxy/github-proxy-monitor.sh"
62+
if [ -f "$GITHUB_PROXY_MONITOR" ]; then
63+
echo "[INFO] Starting GitHub proxy monitor..."
64+
nohup bash "$GITHUB_PROXY_MONITOR" > /tmp/github_proxy_monitor.log 2>&1 &
65+
echo "[INFO] GitHub proxy monitor started (logs: /tmp/github_proxy_monitor.log)"
66+
else
67+
echo "[WARN] GitHub proxy monitor not found at: $GITHUB_PROXY_MONITOR"
68+
fi
69+
else
70+
echo "[INFO] Non-domestic region, skipping network acceleration"
7371
fi
7472
}
7573

@@ -92,9 +90,7 @@ mkdir -p ${MNT_DIR}/output
9290
source ${AGENT_DIR}/venv/bin/activate
9391
echo "Using python venv, python path '$(which python)', pip path '$(which pip)'... "
9492

95-
check_and_init_mitmproxy
96-
97-
# git diff 忽略文件权限变化,ComfyUI 中 Windows 可执行文件在 Linux 中自动没有可执行权限,导致有 git diff,影响 ComfyUI 版本升级
98-
git config --global core.fileMode false
93+
# ==================== 网络配置 ====================
94+
setup_network
9995

10096
python ${AGENT_DIR}/main.py
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# GitHub Proxy Configuration
2+
# 此文件会在容器启动时复制到 ~/.gitconfig
3+
4+
# 用户信息配置
5+
[user]
6+
name = FunArt Agent
7+
8+
9+
[url "https://cap-accor-proxy-qkqnjxeail.ap-southeast-1.fcapp.run/https://github.com/"]
10+
insteadOf = https://github.com/
11+
insteadOf = http://github.com/
12+
insteadOf = [email protected]:
13+
14+
# 其他 Git 配置
15+
[core]
16+
# 忽略文件权限变化
17+
filemode = false
18+
# 自动处理换行符
19+
autocrlf = input
20+
21+
[pull]
22+
# 默认使用 rebase 而不是 merge
23+
rebase = false
24+
25+
[init]
26+
# 默认分支名
27+
defaultBranch = main
28+
29+
# 性能优化
30+
[gc]
31+
auto = 256
32+
33+
[pack]
34+
threads = 0
35+
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#!/bin/bash
2+
3+
# GitHub 代理监控守护进程
4+
# 功能:定期检测代理可用性,自动切换到可用的代理
5+
# 工作原理:
6+
# 1. 每 60 秒检测当前代理是否可用
7+
# 2. 如果不可用,遍历备用代理列表查找可用的
8+
# 3. 更新 ~/.gitconfig 中的代理配置
9+
10+
# ==================== 配置 ====================
11+
12+
# 代理列表(按优先级排序)
13+
PROXIES=(
14+
# 测试 github
15+
"https://cap-accor-proxy-qkqnjxeail.ap-southeast-1.fcapp.run/https://github.com/"
16+
"https://cap-accor-proxy-qkqnjxeail.cn-hongkong.fcapp.run/https://github.com/"
17+
"https://gh.llkk.cc/https://github.com/"
18+
"https://ghproxy.com/https://github.com/"
19+
"https://ghfast.top/https://github.com/"
20+
)
21+
22+
# 日志文件
23+
LOG_FILE="/tmp/github_proxy_monitor.log"
24+
25+
# Git 配置文件
26+
GITCONFIG="$HOME/.gitconfig"
27+
28+
# 检测间隔(秒)
29+
CHECK_INTERVAL=60
30+
31+
# 当前使用的代理
32+
CURRENT_PROXY=""
33+
34+
# ==================== 工具函数 ====================
35+
36+
log() {
37+
# 只写入日志文件,不输出到 stdout(避免被命令替换捕获)
38+
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" >> "$LOG_FILE"
39+
}
40+
41+
# 测试代理是否可用
42+
test_proxy() {
43+
local proxy_url="$1"
44+
45+
# 使用 ComfyUI 仓库进行测试(完整的代理 URL)
46+
local test_repo="${proxy_url}comfyanonymous/ComfyUI.git"
47+
48+
log "🔍 Testing proxy: $proxy_url"
49+
log " Testing repo: $test_repo"
50+
log " Command: GIT_TERMINAL_PROMPT=0 GIT_CONFIG_GLOBAL=/dev/null git ls-remote --exit-code '$test_repo' HEAD"
51+
52+
# 使用 git ls-remote 测试(真实的 git 命令)
53+
# GIT_TERMINAL_PROMPT=0: 禁用交互式提示
54+
# GIT_CONFIG_GLOBAL=/dev/null: 禁用全局配置,避免干扰
55+
# timeout 10: 10秒超时(git 操作比 curl 慢)
56+
if timeout 10 env GIT_TERMINAL_PROMPT=0 GIT_CONFIG_GLOBAL=/dev/null git ls-remote --exit-code "$test_repo" HEAD > /dev/null 2>&1; then
57+
log "✅ Proxy test passed: $proxy_url"
58+
return 0 # 成功
59+
else
60+
log "❌ Proxy test failed: $proxy_url"
61+
return 1 # 失败
62+
fi
63+
}
64+
65+
# 查找可用的代理
66+
find_working_proxy() {
67+
for proxy in "${PROXIES[@]}"; do
68+
if test_proxy "$proxy"; then
69+
log "✅ Found working proxy: $proxy"
70+
echo "$proxy"
71+
return 0
72+
fi
73+
done
74+
log "⚠️ No working proxy found"
75+
return 1
76+
}
77+
78+
# 获取当前 gitconfig 中配置的代理
79+
get_current_proxy() {
80+
# 读取 gitconfig 中的第一个 url.*.insteadOf 配置
81+
# 匹配 [url "代理URL"] 格式,提取引号中的 URL
82+
# 注意:这里的 sed 只是解析文本(从管道读取),不会修改 .gitconfig 文件
83+
local proxy=$(grep -A 1 '^\[url ' "$GITCONFIG" 2>/dev/null | head -1 | sed 's/\[url "\(.*\)"\]/\1/')
84+
85+
# Debug 日志
86+
log "🔍 [DEBUG] get_current_proxy() returned: ${proxy:-<empty>}"
87+
88+
echo "$proxy"
89+
}
90+
91+
# 更新 gitconfig 中的代理配置
92+
update_gitconfig_proxy() {
93+
local new_proxy="$1"
94+
95+
log "📝 Updating gitconfig with new proxy: $new_proxy"
96+
97+
# 备份当前配置
98+
cp "$GITCONFIG" "${GITCONFIG}.bak"
99+
100+
# 删除所有现有的 [url ...] 配置段
101+
# sed 语法:/pattern1/,/pattern2/d 表示删除从 pattern1 到 pattern2 之间的所有行
102+
# /^\[url / 匹配以 [url 开头的行(配置段开始)
103+
# /^$/ 匹配空行(配置段结束)
104+
# d 删除操作
105+
# -i.tmp 直接修改文件并创建 .tmp 备份
106+
sed -i.tmp '/^\[url /,/^$/d' "$GITCONFIG"
107+
108+
# 添加新的代理配置
109+
cat >> "$GITCONFIG" << EOF
110+
111+
# GitHub 代理配置(由 github-proxy-monitor 自动更新)
112+
[url "$new_proxy"]
113+
insteadOf = https://github.com/
114+
insteadOf = http://github.com/
115+
insteadOf = [email protected]:
116+
EOF
117+
118+
# 清理临时文件
119+
rm -f "${GITCONFIG}.tmp"
120+
121+
log "✅ Gitconfig updated successfully"
122+
}
123+
124+
# ==================== 主循环 ====================
125+
126+
log "=========================================="
127+
log "GitHub Proxy Monitor started"
128+
log "=========================================="
129+
130+
# 初始化:读取当前配置的代理
131+
CURRENT_PROXY=$(get_current_proxy)
132+
if [ -n "$CURRENT_PROXY" ]; then
133+
log "📋 Current proxy from gitconfig: $CURRENT_PROXY"
134+
else
135+
log "⚠️ No proxy configured, will set default proxy"
136+
CURRENT_PROXY="${PROXIES[0]}"
137+
update_gitconfig_proxy "$CURRENT_PROXY"
138+
fi
139+
140+
# 主监控循环
141+
while true; do
142+
log "🔍 Checking current proxy: $CURRENT_PROXY"
143+
144+
if test_proxy "$CURRENT_PROXY"; then
145+
log "✅ Current proxy is working"
146+
else
147+
log "❌ Current proxy failed, searching for alternative..."
148+
149+
new_proxy=$(find_working_proxy)
150+
log "🔍 [DEBUG] find_working_proxy() returned: ${new_proxy:-<empty>}"
151+
152+
if [ -n "$new_proxy" ]; then
153+
log "⚡ Found working proxy: $new_proxy"
154+
update_gitconfig_proxy "$new_proxy"
155+
CURRENT_PROXY="$new_proxy"
156+
log "🔍 [DEBUG] CURRENT_PROXY updated to: $CURRENT_PROXY"
157+
else
158+
log "⚠️ No working proxy found! Will retry in $CHECK_INTERVAL seconds"
159+
fi
160+
fi
161+
162+
sleep $CHECK_INTERVAL
163+
done
164+

src/code/agent/services/proxy/mirror_proxy.py

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
[global]
2+
# 主镜像源
23
index-url = https://mirrors.aliyun.com/pypi/simple/
4+
5+
# 备用镜像源(按优先级)
6+
extra-index-url =
7+
https://pypi.tuna.tsinghua.edu.cn/simple/
8+
https://pypi.mirrors.ustc.edu.cn/simple/
9+
310
[install]
4-
trusted-host = mirrors.aliyun.com
11+
# 信任的主机列表
12+
trusted-host =
13+
mirrors.aliyun.com
14+
pypi.tuna.tsinghua.edu.cn
15+
pypi.mirrors.ustc.edu.cn

0 commit comments

Comments
 (0)