forked from alo-is/node-proxmox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node-proxmox.js
133 lines (121 loc) · 3.19 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
const querystring = require('querystring');
const http = require('https');
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
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) {
let body = {password: password, username: user};
body = querystring.stringify(body);
const headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(body)
};
const options = {
host: hostname,
rejectUnauthorized: false, //Allow unauthorized SSL certificate
port: 8006,
path: '/api2/json/access/ticket',
method: 'POST',
headers: headers,
timeout: 120,
};
const par = this;
const req = http.request(options, function (res) {
res.setEncoding('utf8');
let _data = '';
res.on('data', function (chunk) {
//Error handling to do
_data += chunk;
});
res.on('end', function () {
const 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) {
let 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) {
let headers;
let currentTime = new Date().getTime();
//Compute signature
if (body === undefined)
body = '';
else
body = querystring.stringify(body);
if (method === 'GET') {
headers = {
'Cookie': 'PVEAuthCookie=' + this.token.ticket
};
} else {
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(body),
'CSRFPreventionToken': this.token.CSRFPreventionToken,
'Cookie': 'PVEAuthCookie=' + this.token.ticket,
'Origin': 'https://' + this.hostname + ':8006',
Connection: "keep-alive"
};
}
const options = {
host: this.hostname,
rejectUnauthorized: false,
port: 8006,
path: '/api2/json' + url,
method: method,
headers: headers,
timeout: 60,
gzip: true,
};
let req = http.request(options, function (res) {
res.setEncoding('utf8');
let data = '';
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
data = JSON.parse(data).data;
if (typeof (callback) == 'function') {
callback(data);
}
});
});
if (body !== '')
req.write(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);
},
}
}