Browse Source

Fix password comand behavior to match real protocol

fix/pass
Sheogorath 5 years ago
parent
commit
0768520839
No known key found for this signature in database GPG Key ID: 1F05CC3635CDDFFD
  1. 16
      src/server.js
  2. 6
      src/user.js
  3. 16
      test/command_pass.js

16
src/server.js

@ -13,10 +13,17 @@ const ERR_NEEDMOREPARAMS = '461'
let server = {} let server = {}
server.create = function create() {
server.create = function create(config = {}) {
let password
if (config.password) {
password = config.password
}
let userlist = {} let userlist = {}
let server = net.createServer((socket) => { let server = net.createServer((socket) => {
const user = new User(socket) const user = new User(socket)
if (!config.password) {
user.authenticated = true
}
socket.on('data', function (data) { socket.on('data', function (data) {
data.toString("ascii").split("\r\n").forEach(function (commandline) { data.toString("ascii").split("\r\n").forEach(function (commandline) {
if (commandline == "") { if (commandline == "") {
@ -76,8 +83,11 @@ server.create = function create() {
server.closeConnection(user.nickname) server.closeConnection(user.nickname)
break; break;
case "PASS": case "PASS":
if (tokenized[1])
user.setPassword(tokenized[1])
if (tokenized[1] === config.password) {
user.authenticated = true
} else {
user.closeConnection()
}
break; break;
default: default:

6
src/user.js

@ -1,5 +1,6 @@
function User(socket) { function User(socket) {
this.registered = false this.registered = false
this.authenticated = false
this.nickname = "" this.nickname = ""
this.connection = socket this.connection = socket
this.realname = "" this.realname = ""
@ -17,6 +18,9 @@ function User(socket) {
} }
this.register = function(username, realname) { this.register = function(username, realname) {
if (!this.authenticated) {
this.connection.destroy()
}
this.username = username this.username = username
this.realname = realname this.realname = realname
this.registered = true this.registered = true
@ -33,7 +37,7 @@ function User(socket) {
} }
this.closeConnection = function () { this.closeConnection = function () {
socket.destroy()
this.connection.destroy()
} }

16
test/command_pass.js

@ -4,20 +4,30 @@ const IRCServer = require("../src/server.js");
describe("PASS OK", function () { describe("PASS OK", function () {
it("should handle a PASS command -> PASS some_passwd", function (done) { it("should handle a PASS command -> PASS some_passwd", function (done) {
const server = IRCServer.create()
const server = IRCServer.create({
password: "itsJustATest"
})
let mockedSock = new EventEmitter() let mockedSock = new EventEmitter()
mockedSock.address = function() {
return { port: 12346, family: 'IPv4', address: '127.0.0.1' }
}
mockedSock.write = function (data) {
return
}
mockedSock.destroy = function () { mockedSock.destroy = function () {
done("Destroyed socket without answering") done("Destroyed socket without answering")
} }
server.emit("connection", mockedSock) server.emit("connection", mockedSock)
mockedSock.emit('data', Buffer.from("PASS some_pass\r\n", "ascii"))
mockedSock.emit('data', Buffer.from("PASS itsJustATest\r\n", "ascii"))
mockedSock.emit('data', Buffer.from("NICK some_nick\r\n", "ascii")) mockedSock.emit('data', Buffer.from("NICK some_nick\r\n", "ascii"))
mockedSock.emit('data', Buffer.from("USER guest tolmoon tolsun :Ronnie Reagan\r\n", "ascii"))
user = server.getUserlist()["some_nick"] user = server.getUserlist()["some_nick"]
assert.equal("some_pass", user["password"])
assert.equal(user["registered"], true)
done() done()
}) })
Loading…
Cancel
Save