代码拉取完成,页面将自动刷新
同步操作将从 fliu2476/github-hosts 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
import dns from "dns";
import fs from "fs";
import { urls } from "./constants.js";
const retry = async (fn, n) => {
for (let i = 0; i < n; i++) {
try {
return await fn();
} catch {}
}
return {
failed: true,
};
};
const getHostConfig = async (url) => {
const getConfig = async () => {
const response = await dns.promises.lookup(url);
return { url, ip: response.address };
};
try {
const config = await retry(getConfig, 3);
if (config.failed) {
return { url, ip: "" };
}
return config;
} catch {}
};
const resolveUrls = async () => {
const promises = urls.map(getHostConfig);
return Promise.all(promises);
};
const generateHosts = (configs) => {
let hostStr = "# Generated by hosts-generator start \n\n";
configs.forEach((i) => {
const whiteLen = 16 - i.ip.length;
if (!i.ip) {
hostStr += `# ${i.url} update failed\n`;
} else {
hostStr += `${i.ip} ${" ".repeat(whiteLen)} ${i.url}\n`;
}
});
const updateTime = new Date().toLocaleString("en-US", {
timeZone: "Asia/shanghai",
});
hostStr += `\n# Last update: ${updateTime}\n`;
hostStr += "# Please star: https://github.com/fliu2476/gh-hosts.git\n";
hostStr += "# Generated by hosts-generator end";
return {
hostStr,
updateTime,
};
};
const writeHosts = (hosts) => {
const { hostStr, updateTime } = generateHosts(hosts);
const template = fs.readFileSync("./README.template.md", "utf-8");
const nextReadme = template.toString().replace("{{hosts}}", hostStr).replace("{{last_update_time}}", updateTime);
fs.writeFileSync("./hosts", hostStr);
fs.writeFileSync("./README.md", nextReadme);
};
const main = async () => {
const configs = await resolveUrls();
writeHosts(configs);
};
main();
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。