forked from felixfischer/node-red-contrib-fritzbox-presence
-
Notifications
You must be signed in to change notification settings - Fork 7
/
fritz.js
66 lines (61 loc) · 1.77 KB
/
fritz.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
const http = require('http')
const Promise = require('bluebird')
function checkSession(sessionID, options) {
options.path = '/login_sid.lua?sid=' + (sessionID || '')
return Promise.try(() => {
return request(options)
})
.then((response) => {
return (response.match("<SID>(.*?)</SID>")[1] === sessionID)
})
}
function request(options) {
return new Promise((resolve, reject) => {
const req = http.get(options, function(response) {
let data = ''
response.on('data', function(chunk) {
data += chunk
})
response.on('end', function() {
resolve(data)
})
})
req.on('error', (error) => {
reject(error)
})
})
}
function getSessionID(username, password, options) {
options.path = '/login_sid.lua'
let sessionID = ""
return Promise.try(() => {
if (typeof username != 'string') throw new Error('Invalid username')
if (typeof password != 'string') throw new Error('Invalid password')
})
.then(() => {
return request(options)
})
.then((response) => {
var challenge = response.match("<Challenge>(.*?)</Challenge>")[1]
options.path += "?username=" + username + "&response=" + challenge + "-" + require('crypto').createHash('md5').update(Buffer(challenge + '-' + password, 'UTF-16LE')).digest('hex')
return request(options)
})
.then((response) => {
sessionID = response.match("<SID>(.*?)</SID>")[1]
return sessionID
})
}
function getData(sid, options) {
options.path = '/data.lua?xhr=1&page=netDev&type=cleanup&no_sidrenew=&sid=' + sid
return Promise.try(() => {
return request(options)
})
.then((response) => {
return response
})
}
module.exports = {
getData,
getSessionID,
checkSession
}