Skip to content

Commit

Permalink
'init'
Browse files Browse the repository at this point in the history
  • Loading branch information
luozheao committed Mar 31, 2021
0 parents commit af1d40c
Show file tree
Hide file tree
Showing 51 changed files with 6,499 additions and 0 deletions.
79 changes: 79 additions & 0 deletions gitHost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
let axios = require('./node_modules/axios')
const fs = require('fs')
let pathContent = process.argv[2] || 'C:\\Windows\\System32\\drivers\\etc'

const path = require('path')
function exec(cmd) {
return require('child_process')
.execSync(cmd)
.toString()
.trim()
}

const getUrlIp = async () => {
let res = await axios.get('https://github.com.ipaddress.com/')
let val = res.data.match(/(?<=\.com\/ipv4\/)[\d\.]+/g)
return [val[0], 'github.com']
}

// 2. 获取github域名查询
const getDomainIp = async () => {
let res = await axios.get('https://fastly.net.ipaddress.com/github.global.ssl.fastly.net')
let val = res.data.match(/(?<=\.com\/ipv4\/)[\d\.]+/g)
return [val[0], 'github.global.ssl.fastly.net']
}
// 3. 获取github静态资源ip
const getStaticIp = async () => {
let res = await axios.get('https://github.com.ipaddress.com/assets-cdn.github.com')
let val = res.data.match(/(?<=\.com\/ipv4\/)[\d\.]+/g)
return [...new Set(val)].map((item) => {
return [item, 'assets-cdn.github.com']
})
}

// 4. 生成内容
Promise.all([getUrlIp(), getDomainIp(), getStaticIp()]).then((res) => {
let val = [res[0], res[1], ...res[2]]
let str = copyHost(val)
console.log(str)
writeHost(str)
})

// 5. 拼接
const copyHost = (val) => {
let str = ''
val.forEach((item) => {
str += item[0] + ' ' + item[1] + '\n'
})
return str
}

// 6. 写入到host文件中

const writeHost = (str) => {
let pathUrl = path.join(pathContent, 'hosts')
let val = fs.readFileSync(pathUrl, 'utf-8') // 已有的
let newStr = str
let replaceStr = `
#tag_start
${str}
#tag_end
`
if (val.indexOf('#tag_start') > -1) {
newStr = val.replace(/#tag_start[\s\S]+#tag_end/gi, replaceStr)
} else {
newStr = val + replaceStr
}

fs.writeFile(pathUrl, newStr, (err) => {
let execRes = ''
if (!err) {
execRes = exec('ipconfig /flushdns')
} else {
console.log(err)
}
//测试码云公钥是否设置成功
console.error(execRes ? '修改host文件并刷新dns成功' : '刷新dns失败')
})
}

2 changes: 2 additions & 0 deletions index.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node ./gitHost.js C:\Windows\System32\drivers\etc
pause
685 changes: 685 additions & 0 deletions node_modules/axios/CHANGELOG.md

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions node_modules/axios/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit af1d40c

Please sign in to comment.