-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws_test.js
184 lines (140 loc) · 5.49 KB
/
ws_test.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
'use strict';
let hasReplied = false;
const startFrom = +process.argv[2];
const toIncluded = +process.argv[3];
const os = require('os');
const fs = require('fs');
const path = require('path');
const cluster = require('cluster');
const https = require('https');
const querystring = require('querystring');
const msgpack = require('msgpack');
const shortid = require('shortid');
const jwt = require('jsonwebtoken');
const utils = require('microservice-utils');
// const useHost = 'localhost';
const useHost = 'micro02.sgdev.vcube.com';
const wssServers = [
'wss://' + useHost + ':9999/vcube'
];
const msgs = [
'Ṱ̺̺̕o͞ ̷i̲̬͇̪͙n̝̗͕v̟̜̘̦͟o̶̙̰̠kè͚̮̺̪̹̱̤ ̖t̝͕̳̣̻̪͞h̼͓̲̦̳̘̲e͇̣̰̦̬͎ ̢̼̻̱̘h͚͎͙̜̣̲ͅi̦̲̣̰̤v̻͍e̺̭̳̪̰-m̢iͅn̖̺̞̲̯̰d̵̼̟͙̩̼̘̳ ̞̥̱̳̭r̛̗̘e͙p͠r̼̞̻̭̗e̺̠̣͟s̘͇̳͍̝͉e͉̥̯̞̲͚̬͜ǹ̬͎͎̟̖͇̤t͍̬̤͓̼̭͘ͅi̪̱n͠g̴͉ ͏͉ͅc̬̟h͡a̫̻̯͘o̫̟̖͍̙̝͉s̗̦̲.̨̹͈̣',
'𝕿𝖍𝖊 𝖖𝖚𝖎𝖈𝖐 𝖇𝖗𝖔𝖜𝖓 𝖋𝖔𝖝 𝖏𝖚𝖒𝖕𝖘 𝖔𝖛𝖊𝖗 𝖙𝖍𝖊 𝖑𝖆𝖟𝖞 𝖉𝖔𝖌',
'⒯⒣⒠ ⒬⒰⒤⒞⒦ ⒝⒭⒪⒲⒩ ⒡⒪⒳ ⒥⒰⒨⒫⒮ ⒪⒱⒠⒭ ⒯⒣⒠ ⒧⒜⒵⒴ ⒟⒪⒢',
'<script\x0Ctype="text/javascript">javascript:alert(1);</script>',
'../../../../../../../../../../../etc/passwd%00',
'() { _; } >_[$($())] { touch /tmp/blns.shellshock2.fail; }',
'Powerلُلُصّبُلُلصّبُررً ॣ ॣh ॣ ॣ冗',
'🐵 🙈 🙉 🙊',
'0️⃣ 1️⃣ 2️⃣ 3️⃣ 4️⃣ 5️⃣ 6️⃣ 7️⃣ 8️⃣ 9️⃣ 🔟',
'❤️ 💔 💌 💕 💞 💓 💗 💖 💘 💝 💟 💜 💛 💚 💙'
];
const addDelay = (email, delay, userNo) => {
setTimeout(() => {
let postData = querystring.stringify({
email: email,
pwd: 'Sy4ti2Sue',
deviceId: email
});
let postOpts = {
host: useHost,
port: '53547',
path: '/api/v1/auth/SignIn',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData)
},
key: fs.readFileSync(path.join(__dirname, './certs/test-client-cert-private-key.pem')),
cert: fs.readFileSync(path.join(__dirname, './certs/test-client-cert.pem')),
ca: [fs.readFileSync(path.join(__dirname, './certs/test-root-cert.pem'))],
rejectUnauthorized: false
};
postOpts.agent = new https.Agent(postOpts);
let postReq = https.request(postOpts, function(res) {
res.setEncoding('utf8');
res.on('data', function (signInRes) {
const tokens = JSON.parse(signInRes).tokenPair;
let decodedAccessToken = jwt.decode(tokens.accessToken);
/*
* Connect to WebSocket server.
*/
let ws = new (require('ws'))(utils.pickRandomly(wssServers), {
key: fs.readFileSync(path.join(__dirname, './certs/test-client-cert-private-key.pem')),
cert: fs.readFileSync(path.join(__dirname, './certs/test-client-cert.pem')),
ca: [fs.readFileSync(path.join(__dirname, './certs/test-root-cert.pem'))],
rejectUnauthorized: false,
headers: {
access_token: tokens.accessToken,
refresh_token: tokens.refreshToken
}
});
ws.on('open', () => {
/*
* Heartbeat every 15s.
*/
(function heartbeat() {
ws.ping(1);
setTimeout(heartbeat, 15 * 1000);
})();
});
ws.on('message', (data, flags) => {
let rawBinary = new Uint8Array(data);
let unpackedRecvMsg = msgpack.unpack(rawBinary);
console.log('me: ' + email + ', received message from: ' + unpackedRecvMsg.sender + ', and the content is:\n' + JSON.stringify(unpackedRecvMsg) + '\n');
if (!hasReplied) {
ws.send(msgpack.pack({
lang: 'en',
messageType: 'TEXT',
content: utils.pickRandomly(msgs),
toConversationId: unpackedRecvMsg.toConversationId,
forGroupId: unpackedRecvMsg.forGroupId,
mentionedUserUserIds: [],
mentionedMessageMessageIds: [],
conversationType: unpackedRecvMsg.conversationType
}), (err) => {
if (err) {
// What should we do here?
console.warn(error);
}
});
hasReplied = true;
setTimeout(() => {
hasReplied = false;
}, 10000);
}
});
ws.on('error', (error) => {
console.warn(error);
});
ws.on('close', (code, message) => {
// console.warn('Connection closed, code: ' + code + ', message: ' + message + '<<<<<<<<<');
});
});
});
postReq.write(postData);
postReq.end();
postReq.on('error', (error) => {
console.warn(error);
});
}, delay);
};
if (cluster.isMaster) {
const numOfWorkers = os.cpus().length;
for (var i = 0; i < numOfWorkers; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
cluster.fork();
});
} else {
for (let i = startFrom; i <= toIncluded; i++) {
let userNo = i;
let userEmailPrefix = '' + userNo;
while (userEmailPrefix.length < 4) {
userEmailPrefix = '0' + userEmailPrefix;
}
let email = 'leonard.shi+' + userEmailPrefix + '@vcube.co.jp';
addDelay(email, 200, userNo);
}
}