使用 CloudFlare worker 获取优选 ip

尝试使用 Cloudflare worker 获取优选 ip,每天去获取3次并保存为 txt 文件,同时推送给 Telegram bot。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Fetch IP addresses from FETCH_URL
const fetchIPAddresses = async () => {
try {
logDebug(`Fetching IP addresses from: ${FETCH_URL}`);
const response = await fetch(FETCH_URL);
logDebug(`Fetch response status: ${response.status}`);
if (!response.ok) {
logDebug(`Failed to fetch IP addresses: ${response.statusText}`);
throw new Error(`Failed to fetch IP addresses: ${response.statusText}`);
}
const html = await response.text();
logDebug(`Fetched HTML content from FETCH_URL.`);
const ipPattern = /\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/g;
let ipAddresses = html.match(ipPattern);

if (ipAddresses) {
ipAddresses = Array.from(new Set(ipAddresses)).slice(0, 5); // 取唯一的IP地址中的前5个
} else {
ipAddresses = [];
}

logDebug(`Selected IP addresses (up to 5): ${ipAddresses}`);
return ipAddresses;
} catch (error) {
logDebug(`Error in fetchIPAddresses: ${error.message}`);
throw error;
}
};

wrangler.toml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#:schema node_modules/wrangler/config-schema.json
name = "page-name"
main = "src/index.js"
compatibility_date = "2024-05-12"

[[routes]]
pattern = "subpage.your.domain"
zone_name = "your.domain"
custom_domain = true


[[kv_namespaces]]
binding = "FETCH_IP"
id = "kvid"

[triggers]
crons = ["0 13 * * *","0 9 * * *","0 4 * * *","0 23 * * *"]

And here is the end.