-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
68 lines (56 loc) · 1.91 KB
/
app.js
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const axios = require('axios')
const xml2js = require('xml2js')
const getIpService = require('./service/getIpService')
const fs = require('fs')
const path = require('path')
console.info('hello ddns4RaspberryPi start=================>')
let oldIp = ''
const config = require('./config/config')
async function main() {
await getIpService()
await myTask()
setInterval(myTask, config.intervalTime)
}
main()
async function myTask() {
let newIp
try {
newIp = await getIpService()
console.log('getIpService')
console.log(`new IP :${newIp}`)
console.info(`旧的ip是${oldIp} 我目前的ip是${newIp}`)
console.log(`旧的ip是${oldIp} 我目前的ip是${newIp}`)
if (oldIp !== newIp) {
console.info(`要去执行更新ip操作,oldIp:${oldIp},newIp:${newIp}`)
updateMyIp(config)
oldIp = newIp
}
} catch (e) {
console.error(e.message)
}
}
async function updateMyIp({
host,
domain_name,
ddns_password
}) {
const url = `https://dynamicdns.park-your-domain.com/update?host=${host}&domain=${domain_name}&password=${ddns_password}`
console.info(`更新api的 url:${url}`)
let response = await axios.get(url)
console.debug(response.data)
response = await xml2js.parseStringPromise(response.data)
console.debug(JSON.stringify(response))
if (response['interface-response'].ErrCount[0] === '0') {
console.info('==============>更新成功update Success<===============')
updateMonitor()
} else {
console.error('==============>更新失败update error<===============')
console.error(response['interface-response'])
}
}
function updateMonitor() {
let data = fs.readFileSync(path.join(__dirname, 'monitor.json'))
data = JSON.parse(data)
data.successCount++
fs.writeFileSync(path.join(__dirname, 'monitor.json'), JSON.stringify(data))
}