2016-09-22 15:41:37 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
'use strict';
|
|
|
|
let express = require("express");
|
|
|
|
let path = require("path");
|
2016-09-23 21:38:09 +00:00
|
|
|
let sockio = require("socket.io");
|
|
|
|
let dns = require("dns");
|
2016-09-22 15:41:37 +00:00
|
|
|
let app = express();
|
2016-09-23 21:38:09 +00:00
|
|
|
|
2016-09-22 15:41:37 +00:00
|
|
|
let pubdir = path.join(__dirname+"/public");
|
2016-09-23 21:38:09 +00:00
|
|
|
let config = require(__dirname+'/server/config');
|
|
|
|
let ircclient = require(__dirname+'/server/irc');
|
|
|
|
|
|
|
|
let port = config.server.port || 8080;
|
2016-09-22 15:41:37 +00:00
|
|
|
|
2016-09-22 17:15:40 +00:00
|
|
|
let connections = {}
|
|
|
|
|
2016-09-22 15:41:37 +00:00
|
|
|
app.get("/", function(req, res){
|
|
|
|
res.sendFile(pubdir+"/index.html");
|
|
|
|
});
|
|
|
|
|
|
|
|
app.use(express.static(__dirname + '/public'));
|
|
|
|
|
2016-09-22 17:15:40 +00:00
|
|
|
let io = sockio.listen(app.listen(port, function() {
|
|
|
|
console.log("*** Listening on port " + port);
|
|
|
|
}));
|
2016-09-22 15:41:37 +00:00
|
|
|
|
2016-09-23 21:38:09 +00:00
|
|
|
function resolveHostname(hostname) {
|
|
|
|
let promise = new Promise(function(resolve, reject) {
|
|
|
|
dns.lookup(hostname, function(err, address, family) {
|
|
|
|
if(err != null) return reject(err);
|
|
|
|
resolve(address);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
2016-09-22 15:41:37 +00:00
|
|
|
io.sockets.on('connection', function (socket) {
|
2016-09-22 17:15:40 +00:00
|
|
|
console.log('clientID: '+socket.id+' connection: ', socket.request.connection._peername);
|
|
|
|
connections[socket.id] = {}
|
|
|
|
|
|
|
|
socket.on('disconnect', function() {
|
2016-09-23 21:38:09 +00:00
|
|
|
for (let d in connections[socket.id])
|
2016-09-23 22:35:30 +00:00
|
|
|
if(connections[socket.id][d].connected == true)
|
|
|
|
connections[socket.id][d].disconnect();
|
2016-09-22 17:15:40 +00:00
|
|
|
|
|
|
|
delete connections[socket.id];
|
|
|
|
|
|
|
|
console.log('clientID: '+socket.id+' disconnect');
|
|
|
|
});
|
|
|
|
|
2016-09-23 21:38:09 +00:00
|
|
|
socket.on('error', (e) => {
|
|
|
|
console.log(e);
|
2016-09-23 22:35:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('userinput', (data) => {
|
|
|
|
let serv = connections[socket.id][data.server];
|
|
|
|
if(!serv) return;
|
|
|
|
if(serv.authenticated == false) return;
|
|
|
|
|
|
|
|
serv.handler.handleUserLine(data);
|
2016-09-23 21:38:09 +00:00
|
|
|
})
|
|
|
|
|
2016-09-22 17:15:40 +00:00
|
|
|
socket.on('irc_create', function(connectiondata) {
|
|
|
|
console.log(socket.id+" created irc connection: ", connectiondata);
|
2016-09-23 21:38:09 +00:00
|
|
|
socket.emit('act_client', {type: 'connect_message', message: "Connecting to server..", error: false});
|
|
|
|
|
|
|
|
let newConnection = new ircclient(connectiondata);
|
|
|
|
newConnection.connect();
|
|
|
|
|
|
|
|
connections[socket.id][connectiondata.server] = newConnection;
|
|
|
|
|
|
|
|
newConnection.on('authenticated', () => {
|
|
|
|
socket.emit('act_client', {type: "event_connect", address: connectiondata.server, network: newConnection.data.network,
|
2016-09-24 16:09:03 +00:00
|
|
|
supportedModes: newConnection.data.supportedModes, nickname: newConnection.config.nickname,
|
|
|
|
max_channel_length: newConnection.data.max_channel_length});
|
2016-09-23 21:38:09 +00:00
|
|
|
});
|
|
|
|
|
2016-09-23 22:35:30 +00:00
|
|
|
newConnection.on('connerror', (data) => {
|
2016-09-23 21:38:09 +00:00
|
|
|
let message = "An error occured";
|
2016-09-24 12:25:47 +00:00
|
|
|
let inconnect = true;
|
|
|
|
|
2016-09-23 22:35:30 +00:00
|
|
|
if(newConnection.authenticated == false) {
|
2016-09-23 21:38:09 +00:00
|
|
|
message = "Failed to connect to the server!";
|
2016-09-24 12:25:47 +00:00
|
|
|
inconnect = false;
|
2016-09-23 21:38:09 +00:00
|
|
|
}
|
|
|
|
|
2016-09-23 22:35:30 +00:00
|
|
|
socket.emit('act_client', {type: (inconnect == true ? 'server_message' : 'connect_message'), message: message, error: true});
|
2016-09-23 21:38:09 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
newConnection.on('pass_to_client', (data) => {
|
|
|
|
socket.emit('act_client', data);
|
|
|
|
});
|
|
|
|
|
|
|
|
newConnection.on('closed', (data) => {
|
|
|
|
let message = "Connection closed";
|
2016-09-24 12:25:47 +00:00
|
|
|
let inconnect = true;
|
2016-09-23 21:38:09 +00:00
|
|
|
|
2016-09-23 22:35:30 +00:00
|
|
|
if(newConnection.authenticated == false) {
|
2016-09-23 21:38:09 +00:00
|
|
|
message = "Failed to connect to the server!";
|
2016-09-24 12:25:47 +00:00
|
|
|
inconnect = false;
|
2016-09-23 21:38:09 +00:00
|
|
|
}
|
|
|
|
|
2016-09-23 22:35:30 +00:00
|
|
|
socket.emit('act_client', {type: (inconnect == true ? 'server_message' : 'connect_message'), message: message, error: true});
|
2016-09-23 21:38:09 +00:00
|
|
|
});
|
2016-09-22 17:15:40 +00:00
|
|
|
});
|
2016-09-22 15:41:37 +00:00
|
|
|
});
|