Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
131 additions
and
147 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,110 @@ | ||
var connect = require('connect'); | ||
var urlparser = require('url'); | ||
|
||
var minPwdLenght = 1; | ||
|
||
// TODO replace this with some users db | ||
const users = Object.create(null); | ||
users['max'] = {name: 'max', pwd :'max'}; | ||
|
||
var loginOrRegister = function (req, res, next) { | ||
url = req.urlp = urlparser.parse(req.url, true); | ||
console.log('req.body: '+ JSON.stringify(req.body)); | ||
|
||
if(url.body) { | ||
try { | ||
if( url.body.action == "login") { | ||
// TODO access user db here | ||
var user = users[url.body.name]; | ||
if(user && user.pwd === url.body.pwd) { | ||
console.log(user.name + "logged in"); | ||
req.session.auth = true; | ||
} | ||
} else if(url.body.action == "register") { | ||
const pwds = url.body.pwd; | ||
if((pwds[0] + "").length >= minPwdLenght && pwds[0] === pwds[1]) { | ||
const userName = url.body.name; | ||
// TODO really register user | ||
users[userName] = {name : userName, pwd : pwds[0]}; | ||
console.log("user " + userName + " registered"); | ||
req.session.auth = true; | ||
} | ||
} | ||
const http = require('http'); | ||
const url = require('url'); | ||
const socketIO = require('socket.io'); | ||
const nodeStatic = require('node-static'); | ||
const repository = require('./core-foos-repository'); | ||
const util = require('./core-foos-util'); | ||
const config = util.parseConfig(); | ||
var logger = util.createLogger('### HTTP'); | ||
|
||
if(req.sesstion.auth) { | ||
console.log('redirecting to root page'); | ||
res.writeHead(302, {'Location': '/'}); | ||
res.end(); | ||
return; | ||
} | ||
} catch(e){ | ||
console.error(e); | ||
} | ||
} | ||
next(); | ||
}; | ||
const clientFiles = new nodeStatic.Server(config.dir ? config.dir : './client'); | ||
|
||
const COOKIE_USER_NAME = 'core_foos_user_name'; | ||
|
||
var authCheck = function (req, res, next) { | ||
url = req.urlp = urlparser.parse(req.url, true); | ||
function getCookies(request) { | ||
const cookies = Object.create(null); | ||
request.headers.cookie && request.headers.cookie.split(';').forEach(function( cookie ) { | ||
var parts = cookie.split('='); | ||
cookies[ parts[ 0 ].trim() ] = ( parts[ 1 ] || '' ).trim(); | ||
}); | ||
return cookies; | ||
} | ||
|
||
// #### | ||
// Logout | ||
if ( url.pathname == "/logout" ) { | ||
req.session.destroy(); | ||
function setCookie(response, name, value, secondsToLive) { | ||
const object = { | ||
'Set-Cookie': name + "="+value, | ||
'Content-Type': 'text/plain' | ||
}; | ||
if(millisToLive){ | ||
object['Max-Age'] = secondsToLive; | ||
} | ||
response.writeHead(200, object); | ||
} | ||
|
||
// ### | ||
// Do nothing if user wants to register or to login | ||
if(['/login.html', '/register.html'].indexOf(url.pathname) > -1 | ||
|| | ||
(req.body && ['login','register'].indexOf(req.body.action) > -1) | ||
) { | ||
console.log("login or register request detected"); | ||
next(); | ||
return; | ||
} else { | ||
console.log("########## " + url.pathname + (req.body ? " / " + JSON.stringify(req.body) : "")); | ||
function determineUserName(request,response) { | ||
const cookies = getCookies(request); | ||
var userName = cookies[COOKIE_USER_NAME]; | ||
if(!userName){ | ||
userName = "quick-match"; | ||
} | ||
return userName; | ||
} | ||
|
||
function serveQuickHandles(webSocket, request, response){ | ||
|
||
// #### | ||
// Is User already validated? | ||
if (req.session && req.session.auth == true) { | ||
next(); // stop here and pass to the next onion ring of connect | ||
return; | ||
function broadcastStateUpdate(upsertMatch, removeMatch, waitingPlayers) { | ||
const newState = {upsert:upsertMatch, remove:removeMatch, waiting_players:waitingPlayers}; | ||
logger.log('broadcasting state update: ' + JSON.stringify(newState)); | ||
|
||
webSocket.sockets.emit('update_state', newState); | ||
} | ||
|
||
console.log('redirecting to login page'); | ||
res.writeHead(302, {'Location': '/login.html'}); | ||
res.end(); | ||
return; | ||
const parsedURL = url.parse(request.url, true); | ||
const playerName = determineUserName(request, response); | ||
|
||
if(parsedURL.pathname == '/quickmatch') { | ||
logger.log("handling quickmatch request: "+ JSON.stringify(parsedURL) + " (user: "+playerName+")"); | ||
repository.requestImmediateMatch(playerName, broadcastStateUpdate); | ||
return true; | ||
|
||
} else if(parsedURL.pathname == '/endactivematch'){ | ||
logger.log("handling quickmatch request: "+ JSON.stringify(parsedURL) + " (user: "+playerName+")"); | ||
repository.getActiveMatch(function(activeMatch){ | ||
if(activeMatch) { | ||
repository.endMatch({matchId : activeMatch._id}, function(finishedMatch){ | ||
|
||
// #### | ||
// This user is not authorized. | ||
res.writeHead(403); | ||
res.end('You are not authorized.'); | ||
return; | ||
if (finishedMatch) { | ||
repository.startMatch(function(startedMatch){ | ||
logger.log("starting match: "+ JSON.stringify(startedMatch)); | ||
broadcastStateUpdate(startedMatch, finishedMatch); | ||
}); | ||
} | ||
|
||
}); | ||
} | ||
}); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
var server = connect.createServer( | ||
connect.logger({ format: ':method :url' }), | ||
connect.cookieParser("secret"), | ||
connect.session({ secret: 'foobar' }), | ||
connect.bodyParser(), | ||
loginOrRegister, | ||
authCheck, | ||
connect.static('../client') | ||
); | ||
|
||
server.listen(3000); | ||
module.exports = { | ||
createSocketServer : function(){ | ||
|
||
const httpServer = http.createServer(function (request, response) { | ||
if(serveQuickHandles(webSocket, request, response)) { | ||
// serve something useful | ||
response.writeHead(200, {'Content-Type': 'text/plain'}); | ||
response.end('okay'); | ||
} else { | ||
request.addListener('end', function () { | ||
clientFiles.serve(request, response); | ||
}); | ||
} | ||
}); | ||
|
||
httpServer.listen(config.port ? config.port : 2000); | ||
|
||
const webSocket = socketIO.listen(httpServer); | ||
|
||
if(config.deployment == "heroku") { | ||
webSocket.configure(function () { | ||
// taken from https://devcenter.heroku.com/articles/using-socket-io-with-node-js-on-heroku | ||
webSocket.set("transports", ["xhr-polling"]); | ||
webSocket.set("polling duration", 10); | ||
}); | ||
} | ||
|
||
return webSocket; | ||
} | ||
}; |
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
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