-
Notifications
You must be signed in to change notification settings - Fork 55
/
Geek.cs
557 lines (531 loc) · 19.8 KB
/
Geek.cs
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
using System;
using System.Linq;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Threading;
using ServiceStack.Text;
namespace GeetestCrack
{
public class Geek
{
private JsonObject config = null;
private string referer = "";
private string userAgent = "[your user agent]";
Random rnd = new Random();
public string challenge
{
get
{
if (config != null)
return config.Get<string>("challenge");
else
return "";
}
private set
{
if (config == null)
config = new JsonObject();
config["challenge"] = value;
}
}
public string gt
{
get
{
if (config != null)
return config.Get<string>("gt");
else
return "";
}
set
{
if (config == null)
config = new JsonObject();
config["gt"] = value;
}
}
public Geek(string gt = "", string referer = "")
{
if (!string.IsNullOrEmpty(gt))
this.gt = gt;
if(!string.IsNullOrEmpty(referer))
this.referer = referer;
GetConfig();
}
public Geek(Uri uri)
{
string source = "";
HttpWebRequest hreq = (HttpWebRequest)HttpWebRequest.Create(uri);
HttpWebResponse hres = (HttpWebResponse)hreq.GetResponse();
using (StreamReader sr = new StreamReader(hres.GetResponseStream()))
source = sr.ReadToEnd();
hres.Close();
var obj = JsonObject.Parse(source);
this.gt = obj.Get<string>("gt");
this.referer = uri.Host;
challenge = obj.Get<string>("challenge");
GetConfig();
}
public void GetConfig()
{
if (string.IsNullOrEmpty(gt))
return;
var getUrl = string.Format("http://api.geetest.com/get.php?gt={0}&challenge={1}&product=embed&offline=false", gt, challenge);
Uri uri = new Uri(getUrl);
HttpWebRequest hreq = (HttpWebRequest)HttpWebRequest.Create(uri);
hreq.Timeout = 10000;
hreq.Referer = this.referer;
hreq.CookieContainer = new CookieContainer();
HttpWebResponse hres = (HttpWebResponse)hreq.GetResponse();
var cookies = hreq.CookieContainer.GetCookieHeader(uri);
string js = "";
using (StreamReader sr = new StreamReader(hres.GetResponseStream()))
js = sr.ReadToEnd();
hres.Close();
var sIndex = js.IndexOf("new Geetest({");
if (sIndex < 1)
return;
var eIndex = js.IndexOf("},true)", sIndex);
if (eIndex < sIndex)
return;
var json = js.Substring(sIndex + 12, eIndex - sIndex - 11);
config = JsonObject.Parse(json);
config["cookie"] = cookies;
config["initchallenge"] = config["challenge"];
//Console.WriteLine("config: " + config.Dump());
}
public bool RefreshConfig()
{
if (string.IsNullOrEmpty(gt))
return false;
var refreshUrl = string.Format("http://api.geetest.com/refresh.php?gt={0}&challenge={1}&callback=cb", gt, challenge);
Uri uri = new Uri(refreshUrl);
string js = "";
try
{
HttpWebRequest hreq = (HttpWebRequest)HttpWebRequest.Create(uri);
hreq.Timeout = 10000;
hreq.Referer = this.referer;
hreq.CookieContainer = new CookieContainer();
var cookies = config.Get("cookie");
hreq.CookieContainer.SetCookies(uri, cookies);
HttpWebResponse hres = (HttpWebResponse)hreq.GetResponse();
using (StreamReader sr = new StreamReader(hres.GetResponseStream()))
js = sr.ReadToEnd();
hres.Close();
}
catch(Exception ex)
{
//Console.WriteLine("refresh error: " + ex.Message);
challenge = config["initchallenge"];
}
if (string.IsNullOrEmpty(js))
return false;
var json = js.Substring(3, js.Length - 4);
var newParams = JsonObject.Parse(json);
config["ypos"] = newParams["ypos"];
config["challenge"] = newParams["challenge"];
config["bg"] = newParams["bg"];
config["fullbg"] = newParams["fullbg"];
config["slice"] = newParams["slice"];
//Console.WriteLine("config refreshed: " + config.Dump());
return true;
}
public JsonObject GetValidate()
{
// load images
var imgUrlBase = string.Format("http://{0}", config.Get<string[]>("staticservers")[0]);
Image bg = LoadImage(imgUrlBase + config.Get<string>("bg"));
Image full = LoadImage(imgUrlBase + config.Get<string>("fullbg"));
int ypos = config.Get<int>("ypos") + 3;
bg = AlignImage(bg, ypos);
full = AlignImage(full, ypos);
// get position
int xpos = GetPositionX(bg, full);
config["xpos"] = xpos.ToString();
var actions = GetActions(xpos);
//Console.WriteLine("xpos: " + xpos);
// try actions
var cookies = config.Get<string>("cookie");
foreach(var action in actions)
{
xpos = action.Get<int>("pos");
//Console.WriteLine("try pos: " + xpos);
string response = GetResponseString(xpos, challenge);
int passTime = action.Get<int>("passtime");
string actString = action.Get<string>("action");
int imgLoadTime = rnd.Next(0, 200) + 50;
Thread.Sleep(passTime - imgLoadTime);
var ajaxUrl = string.Format("{0}ajax.php?gt={1}&challenge={2}&imgload={3}&passtime={4}&userresponse={5}&a={6}&callback=cb",
config.Get<String>("apiserver"),
gt,
challenge,
imgLoadTime,
passTime,
response,
actString
);
var uri = new Uri(ajaxUrl);
var hreq = (HttpWebRequest)HttpWebRequest.Create(uri);
hreq.Timeout = 30000;
hreq.Referer = referer;
hreq.UserAgent = userAgent;
hreq.CookieContainer = new CookieContainer();
hreq.CookieContainer.SetCookies(uri, cookies);
var hres = (HttpWebResponse)hreq.GetResponse();
var js = "";
using (StreamReader sr = new StreamReader(hres.GetResponseStream()))
js = sr.ReadToEnd();
hres.Close();
var json = js.Substring(3, js.Length - 4);
//Console.WriteLine(json);
var result = JsonObject.Parse(json);
result["challenge"] = challenge;
if (result.Get<int>("success") == 1)
{
// success
return result;
}
else if (result.Get("message") == "abuse")
{
// return abuse ,refresh config and try again
return result;
}
else if (result.Get("message") == "forbidden")
{
// try next action
}
}
// failed
return null;
}
private List<JsonObject> GetActions(int xpos)
{
var acts = new List<JsonObject>();
for (var i = 0; i < 4; i++)
{
JsonObject act = new JsonObject();
act["pos"] = xpos.ToString();
var action = generate(xpos);
act["action"] = encrypt(action);
int pt = 0;
foreach (var a in action)
{
pt += a[2];
}
act["passtime"] = pt.ToString();
acts.Add(act);
}
return acts;
}
private List<int[]> generate2(int xpos)
{
var sx = rnd.Next(15, 30);
var sy = rnd.Next(15, 30);
var arr = new List<int[]>();
arr.Add(new int[] { sx, sy, 0 });
var maxCount = 100; // max len 100
var mds = 0.25;
var speed = rnd.NextDouble() * 0.3 + 0.05;
var ds = rnd.NextDouble() * 0.5 * mds;
var dsign = 1;
double x = 0;
double lx = xpos - x;
while (Math.Abs(lx) > 1.0 && maxCount-- > 0)
{
var rn = rnd.NextDouble();
var dt = rn * 100 + 10;
if (rn < 0.2)
{
dt += rn * 200;
}
speed += ds * dsign;
if (speed > 0.25)
speed = 0.25;
rn = rnd.NextDouble();
if (rn < (speed / 0.25))
dsign = -dsign;
ds = rnd.NextDouble() * mds * 0.5;
if (Math.Abs(lx) < 10)
{
speed *= lx / 20;
}
else if (x < xpos / 3)
{
speed *= (x / xpos + 1.0);
}
if (speed < 0)
speed = -speed;
double dx = speed * dt;
if (Math.Abs(dx) < 0.6)
continue;
x += dx;
if (x - xpos > 0 && dx > 0)
{
speed = -speed;
x -= 2 * dx;
}
rn = rnd.NextDouble();
double dy = 0;
if (rn < 0.1 && dt > 70)
{
dy = rn * 30;
if (rn < 0.05)
dy = -rn * 60;
}
arr.Add(new int[] { (int)(dx + 0.5), (int)(dy + 0.5), (int)(dt + 0.5) });
lx = xpos - x;
}
var dtlast = 500.0 * rnd.NextDouble() + 100.0;
arr.Add(new int[] { 0, 0, (int)(dtlast) });
return arr;
}
private List<int[]> generate(int xpos)
{
var sx = rnd.Next(15, 30);
var sy = rnd.Next(15, 30);
var arr = new List<int[]>();
arr.Add(new int[] { sx, sy, 0 });
var maxCount = 100; // max len 100
double x = 0;
double lx = xpos - x;
while (Math.Abs(lx) > 0.8 && maxCount-- > 0)
{
var rn = rnd.NextDouble();
var dx = rn * lx * 0.6;
if (Math.Abs(dx) < 0.5)
continue;
var dt = rnd.NextDouble() * (rn * 80 + 50)+ 10;
rn = rnd.NextDouble();
double dy = 0;
if (rn < 0.2 && dx > 10) //
{
dy = rn * 20.0;
if (rn < 0.05)
dy = -rn * 80;
}
x += dx;
arr.Add(new int[] { (int)(dx + 0.5), (int)(dy + 0.5), (int)(dt + 0.5) });
lx = xpos - x;
}
var dtlast = 500.0 * rnd.NextDouble() + 100.0;
arr.Add(new int[] { 0, 0, (int)(dtlast) });
return arr;
}
private List<int[]> diff(List<int[]> arr)
{
List<int[]> b = new List<int[]>();
for (var c = 0; c < arr.Count - 1; c++)
{
int[] e = new int[3];
e[0] = (arr[c + 1][0] - arr[c][0]);
e[1] = (arr[c + 1][1] - arr[c][1]);
e[2] = (arr[c + 1][2] - arr[c][2]);
if (e[0] == 0 && e[1] == 0 && e[2] == 0)
continue;
b.Add(e);
}
return b;
}
private string encode(int n)
{
const string b = "()*,-./0123456789:?@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqr";
var c = b.Length;
char d = (char)0;
var e = Math.Abs(n);
var f = e / c;
if (f >= c)
f = c - 1;
if (f != 0)
{
d = b[f];
e %= c;
}
var g = "";
if (n < 0)
g += "!";
if (d != 0)
g += "$";
return g + (d == 0 ? "": d.ToString()) + b[e];
}
private char replace(int[] a2)
{
var b = new int[][] { new int[] { 1, 0 }, new int[] { 2, 0 }, new int[] { 1, -1 },
new int[] { 1, 1 }, new int[] { 0, 1 }, new int[] { 0, -1 },
new int[] { 3, 0 }, new int[] { 2, -1 }, new int[] { 2, 1 } };
var c = "stuvwxyz~";
for (var d = 0; d < b.Length; d++)
if (a2[0] == b[d][0] && a2[1] == b[d][1])
return c[d];
return '\0';
}
private string encrypt(List<int[]> action)
{
var d = action;// diff(action);
string dx = "", dy = "", dt = "";
for(var j=0; j<d.Count; j++)
{
var b = replace(d[j]);
if(b != 0){
dy += b.ToString();
}
else
{
dx += (encode(d[j][0]));
dy += (encode(d[j][1]));
}
dt += (encode(d[j][2]));
}
return dx + "!!" + dy + "!!" + dt;
}
private string GetResponseString(int posx, string challenge)
{
var ct = challenge.Substring(32);
if (ct.Length < 2)
return "";
int [] d = new int[ct.Length];
for (var e = 0; e < ct.Length; e++)
{
var f = ct[e];
if (f > 57)
d[e] = f - 87;
else
d[e] = f - 48;
}
var c = 36 * d[0] + d[1];
var g = posx + c;
ct = challenge.Substring(0, 32);
var i = new List<List<char>>(5);
for(var ii =0; ii<5; ii++)
{
i.Add(new List<char>());
}
Dictionary<char, int> j = new Dictionary<char, int>();
int k = 0;
foreach (var h in ct)
{
if (!j.Keys.Contains(h) || j[h] != 1)
{
j[h] = 1;
i[k].Add(h);
k++;
k %= 5;
}
}
int n = g, o = 4;
var p = "";
var q = new int[] { 1, 2, 5, 10, 50 }.ToList(); ;
Random rnd = new Random();
while (n > 0)
{
if (n - q[o] >= 0)
{
int m = rnd.Next(0, i[o].Count);
p += i[o][m];
n -= q[o];
}
else
{
i.RemoveAt(o);
q.RemoveAt(o);
o--;
}
}
return p;
}
private Image LoadImage(string url)
{
Image image;
HttpWebRequest hreq = (HttpWebRequest)HttpWebRequest.Create(url);
HttpWebResponse hres = (HttpWebResponse)hreq.GetResponse();
using (var stream = hres.GetResponseStream())
{
image = Image.FromStream(stream);
}
hres.Close();
return image;
}
private Image AlignImage(Image img, int ypos = 0, int height = 52)
{
const int width = 260;
Bitmap bmp = new Bitmap(width, height);
var pos = new int[] {157, 145, 265, 277,181, 169, 241, 253, 109, 97, 289, 301, 85, 73, 25, 37, 13, 1, 121, 133, 61, 49, 217, 229, 205, 193,
145, 157, 277, 265, 169, 181, 253, 241, 97, 109, 301, 289, 73, 85, 37, 25, 1, 13, 133, 121, 49, 61, 229, 217, 193, 205};
int dx = 0, sy = 58, dy = 0;
var g = Graphics.FromImage(bmp);
for (var i = 0; i < pos.Length; i++) {
g.DrawImage(img, new Rectangle(dx, dy - ypos, 10, 58), new Rectangle(pos[i], sy, 10, 58), GraphicsUnit.Pixel);
dx += 10;
if (dx == width)
{
dx = 0;
dy = 58;
sy = 0;
}
}
g.Dispose();
return bmp;
}
private int GetPositionX(Image imgBg, Image imgFullBg, Image imgSlice = null)
{
var bg = new Bitmap(imgBg);
var full = new Bitmap(imgFullBg);
Rectangle rect = new Rectangle(0, 0, bg.Width, bg.Height);
const int bytesCount = 4;
var bgData = bg.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
var fullData = full.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
int xpos = -1;
unsafe
{
byte * pBg =(byte *)bgData.Scan0;
byte * pFull = (byte *)fullData.Scan0;
//sub 2 images
for (var i = 0; i < bgData.Stride * bgData.Height; i+=4)
{
pBg[i] = (byte)Math.Abs((int)pBg[i] - pFull[i]);
pBg[i + 1] = (byte)Math.Abs((int)pBg[i + 1] - pFull[i + 1]);
pBg[i + 2] = (byte)Math.Abs((int)pBg[i + 2] - pFull[i + 2]);
}
var w = bgData.Width;
// Roberts edge detect and calculate histgram
int[] histgram = new int[w];
int[] histSum = new int[w];
for (var y = 0; y < bgData.Height - 1; y++)
{
for (var x = 0; x < w - 1; x++)
{
var i00 = (x + y * w);
var i11 = (i00 + w + 1) * bytesCount;
var i01 = (i00 + 1) * bytesCount;
var i10 = (i00 + w) * bytesCount;
i00 *= bytesCount;
pFull[i00] = (byte)(Math.Abs(pBg[i00] - pBg[i11]) + Math.Abs(pBg[i01] - pBg[i10])); // b
pFull[i00 + 1] = (byte)(Math.Abs(pBg[i00 + 1] - pBg[i11 + 1]) + Math.Abs(pBg[i01 + 1] - pBg[i10 + 1])); // g
pFull[i00 + 2] = (byte)(Math.Abs(pBg[i00 + 2] - pBg[i11 + 2]) + Math.Abs(pBg[i01 + 2] - pBg[i10 + 2])); // r
histgram[x] += pFull[i00] + pFull[i00 + 1] + pFull[i00 + 2];
}
}
// find xpos
int ww = 48, maxValue = -1;
for (var i = 0; i < ww; i++)
histSum[0] += histgram[i];
for (var x = 1; x < w - ww; x++)
{
histSum[x] = histSum[x - 1] + histgram[x + ww - 1] - histgram[x - 1];
if (histSum[x] > maxValue)
{
xpos = x;
maxValue = histSum[x];
}
}
} // exit unsafe
bg.UnlockBits(bgData);
full.UnlockBits(fullData);
//offset 6 pixels
return xpos - 6;
}
}
}