-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathnode-proxmox.js
133 lines (121 loc) · 3.12 KB
/
node-proxmox.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
123
124
125
126
127
128
129
130
131
132
133
module.exports = function ProxmoxApi(hostname, user, pve, password){
// INIT vars
this.hostname = hostname;
this.user = user;
this.password = pve;
this.pve = password;
this.token = {};
this.tokenTimestamp = 0;
function login(hostname, user, pve, password, callback)
{
var querystring = require('querystring');
body = { password: password, username: user, realm: pve };
body = querystring.stringify(body);
var headers = {
'Content-Type':'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(body)
};
var options = {
host: hostname,
rejectUnauthorized: false, //Allow unauthorized SSL certificate
port: 8006,
path: '/api2/json/access/ticket',
method: 'POST',
headers: headers
};
var http = require('https');
var par = this;
var req = http.request(options, function(res){
res.setEncoding('utf8');
var _data = '';
res.on('data', function (chunk) {
//Error handling to do
_data += chunk;
});
res.on('end', function () {
var data = JSON.parse(_data).data;
par.token = {ticket: data.ticket, CSRFPreventionToken: data.CSRFPreventionToken};
par.tokenTimestamp = new Date().getTime();
if (typeof(callback) == 'function')
callback();
});
});
req.write(body);
req.end();
}
function call(method, url, body, callback)
{
currentTime = new Date().getTime();
//1 hour login timeout
if(currentTime - this.tokenTimestamp > 60 * 60 * 1000)
{
login(this.hostname, this.user, this.password, this.pve, function(){callApi(method, url, body, callback);});
}
else
{
callApi(method, url, body, callback);
}
}
function callApi(method, url, body, callback)
{
var currentTime = new Date().getTime();
var querystring = require('querystring');
//Compute signature
if(body == undefined)
body = '';
else
body = querystring.stringify(body);
if(method == 'GET')
{
var headers = {
'Cookie':'PVEAuthCookie='+this.token.ticket
};
}
else {
var headers = {
'Content-Type':'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(body),
'CSRFPreventionToken':this.token.CSRFPreventionToken,
'Cookie':'PVEAuthCookie='+this.token.ticket
};
}
var options = {
host: this.hostname,
rejectUnauthorized: false,
port: 8006,
path: '/api2/json'+url,
method: method,
headers: headers
};
var http = require('https');
var req = http.request(options, function(res){
res.setEncoding('utf8');
var data = '';
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function(){
var dataa = JSON.parse(data).data;
if(typeof(callback) == 'function')
callback(dataa);
});
});
if(body != '')
req.write(querystring.stringify(body));
req.end();
}
return {
get: function get(url, callback){
call('GET', url, '', callback);
},
post: function post(url, body, callback){
call('POST', url, body, callback);
},
put: function put(url, body, callback){
call('PUT', url, body, callback);
},
del: function del(url, callback){
call('DELETE', url, '', callback);
},
}
}