Guest User

Untitled

a guest
Feb 4th, 2023
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.87 KB | None | 0 0
  1.     @TargetApi(Build.VERSION_CODES.KITKAT)
  2.     @SuppressWarnings("BusyWait")
  3.     @Override
  4.     public ReadCaptchaResult onReadCaptcha(ReadCaptchaData data) throws HttpException, InvalidResponseException {
  5.         AtomicReference<String> captcha = new AtomicReference<>("");
  6.  
  7.         WebView webView = data.webView;
  8.         webView.post(() -> {
  9.             WebSettings webSettings = webView.getSettings();
  10.             webSettings.setJavaScriptEnabled(true);
  11.             webView.setWebViewClient(new WebViewClient() {
  12.                 @TargetApi(Build.VERSION_CODES.KITKAT)
  13.                 public boolean shouldOverrideUrlLoading(WebView view, String url) {
  14.                     view.post(() -> view.loadUrl(url));
  15.                     return false;
  16.                 }
  17.             });
  18.         });
  19.  
  20.         boolean banned = REQUIREMENT_BANNED.equals(data.requirement);
  21.         if (!banned) {
  22.             String token = data.captchaPass != null ? data.captchaPass[0] : null;
  23.             String pin = data.captchaPass != null ? data.captchaPass[1] : null;
  24.             String captchaPassCookie = null;
  25.             if (token != null || pin != null) {
  26.                 if (getCaptchaPassData(token, pin).equals(lastCaptchaPassData)) {
  27.                     captchaPassCookie = lastCaptchaPassCookie;
  28.                 } else {
  29.                     captchaPassCookie = readCaptchaPass(data, token, pin);
  30.                 }
  31.             }
  32.             if (captchaPassCookie != null) {
  33.                 CaptchaData captchaData = new CaptchaData();
  34.                 captchaData.put(CAPTCHA_DATA_KEY_PASS_COOKIE, captchaPassCookie);
  35.                 return new ReadCaptchaResult(CaptchaState.PASS, captchaData)
  36.                         .setValidity(FourchanChanConfiguration.Captcha.Validity.LONG_LIFETIME);
  37.             }
  38.         }
  39.         String captchaType = banned ? FourchanChanConfiguration.CAPTCHA_TYPE_RECAPTCHA_2 : data.captchaType;
  40.         FourchanChanConfiguration configuration = FourchanChanConfiguration.get(this);
  41.         FourchanChanLocator locator = FourchanChanLocator.get(this);
  42.         ReadCaptchaResult result;
  43.         if (FourchanChanConfiguration.CAPTCHA_TYPE_4CHAN_CAPTCHA.equals(captchaType)) {
  44.             if (data.mayShowLoadButton) {
  45.                 return new ReadCaptchaResult(CaptchaState.NEED_LOAD, null);
  46.             }
  47.             String threadNumber = data.requirement == null ? data.threadNumber : "1";
  48.             Uri.Builder builder = locator.createSysUri("captcha").buildUpon()
  49.                     .appendQueryParameter("board", data.boardName);
  50.             if (threadNumber != null) {
  51.                 builder.appendQueryParameter("thread_id", threadNumber);
  52.             }
  53.             Uri uri = builder.build();
  54.             String challenge;
  55.             Bitmap image;
  56.             Bitmap background;
  57.             int wait = 2000;
  58.             while (true) {
  59.                 try {
  60.                     webView.post(() -> webView.loadUrl(uri.toString()));
  61.                     // waiting for the result above
  62.                     try {
  63.                         Thread.sleep(3000);
  64.                     } catch (InterruptedException e) {
  65.                         throw new RuntimeException(e);
  66.                     }
  67.                     webView.post(() -> webView.evaluateJavascript(
  68.                             "(function() { return document.querySelector('pre').textContent; })();",
  69.                             captcha::set));
  70.                     // wait for the result above
  71.                     try {
  72.                         Thread.sleep(1000);
  73.                     } catch (InterruptedException e) {
  74.                         throw new RuntimeException(e);
  75.                     }
  76.                     JSONObject jsonObject = new JSONObject(captcha.get().substring(1, captcha.get().length() - 1).replace("\\\"", "\""));
  77.                     String error = jsonObject.optString("error");
  78.                     if ("You have to wait a while before doing this again".equals(error)) {
  79.                         try {
  80.                             Thread.sleep(wait);
  81.                         } catch (InterruptedException e) {
  82.                             throw new HttpException(0, null);
  83.                         }
  84.                         wait += 1000;
  85.                     } else {
  86.                         challenge = jsonObject.getString("challenge");
  87.                         byte[] imageBytes = Base64.decode(jsonObject.getString("img"), 0);
  88.                         byte[] backgroundBytes = Base64.decode(jsonObject.optString("bg"), 0);
  89.                         image = imageBytes.length == 0 ? null
  90.                                 : BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
  91.                         background = backgroundBytes.length == 0 ? null
  92.                                 : BitmapFactory.decodeByteArray(backgroundBytes, 0, backgroundBytes.length);
  93.                         break;
  94.                     }
  95.                 } catch (JSONException e) {
  96.                     throw new InvalidResponseException(e);
  97.                 }
  98.             }
Add Comment
Please, Sign In to add comment