package com.dlbridge.jadp.plugins.geetest; 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.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 org.apache.log4j.Logger; public class GeetestLib { private static Logger logger = Logger.getLogger(GeetestLib.class); protected final String verName = "3.2.0"; protected final String sdkLang = "java"; protected final String apiUrl = "http://api.geetest.com"; protected final String baseUrl = "api.geetest.com"; protected final String registerUrl = "/register.php"; protected final String validateUrl = "/validate.php"; public static final String fn_geetest_challenge = "geetest_challenge"; public static final String fn_geetest_validate = "geetest_validate"; public static final String fn_geetest_seccode = "geetest_seccode"; private String captchaId = ""; private String privateKey = ""; private String userId = ""; private String responseStr = ""; public boolean debugCode = true; public String gtServerStatusSessionKey = "gt_server_status"; public GeetestLib(String captchaId, String privateKey) { this.captchaId = captchaId; this.privateKey = privateKey; } public String getResponseStr() { return this.responseStr; } public String getVersionInfo() { return "3.2.0"; } private String getFailPreProcessRes() { Long rnd1 = Math.round(Math.random() * 100.0D); Long rnd2 = Math.round(Math.random() * 100.0D); String md5Str1 = this.md5Encode(rnd1 + ""); String md5Str2 = this.md5Encode(rnd2 + ""); String challenge = md5Str1 + md5Str2.substring(0, 2); return String.format("{\"success\":%s,\"gt\":\"%s\",\"challenge\":\"%s\"}", 0, this.captchaId, challenge); } private String getSuccessPreProcessRes(String challenge) { this.gtlog("challenge:" + challenge); return String.format("{\"success\":%s,\"gt\":\"%s\",\"challenge\":\"%s\"}", 1, this.captchaId, challenge); } public int preProcess() { if (this.registerChallenge() != 1) { this.responseStr = this.getFailPreProcessRes(); return 0; } else { return 1; } } public int preProcess(String userid) { this.userId = userid; return this.preProcess(); } private int registerChallenge() { try { String GET_URL = "http://api.geetest.com/register.php?gt=" + this.captchaId; if (this.userId != "") { GET_URL = GET_URL + "&user_id=" + this.userId; this.userId = ""; } this.gtlog("GET_URL:" + GET_URL); String result_str = this.readContentFromGet(GET_URL); this.gtlog("register_result:" + result_str); if (32 == result_str.length()) { this.responseStr = this.getSuccessPreProcessRes(this.md5Encode(result_str + this.privateKey)); return 1; } else { this.gtlog("gtServer register challenge failed"); return 0; } } catch (Exception var3) { this.gtlog("exception:register api"); return 0; } } 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(); StringBuffer sBuffer = new StringBuffer(); InputStream inStream = null; byte[] buf = new byte[1024]; inStream = connection.getInputStream(); int n; while((n = inStream.read(buf)) != -1) { sBuffer.append(new String(buf, 0, n, "UTF-8")); } inStream.close(); connection.disconnect(); return sBuffer.toString(); } protected boolean objIsEmpty(Object gtObj) { if (gtObj == null) { return true; } else { return gtObj.toString().trim().length() == 0; } } private boolean resquestIsLegal(String challenge, String validate, String seccode) { if (this.objIsEmpty(challenge)) { return false; } else if (this.objIsEmpty(validate)) { return false; } else { return !this.objIsEmpty(seccode); } } public int enhencedValidateRequest(String challenge, String validate, String seccode) { if (!this.resquestIsLegal(challenge, validate, seccode)) { return 0; } else { this.gtlog("request legitimate"); String host = "api.geetest.com"; String path = "/validate.php"; int port = 80; Object[] var10001 = new Object[]{seccode, null}; StringBuilder var10004 = new StringBuilder(); this.getClass(); var10004 = var10004.append("java").append("_"); this.getClass(); var10001[1] = var10004.append("3.2.0").toString(); String query = String.format("seccode=%s&sdk=%s", var10001); String response = ""; if (this.userId != "") { query = query + "&user_id=" + this.userId; this.userId = ""; } this.gtlog(query); try { if (validate.length() <= 0) { return 0; } if (!this.checkResultByPrivate(challenge, validate)) { return 0; } this.gtlog("checkResultByPrivate"); response = this.postValidate(host, path, query, port); this.gtlog("response: " + response); } catch (Exception var10) { logger.error(var10.getMessage(), var10); } this.gtlog("md5: " + this.md5Encode(seccode)); return response.equals(this.md5Encode(seccode)) ? 1 : 0; } } public int enhencedValidateRequest(String challenge, String validate, String seccode, String userid) { this.userId = userid; return this.enhencedValidateRequest(challenge, validate, seccode); } public int failbackValidateRequest(String challenge, String validate, String seccode) { this.gtlog("in failback validate"); if (!this.resquestIsLegal(challenge, validate, seccode)) { return 0; } else { 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) { 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 = answer_decode + full_bg_name.charAt(i); } else if (i % 2 == 1) { answer_decode = answer_decode + bg_name.charAt(i); } else { this.gtlog("exception"); } } String x_decode = answer_decode.substring(4, answer_decode.length()); int x_int = Integer.valueOf(x_decode, 16); int result = x_int % 200; if (result < 40) { result = 40; } if (Math.abs(ans - result) <= 3) { return 1; } else { return 0; } } private int decodeResponse(String challenge, String string) { if (string.length() > 100) { return 0; } else { int[] shuzi = new int[]{1, 2, 5, 10, 50}; String chongfu = ""; HashMap key = new HashMap(); int count = 0; int res; for(res = 0; res < challenge.length(); ++res) { String item = challenge.charAt(res) + ""; if (!chongfu.contains(item)) { int value = shuzi[count % 5]; chongfu = chongfu + item; ++count; key.put(item, value); } } res = 0; for(int j = 0; j < string.length(); ++j) { res += (Integer)key.get(string.charAt(j) + ""); } res -= this.decodeRandBase(challenge); return res; } } private int decodeRandBase(String challenge) { String base = challenge.substring(32, 34); ArrayList tempArray = new ArrayList(); int decodeRes; for(decodeRes = 0; decodeRes < base.length(); ++decodeRes) { char tempChar = base.charAt(decodeRes); Integer tempAscii = Integer.valueOf(tempChar); Integer result = tempAscii > 57 ? tempAscii - 87 : tempAscii - 48; tempArray.add(result); } decodeRes = (Integer)tempArray.get(0) * 36 + (Integer)tempArray.get(1); return decodeRes; } public void gtlog(String message) { if (this.debugCode) { System.out.println("gtlog: " + message); } } protected boolean checkResultByPrivate(String challenge, String validate) { String encodeStr = this.md5Encode(this.privateKey + "geetest" + challenge); return validate.equals(encodeStr); } 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; String line; for(rd = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8")); (line = rd.readLine()) != null; response = line) { } wr.close(); rd.close(); socket.close(); return response; } private String md5Encode(String plainText) { String re_md5 = new String(); try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(plainText.getBytes()); byte[] b = md.digest(); StringBuffer buf = new StringBuffer(""); for(int offset = 0; offset < b.length; ++offset) { int 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 var8) { logger.error(var8.getMessage(), var8); } return re_md5; } }