-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathanon_validation.html
334 lines (286 loc) · 16.1 KB
/
anon_validation.html
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<!DOCTYPE html>
<html lang="en" ng-app="fluxMembersApp">
<head>
<meta charset="UTF-8">
<title>Flux - Magic Validation</title>
<!-- vendor -->
<link rel='stylesheet' href='css/basscss.min.css'>
<link rel="stylesheet" href="css/members.css">
<link rel="stylesheet" href="css/spinners.css">
</head>
<body ng-controller="FluxController as flux">
<div ng-show="flux.showLoading" class="absolute bg-white block col-12" style="height: 100%;">
<h1 ng-show="flux.showLoading" class="block px2 center">Loading...</h1>
<div class="sk-folding-cube">
<div class="sk-cube1 sk-cube"></div>
<div class="sk-cube2 sk-cube"></div>
<div class="sk-cube4 sk-cube"></div>
<div class="sk-cube3 sk-cube"></div>
</div>
</div>
<div class="clearfix" style="min-height: 200px;">
<h3 ng-show="flux.error" class="m2 p2 rounded bg-red white">A (probably) non-fatal error has occured: {{flux.error}}</h3>
<h2 class="m2 p2 rounded bg-green white">Validation queue: {{flux.stats.toValidate}} / {{flux.stats.totalMembers}}. Validations so far: {{flux.stats.totalValidations}} (within last 3 months: {{flux.stats.totalValidationsRecent}})</h2>
<hr>
<div class="m2 p2" ng-show="!flux.started">
<h3>Settings</h3>
<form>
<label for="settings-state" class="block">State:
<select id="settings-state" class="field" ng-model="flux.state" ng-options="opt.name for opt in flux.stateOptions" ng-change="flux.doGetInfo()"></select>
</label>
<label class="block mt1">Selection Method:
<select id="settings-method" class="field" ng-model="flux.selectionMethod" ng-options="opt.name for opt in flux.selectionMethodOptions"></select>
</label>
<label class="block mt1">N. Parallel Sessions:
<input class="field" type="number" ng-model="flux.nSessions" />
</label>
<button class="btn btn-primary mt2 block" ng-click="flux.startValidation()">Start Validation</button>
</form>
</div>
<hr>
<h1 class="m1 p1 rounded bg-white black">{{ flux.currentSession.status }}</h1>
<h2 ng-if="flux.sessions[flux.curSocN].validMsg && flux.sessions[flux.curSocN].goodResponse" class="green">{{ flux.sessions[flux.curSocN].validMsg }}</h2>
<h2 ng-if="flux.sessions[flux.curSocN].validMsg && !flux.sessions[flux.curSocN].goodResponse" class="red">{{ flux.sessions[flux.curSocN].validMsg }}</h2>
<h1 ng-if="flux.sessions[flux.curSocN].errorMsg" class="white bg-red rounded p3 m2">Error: {{flux.sessions[flux.curSocN].errorMsg}}</h1>
<!--<h1 class="white bg-red rounded p3 m2">Notice 8/8/17: We seem to be getting a lot of false negative "CAPTCHA incorrect messages"; will investigate over the next few days. For now this page will only run 1 session simultaneously.</h1>-->
<div ng-show="flux.started && !flux.sessions[flux.curSocN].disableCaptcha" class="m3 block">
<h3 class="">#{{ flux.sessions[flux.curSocN].i }} - Input Captcha</h3>
<img ng-src="{{ flux.sessions[flux.curSocN].imageUrl }}">
<form ng-submit="flux.submitCaptcha()">
<strong>NB:</strong> input is not case sensitive. <br>
<input autofocus id='captchaInput' class='field' type="text" placeholder="captcha" ng-model="flux.sessions[flux.curSocN].captcha" ng-enabled="{{ !flux.sessions[flux.curSocN].disableCaptcha }}"><br>
<button class="btn btn-primary mv3 db" ng-click="flux.submitCaptcha()" ng-show="flux.sessions[flux.curSocN].captcha.length === 4">Submit CAPTCHA</button>
</form>
</div>
<div class="m3">
<ul>
<li ng-repeat="logMsg in flux.log track by $index">{{logMsg}}</li>
</ul>
</div>
<p class="m3 p3">
Note, sometimes the server gets overloaded (it's fairly intensive on the server-side) so if it doesn't work give it a few minutes or come back later. If it's really broken please email leadership@voteflux.org.
</p>
<p class="m3 p3 white bg-green">
Wondering what this does? By filling in the CAPTCHA you're interfacing with the AEC electoral roll data.
Our server pre-fills the form, then you essentially fill in the last bit and press 'go'.
This let's us automate this process as much as possible. It's easily 10x faster than manually copy and pasting
details into the AEC website (which is what other parties do). What used to take a week (yes, really) can now be done in mere hours.
</p>
<div class="m1 p1 bg-blue white rounded">
<h4 ng-click="flux.showAdvanced = !flux.showAdvanced">Show/Hide Advanced Notes</h4>
<div class="m1 p1" ng-show="flux.showAdvanced">
<h5>Advanced notes:</h5>
<ul>
<li>Set the 'nSessions' param to an integer to use that many sessions (default: 2). This can increase the number of validations you can perform per minute provided the server is not overloaded and you're internet connection is fine. If you see the red star (loading icon) instead of a CAPTCHA often increasing nSessions might help.</li>
<li>This micro app uses websockets for all the CAPTCHA stuff, and refreshes via prod.v1.api.flux.party/getinfo every 10 seconds for the other stats</li>
<li>Set the 'state' param to one of nsw,vic,qld,sa,tas,nt,act,wa to validate only that state.</li>
<li>Set the 'debug' param to anything to use local dev.</li>
<li>Set the 'prod' param to anything to use production.</li>
</ul>
</div>
</div>
</div>
<script src="js/angular.min.js"></script>
<script src="js/lodash.min.js"></script>
<script src="js/qrcode.js"></script>
<script src="js/common.js"></script>
<script src="js/vendor/socket.io-1.4.5.js"></script>
<script type="text/javascript">
var app = angular.module('fluxMembersApp', []);
app.controller('FluxController', function($scope, $log, $rootScope, $http, $window){
const flux = this;
flux.nSessions = getParam('nSessions') ? parseInt(getParam('nSessions')) : 2;
flux.currentSocket = null;
flux.currentSession = null;
flux.log = [];
flux.sessions = [];
flux.sockets = [];
flux.curSocN = 0;
flux.error = '';
flux.getinfo = {};
flux.stats = {toValidate: 0, totalMembers: 0, totalValidations: 0}
flux.stateOptions = [
{name: "None (All Members)", value: ""},
{name: "nsw", value: "nsw"},
{name: "vic", value: "vic"},
{name: "wa", value: "wa"},
{name: "tas", value: "tas"},
{name: "qld", value: "qld"},
{name: "act", value: "act"},
{name: "sa", value: "sa"},
{name: "nt", value: "nt"},
];
flux.state = flux.stateOptions[0];
if (getParam('state') !== undefined) {
let tmpState = getParam('state');
flux.state = flux.stateOptions.filter(o => o.value === tmpState)[0];
}
flux.selectionMethodOptions = [
{name: "Most recent signups first", value:"recent-signups"},
{name: "Random", value: "random"},
];
flux.selectionMethod = flux.selectionMethodOptions[0];
flux.doGetInfo = function() {
$http.get(flux_api('getinfo'))
.then(function(resp) {
flux.getinfo = resp.data;
if (flux.state.value !== "") {
flux.stats.toValidate = flux.getinfo['validation_queue_state'][flux.state.value];
flux.stats.totalMembers = flux.getinfo['n_members_state'][flux.state.value];
flux.stats.totalValidations = flux.getinfo['n_members_validated_state'][flux.state.value];
} else {
flux.stats.toValidate = flux.getinfo['validation_queue'];
flux.stats.totalMembers = flux.getinfo['n_members'];
flux.stats.totalValidations = flux.getinfo['n_members_validated'];
flux.stats.totalValidationsRecent = flux.getinfo['n_valid_in_last_3_months'];
}
console.log(flux.getinfo)
})
.catch(flux.handleHttpError);
}
flux.doGetInfo();
setInterval(flux.doGetInfo, 10000); // no point going faster than once per 10s
flux.handleHttpError = function(err) {
flux.error = err;
console.log(err);
}
flux.setInit = function(sess){
sess.disableCaptcha = true;
sess.imageUrl = '';
sess.captcha = '';
sess.errorMsg = '';
sess.validMsg = null;
sess.submitted = false;
}
flux.reset = function(soc){
flux.sendInit(soc);
console.log('reset');
flux.errorMsg = '';
$scope.$apply();
}
flux.newSocket = function(){
return new WebSocket(flux_api('ws_validation').replace('http', 'ws'));
}
var sendJson = function(data, soc){
soc.send(jsonDumps(data));
}
flux.sendInit = function(soc){
var toSend = {method: 'begin'};
if (flux.state.value !== "")
toSend.state = flux.state.value
toSend.selectionMethod = flux.selectionMethod.value;
console.log(toSend);
if (getParam('s') !== undefined)
toSend.s = getParam('s')
sendJson(toSend, soc);
}
flux.started = false;
flux.initSocket = function (i) {
flux.sockets[i] = flux.newSocket();
flux.sessions[i] = {i: i};
var mysession = flux.sessions[i];
flux.setInit(mysession);
var soc = flux.sockets[i];
const addLogMsg = function(msg) {
flux.log.unshift(`[${i}] ${msg}`);
}
var socketHandlers = {
finish: function (data) {
addLogMsg(data['success'] === true ? "User was valid!" : data['captcha_incorrect'] === true ? "CAPTCHA Wrong" : "User wasn't valid :(");
mysession.goodResponse = data['success'] === true;
flux.reset(soc);
console.log('Finished', mysession.goodResponse);
},
'deliver_session': function (data) {
flux.setInit(mysession);
mysession.session = data.session;
mysession.imageUrl = flux_api('au/captcha_img/' + mysession.session);
mysession.status = `[${i}] Session initiated, captcha loading`;
mysession.disableCaptcha = false;
$scope.$apply();
var captchaInput = document.getElementById("captchaInput");
captchaInput.focus();
console.log('deliver_session', mysession);
mysession.image = new Image();
mysession.image.src = mysession.imageUrl;
},
'handle_error': function (data) {
addLogMsg(data.msg);
if (data['canContinue']) {
mysession.goodResponse = false;
console.log(`Error in validation: ${data.msg} but able to continue. Sleeping for ${data['canContinue']} s`)
setTimeout(() => { flux.reset(soc) }, data['canContinue'] * 1000);
if (flux.curSocN === i) {
flux.advanceSocket();
}
} else {
mysession.errorMsg = data.msg;
}
},
'timeout': function (data) {
if (flux.curSocN === i && mysession.session === data.session) {
addLogMsg("timeout");
mysession.goodResponse = false;
mysession.disableCaptcha = true;
flux.advanceSocket();
setTimeout(() => { flux.reset(soc) }, 2000);
}
}
}
soc.onopen = function () {
console.log('opened');
flux.sendInit(soc);
}
soc.onmessage = function (evt) {
console.log(evt);
var msg = JSON.parse(evt.data);
if (msg.status === 'error')
socketHandlers['handle_error'](msg);
else
socketHandlers[msg.method](msg);
$scope.$apply();
}
soc.onclose = function () {
console.log('sock closed');
flux.status = 'Connection lost, resetting... in 1 seconds';
setTimeout(function () {
// location.reload();
flux.reset(soc);
}, 1000);
}
mysession.status = `[${i}] Session started... waiting for server (can take 5-10 seconds)`;
};
flux.startValidation = function() {
flux.started = true;
flux.doGetInfo();
_.forEach(_.range(flux.nSessions), function(i) {
setTimeout(() => { flux.initSocket(i) }, i * 750);
});
setTimeout(() => {
flux.currentSocket = flux.sockets[0];
flux.currentSession = flux.sessions[0];
flux.showLoading = false;
}, 250);
}
flux.advanceSocket = function(){
var next = (flux.curSocN + 1) % flux.nSessions;
flux.currentSocket = flux.sockets[next];
flux.currentSession = flux.sessions[next];
flux.curSocN = next;
}
flux.submitCaptcha = function(){
if (flux.currentSession.submitted === false && flux.currentSession.captcha.length === 4){
flux.currentSession.submitted = true;
var toSend = jsonDumps({method: 'captcha_answer', captcha: flux.currentSession.captcha, session: flux.currentSession.session});
console.log('Sending...', flux.curSocN, toSend);
flux.currentSocket.send(toSend, flux.currentSocket);
flux.currentSession.status = `[${flux.curSocN}] Submitted captcha. Waiting for response...`;
flux.currentSession.disableCaptcha = true;
flux.currentSession.imageUrl = 'data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7'; // found here https://css-tricks.com/data-uris/
flux.advanceSocket()
}
}
});
</script>
</body>
</html>