-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathjudges.js
189 lines (153 loc) · 5.16 KB
/
judges.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
import rp from 'request-promise';
import store from '../store';
import { changeState, changeJudgePingState, startPing } from '../actions/OverlayJudgesActions';
import { wait } from '../misc/wait';
import { arrayToChunks } from './misc.js';
export default class Judges {
constructor(config, targetProtocols) {
this.usingStatus = {
ssl: {
current: 0,
max: null
},
usual: {
current: 0,
max: null
},
any: {
current: 0,
max: null
}
};
this.swap = config.swap;
this.targetProtocols = targetProtocols;
this.counter = {
all: config.items.length,
done: 0
};
this.list = {
ssl: [],
usual: [],
any: []
};
this.data = {
// sets on ping response
};
return new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
this.launch(config.items);
}).catch(error => alert(error));
}
buildGetSSL() {
if (this.list.ssl.length == 1 || !this.swap) {
return () => this.list.ssl[0];
}
return () => {
const current = this.list.ssl[this.usingStatus.ssl.current];
this.usingStatus.ssl.current = this.usingStatus.ssl.current == this.usingStatus.ssl.max - 1 ? 0 : this.usingStatus.ssl.current + 1;
return current;
};
}
buildGetAny() {
if (this.list.any.length == 1 || !this.swap) {
return () => this.list.any[0];
}
return () => {
const current = this.list.any[this.usingStatus.any.current];
this.usingStatus.any.current = this.usingStatus.any.current == this.usingStatus.any.max - 1 ? 0 : this.usingStatus.any.current + 1;
return current;
};
}
buildGetUsual() {
if (this.list.usual.length == 1 || !this.swap) {
return () => this.list.usual[0];
}
return () => {
const current = this.list.usual[this.usingStatus.usual.current];
this.usingStatus.usual.current = this.usingStatus.usual.current == this.usingStatus.usual.max - 1 ? 0 : this.usingStatus.usual.current + 1;
return current;
};
}
validate(body, judge) {
if (this.data[judge].validate.length > 0) {
return body.match(new RegExp(this.data[judge].validate));
}
return true;
}
async launch(list) {
store.dispatch(startPing());
await wait(1500);
for await (const chunk of arrayToChunks(list, 5)) {
await Promise.all(
chunk.map(async judge => {
try {
const response = await rp.get({ url: judge.url, resolveWithFullResponse: true, time: true, timeout: 10000 });
this.onSuccess(judge, response);
} catch {
this.onError(judge);
}
})
);
}
}
onSuccess(judge, response) {
const typeLink = judge.url.match(/https:\/\//) ? this.list.ssl : this.list.usual;
typeLink.push(judge.url);
this.data[judge.url] = {
...judge,
response
};
store.dispatch(
changeJudgePingState(judge.url, {
state: {
checking: false,
working: true,
timeout: response.elapsedTime
}
})
);
this.isDone();
}
onError(judge) {
store.dispatch(
changeJudgePingState(judge.url, {
state: {
checking: false,
working: false
}
})
);
this.isDone();
}
async isDone() {
this.counter.done++;
if (this.counter.done == this.counter.all) {
this.checkAtAliveJudges();
this.list.any = [...this.list.usual, ...this.list.ssl];
this.usingStatus.ssl.max = this.list.ssl.length;
this.usingStatus.usual.max = this.list.usual.length;
this.usingStatus.any.max = this.list.any.length;
this.getSSL = this.buildGetSSL();
this.getUsual = this.buildGetUsual();
this.getAny = this.buildGetAny();
await wait(1500);
store.dispatch(changeState({ isActive: false, locked: false }));
this.resolve(this);
}
}
checkAtAliveJudges() {
if (this.isRequiredSSLButNotContains()) {
this.reject('You have no working SSL judges.');
}
if (this.isRequiredUsualButNotContains()) {
this.reject('You have no working usual judges.');
}
}
isRequiredSSLButNotContains() {
return this.targetProtocols.includes('https') && this.list.ssl.length == 0;
}
isRequiredUsualButNotContains() {
return this.targetProtocols.some(protocol => ['http'].includes(protocol)) && this.list.usual.length == 0;
}
}