-
Notifications
You must be signed in to change notification settings - Fork 66
/
utils.js
executable file
·189 lines (170 loc) · 4.83 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
var crypto = require('crypto');
var util = require('util');
var path = require('path');
var fs = require('fs');
var utils = module.exports;
/**
* Check and invoke callback
*/
utils.invokeCallback = function(cb) {
if (!!cb && typeof cb === 'function') {
cb.apply(null, Array.prototype.slice.call(arguments, 1));
}
};
/*
* Date format
*/
utils.format = function(date, format) {
format = format || 'MM-dd-hhmm';
var o = {
"M+": date.getMonth() + 1, //month
"d+": date.getDate(), //day
"h+": date.getHours(), //hour
"m+": date.getMinutes(), //minute
"s+": date.getSeconds(), //second
"q+": Math.floor((date.getMonth() + 3) / 3), //quarter
"S": date.getMilliseconds() //millisecond
};
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1,
RegExp.$1.length === 1 ? o[k] :
("00" + o[k]).substr(("" + o[k]).length));
}
}
return format;
};
utils.compareServer = function(server1, server2) {
return (server1['host'] === server2['host']) &&
(server1['port'] === server2['port']);
}
/**
* Get the count of elements of object
*/
utils.size = function(obj, type) {
var count = 0;
for (var i in obj) {
if (obj.hasOwnProperty(i) && typeof obj[i] !== 'function') {
if (!type) {
count++;
continue;
}
if (type && type === obj[i]['type']) {
count++;
}
}
}
return count;
};
utils.md5 = function(str) {
var md5sum = crypto.createHash('md5');
md5sum.update(str);
str = md5sum.digest('hex');
return str;
}
utils.defaultAuthUser = function(msg, env, cb) {
var adminUser = null;
var appBase = path.dirname(require.main.filename);
var adminUserPath = path.join(appBase, '/config/adminUser.json');
var presentPath = path.join(appBase, 'config', env, 'adminUser.json');
if (fs.existsSync(adminUserPath)) {
adminUser = require(adminUserPath);
} else if (fs.existsSync(presentPath)) {
adminUser = require(presentPath);
} else {
cb(null);
return;
}
var username = msg['username'];
var password = msg['password'];
var md5 = msg['md5'];
var len = adminUser.length;
if (md5) {
for (var i = 0; i < len; i++) {
var user = adminUser[i];
var p = "";
if (user['username'] === username) {
p = utils.md5(user['password']);
if (password === p) {
cb(user);
return;
}
}
}
} else {
for (var i = 0; i < len; i++) {
var user = adminUser[i];
if (user['username'] === username && user['password'] === password) {
cb(user);
return;
}
}
}
cb(null);
}
utils.defaultAuthServerMaster = function(msg, env, cb) {
var id = msg['id'];
var type = msg['serverType'];
var token = msg['token'];
if (type === 'master') {
cb('ok');
return;
}
var servers = null;
var appBase = path.dirname(require.main.filename);
var serverPath = path.join(appBase, '/config/adminServer.json');
var presentPath = null;
if (env) {
presentPath = path.join(appBase, 'config', env, 'adminServer.json');
}
if (fs.existsSync(serverPath)) {
servers = require(serverPath);
} else if (fs.existsSync(presentPath)) {
servers = require(presentPath);
} else {
cb('ok');
return;
}
var len = servers.length;
for (var i = 0; i < len; i++) {
var server = servers[i];
if (server['type'] === type && server['token'] === token) {
cb('ok');
return;
}
}
cb('bad');
return;
}
utils.defaultAuthServerMonitor = function(msg, env, cb) {
var id = msg['id'];
var type = msg['serverType'];
var servers = null;
var appBase = path.dirname(require.main.filename);
var serverPath = path.join(appBase, '/config/adminServer.json');
var presentPath = null;
if (env) {
presentPath = path.join(appBase, 'config', env, 'adminServer.json');
}
if (fs.existsSync(serverPath)) {
servers = require(serverPath);
} else if (fs.existsSync(presentPath)) {
servers = require(presentPath);
} else {
cb('ok');
return;
}
var len = servers.length;
for (var i = 0; i < len; i++) {
var server = servers[i];
if (server['type'] === type) {
cb(server['token']);
return;
}
}
cb(null);
return;
}