-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
217 lines (194 loc) · 5.92 KB
/
script.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env node
'use strict';
/* SÆT DATA HER */
var userId = '12345'; // COOP medlemsnummer
var fullName = 'fuldt navn';
var address = 'vej 1';
var zip = '0000';
var city = 'by';
var phone = '12345678';
var email = '[email protected]';
var minWait = 2000; // Min antal millisekunder ventetid mellem koder sendes
var maxWait = 4000; // Max antal millisekunder
////////////////////////////////////////////////////////////////
var http = require('http');
var fs = require('fs');
var codes = fs.readFileSync('koder.txt').toString().split("\n");
var hostname = 'www.madkonkurrence.dk';
var count = 0;
var firstPriceWins = 0;
var secondPriceWins = 0;
var submittedWins = 0;
var timeToWait = 0;
var failedCodes = [];
var security;
console.log(codes.length + ' koder fundet.');
function start() {
for (var i in codes) {
codes[i] = codes[i].substr(0, 5);
setTimeout(checkCode, timeToWait, codes[i]);
timeToWait += getRandomInt(minWait, maxWait);
}
setTimeout(end, timeToWait + 1500);
}
function getGlobalSecurity(callback) {
var data = [];
var options = {
host: hostname,
port: 80,
path: '/'
};
http.get(options, function(res) {
res.on("data", function(chunk) {
data.push(chunk);
});
res.on('end', function() {
var htmlString = data.join('');
var indexOfSec = htmlString.indexOf('GlobalSecurity');
var sec = htmlString.substr(indexOfSec);
var arr = sec.split("'");
security = arr[1];
callback();
});
}).on('error', function(err) {
console.log('failed to get GlobalSecurity variable');
console.log(err);
process.exit();
});
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
function checkCode(code) {
var data = [];
var checkCodeQuestion = JSON.stringify({
UserId: userId,
Code: code,
UserIdType: 'numbe',
Security: security
});
var options = {
hostname: hostname,
path: '/api/CheckCode',
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Content-Length': Buffer.byteLength(checkCodeQuestion)
}
};
var req = http.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function(chunk) {
data.push(chunk);
});
res.on('end', function() {
var parsedData = JSON.parse(data.join(''));
count++;
if (parsedData.Message) {
console.log(parsedData.Message + ' = muligt IP ban.');
return;
}
console.log(count + ': ' + code + ': ' + parsedData.Status);
if (parsedData.Status === 'WonFirstPrice' || parsedData.Status === 'WonSecondPrice') {
console.log('Du har vundet. ' + 'Vinder ID: ' + parsedData.WinnerId);
console.log(parsedData.Price);
if (parsedData.Status === 'WonFirstPrice') {
submitWinnerInfo(parsedData.WinnerId);
firstPriceWins++;
} else {
secondPriceWins++;
}
}
});
});
req.on('error', function(err) {
failedCodes.push(code);
console.log(code + ': Error.');
console.log(err);
});
req.write(checkCodeQuestion);
req.end();
}
function submitWinnerInfo(winnerId) {
var reqData = JSON.stringify({
WinId: winnerId,
UserNumber: userId,
FullName: fullName,
Address: address,
ZipCode: zip,
City: city,
Phone: phone,
Email: email
});
var options = {
hostname: hostname,
path: '/api/profile',
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Content-Length': Buffer.byteLength(reqData)
}
};
var req = http.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function(chunk) {
console.log(chunk);
});
res.on('end', function() {
submittedWins++;
console.log('Vinderinfo sendt.');
});
});
req.on('error', function(err) {
console.log('ERROR:');
console.log(err);
});
req.write(reqData);
req.end();
}
function end() {
var _timeToWait = 0;
if (failedCodes.length > 0) {
console.log(failedCodes.length + ' failed. Retrying.');
for (var i in failedCodes) {
_timeToWait += getRandomInt(minWait, maxWait);
setTimeout(checkCode, _timeToWait, failedCodes.pop());
}
} else if (count === codes.length && firstPriceWins === submittedWins) {
var data = [];
var reqData = JSON.stringify({
UserId: userId,
UserIdType: "numbe",
});
var options = {
hostname: hostname,
path: '/api/MyKodes',
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Content-Length': Buffer.byteLength(reqData)
}
};
var req = http.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function(chunk) {
data.push(chunk);
});
res.on('end', function() {
console.log('Du har nu ' + JSON.parse(data.join('')).length + ' lodder.');
process.exit();
});
});
req.on('error', function(err) {
console.log('ERROR:');
console.log(err);
});
req.write(reqData);
req.end();
console.log('Submitted ' + count + ' codes.');
console.log(firstPriceWins + secondPriceWins + ' wins.');
} else {
setInterval(end, 1000);
}
}
getGlobalSecurity(start);