@@ -28,6 +28,7 @@ const state = {
2828 background : { source : "default" , url : "" } ,
2929 theme : null ,
3030 version : null ,
31+ proxy : { proxy : "" , presets : [ ] } ,
3132} ;
3233
3334const $ = ( selector , context = document ) => context . querySelector ( selector ) ;
@@ -186,6 +187,8 @@ function bindEvents() {
186187 $ ( "#theme-reset" ) . addEventListener ( "click" , resetTheme ) ;
187188 $ ( "#restart-button" ) . addEventListener ( "click" , restartNonebot ) ;
188189 $ ( "#check-update-btn" ) . addEventListener ( "click" , checkUpdate ) ;
190+ $ ( "#proxy-save" ) . addEventListener ( "click" , saveProxySettings ) ;
191+ $ ( "#proxy-test-btn" ) . addEventListener ( "click" , testProxySettings ) ;
189192 $$ ( ".view-toggle button" ) . forEach ( ( button ) => button . addEventListener ( "click" , ( ) => {
190193 $$ ( ".view-toggle button" ) . forEach ( ( item ) => item . classList . remove ( "active" ) ) ;
191194 button . classList . add ( "active" ) ;
@@ -242,7 +245,7 @@ function enterApp(username) {
242245 clearTimers ( ) ;
243246 state . timers . push ( setInterval ( ( ) => loadDashboard ( false ) , 5000 ) ) ;
244247 state . timers . push ( setInterval ( loadLogs , 2000 ) ) ;
245- Promise . allSettled ( [ loadDashboard ( ) , loadPlugins ( ) , loadLogs ( ) , loadBackground ( ) , loadVersion ( ) ] ) ;
248+ Promise . allSettled ( [ loadDashboard ( ) , loadPlugins ( ) , loadLogs ( ) , loadBackground ( ) , loadVersion ( ) , loadProxySettings ( ) ] ) ;
246249}
247250
248251async function signOut ( requestLogout ) {
@@ -1146,6 +1149,106 @@ function renderVersion(data) {
11461149 $ ( "#version-current" ) . textContent = data . current ? `v${ data . current } ` : "--" ;
11471150}
11481151
1152+ const PROXY_CUSTOM_VALUE = "__custom__" ;
1153+
1154+ function presetLabel ( url ) {
1155+ return url . includes ( "cnb.cool" ) ? "CNB 镜像仓库" : "GitHub 加速代理" ;
1156+ }
1157+
1158+ async function loadProxySettings ( ) {
1159+ try {
1160+ state . proxy = await api ( "/system/github-proxy" ) ;
1161+ renderProxySettings ( ) ;
1162+ } catch ( _ ) { /* 读取失败不影响使用 */ }
1163+ }
1164+
1165+ function renderProxySettings ( ) {
1166+ const container = $ ( "#proxy-modes" ) ;
1167+ if ( ! container ) return ;
1168+ const presets = state . proxy . presets || [ ] ;
1169+ const current = state . proxy . proxy || "" ;
1170+ const options = [
1171+ { value : "" , title : "不使用 GitHub 加速" , desc : "直连 GitHub" } ,
1172+ ...presets . map ( ( url ) => ( { value : url , title : url , desc : presetLabel ( url ) } ) ) ,
1173+ { value : PROXY_CUSTOM_VALUE , title : "自定义" , desc : "填入自定义加速前缀或镜像仓库地址" } ,
1174+ ] ;
1175+ container . innerHTML = options
1176+ . map (
1177+ ( opt ) => `<label class="bg-mode">
1178+ <input type="radio" name="gh-proxy" value="${ escapeHtml ( opt . value ) } "${ opt . value === "" ? " data-proxy-off" : "" } >
1179+ <span class="bg-mode-text"><strong>${ escapeHtml ( opt . title ) } </strong><small>${ escapeHtml ( opt . desc ) } </small></span>
1180+ </label>` ,
1181+ )
1182+ . join ( "" ) ;
1183+ const mode = current === "" ? "" : presets . includes ( current ) ? current : PROXY_CUSTOM_VALUE ;
1184+ $$ ( 'input[name="gh-proxy"]' , container ) . forEach ( ( input ) => {
1185+ input . checked = input . value === mode ;
1186+ } ) ;
1187+ $ ( "#proxy-custom-field" ) . classList . toggle ( "hidden" , mode !== PROXY_CUSTOM_VALUE ) ;
1188+ if ( mode === PROXY_CUSTOM_VALUE ) $ ( "#proxy-custom-input" ) . value = current ;
1189+ container . onchange = ( ) => {
1190+ const checked = $ ( 'input[name="gh-proxy"]:checked' , container ) ;
1191+ $ ( "#proxy-custom-field" ) . classList . toggle (
1192+ "hidden" ,
1193+ ! checked || checked . value !== PROXY_CUSTOM_VALUE ,
1194+ ) ;
1195+ } ;
1196+ updateProxyStatus ( ) ;
1197+ }
1198+
1199+ function selectedProxyValue ( ) {
1200+ const checked = $ ( 'input[name="gh-proxy"]:checked' ) ;
1201+ if ( ! checked ) return "" ;
1202+ if ( checked . value === PROXY_CUSTOM_VALUE ) return ( $ ( "#proxy-custom-input" ) . value || "" ) . trim ( ) ;
1203+ return checked . value ;
1204+ }
1205+
1206+ function updateProxyStatus ( ) {
1207+ const current = state . proxy . proxy || "" ;
1208+ $ ( "#proxy-status" ) . textContent = current ? `当前:${ current } ` : "当前:直连" ;
1209+ }
1210+
1211+ async function saveProxySettings ( ) {
1212+ const button = $ ( "#proxy-save" ) ;
1213+ button . disabled = true ;
1214+ try {
1215+ const data = await api ( "/system/github-proxy" , {
1216+ method : "PUT" ,
1217+ body : JSON . stringify ( { proxy : selectedProxyValue ( ) } ) ,
1218+ } ) ;
1219+ state . proxy . proxy = data . proxy || "" ;
1220+ renderProxySettings ( ) ;
1221+ toast ( "GitHub 加速设置已保存,立即生效" ) ;
1222+ } catch ( error ) {
1223+ toast ( error . message , "error" ) ;
1224+ } finally {
1225+ button . disabled = false ;
1226+ }
1227+ }
1228+
1229+ async function testProxySettings ( ) {
1230+ const button = $ ( "#proxy-test-btn" ) ;
1231+ const original = button . textContent ;
1232+ button . disabled = true ;
1233+ button . textContent = "测试中…" ;
1234+ try {
1235+ const data = await api ( "/system/github-proxy/test" , {
1236+ method : "POST" ,
1237+ body : JSON . stringify ( { proxy : selectedProxyValue ( ) } ) ,
1238+ } ) ;
1239+ if ( data . ok ) {
1240+ toast ( `连通正常(${ data . latency_ms } ms)` ) ;
1241+ } else {
1242+ toast ( data . detail || "连接失败" , "error" ) ;
1243+ }
1244+ } catch ( error ) {
1245+ toast ( error . message , "error" ) ;
1246+ } finally {
1247+ button . disabled = false ;
1248+ button . textContent = original ;
1249+ }
1250+ }
1251+
11491252async function checkUpdate ( ) {
11501253 const button = $ ( "#check-update-btn" ) ;
11511254 const original = button . textContent ;
0 commit comments