1
zj
2025-06-18 69d7ae376a58c399c97ee42e5ff7a13860cb2b7e
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
package project.user.captcha.internal;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
 
import project.user.captcha.GeetestService;
 
/**
 * Geetest滑动图片验证
 */
public class GeetestServiceImpl implements GeetestService {
 
    // SDK版本编号
    protected final String verName = "3.3.0";
    // SD的语言类型
    protected final String sdkLang = "java";
 
    //极验验证API URL
    protected final String apiUrl = "http://api.geetest.com"; 
    protected final String baseUrl = "project.web.api.geetest.com";
    
    //register url
    protected final String registerUrl = "/register.php";
    //validate url
    protected final String validateUrl = "/validate.php"; 
    
    /**
     * 调试开关,是否输出调试日志
     */
    public boolean debugCode = true;
    
    /**
     * 验证初始化预处理
     * 用captchaID进行注册,更新challenge: 1/注册成功;0/注册失败;
     * @throws UnsupportedEncodingException 
     */
    public HashMap<String, String> preProcess(HashMap<String, String> data) {
        
        try {
 
            String userId = data.get("user_id");
            String geetestId = data.get("geetest_id");
            String geetestKey = data.get("geetest_key");
                
            String getUrl = this.apiUrl + this.registerUrl + "?";
            
            String param = "gt=" + geetestId;
            
            if (userId != null) {
                param = param + "&user_id=" + userId;
            }
            
            this.gtlog("GET_URL:" + getUrl + param);
            
            String result_str = this.readContentFromGet(getUrl + param);
            
            this.gtlog("register_result:" + result_str);
 
            if (32 == result_str.length()) {
                return this.getSuccessPreProcessRes(this.md5Encode(result_str + geetestKey), geetestId);
            } else {
                this.gtlog("gtServer register challenge failed");
                return this.getFailPreProcessRes(geetestId);
            }
            
        } catch (Throwable t) {
            this.gtlog(t.toString());
            this.gtlog("exception:preProcess project.web.api");
        }
        
        return null;
    }
 
    /**
     * 预处理失败后的返回格式串
     */
    private HashMap<String, String> getFailPreProcessRes(String geetestId) {
 
        Long rnd1 = Math.round(Math.random() * 100);
        Long rnd2 = Math.round(Math.random() * 100);
        String md5Str1 = md5Encode(rnd1 + "");
        String md5Str2 = md5Encode(rnd2 + "");
        String challenge = md5Str1 + md5Str2.substring(0, 2);
        
        HashMap<String, String> ret = new HashMap<String, String>();
        ret.put("success", "0");
        ret.put("gt", geetestId);
        ret.put("challenge", challenge);
        
        return ret;        
    }
 
    /**
     * 预处理成功后的标准串
     */
    private HashMap<String, String> getSuccessPreProcessRes(String challenge, String geetestId) {
        
        this.gtlog("challenge:" + challenge);
        
        HashMap<String, String> ret = new HashMap<String, String>();
        ret.put("success", "1");
        ret.put("gt", geetestId);
        ret.put("challenge", challenge);
 
        return ret;
    }
    
