-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
144 lines (133 loc) · 4.9 KB
/
utils.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
138
139
140
141
142
143
144
// for secure token generation
var cryptoApi = require('crypto');
var tokenChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
var maxLen = 30;
var serverLog = require('./logger').getServerLog();
var serverState = require('./serverState');
// return values of name validation
var VALIDATION_STATUS = {
SUCCESS: {id: 0, value: true, name: "SUCCESS", // successful validation
description: "Valid name.\n"},
NO_NAME: {id: 1, value: false, name: "NO_NAME", // no name provided
description: "No name specified.\n"},
TOO_LONG: {id: 2, value: false, name: "TOO_LONG", // name longer than maxLen
description: "Name must not be longer than "+maxLen.toString()+".\n"},
LOBBY_ERR: {id: 3, value: false, name: "LOBBY_ERR", // 'lobby' included in name
description: "Names must not contain 'lobby' to avoid confusion.\n"},
BCK_COLL: {id: 4, value: false, name: "BCK_COLL", // collision with backup format chars
description: "The name contains at least one char of the backup format ("+
serverState.category_separator +
serverState.team_client_separator +
serverState.attribute_separator +
":" +
")\n"}
};
/**
* Generation of sequence numbers.
* @returns {number}
*/
exports.rand = function () {
return Math.ceil(Math.random() * 10000000000);
};
exports.randMinMax = function (min, max) {
return Math.floor(Math.random() * (max - min)) + min;
};
/**
* Generates (admin) token as specified in the wiki, i.e. length of 15, containing tall and small
* letters, as well as digits from 0 to 9.
*/
exports.generateToken = function () {
//Note: standard same algorithm, but randomBytes returns error if entropy of underlying pool
// has not yet been seeded enough
var buf = cryptoApi.pseudoRandomBytes(15);
// transform to our encoding
var token = '';
for (var i = 0; i < buf.length; ++i) {
token += tokenChars[buf[i] % tokenChars.length];
}
return token;
};
/**
* Returns true if the given randomNumberToken is valid
* @param randomNumberToken
* @returns {boolean}
*/
exports.isValidRandomNumberToken = function (randomNumberToken) {
//TODO make the length check working!
if (!randomNumberToken || !(/^[0-9]+$/.test(randomNumberToken)) /*||
!randomNumberToken.length >= randomNumberMinLength || !randomNumberToken.length <=
randomNumberMaxLength */) {
return false
} else {
return true;
}
};
/**
* Returns true if the given adminAuthKey is valid
* @param adminAuthKey
* @returns {boolean}
*/
exports.isValidAdminAuthKey = function (adminAuthKey) {
// TODO: Remove this function completely to avoid dependency on 'serverState' in utils?
if (!adminAuthKey || require('./serverState').getAdminAuthKey() != adminAuthKey) {
return false;
}
else {
return true;
}
};
/**
* Checks whether provided name is valid.
* Value attribute of return value is true on success, else false.
* @param clientName
*/
exports.isValidClientName = function (clientName) {
// must provide name
if (!clientName) {
serverLog.debug("Empty clientName provided!\n");
return VALIDATION_STATUS.NO_NAME;
}
// length restriction
if (clientName.length > maxLen) {
serverLog.warn("Given name too long: "+String(clientName.length));
return VALIDATION_STATUS.TOO_LONG;
}
// filter out "lobby" and some versions of it
var cpyName = clientName.toLowerCase();
cpyName = cpyName.replace(/\s/g, '');
// tall 'i' looks like small 'L' sometimes
var pattern = RegExp(".*[li][o0]bby.*");
if (!cpyName.localeCompare("lobby") || pattern.test(cpyName)) {
serverLog.warn("Given name: "+String(clientName)+" too similar to 'lobby'");
return VALIDATION_STATUS.LOBBY_ERR;
}
// check for backup format special unicode chars + CLI :
var bck_pattern = RegExp("["
+serverState.category_separator
+serverState.team_client_separator
+serverState.attribute_separator
+":"+
"]");
if(bck_pattern.test(clientName)) {
serverLog.warn("Given name: "+String(clientName)+" contains char from backup format");
return VALIDATION_STATUS.BCK_COLL;
}
return VALIDATION_STATUS.SUCCESS;
};
/**
* Checks whether given team name is valid.
* Value attribute of return value is true on success, else false.
* @param teamName
*/
exports.isValidTeamName = function (teamName) {
// enforce the same requirements as for client names
return this.isValidClientName(teamName);
};
/**
* Returns true if the given object is a boolean, i.e. true or false
* @param boolean
* @returns {boolean}
*/
exports.isValidBoolean = function (boolean) {
return boolean == true || boolean == false;
};