-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (47 loc) · 1.39 KB
/
index.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
'use strict';
var util = require('util');
var binding = null;
try {
binding = require(__dirname + '/build/Debug/obj.target/isc_dhclient.node');
} catch(e) {
if(e.code == 'MODULE_NOT_FOUND')
binding = require(__dirname + '/build/Release/obj.target/isc_dhclient.node');
else
console.error("ERROR: " + util.inspect(e));
}
if(binding) {
module.exports = binding;
} else {
console.error("Error loading node-isc-dhclient native.");
}
module.exports.newClient = function() {
var o = binding._newClient();
o.setLeaseCallback = function(cb) {
return o._setLeaseCallback(function(lease) {
// console.log("Got lease: " + lease);
lease = lease.replace(/\\n/g, "\\n")
.replace(/\\'/g, "\\'")
.replace(/\\"/g, '\\"')
.replace(/\\&/g, "\\&")
.replace(/\\r/g, "\\r")
.replace(/\\t/g, "\\t")
.replace(/\\b/g, "\\b")
.replace(/\\f/g, "\\f");
// remove non-printable and other non-valid JSON chars
lease = lease.replace(/[\u0000-\u0019]+/g,"");
var obj = null;
try {
obj = JSON.parse(lease);
// console.log("valid json:");
// console.dir(obj);
} catch(e) {
console.error("Invalid JSON in lease: " + util.inspect(e));
cb(null);
}
if(obj) {
cb(obj);
}
});
};
return o;
};