-
Notifications
You must be signed in to change notification settings - Fork 25
/
tool_get_captcha.js
210 lines (193 loc) · 5.82 KB
/
tool_get_captcha.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
function _log(msg, obj) {
if (obj) {
console.log(msg, obj);
} else {
console.log(msg);
}
}
async function _delay(timeountMS) {
return new Promise((fin) => {
setTimeout(fin, timeountMS);
});
}
async function _refreshImg() {
return new Promise((fin) => {
let a = user.get("chkImgFlag");
if (!a) {
a = generateRandomFlag(18);
user.set("chkImgFlag", a)
}
let timeout = 2000;
let timer = setTimeout(() => {
timer = null;
_log('chkImg.do timed out after ' + timeout + ' ms');
fin(null);
}, timeout);
new Ajax.Request(getURL("chkImg.do"), {
method: "post",
parameters: "chkImgFlag=" + a,
requestHeaders: {
RequestType: "ajax"
},
onSuccess: function (g) {
let h = g.responseJSON;
if (h == null) {
g.request.options.onFailure();
return
}
if (timer) {
clearTimeout(timer);
timer = null;
} else {
return;
}
_log("chkImg.do", h);
if (!h.retVal) {
_log("获取验证码失败:" + errorCode[h.errorNum]);
fin(null);
return
}
user.set("chkImgSrc", h.chkImgFilename);
fin(h.chkImgFilename);
},
onFailure: function (g) {
if (timer) {
clearTimeout(timer);
timer = null;
} else {
return;
}
_log("获取验证码失败");
fin(null);
}
})
});
}
var _username = "123"
var _password = "456"
async function _login(code) {
return new Promise((fin) => {
let timeout = 5000;
let timer = setTimeout(() => {
timer = null;
_log('login.do timed out after ' + timeout + ' ms');
fin(null);
}, timeout);
user.set("ksIdNo", _username);
user.set("sFlag", escape(escape(_username)));
let formData = new FormData();
formData.append("ksIDNO", _username);
formData.append("ksPwd", _password);
formData.append("clientTime", "");
formData.append("chkImgFlag", user.get("chkImgFlag"));
formData.append("chkImgCode", code);
formData.append("btnlogin", "登录");
new Ajax.Request(getURL("login.do"), {
method: "post",
parameters: new URLSearchParams(formData).toString(),
requestHeaders: {
RequestType: "ajax"
},
onSuccess: function (e) {
var f = e.responseJSON;
if (f == null) {
e.request.options.onFailure();
return
}
if (timer) {
clearTimeout(timer);
timer = null;
} else {
return;
}
clearChkimgCache();
_log("login.do", f);
if (f.retVal == 0) {
_log("登录失败:" + errorCode[f.errorNum]);
}
fin(f);
},
onFailure: function (g) {
if (timer) {
clearTimeout(timer);
timer = null;
} else {
return;
}
_log("登录请求失败");
fin(null);
}
})
});
}
async function _ocr(url) {
return new Promise((fin) => {
let xhr = new XMLHttpRequest();
url = encodeURIComponent(url);
xhr.open('GET', `http://localhost:5000/ocr?url=${url}`);
xhr.onload = function () {
if (xhr.status === 200) {
let ans = xhr.responseText;
if (ans.length == 4) {
fin(ans.toUpperCase());
} else {
_log("验证码OCR识别失败");
fin(null);
}
} else {
fin(null);
}
};
xhr.onerror = () => {
fin(null);
}
xhr.send();
});
}
async function _report(url, success, answer) {
return new Promise((fin) => {
let xhr = new XMLHttpRequest();
url = encodeURIComponent(url);
xhr.open('GET', `http://localhost:5000/report?url=${url}&success=${success}&answer=${answer}`);
xhr.onload = function () {
fin(null);
};
xhr.onerror = () => {
fin(null);
}
xhr.send();
});
}
var canExit = false;
var delayMs = 1;
async function loop() {
while (!canExit) {
let url = await _refreshImg();
if (url) {
let answer = await _ocr(url);
if (!answer) {
await _report(url, 0, "0000");
} else {
let ret = await _login(answer);
if (ret.errorNum === 306) {
//验证码错误
await _report(url, 0, answer);
} else if (ret.errorNum === 200) {
//验证码正确
await _report(url, 1, answer);
}
}
}
await _delay(delayMs);
}
}
function start(delay) {
canExit = false;
if (delay) {
delayMs = delay;
}
loop();
}
function stop() {
canExit = true;
}