|
|
|
@ -50,45 +50,45 @@ async function _winUnsetProxy (exec, setEnv) {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function getChinaDomainAllowListTmpFilePath () { |
|
|
|
|
return path.join(config.get().server.setting.userBasePath, '/china-domain-allowlist.txt') |
|
|
|
|
function getDomesticDomainAllowListTmpFilePath () { |
|
|
|
|
return path.join(config.get().server.setting.userBasePath, '/domestic-domain-allowlist.txt') |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async function downloadChinaDomainAllowListAsync () { |
|
|
|
|
async function downloadDomesticDomainAllowListAsync () { |
|
|
|
|
loadConfig() |
|
|
|
|
|
|
|
|
|
const remoteFileUrl = config.get().proxy.remoteChinaDomainAllowListFileUrl |
|
|
|
|
log.info('开始下载远程 china-domain-allowlist.txt 文件:', remoteFileUrl) |
|
|
|
|
const remoteFileUrl = config.get().proxy.remoteDomesticDomainAllowListFileUrl |
|
|
|
|
log.info('开始下载远程 domestic-domain-allowlist.txt 文件:', remoteFileUrl) |
|
|
|
|
request(remoteFileUrl, (error, response, body) => { |
|
|
|
|
if (error) { |
|
|
|
|
log.error('下载远程 china-domain-allowlist.txt 文件失败, error:', error, ', response:', response, ', body:', body) |
|
|
|
|
log.error('下载远程 domestic-domain-allowlist.txt 文件失败, error:', error, ', response:', response, ', body:', body) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
if (response && response.statusCode === 200) { |
|
|
|
|
if (body == null || body.length < 100) { |
|
|
|
|
log.warn('下载远程 china-domain-allowlist.txt 文件成功,但内容为空或内容太短,判断为无效的 china-domain-allowlist.txt 文件:', remoteFileUrl, ', body:', body) |
|
|
|
|
log.warn('下载远程 domestic-domain-allowlist.txt 文件成功,但内容为空或内容太短,判断为无效的 domestic-domain-allowlist.txt 文件:', remoteFileUrl, ', body:', body) |
|
|
|
|
return |
|
|
|
|
} else { |
|
|
|
|
log.info('下载远程 china-domain-allowlist.txt 文件成功:', remoteFileUrl) |
|
|
|
|
log.info('下载远程 domestic-domain-allowlist.txt 文件成功:', remoteFileUrl) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let fileTxt = body |
|
|
|
|
try { |
|
|
|
|
if (fileTxt.indexOf('*.') < 0) { |
|
|
|
|
fileTxt = Buffer.from(fileTxt, 'base64').toString('utf8') |
|
|
|
|
// log.debug('解析 base64 后的 china-domain-allowlist:', fileTxt)
|
|
|
|
|
// log.debug('解析 base64 后的 domestic-domain-allowlist:', fileTxt)
|
|
|
|
|
} |
|
|
|
|
} catch (e) { |
|
|
|
|
if (fileTxt.indexOf('*.') < 0) { |
|
|
|
|
log.error(`远程 china-domain-allowlist.txt 文件内容即不是base64格式,也不是要求的格式,url: ${remoteFileUrl},body: ${body}`) |
|
|
|
|
log.error(`远程 domestic-domain-allowlist.txt 文件内容即不是base64格式,也不是要求的格式,url: ${remoteFileUrl},body: ${body}`) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 保存到本地
|
|
|
|
|
saveChinaDomainAllowListFile(fileTxt) |
|
|
|
|
saveDomesticDomainAllowListFile(fileTxt) |
|
|
|
|
} else { |
|
|
|
|
log.error('下载远程 china-domain-allowlist.txt 文件失败, response:', response, ', body:', body) |
|
|
|
|
log.error('下载远程 domestic-domain-allowlist.txt 文件失败, response:', response, ', body:', body) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
@ -104,25 +104,25 @@ function loadLastModifiedTimeFromTxt (fileTxt) {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 保存 中国域名白名单 内容到 `~/china-domain-allowlist.txt.txt` 文件中
|
|
|
|
|
function saveChinaDomainAllowListFile (fileTxt) { |
|
|
|
|
const filePath = getChinaDomainAllowListTmpFilePath() |
|
|
|
|
// 保存 国内域名白名单 内容到 `~/domestic-domain-allowlist.txt` 文件中
|
|
|
|
|
function saveDomesticDomainAllowListFile (fileTxt) { |
|
|
|
|
const filePath = getDomesticDomainAllowListTmpFilePath() |
|
|
|
|
fs.writeFileSync(filePath, fileTxt.replaceAll(/\r\n?/g, '\n')) |
|
|
|
|
log.info('保存 china-domain-allowlist.txt 文件成功:', filePath) |
|
|
|
|
log.info('保存 domestic-domain-allowlist.txt 文件成功:', filePath) |
|
|
|
|
|
|
|
|
|
// 尝试解析和修改 china-domain-allowlist.txt 文件时间
|
|
|
|
|
// 尝试解析和修改 domestic-domain-allowlist.txt 文件时间
|
|
|
|
|
const lastModifiedTime = loadLastModifiedTimeFromTxt(fileTxt) |
|
|
|
|
if (lastModifiedTime) { |
|
|
|
|
fs.stat(filePath, (err, stats) => { |
|
|
|
|
if (err) { |
|
|
|
|
log.error('修改 china-domain-allowlist.txt 文件时间失败:', err) |
|
|
|
|
log.error('修改 domestic-domain-allowlist.txt 文件时间失败:', err) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 修改文件的访问时间和修改时间为当前时间
|
|
|
|
|
fs.utimes(filePath, lastModifiedTime, lastModifiedTime, (utimesErr) => { |
|
|
|
|
if (utimesErr) { |
|
|
|
|
log.error('修改 china-domain-allowlist.txt 文件时间失败:', utimesErr) |
|
|
|
|
log.error('修改 domestic-domain-allowlist.txt 文件时间失败:', utimesErr) |
|
|
|
|
} else { |
|
|
|
|
log.info(`'${filePath}' 文件的修改时间已更新为其最近更新时间 '${formatDate(lastModifiedTime)}'`) |
|
|
|
|
} |
|
|
|
@ -143,40 +143,40 @@ function formatDate (date) {
|
|
|
|
|
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}` |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function getChinaDomainAllowList () { |
|
|
|
|
function getDomesticDomainAllowList () { |
|
|
|
|
loadConfig() |
|
|
|
|
|
|
|
|
|
if (!config.get().proxy.excludeChinaDomainAllowList) { |
|
|
|
|
if (!config.get().proxy.excludeDomesticDomainAllowList) { |
|
|
|
|
return null |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 判断是否需要自动更新中国域名
|
|
|
|
|
let fileAbsolutePath = config.get().proxy.chinaDomainAllowListFileAbsolutePath |
|
|
|
|
if (!fileAbsolutePath && config.get().proxy.autoUpdateChinaDomainAllowList) { |
|
|
|
|
// 判断是否需要自动更新国内域名
|
|
|
|
|
let fileAbsolutePath = config.get().proxy.domesticDomainAllowListFileAbsolutePath |
|
|
|
|
if (!fileAbsolutePath && config.get().proxy.autoUpdateDomesticDomainAllowList) { |
|
|
|
|
// 异步下载,下载成功后,下次系统代理生效
|
|
|
|
|
downloadChinaDomainAllowListAsync().then() |
|
|
|
|
downloadDomesticDomainAllowListAsync().then() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 加载本地文件
|
|
|
|
|
if (!fileAbsolutePath) { |
|
|
|
|
const tmpFilePath = getChinaDomainAllowListTmpFilePath() |
|
|
|
|
const tmpFilePath = getDomesticDomainAllowListTmpFilePath() |
|
|
|
|
if (fs.existsSync(tmpFilePath)) { |
|
|
|
|
// 如果临时文件已存在,则使用临时文件
|
|
|
|
|
fileAbsolutePath = tmpFilePath |
|
|
|
|
log.info('读取已下载的 china-domain-allowlist.txt 文件:', fileAbsolutePath) |
|
|
|
|
log.info('读取已下载的 domestic-domain-allowlist.txt 文件:', fileAbsolutePath) |
|
|
|
|
} else { |
|
|
|
|
// 如果临时文件不存在,则使用内置文件
|
|
|
|
|
fileAbsolutePath = path.join(__dirname, '../../gui/', config.get().proxy.chinaDomainAllowListFilePath) |
|
|
|
|
log.info('读取内置的 china-domain-allowlist.txt 文件:', fileAbsolutePath) |
|
|
|
|
fileAbsolutePath = path.join(__dirname, '../../gui/', config.get().proxy.domesticDomainAllowListFilePath) |
|
|
|
|
log.info('读取内置的 domestic-domain-allowlist.txt 文件:', fileAbsolutePath) |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
log.info('读取自定义路径的 china-domain-allowlist.txt 文件:', fileAbsolutePath) |
|
|
|
|
log.info('读取自定义路径的 domestic-domain-allowlist.txt 文件:', fileAbsolutePath) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
return fs.readFileSync(fileAbsolutePath).toString() |
|
|
|
|
} catch (e) { |
|
|
|
|
log.error('读取 china-domain-allowlist.txt 文件失败:', fileAbsolutePath) |
|
|
|
|
log.error('读取 domestic-domain-allowlist.txt 文件失败:', fileAbsolutePath) |
|
|
|
|
return null |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -192,21 +192,21 @@ async function _winSetProxy (exec, ip, port, setEnv) {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 排除中国域名
|
|
|
|
|
if (config.get().proxy.excludeChinaDomainAllowList) { |
|
|
|
|
// 排除国内域名
|
|
|
|
|
if (config.get().proxy.excludeDomesticDomainAllowList) { |
|
|
|
|
try { |
|
|
|
|
let chinaDomainAllowList = getChinaDomainAllowList() |
|
|
|
|
if (chinaDomainAllowList) { |
|
|
|
|
chinaDomainAllowList = (chinaDomainAllowList + '\n').replaceAll(/[\r\n]+/g, '\n').replaceAll(/[^\n]*[^*.a-zA-Z\d-\n]+[^\n]*\r?\n/g, '').replaceAll(/\s*\n+\s*/g, ';') |
|
|
|
|
if (chinaDomainAllowList) { |
|
|
|
|
excludeIpStr += chinaDomainAllowList |
|
|
|
|
log.info('系统代理排除列表拼接中国域名') |
|
|
|
|
let domesticDomainAllowList = getDomesticDomainAllowList() |
|
|
|
|
if (domesticDomainAllowList) { |
|
|
|
|
domesticDomainAllowList = (domesticDomainAllowList + '\n').replaceAll(/[\r\n]+/g, '\n').replaceAll(/[^\n]*[^*.a-zA-Z\d-\n]+[^\n]*\r?\n/g, '').replaceAll(/\s*\n+\s*/g, ';') |
|
|
|
|
if (domesticDomainAllowList) { |
|
|
|
|
excludeIpStr += domesticDomainAllowList |
|
|
|
|
log.info('系统代理排除列表拼接国内域名') |
|
|
|
|
} else { |
|
|
|
|
log.info('中国域名为空,不进行系统代理排除列表拼接中国域名') |
|
|
|
|
log.info('国内域名为空,不进行系统代理排除列表拼接国内域名') |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} catch (e) { |
|
|
|
|
log.error('系统代理排除列表拼接中国域名失败:', e) |
|
|
|
|
log.error('系统代理排除列表拼接国内域名失败:', e) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|