    /**
     * 服务正常的情况下使用的验证方式,,向gt-server进行二次验证,获取验证结果
     * 验证结果:1/验证成功;0/验证失败;
     */
    public int enhencedValidateRequest(HashMap<String, String> data) throws UnsupportedEncodingException {    
        
        String userId = data.get("user_id");
        String challenge = data.get("challenge");
        String validate = data.get("validate");
        String seccode = data.get("seccode");
        String geetestKey = data.get("geetest_key");
        
        if (!this.resquestIsLegal(challenge, validate, seccode)) {
            return 0;
        }
        
        this.gtlog("request legitimate");
        
        String host = baseUrl;
        String path = validateUrl;
        int port = 80;
        String query = String.format("seccode=%s&sdk=%s", seccode,
                (this.sdkLang + "_" + this.verName));
        
        String response = "";
        
        if (userId != ""){
            query = query + "&user_id=" + userId;
            userId = "";
        }
        
        this.gtlog(query);
        
        try {
            
            if (validate.length() <= 0) {
                return 0;
            }
 
            if (!this.checkResultByPrivate(challenge, validate, geetestKey)) {
                return 0;
            }
            
            this.gtlog("checkResultByPrivate");
            
            response = this.postValidate(host, path, query, port);
 
            this.gtlog("response: " + response);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        this.gtlog("md5: " + this.md5Encode(seccode));
 
        if (response.equals(this.md5Encode(seccode))) {
            return 1;
        } else {
            return 0;
        }    
    }
 
    /**
     * 貌似不是Post方式,后面重构时修改名字
     */
    protected String postValidate(String host, String path, String data, int port) throws Exception {
        
        String response = "error";
        
        InetAddress addr = InetAddress.getByName(host);
        Socket socket = new Socket(addr, port);
        BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
        wr.write("POST " + path + " HTTP/1.0\r\n");
        wr.write("Host: " + host + "\r\n");
        wr.write("Content-Type: application/x-www-form-urlencoded\r\n");
        wr.write("Content-Length: " + data.length() + "\r\n");
        // 以空行作为分割
        wr.write("\r\n"); 
        
        // 发送数据
        wr.write(data);
        wr.flush();
        
        // 读取返回信息
        BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
        String line;
        while ((line = rd.readLine()) != null) {
            response = line;
        }
        wr.close();
        rd.close();
        socket.close();
        return response;
    }
    
    /**
     * failback使用的验证方式
     * @return 验证结果,1表示验证成功0表示验证失败
     */
    public int failbackValidateRequest(HashMap<String, String> data) {
 
        this.gtlog("in failback validate");
        
        String challenge = data.get("challenge");
        String validate = data.get("validate");
        String seccode = data.get("seccode");
 
        if (!this.resquestIsLegal(challenge, validate, seccode)) {
            return 0;
        }
        
        this.gtlog("request legitimate");
 
        String[] validateStr = validate.split("_");
        String encodeAns = validateStr[0];
        String encodeFullBgImgIndex = validateStr[1];
        String encodeImgGrpIndex = validateStr[2];
 
        this.gtlog(String.format("encode----challenge:%s--ans:%s,bg_idx:%s,grp_idx:%s",
                challenge, encodeAns, encodeFullBgImgIndex, encodeImgGrpIndex));
            
        int decodeAns = this.decodeResponse(challenge, encodeAns);
        int decodeFullBgImgIndex = this.decodeResponse(challenge, encodeFullBgImgIndex);
        int decodeImgGrpIndex = this.decodeResponse(challenge, encodeImgGrpIndex);
 
        this.gtlog(String.format("decode----ans:%s,bg_idx:%s,grp_idx:%s", 
                decodeAns, decodeFullBgImgIndex, decodeImgGrpIndex));
 
        int validateResult = this.validateFailImage(decodeAns, decodeFullBgImgIndex, decodeImgGrpIndex);
 
        return validateResult;
    }
    
    private int validateFailImage(int ans, int full_bg_index, int img_grp_index) {
        
        // 容差值
        final int thread = 3;
        String full_bg_name = this.md5Encode(full_bg_index + "").substring(0, 9);
        String bg_name = this.md5Encode(img_grp_index + "").substring(10, 19);
        String answer_decode = "";
 
        // 通过两个字符串奇数和偶数位拼接产生答案位
        for (int i = 0; i < 9; i++) {
            if (i % 2 == 0) {
                answer_decode += full_bg_name.charAt(i);
            } else if (i % 2 == 1) {
                answer_decode += bg_name.charAt(i);
            } else {
                this.gtlog("exception");
            }
        }
 
        String x_decode = answer_decode.substring(4, answer_decode.length());
        
        // 16 to 10
        int x_int = Integer.valueOf(x_decode, 16);
 
        int result = x_int % 200;
        if (result < 40) {
            result = 40;
        }
 
        if (Math.abs(ans - result) <= thread) {
            return 1;
        } else {
            return 0;
        }
    }
    
    /**
     * 解码随机参数
     */
    private int decodeResponse(String challenge, String string) {
        
        if (string.length() > 100) {
            return 0;
        }
 
        int[] shuzi = new int[] { 1, 2, 5, 10, 50 };
        String chongfu = "";
        HashMap<String, Integer> key = new HashMap<String, Integer>();
        int count = 0;
 
        for (int i = 0; i < challenge.length(); i++) {
            String item = challenge.charAt(i) + "";
            if (chongfu.contains(item) == true) {
                continue;
            } else {
                int value = shuzi[count % 5];
                chongfu += item;
                count++;
                key.put(item, value);
            }
        }
 
        int res = 0;
        for (int j = 0; j < string.length(); j++) {
            res += key.get(string.charAt(j) + "");
        }
        res = res - decodeRandBase(challenge);
        return res;
    }
 
    /**
     * 输入的两位的随机数字,解码出偏移量
     */
    private int decodeRandBase(String challenge) {
 
        String base = challenge.substring(32, 34);
        ArrayList<Integer> tempArray = new ArrayList<Integer>();
 
        for (int i = 0; i < base.length(); i++) {
            char tempChar = base.charAt(i);
            Integer tempAscii = (int) (tempChar);
            Integer result = (tempAscii > 57) ? (tempAscii - 87) : (tempAscii - 48);
            tempArray.add(result);
        }
 
        int decodeRes = tempArray.get(0) * 36 + tempArray.get(1);
        return decodeRes;
    }
 
    /**
     * 发送请求,获取服务器返回结果
     */
    private String readContentFromGet(String getURL) throws IOException {
 
        URL getUrl = new URL(getURL);
        HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
        // 设置连接主机超时(单位:毫秒)
        connection.setConnectTimeout(2000);
        // 设置从主机读取数据超时(单位:毫秒)
        connection.setReadTimeout(2000);
 
        // 建立与服务器的连接,并未发送数据
        connection.connect();
        
        // 发送数据到服务器并使用Reader读取返回的数据
        StringBuffer sBuffer = new StringBuffer();
 
        InputStream inStream = null;
        byte[] buf = new byte[1024];
        inStream = connection.getInputStream();
        for (int n; (n = inStream.read(buf)) != -1;) {
            sBuffer.append(new String(buf, 0, n, "UTF-8"));
        }
        inStream.close();
        // 断开连接
        connection.disconnect();
 
        return sBuffer.toString();
    }
 
    /**
     * 输出debug信息,需要开启debugCode
     */
    public void gtlog(String message) {
        if (this.debugCode) {
            System.out.println("gtlog: " + message);
        }
    }
    
    /**
     * 判断一个表单对象值是否为空
     */
    protected boolean objIsEmpty(Object gtObj) {
        if (gtObj == null) {
            return true;
        }
        if (gtObj.toString().trim().length() == 0) {
            return true;
        }
        return false;
    }
 
    /**
     * 检查客户端的请求是否合法,三个只要有一个为空,则判断不合法
     */
    private boolean resquestIsLegal(String challenge, String validate, String seccode) {
        if (objIsEmpty(challenge)) {
            return false;
        }
        if (objIsEmpty(validate)) {
            return false;
        }
        if (objIsEmpty(seccode)) {
            return false;
        }
        return true;
    }
 
    protected boolean checkResultByPrivate(String challenge, String validate, String privateKey) {
        String encodeStr = this.md5Encode(privateKey + "geetest" + challenge);
        return validate.equals(encodeStr);
    }
 
    /**
     * md5 加密
     * 
     * @time 2014年7月10日 下午3:30:01
     * @param plainText
     * @return
     */
    private String md5Encode(String plainText) {
        String re_md5 = new String();
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes());
            byte b[] = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
 
            re_md5 = buf.toString();
 
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return re_md5;
    }
    
    /**
     * 获取版本信息
     */
    public String getVersionInfo() {
        return this.verName;
    }
        
}