-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault-generator.js
138 lines (123 loc) · 3.34 KB
/
default-generator.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'use strict';
var http = require('http');
var crypto = require('crypto');
var url = require('url');
var cookie = require('cookie');
var authName;
var authPass;
var userBase = 'test';
var passBase = 'test';
/**
* What the client responds with when the `connect` message is sent. Called by
* the `connect` haqndler in the `exports.events` handler
* @method clientReadyMessage
* @param {object} cookies the cookies
* @param {string} user the username
* @param {string} pass the password
* @returns {object} the message
*/
function clientReadyMessage(cookies, user, pass) {
return {
type: 'connected',
username: user,
sessionID: cookies.sessionID,
password: pass,
};
}
exports.events = [{
name: 'connect',
method: function(event, socket, cookies, user, pass) {
socket.json.send(clientReadyMessage(cookies, user, pass));
}
}, {
name: 'message',
method: function() {
var obj = Array.prototype.slice.call(arguments, -1)[0];
console.log('got message', obj);
}
}, {
name: 'disconnect',
method: function() {
console.log('recieved a disconnect event from the server');
}
}];
/**
* Autnenticate for a client
* @method authenticate
* @async
* @param {string} host hostname
* @param {integer} port portname
* @param {integer} iteration what client number to generate
* @param {Function} cb `err` on error, `err`, `cookies`, `username`, `password` on success
* @returns {object} undefined
*/
exports.authenticate = function(host, port, iteration, cb) {
authName = userBase + iteration;
authPass = passBase + iteration;
var payload = ['username=', authName, '&password=', authPass].join('');
var opts = {
method: 'POST',
path: '/authenticate',
host: host,
port: port,
headers: {
'content-type': 'application/x-www-form-urlencoded',
'content-length': payload.length
}
};
var req = http.request(opts, function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk.toString();
});
res.on('error', function(err) {
return cb(err);
});
res.on('end', function() {
var cookies = cookie.parse(res.headers.cookies);
if (typeof res.headers.location !== 'string') {
return cb(new Error('Auth failed:\nStatus: ' + res.statusCode + '\nMessage: ' + body));
}
return cb(null, cookies, authName, authPass);
});
});
req.on('error', function(err) {
return cb(err);
});
req.end(payload);
};
/**
* Returns the URL to send to the `io.connect` method.
* @method getSocketURL
* @param {string} host host name
* @param {integer} port port
* @param {object} cookies if you need them
* @returns {object} undefined
*/
exports.getSocketURL = function(host, port, cookies) {
return url.format({
protocol: 'http',
hostname: host,
port: port,
path: '/',
query: {
session: cookies.sessionId
}
});
};
/**
* Returns a message to send to the client
* @method clientIterateMessage
* @param {object} cookies Any cookies you might need
* @param {string} user the name of the user who authenciated
* @param {string} pass the password of the user who authenciated
* @returns {object} undefined
*/
exports.clientIterateMessage = function(cookies, user, pass) {
return {
type: 'message',
user: user,
pass: pass,
session: cookies.sessionId
};
};