-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
109 lines (99 loc) · 2.9 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
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
(function() {
const config = require("./config");
var dgram, http, https, radius, server, session_cache, verify_remote;
radius = require("radius");
dgram = require("dgram");
https = require("https");
http = require("http");
server = dgram.createSocket("udp4");
verify_remote = function(user, pass, cb) {
if(pass == ""){ // Skip if password blank
return cb(false);
}
var options, post_data, req;
post_data = JSON.stringify({
'Username': user,
'Password': pass
});
options = {
hostname: config.rock_hostname,
path: '/api/auth/login',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': post_data.length
}
};
req = https.request(options, function(res) {
var data;
if (res.statusCode !== 204) {
console.log("Error:" + res.statusCode + " / " + res.body);
return cb(false);
}
data = "";
res.setEncoding('utf8');
res.on('data', function(chunk) {
return data += chunk;
});
return res.on('end', function() {
try {
return cb(true);
} catch (error) {
console.error(error);
return cb(false);
}
});
});
req.on('error', function(e) {
console.error("problem with request: " + e.message);
return cb(false);
});
req.write(post_data);
return req.end();
};
session_cache = {};
server.on("message", function(msg, rinfo) {
var packet, password, send_response, username;
try {
packet = radius.decode({packet: msg, secret: config.radius_secret});
} catch (e) {
console.log("Malformed RADIUS Packet. Dropping silently.")
}
username = packet.attributes['User-Name'];
password = packet.attributes['User-Password'];
console.log("Recv " + packet.code + " for " + username);
if (packet.code !== 'Access-Request') {
console.log('unknown packet type: ', packet.code);
return;
}
send_response = function(code, attr) {
var response;
console.log("Send " + code + " for user " + username);
response = radius.encode_response({
packet: packet,
code: code,
secret: config.radius_secret,
attributes: attr || []
});
return server.send(response, 0, response.length, rinfo.port, rinfo.address, function(err, bytes) {
if (err) {
return console.log('Error sending response to ', rinfo);
}
});
};
return verify_remote(username, password, function(ok) {
var code;
code = "Access-Reject";
if (ok) {
code = "Access-Accept";
}
return send_response(code);
});
});
server.on("listening", function() {
var address;
address = server.address();
return console.log("RadiusRock listening " + address.address + ":" + address.port);
});
server.bind(1812);
}).call(this);