forked from alo-is/node-proxmox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode-proxmox.js
138 lines (126 loc) · 3.28 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
134
135
136
137
138
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 https = require('https');
var that = this;
var req = https.request(options, function(res){
var response = "";
res.setEncoding('utf8');
res.on('data', function (chunk) {
response += chunk;
});
res.on('end', function(){
parsedResponse = JSON.parse(response);
that.token = {ticket: parsedResponse.data.ticket, CSRFPreventionToken: parsedResponse.data.CSRFPreventionToken};
that.tokenTimestamp = new Date().getTime();
if(typeof(callback) == 'function')
callback();
});
});
req.on('error', function(error){
console.log(error);
})
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 https = require('https');
var req = https.request(options, function(res){
var response = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
response += chunk;
});
res.on('end', function(){
var parsedResponse = JSON.parse(response);
if(typeof(callback) == 'function')
callback(parsedResponse.data);
});
});
req.on('error', function(error){
console.log(error);
})
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);
},
}
}