// GFW Radar 自家 Worker — 种子名单内置在自己代码里 + CIDR 生成 + 服务端测试 + KV + 订阅(不拉别人站)
// wrangler / 控制台绑定: KV 变量名 NODES_KV ; Cron: */5 * * * *
const CF_CIDRS = ["173.245.48.0/20","103.21.244.0/22","103.22.200.0/22","103.31.4.0/22","141.101.64.0/18","108.162.192.0/18","190.93.240.0/20","188.114.96.0/20","197.234.240.0/22","198.41.128.0/17","162.158.0.0/15","104.16.0.0/13","104.24.0.0/14","172.64.0.0/13","131.0.72.0/22"];
// 三方优选种子:已从公开名单搬进你自己的 Worker,以后有新名单直接往这里加行即可
const SEED_THIRDPARTY = ["203.218.68.64:443","42.200.230.171:443","112.120.14.65:443","42.98.74.174:443","58.176.95.46:443","150.109.117.145:443","47.242.65.13:443","203.198.72.11:443","35.236.156.210:443","111.252.185.150:443","124.156.239.218:443","14.53.103.71:443","157.230.248.157:443","108.241.87.220:443","116.203.71.205:443","88.198.248.91:443"];
const PORTS = [443,8443,2053,2083,2087,2096];
function expandCIDR(cidr, n){ const [ip,mask]=cidr.split("/"); const p=ip.split(".").map(Number);
const base=((p[0]<<24)>>>0)+(p[1]<<16)+(p[2]<<8)+p[3]; const size=Math.pow(2,32-Number(mask));
const out=[]; for(let i=0;i>>0;
out.push([v>>>24,(v>>>16)&255,(v>>>8)&255,v&255].join(".")); } return out; }
function extract(text){ const s=new Set(); const re=/(\d{1,3}\.){3}\d{1,3}(:\d{1,5})?/g; let m;
while((m=re.exec(text||""))!==null) s.add(m[0].includes(":")?m[0]:m[0]+":443"); return [...s]; }
async function testNode(addr){ // 服务端测裸 IP 不能用 fetch https://IP(证书/SNI 同样对不上),要用 TCP socket + TLS(SNI) 探测
const [host,portS]=addr.split(":"); const port=Number(portS)||443; let ok=0; const ts=[];
let connectFn=null; try{ connectFn=(await import("cloudflare:sockets")).connect; }catch(e){}
for(let i=0;i<3;i++){ const t0=Date.now();
if(connectFn){ try{ const sock=connectFn({hostname:host,port},{secureTransport:"on",allowHalfOpen:false}); await sock.opened; ts.push(Date.now()-t0); ok++; try{await sock.close();}catch(e){} }catch(e){} }
else { try{ await fetch("https://"+addr+"/cdn-cgi/trace",{signal:AbortSignal.timeout(8000)}); ok++; ts.push(Date.now()-t0);}catch(e){} } }
return { ok, avg: ts.length?Math.round(ts.reduce((a,b)=>a+b,0)/ts.length):null, pass: ok>=2 }; }
const SYNC_SOURCES = [ // 和上游一样的同步链路:多路源定时拉,任何一路挂了跳过、用种子和其它源继续
"https://776771.xyz/hk.txt","https://776771.xyz/tw.txt","https://776771.xyz/jp.txt",
"https://776771.xyz/kr.txt","https://776771.xyz/sg.txt","https://776771.xyz/us.txt","https://776771.xyz/de.txt",
"https://www.cloudflare.com/ips-v4"
];
async function refresh(env){ const status={}; let all=[...SEED_THIRDPARTY]; status["本地种子(三方优选)"]={ok:true,count:SEED_THIRDPARTY.length,at:new Date().toISOString()};
for(const u of SYNC_SOURCES){ try{ const r=await fetch(u,{signal:AbortSignal.timeout(10000)});
const t=await r.text(); const got=(u.includes("ips-v4"))?[]:extract(t); status[u]={ok:r.ok,count:got.length,at:new Date().toISOString()}; all.push(...got);
}catch(e){ status[u]={ok:false,count:0,at:new Date().toISOString()}; } }
CF_CIDRS.forEach(c=>expandCIDR(c,3).forEach(ip=>all.push(ip+":443"))); // 官网段本地生成,永远可用
all=[...new Set(all)]; // 去重;地区按订阅源路径/种子顺序归类,三网标签需 CT/CU/CM 探针节点回填(见下方说明)
const oldPool=JSON.parse(await env.NODES_KV.get("pool")||"[]");
const passed=[], detail={};
for(const a of all){ const r=await testNode(a); detail[a]=r; if(r.pass) passed.push(a); } // 实测延迟/成功率(丢包)/可用性,只留合格
const added=passed.filter(x=>!oldPool.includes(x)); const removed=oldPool.filter(x=>!passed.includes(x));
await env.NODES_KV.put("pool", JSON.stringify(passed));
await env.NODES_KV.put("detail", JSON.stringify(detail));
await env.NODES_KV.put("status", JSON.stringify({at:new Date().toISOString(),sources:status,total:all.length,passed:passed.length,added:added.length,removed:removed.length}));
return {total:all.length,passed:passed.length,added:added.length,removed:removed.length}; }
export default {
async scheduled(event, env, ctx){ ctx.waitUntil(refresh(env)); },
async fetch(req, env){ const url=new URL(req.url);
if(url.pathname==="/api/refresh"){ return Response.json(await refresh(env)); }
if(url.pathname==="/api/status"){ const s=await env.NODES_KV.get("status"); return new Response(s||"{}",{headers:{"content-type":"application/json"}}); }
if(url.pathname==="/api/test"){ const addr=url.searchParams.get("addr")||""; // 页面「测速」按钮走这里:服务端 TCP+TLS 实测裸 IP,浏览器直连测不了
if(!addr) return Response.json({ok:false,error:"no addr"});
const r=await testNode(addr); return Response.json({addr:addr,ok:r.pass,ms:r.avg,success:r.ok,loss:Math.round((1-r.ok/3)*100),kbps:null}); }
const m=url.pathname.match(/^\/(all|[a-z]{2}(?:-[a-z]{2})*)(\.txt)?(=(\d+))?$/);
if(m){ const pool=JSON.parse(await env.NODES_KV.get("pool")||"[]");
const per=m[4]?Number(m[4]):pool.length; // 默认全量,=N 才截断
return new Response(pool.slice(0,per).join("\n"),{headers:{"content-type":"text/plain; charset=utf-8"}}); }
return new Response("GFW Radar Worker OK — /hk.txt /all.txt /api/status",{headers:{"content-type":"text/plain"}}); }
};