Last active
December 25, 2021 16:42
-
-
Save okaprinarjaya/34508ddcddad0cb190e52ad39cf1181c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable no-console */ | |
require('dotenv').config(); | |
const radius = require('radius'); | |
const dgram = require('dgram'); | |
const RADIUS_SECRET = process.env.RADIUS_SECRET; | |
const client = dgram.createSocket('udp4'); | |
const sent_packets = {}; | |
radius.add_dictionary(`${__dirname}/src/radius-server/dictionary.mikrotik`); | |
client.bind(49001); | |
client.on('message', function (msg, rinfo) { | |
const packetResp = radius.decode({ | |
packet: msg, | |
secret: RADIUS_SECRET, | |
}); | |
const request = sent_packets[packetResp.identifier]; | |
// verify responses to make sure you are talking to a server with the same shared secret | |
const valid_response = radius.verify_response({ | |
response: msg, | |
request: request.raw_packet, | |
secret: request.secret, | |
}); | |
console.log('### LOGGING - packet.code:', packetResp.code); | |
console.log('### LOGGING - packet.identifier:', packetResp.identifier); | |
console.log('### LOGGING - packet.length:', packetResp.length); | |
console.log('### LOGGING - packet.attributes contains'); | |
console.log(packetResp.attributes); | |
console.log('### END LOGGING'); | |
console.log('### LOGGING - packet.raw_attributes contains'); | |
console.log(packetResp.raw_attributes); | |
console.log('### END LOGGING'); | |
console.log('### LOGGING - rinfo contains'); | |
console.log(rinfo); | |
console.log('### END LOGGING'); | |
if (valid_response) { | |
console.log(`Got valid response: ${packetResp.code} for packet id ${packetResp.identifier}`); | |
} else { | |
console.log(`WARNING! Invalid response: ${packetResp.code} for packet id ${packetResp.identifier}`); | |
} | |
client.close(); | |
}); | |
// Packet 1 | |
const packet = { | |
code: 'Access-Request', | |
secret: RADIUS_SECRET, | |
identifier: 0, | |
attributes: [ | |
['NAS-IP-Address', '10.5.5.5'], | |
['User-Name', 'oka'], | |
['User-Password', '123'], | |
], | |
}; | |
const encodedPacket = radius.encode(packet); | |
sent_packets[packet.identifier] = { | |
raw_packet: encodedPacket, | |
secret: packet.secret, | |
}; | |
client.send(encodedPacket, 0, encodedPacket.length, 1812, 'localhost'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment