-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
122 lines (106 loc) · 3.34 KB
/
node_helper.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
var NodeHelper = require("node_helper");
var request = require("request")
const Log = require("logger");
module.exports = NodeHelper.create({
start: function() {
this.cookie = '';
},
getVouchers: function() {
var self = this;
if (this.config.isGen2) {
this.api_url_voucher = '/proxy/network/api/s/default/stat/voucher';
} else {
this.api_url_voucher = '/api/s/default/stat/voucher';
}
var options = {
method: 'GET',
url: this.config.url + this.api_url_voucher,
headers: {
'cache-control': 'no-cache',
'content-type': 'application/json;charset=UTF-8',
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36',
origin: this.config.url,
accept: 'application/json, text/plain, */*',
'cookie': self.cookie
},
rejectUnauthorized: false,
};
Log.info("[UniFi-Voucher] " + "get vouchers " + this.config.url);
request(options, function (error, response, body) {
if (!error && response.statusCode === 200) {
var vouchers = body;
Log.debug("[UniFi-Voucher] " + body);
self.sendSocketNotification("UNIFI_VOUCHER_ITEMS", vouchers);
} else {
Log.error("[UniFi-Voucher] " + error);
Log.debug("[UniFi-Voucher] " + body);
var msg = {
"error": "cannot get vouchers from " + self.config.url
}
self.sendSocketNotification("UNIFI_VOUCHER_ERROR", JSON.stringify(msg));
self.cookie = '';
}
});
},
login: function() {
var self = this;
if (this.config.isGen2) {
this.api_url_login = '/api/auth/login';
} else {
this.api_url_login = '/api/login';
}
var options = {
method: 'POST',
url: this.config.url + this.api_url_login,
headers: {
'cache-control': 'no-cache',
'content-type': 'application/json;charset=UTF-8',
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36',
origin: this.config.url,
accept: 'application/json, text/plain, */*'
},
body: '{"username":"' + this.config.user +'","password":"' + this.config.pwd + '","for_hotspot":true,"strict":true,"remember":true,"site_name":"default"}',
rejectUnauthorized: false,
};
Log.info("[UniFi-Voucher] " + "login /api/login");
request(options, function (error, response, body) {
if (!error && response.statusCode === 200) {
var setCookie = response.headers['set-cookie'];
Log.debug("[UniFi-Voucher] " + body);
self.cookie = setCookie[0].split(' ')[0];
self.getVouchers();
} else {
Log.error("[UniFi-Voucher] " + error);
Log.debug("[UniFi-Voucher] " + body);
var msg = {
"error": "cannot login to " + self.config.url
}
self.sendSocketNotification("UNIFI_VOUCHER_ERROR", JSON.stringify(msg));
return null;
}
});
},
fetch: function() {
Log.debug("[UniFi-Voucher] cookie:" + this.cookie);
if (this.cookie === '') {
this.login();
} else {
this.getVouchers();
}
},
scheduleUpdateInterval: function() {
var self = this;
self.fetch();
setInterval(function() {
self.fetch();
}, this.config.updateInterval);
},
socketNotificationReceived: function(notification, payload) {
if (notification === "UNIFI_VOUCHER_UPDATE") {
var self = this;
self.config = payload.config;
this.scheduleUpdateInterval();
return;
}
}
});