| 30209 |
amit.gupta |
1 |
package com.smartdukaan.cron.scheduled;
|
|
|
2 |
|
|
|
3 |
import nu.pattern.OpenCV;
|
|
|
4 |
import org.apache.commons.io.FileUtils;
|
|
|
5 |
import org.apache.logging.log4j.LogManager;
|
|
|
6 |
import org.apache.logging.log4j.Logger;
|
|
|
7 |
import org.opencv.core.Mat;
|
| 34413 |
amit.gupta |
8 |
import org.opencv.core.Size;
|
| 30209 |
amit.gupta |
9 |
import org.opencv.imgcodecs.Imgcodecs;
|
|
|
10 |
import org.opencv.imgproc.Imgproc;
|
| 37423 |
amit |
11 |
import org.json.JSONObject;
|
| 30209 |
amit.gupta |
12 |
import org.openqa.selenium.*;
|
|
|
13 |
import org.openqa.selenium.chrome.ChromeDriver;
|
|
|
14 |
import org.openqa.selenium.chrome.ChromeOptions;
|
|
|
15 |
import org.openqa.selenium.interactions.Actions;
|
|
|
16 |
import org.openqa.selenium.support.ui.ExpectedConditions;
|
|
|
17 |
import org.openqa.selenium.support.ui.WebDriverWait;
|
| 37472 |
amit |
18 |
import com.smartdukaan.cron.monitored.ImeiActivationGauges;
|
|
|
19 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 30373 |
amit.gupta |
20 |
import org.springframework.stereotype.Component;
|
| 30209 |
amit.gupta |
21 |
|
|
|
22 |
import javax.imageio.ImageIO;
|
|
|
23 |
import java.awt.image.BufferedImage;
|
|
|
24 |
import java.io.File;
|
|
|
25 |
import java.time.LocalDate;
|
|
|
26 |
import java.time.format.DateTimeFormatter;
|
|
|
27 |
import java.util.HashMap;
|
|
|
28 |
import java.util.List;
|
|
|
29 |
import java.util.Map;
|
|
|
30 |
import java.util.concurrent.TimeUnit;
|
|
|
31 |
import java.util.stream.Collectors;
|
|
|
32 |
|
| 30373 |
amit.gupta |
33 |
@Component
|
| 34416 |
amit.gupta |
34 |
public class CheckRealmeWarrantyTask {
|
| 30209 |
amit.gupta |
35 |
|
| 37472 |
amit |
36 |
@Autowired
|
|
|
37 |
private ImeiActivationGauges gauges;
|
|
|
38 |
|
|
|
39 |
private static final String BRAND = "Realme";
|
|
|
40 |
|
| 34416 |
amit.gupta |
41 |
private static final Logger LOGGER = LogManager.getLogger(CheckRealmeWarrantyTask.class);
|
| 30209 |
amit.gupta |
42 |
|
| 36253 |
amit |
43 |
static {
|
| 36306 |
amit |
44 |
try {
|
|
|
45 |
OpenCV.loadShared();
|
|
|
46 |
} catch (Throwable t) {
|
|
|
47 |
LogManager.getLogger(CheckRealmeWarrantyTask.class).warn("OpenCV not available on this platform: {}", t.getMessage());
|
|
|
48 |
}
|
| 36253 |
amit |
49 |
}
|
|
|
50 |
|
| 32146 |
amit.gupta |
51 |
public Map<String, LocalDate> checkWarranty(List<String> imeis) {
|
|
|
52 |
Map<String, LocalDate> dateMap = new HashMap<>();
|
|
|
53 |
LOGGER.info("Initiating webdriver...");
|
| 30209 |
amit.gupta |
54 |
|
| 32146 |
amit.gupta |
55 |
Map<String, Object> prefsMap = new HashMap<>();
|
|
|
56 |
prefsMap.put("profile.default_content_settings.popups", 0);
|
|
|
57 |
prefsMap.put("download.prompt", false);
|
|
|
58 |
prefsMap.put("download.directory_upgrade", true);
|
| 34413 |
amit.gupta |
59 |
System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver");
|
| 32146 |
amit.gupta |
60 |
//I need this
|
| 34413 |
amit.gupta |
61 |
//System.setProperty("webdriver.chrome.driver", "/Users/amit/cft/chromedriver/chromedriver");
|
| 32146 |
amit.gupta |
62 |
ChromeOptions options = new ChromeOptions();
|
| 34413 |
amit.gupta |
63 |
//options.setBinary("/Users/amit/cft/chrome/Google Chrome for Testing.app/Contents/MacOS/Google Chrome For Testing");
|
| 32146 |
amit.gupta |
64 |
options.setExperimentalOption("prefs", prefsMap);
|
|
|
65 |
//options.setExperimentalOption("detach", true);
|
|
|
66 |
options.addArguments("--headless");
|
| 34419 |
amit.gupta |
67 |
options.addArguments("--user-agent=\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36\"");
|
| 30209 |
amit.gupta |
68 |
|
| 32146 |
amit.gupta |
69 |
options.addArguments("--no-sandbox");
|
|
|
70 |
options.addArguments("start-maximized");
|
|
|
71 |
options.addArguments("disable-infobars");
|
|
|
72 |
options.addArguments("--disable-extensions");
|
|
|
73 |
options.addArguments("--force-device-scale-factor=1");
|
|
|
74 |
options.addArguments("--test-type");
|
| 37446 |
amit |
75 |
// Headless needs no GPU, yet chrome still forks a gpu-process per browser (6 were
|
|
|
76 |
// alive across the fleet). --disable-gpu removes them; --disable-dev-shm-usage
|
|
|
77 |
// keeps chrome off the small /dev/shm on this box. Both are pure overhead removal,
|
|
|
78 |
// no behaviour change. This box co-hosts a 9.4GB tomcat with ~3GB available and
|
|
|
79 |
// has been OOM-killed twice this month, and tomcat was the victim both times.
|
|
|
80 |
options.addArguments("--disable-gpu");
|
|
|
81 |
options.addArguments("--disable-dev-shm-usage");
|
|
|
82 |
options.addArguments("--disable-software-rasterizer");
|
| 34420 |
amit.gupta |
83 |
options.addArguments(
|
|
|
84 |
"--disable-blink-features=AutomationControlled",
|
|
|
85 |
"--disable-infobars",
|
|
|
86 |
"--disable-popup-blocking",
|
|
|
87 |
"--disable-notifications",
|
|
|
88 |
"--disable-extensions"
|
|
|
89 |
);
|
| 30209 |
amit.gupta |
90 |
|
| 34679 |
amit.gupta |
91 |
WebDriver driver = null;
|
| 30209 |
amit.gupta |
92 |
|
| 34679 |
amit.gupta |
93 |
try {
|
|
|
94 |
driver = new ChromeDriver(options);
|
|
|
95 |
driver.manage().timeouts().setScriptTimeout(6, TimeUnit.SECONDS);
|
|
|
96 |
driver.manage().window().setSize(new Dimension(1600, 900));
|
|
|
97 |
driver.manage().window().maximize();
|
|
|
98 |
// Deleting all the cookies
|
|
|
99 |
driver.manage().deleteAllCookies();
|
|
|
100 |
// Specifiying pageLoadTimeout and Implicit wait
|
|
|
101 |
driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
|
|
|
102 |
driver.manage().timeouts().implicitlyWait(9, TimeUnit.SECONDS);
|
|
|
103 |
|
|
|
104 |
int mainCount = 0;
|
|
|
105 |
List<String> remainingImeis = imeis;
|
|
|
106 |
WebDriverWait wait10Sec = new WebDriverWait(driver, 15);
|
|
|
107 |
WebDriverWait wait5Sec = new WebDriverWait(driver, 4);
|
|
|
108 |
WebElement slideButton;
|
|
|
109 |
Actions actionProvider = new Actions(driver);
|
|
|
110 |
do {
|
|
|
111 |
driver.get("https://www.realme.com/in/support/phonecheck");
|
| 37423 |
amit |
112 |
installResponseCapture(driver);
|
| 34424 |
amit.gupta |
113 |
/*try {
|
| 34415 |
amit.gupta |
114 |
driver.findElement(By.cssSelector(".cp-cookie-tip a.close")).click();
|
|
|
115 |
} catch (Exception e) {
|
|
|
116 |
try {
|
| 37423 |
amit |
117 |
LOGGER.info("Cookie not found");
|
| 34415 |
amit.gupta |
118 |
} catch (Exception e1) {
|
|
|
119 |
}
|
| 34424 |
amit.gupta |
120 |
}*/
|
| 34415 |
amit.gupta |
121 |
|
| 34679 |
amit.gupta |
122 |
for (int i = 0; i < remainingImeis.size(); i++) {
|
|
|
123 |
String imei = remainingImeis.get(i);
|
|
|
124 |
try {
|
| 37423 |
amit |
125 |
LOGGER.info("Starting fresh with new IMEI " + imei);
|
|
|
126 |
// realme rebuilt this page: the field is no longer .sn-input and the
|
|
|
127 |
// submit is no longer div.check-btn but an icon inside the same box.
|
|
|
128 |
if (driver.findElement(By.cssSelector(IMEI_INPUT)).getAttribute("value").length() > 0) {
|
| 34679 |
amit.gupta |
129 |
//System.out.println("Darn.. leaving" + driver.findElement(By.className("el-input__inner")).getAttribute("value"));
|
|
|
130 |
driver.get("https://www.realme.com/in/support/phonecheck");
|
| 37423 |
amit |
131 |
installResponseCapture(driver);
|
| 34679 |
amit.gupta |
132 |
break;
|
|
|
133 |
}
|
| 37423 |
amit |
134 |
driver.findElement(By.cssSelector(IMEI_INPUT)).sendKeys(imei);
|
| 30209 |
amit.gupta |
135 |
|
| 34679 |
amit.gupta |
136 |
//driver.findElement(By.className("el-button--primary")).click();
|
| 37423 |
amit |
137 |
driver.findElement(By.cssSelector(SUBMIT_ICON)).click();
|
| 34413 |
amit.gupta |
138 |
|
| 34679 |
amit.gupta |
139 |
// Perform click-and-hold action on the element
|
|
|
140 |
int counter = 1;
|
|
|
141 |
boolean captchaNotBroken = true;
|
| 37435 |
amit |
142 |
// release() is only reached on the moveByOffset > 0 path, so an attempt
|
|
|
143 |
// that fails detection or calibration leaves the button DOWN -- measured
|
|
|
144 |
// at 77% of realme presses in production. Actions is one instance per
|
|
|
145 |
// driver, created outside the imei loop, so the stuck press leaks into the
|
|
|
146 |
// next attempt and the next imei: the following clickAndHold lands on an
|
|
|
147 |
// already-held button and the widget never repositions the handle.
|
|
|
148 |
boolean pressed = false;
|
| 34679 |
amit.gupta |
149 |
do {
|
|
|
150 |
String fileName = "/tmp/oppo-simple-i-" + counter + " " + System.currentTimeMillis() + ".png";
|
|
|
151 |
try {
|
| 37423 |
amit |
152 |
LOGGER.info("Starting Do");
|
| 34679 |
amit.gupta |
153 |
//Thread.sleep(5000);
|
|
|
154 |
wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("canvas")));
|
| 34420 |
amit.gupta |
155 |
|
| 34679 |
amit.gupta |
156 |
wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_bg_')]")));
|
|
|
157 |
wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_wrapper_')]")));
|
|
|
158 |
wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_slider_')]")));
|
|
|
159 |
slideButton = driver.findElement(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_slider_')]"));
|
|
|
160 |
//System.out.println("Slide button - " + slideButton);
|
| 37423 |
amit |
161 |
LOGGER.info("Margin Left before- " + slideButton.getCssValue("margin-left"));
|
| 37472 |
amit |
162 |
gauges.captchaShown(BRAND);
|
| 34679 |
amit.gupta |
163 |
actionProvider.moveToElement(slideButton).clickAndHold()
|
|
|
164 |
.moveByOffset(1, 0)//.pause(2000)
|
|
|
165 |
.perform();
|
| 37435 |
amit |
166 |
pressed = true;
|
| 34413 |
amit.gupta |
167 |
|
| 34679 |
amit.gupta |
168 |
//actionProvider.moveByOffset(-1,0).pause(2000).perform();
|
| 37423 |
amit |
169 |
LOGGER.info("Margin Left after - " + slideButton.getCssValue("margin-left"));
|
| 34679 |
amit.gupta |
170 |
//Thread.sleep(5000);
|
|
|
171 |
} catch (Exception e) {
|
| 37423 |
amit |
172 |
LOGGER.info(e.getMessage());
|
| 34679 |
amit.gupta |
173 |
driver.get("https://www.realme.com/in/support/phonecheck");
|
| 37423 |
amit |
174 |
installResponseCapture(driver);
|
| 34679 |
amit.gupta |
175 |
break;
|
|
|
176 |
}
|
|
|
177 |
double[] circles = getMatCircles2(driver, fileName);
|
|
|
178 |
if (circles != null) {
|
|
|
179 |
double firstCircleX = circles[0];
|
|
|
180 |
double secondCircleX = circles[1];
|
|
|
181 |
double distance = Math.abs(firstCircleX - secondCircleX);
|
| 37423 |
amit |
182 |
LOGGER.info("Distance is " + distance);
|
| 30209 |
amit.gupta |
183 |
|
| 37423 |
amit |
184 |
double[] cal = calibrate(driver, actionProvider, distance);
|
|
|
185 |
int moveByOffset = cal == null ? 0 : (int) cal[0];
|
| 34679 |
amit.gupta |
186 |
if (moveByOffset > 0) {
|
| 37423 |
amit |
187 |
// Move, measure again, close the remainder, THEN release.
|
|
|
188 |
actionProvider.moveByOffset(moveByOffset, 0).perform();
|
|
|
189 |
refine(driver, actionProvider, cal[1]);
|
|
|
190 |
actionProvider.release().perform();
|
| 37435 |
amit |
191 |
pressed = false;
|
| 37423 |
amit |
192 |
LOGGER.info("Move click performed (main {}px)", moveByOffset);
|
| 32146 |
amit.gupta |
193 |
try {
|
| 37426 |
amit |
194 |
// Success used to mean "the dx_captcha_basic_bar-inform error element
|
|
|
195 |
// did not appear". realme rebuilt this page, so that element may no
|
|
|
196 |
// longer exist -- in which case the wait always times out and EVERY
|
|
|
197 |
// attempt reports success while the captcha was never solved. That is
|
|
|
198 |
// exactly what happened: 27 successes, 27 missed responses, 0 rows.
|
|
|
199 |
//
|
|
|
200 |
// The lookup response arriving is the only signal that actually proves
|
|
|
201 |
// the captcha passed, so wait for that instead.
|
|
|
202 |
String json = awaitResponse(driver, 8000);
|
|
|
203 |
if (json == null) {
|
| 37430 |
amit |
204 |
LOGGER.warn("Failed = {} (captcha said: {}) urls={}",
|
|
|
205 |
counter, captchaSignal(driver), seenUrls(driver));
|
| 37426 |
amit |
206 |
} else {
|
| 37423 |
amit |
207 |
LOGGER.info("Success at attempt " + counter);
|
| 37472 |
amit |
208 |
gauges.captchaSolved(BRAND);
|
| 34679 |
amit.gupta |
209 |
//List<WebElement> warrantyDateElements = driver.findElements(By.className("warranty-service--result_info__dateValue"));
|
|
|
210 |
//WebElement activationTimeElement = driver.findElement(By.xpath("//*[contains(text(), 'UTC+5.5') or contains(text(), 'Non-Activate')]"));
|
| 37423 |
amit |
211 |
// Read the JSON the page already received. The old locator
|
|
|
212 |
// keyed off a label that no longer exists, and its "| //h1" branch
|
|
|
213 |
// would silently grab a page heading and try to parse it as a date.
|
|
|
214 |
// The API is explicit: expiryDate (yyyy.MM.dd) plus an isActivation
|
|
|
215 |
// flag, so nothing has to be inferred from message text.
|
|
|
216 |
LocalDate activationDate = activationFrom(json);
|
| 37426 |
amit |
217 |
LOGGER.info("Activation lookup for {} -> {}", imei,
|
|
|
218 |
activationDate != null ? activationDate : "not activated");
|
| 37423 |
amit |
219 |
// Always record, even with a null date. findElement used to THROW
|
|
|
220 |
// when the element was missing, so nothing was written and the imei
|
|
|
221 |
// came back on every run indefinitely.
|
|
|
222 |
dateMap.put(imei, activationDate);
|
| 37472 |
amit |
223 |
gauges.answered(BRAND, activationDate != null);
|
| 37423 |
amit |
224 |
clearCapture(driver);
|
|
|
225 |
|
| 34679 |
amit.gupta |
226 |
if (dateMap.size() == imeis.size()) {
|
|
|
227 |
return dateMap;
|
|
|
228 |
}
|
|
|
229 |
WebElement checkAgainButton = driver.findElement(By.className("recheck-btn"));
|
|
|
230 |
captchaNotBroken = false;
|
|
|
231 |
checkAgainButton.click();
|
|
|
232 |
break;
|
| 32146 |
amit.gupta |
233 |
}
|
| 34679 |
amit.gupta |
234 |
} catch (Throwable e) {
|
| 37423 |
amit |
235 |
LOGGER.warn("Failed = " + counter + " ----------" + e.getMessage());
|
| 32146 |
amit.gupta |
236 |
}
|
|
|
237 |
}
|
|
|
238 |
}
|
| 37435 |
amit |
239 |
// Any path that got here without releasing (null circles, unusable
|
|
|
240 |
// calibration) must let go before the next attempt re-presses.
|
|
|
241 |
if (pressed) {
|
|
|
242 |
try {
|
|
|
243 |
actionProvider.release().perform();
|
|
|
244 |
} catch (Exception releaseFailed) {
|
|
|
245 |
LOGGER.warn("Could not release the slider: " + releaseFailed.getMessage());
|
|
|
246 |
}
|
|
|
247 |
pressed = false;
|
|
|
248 |
}
|
| 37423 |
amit |
249 |
LOGGER.info("After circles conditions");
|
| 34679 |
amit.gupta |
250 |
wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_wrapper_')]")));
|
|
|
251 |
wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_slider_')]")));
|
|
|
252 |
slideButton = driver.findElement(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_slider_')]"));
|
|
|
253 |
actionProvider.moveToElement(slideButton).doubleClick().perform();
|
| 37423 |
amit |
254 |
LOGGER.info("Ending Do" + counter++);
|
| 30209 |
amit.gupta |
255 |
|
| 37445 |
amit |
256 |
} while (captchaNotBroken && counter <= MAX_CAPTCHA_ATTEMPTS);
|
| 37435 |
amit |
257 |
// The widget-not-found path breaks out mid-iteration; do not carry a held
|
|
|
258 |
// button into the next imei.
|
|
|
259 |
if (pressed) {
|
|
|
260 |
try {
|
|
|
261 |
actionProvider.release().perform();
|
|
|
262 |
} catch (Exception releaseFailed) {
|
|
|
263 |
LOGGER.warn("Could not release the slider: " + releaseFailed.getMessage());
|
|
|
264 |
}
|
|
|
265 |
}
|
| 37445 |
amit |
266 |
LOGGER.warn("Gave up after {} captcha attempts - imei stays pending for the next tick", MAX_CAPTCHA_ATTEMPTS);
|
| 34679 |
amit.gupta |
267 |
} catch (Exception e) {
|
| 37423 |
amit |
268 |
LOGGER.info("Caught unknown - for imei " + imei);
|
|
|
269 |
LOGGER.error("Realme warranty check failed for imei " + imei, e);
|
| 34679 |
amit.gupta |
270 |
}
|
|
|
271 |
driver.get("https://www.realme.com/in/support/phonecheck");
|
| 37423 |
amit |
272 |
installResponseCapture(driver);
|
| 34679 |
amit.gupta |
273 |
}
|
|
|
274 |
mainCount++;
|
|
|
275 |
remainingImeis = imeis.stream().filter(x -> !dateMap.containsKey(x)).collect(Collectors.toList());
|
| 36253 |
amit |
276 |
if (remainingImeis.isEmpty()) break;
|
| 34679 |
amit.gupta |
277 |
} while (mainCount < 5);
|
|
|
278 |
} finally {
|
|
|
279 |
if (driver != null) {
|
|
|
280 |
try {
|
| 34681 |
amit.gupta |
281 |
driver.quit(); // Always shuts everything down
|
| 32146 |
amit.gupta |
282 |
} catch (Exception e) {
|
| 37423 |
amit |
283 |
LOGGER.warn("Error closing driver: " + e.getMessage());
|
| 32146 |
amit.gupta |
284 |
}
|
|
|
285 |
}
|
| 34679 |
amit.gupta |
286 |
}
|
|
|
287 |
|
| 32146 |
amit.gupta |
288 |
return dateMap;
|
|
|
289 |
}
|
| 30352 |
amit.gupta |
290 |
|
| 34679 |
amit.gupta |
291 |
|
| 37423 |
amit |
292 |
/** realme rebuilt the page; the old .sn-input / div.check-btn no longer render. */
|
|
|
293 |
private static final String IMEI_INPUT = ".imei-input input";
|
|
|
294 |
private static final String SUBMIT_ICON = ".imei-input .search-icon";
|
| 30209 |
amit.gupta |
295 |
|
| 37423 |
amit |
296 |
/**
|
| 37424 |
amit |
297 |
* Calibration nudge, in slider px, sized from the gap rather than fixed.
|
|
|
298 |
*
|
|
|
299 |
* A fixed 15px was far too small: it closed only ~25px, so the ratio came from a
|
|
|
300 |
* measurement ~20% noise and the aim landed ~30px out. A fixed 60px then proved too
|
|
|
301 |
* large in the other direction -- production data showed 85% of attempts with a gap
|
|
|
302 |
* under 60px ended up UNUSABLE (the piece shot past the hole and the measured gap
|
|
|
303 |
* grew), and 45% overall.
|
|
|
304 |
*
|
|
|
305 |
* Aim to close about half the gap: the piece travels roughly ASSUMED_RATIO px per
|
|
|
306 |
* slider px, so nudge ~= gap / (2 * ratio), clamped. The real ratio is still derived
|
|
|
307 |
* from the measurement; this only sizes the probe.
|
| 37423 |
amit |
308 |
*/
|
| 37433 |
amit |
309 |
/*
|
|
|
310 |
* REALME's tuning values, deliberately independent of oppo's -- do not sync the two.
|
|
|
311 |
* Realme currently converts ~60% of completed drags against oppo's ~12% on the same
|
|
|
312 |
* widget, so it is the one that must not be disturbed to chase oppo.
|
|
|
313 |
*
|
|
|
314 |
* Left at 1.7 on purpose. Realme's measured ratio over 1,683 production calibrations
|
|
|
315 |
* is 0.70, so this is wrong here too and the probe is smaller than intended -- but it
|
|
|
316 |
* is wrong while converting 60%, and oppo is carrying the corrected value first. If
|
|
|
317 |
* oppo improves, bring 0.70 over here and measure it on its own.
|
|
|
318 |
*/
|
| 37424 |
amit |
319 |
private static final double ASSUMED_RATIO = 1.7;
|
|
|
320 |
private static final int MIN_NUDGE = 10;
|
|
|
321 |
private static final int MAX_NUDGE = 60;
|
| 37445 |
amit |
322 |
/**
|
|
|
323 |
* Captcha attempts per imei per tick, before giving up and letting the next tick
|
|
|
324 |
* retry with a FRESH page and session. The imei is never abandoned -- it stays
|
|
|
325 |
* pending and returns in ~5 minutes.
|
|
|
326 |
*
|
|
|
327 |
* 12, NOT oppo's 7. Realme has not had the glide change, so its successes still
|
|
|
328 |
* spread late. Measured over 399 successes and 610 exhausted imeis:
|
|
|
329 |
* cap 7 -> only 83.5% of successes kept (16.5% lost), 59.2% of work saved
|
|
|
330 |
* cap 12 -> 96.0% kept, 35.7% saved <- here
|
|
|
331 |
* cap 14 -> 97.7% kept, 26.7% saved
|
|
|
332 |
* Realme burns more than twice oppo's captcha work, so even this gentler cap saves
|
|
|
333 |
* more in absolute terms (4,937 attempts vs oppo's 3,648).
|
|
|
334 |
*
|
|
|
335 |
* If glide is ever ported here, expect the curve to shift left as oppo's did
|
|
|
336 |
* (33% -> 2% of successes from attempts 8-20) and this can come down to ~7.
|
|
|
337 |
*/
|
|
|
338 |
private static final int MAX_CAPTCHA_ATTEMPTS = 12;
|
|
|
339 |
|
| 37423 |
amit |
340 |
private static final double ALIGNED_PX = 2.0;
|
|
|
341 |
|
| 37424 |
amit |
342 |
private static int nudgeFor(double gap) {
|
|
|
343 |
int n = (int) Math.round(gap / (2 * ASSUMED_RATIO));
|
|
|
344 |
return Math.max(MIN_NUDGE, Math.min(MAX_NUDGE, n));
|
|
|
345 |
}
|
|
|
346 |
|
| 37423 |
amit |
347 |
/** Stash the active/check response on window as the page receives it. */
|
|
|
348 |
private void installResponseCapture(WebDriver driver) {
|
|
|
349 |
try {
|
|
|
350 |
((JavascriptExecutor) driver).executeScript(
|
|
|
351 |
"if (!window.__rmCapInstalled) {"
|
| 37430 |
amit |
352 |
+ " window.__rmCapInstalled = true; window.__rmResp = null; window.__rmCaptcha = null; window.__rmSeen = [];"
|
| 37431 |
amit |
353 |
// Three transports, because realme's page uses all of them and only the
|
|
|
354 |
// prototype-level patches are reliable.
|
|
|
355 |
//
|
|
|
356 |
// The lookup goes through Nuxt's $fetch (ofetch), which grabs its
|
|
|
357 |
// reference to globalThis.fetch when its bundle is evaluated -- long
|
|
|
358 |
// before driver.get() returns and we can wrap window.fetch. So wrapping
|
|
|
359 |
// window.fetch NEVER sees /active/check: verified on the live page, the
|
|
|
360 |
// same $fetch call is invisible to a window.fetch wrapper and visible to
|
|
|
361 |
// a Response.prototype one. That is why 526 solved captchas produced 0
|
|
|
362 |
// rows: the answer arrived and we could not see it.
|
|
|
363 |
//
|
|
|
364 |
// Response.prototype.json / .text are resolved at call time, so patching
|
|
|
365 |
// them catches any client no matter when it took its fetch reference.
|
| 37423 |
amit |
366 |
+ " var O = XMLHttpRequest.prototype.open;"
|
|
|
367 |
+ " XMLHttpRequest.prototype.open = function(m,u){"
|
|
|
368 |
+ " this.addEventListener('load', function(){"
|
| 37428 |
amit |
369 |
+ " try { var s=String(u);"
|
| 37430 |
amit |
370 |
+ " if (s.indexOf('active/check') !== -1 || s.indexOf('customer-api') !== -1) window.__rmResp = this.responseText;"
|
| 37428 |
amit |
371 |
+ " else if (s.indexOf('captcha') !== -1) window.__rmCaptcha = s + ' -> ' + (this.responseText||'').slice(0,200);"
|
| 37430 |
amit |
372 |
+ " else if (window.__rmSeen.length < 12) window.__rmSeen.push(s.slice(0,80));"
|
| 37428 |
amit |
373 |
+ " } catch(e){}"
|
| 37423 |
amit |
374 |
+ " });"
|
|
|
375 |
+ " return O.apply(this, arguments);"
|
|
|
376 |
+ " };"
|
| 37425 |
amit |
377 |
+ " var F = window.fetch;"
|
|
|
378 |
+ " if (F) { window.fetch = function(){"
|
|
|
379 |
+ " var u = arguments[0]; u = (u && u.url) ? u.url : String(u);"
|
|
|
380 |
+ " return F.apply(this, arguments).then(function(r){"
|
| 37430 |
amit |
381 |
+ " try { if (u.indexOf('active/check') !== -1 || u.indexOf('customer-api') !== -1) { r.clone().text().then(function(t){ window.__rmResp = t; }); }"
|
|
|
382 |
+ " else if (window.__rmSeen.length < 12) window.__rmSeen.push('F ' + u.slice(0,80)); } catch(e){}"
|
| 37425 |
amit |
383 |
+ " return r;"
|
|
|
384 |
+ " });"
|
|
|
385 |
+ " }; }"
|
| 37431 |
amit |
386 |
// Read the body off the Response itself. Must use the ORIGINAL reader on
|
|
|
387 |
// the clone -- calling the patched one recurses forever (it hangs the page).
|
|
|
388 |
+ " ['json','text'].forEach(function(name){"
|
|
|
389 |
+ " var orig = Response.prototype[name];"
|
|
|
390 |
+ " if (!orig) return;"
|
|
|
391 |
+ " Response.prototype[name] = function(){"
|
|
|
392 |
+ " try { var u = this.url || '';"
|
|
|
393 |
+ " if ((u.indexOf('active/check') !== -1 || u.indexOf('customer-api') !== -1) && !this.__rmSeenBody) {"
|
|
|
394 |
+ " var c = this.clone(); c.__rmSeenBody = 1;"
|
|
|
395 |
+ " orig.call(c).then(function(t){ window.__rmResp = (typeof t === 'string') ? t : JSON.stringify(t); });"
|
|
|
396 |
+ " } else if (u && window.__rmSeen.length < 12 && u.indexOf('captcha') === -1) {"
|
|
|
397 |
+ " window.__rmSeen.push('R ' + u.slice(0,80));"
|
|
|
398 |
+ " } } catch(e){}"
|
|
|
399 |
+ " return orig.apply(this, arguments);"
|
|
|
400 |
+ " };"
|
|
|
401 |
+ " });"
|
| 37423 |
amit |
402 |
+ "}");
|
|
|
403 |
} catch (Exception e) {
|
|
|
404 |
LOGGER.warn("Could not install the response capture hook: " + e.getMessage());
|
|
|
405 |
}
|
|
|
406 |
}
|
|
|
407 |
|
| 37426 |
amit |
408 |
/**
|
|
|
409 |
* Wait for the lookup response to arrive, which is the only reliable proof that
|
|
|
410 |
* the captcha was actually solved. Polls rather than blocking so a solved captcha
|
|
|
411 |
* is picked up as soon as the request completes.
|
|
|
412 |
*/
|
|
|
413 |
private String awaitResponse(WebDriver driver, long timeoutMs) {
|
|
|
414 |
long deadline = System.currentTimeMillis() + timeoutMs;
|
|
|
415 |
while (System.currentTimeMillis() < deadline) {
|
|
|
416 |
String json = capturedResponse(driver);
|
|
|
417 |
if (json != null && !json.trim().isEmpty()) return json;
|
|
|
418 |
try {
|
|
|
419 |
Thread.sleep(300);
|
|
|
420 |
} catch (InterruptedException ie) {
|
|
|
421 |
Thread.currentThread().interrupt();
|
|
|
422 |
return null;
|
|
|
423 |
}
|
|
|
424 |
}
|
|
|
425 |
return null;
|
|
|
426 |
}
|
|
|
427 |
|
| 37428 |
amit |
428 |
/** Last response from the captcha widget itself -- says WHY a drag was refused. */
|
|
|
429 |
private String captchaSignal(WebDriver driver) {
|
|
|
430 |
try {
|
|
|
431 |
Object v = ((JavascriptExecutor) driver).executeScript("return window.__rmCaptcha || null;");
|
|
|
432 |
return v == null ? null : v.toString();
|
|
|
433 |
} catch (Exception e) {
|
|
|
434 |
return null;
|
|
|
435 |
}
|
|
|
436 |
}
|
|
|
437 |
|
| 37430 |
amit |
438 |
/** Non-captcha URLs the page called -- shows what to match on when the lookup is missed. */
|
|
|
439 |
private String seenUrls(WebDriver driver) {
|
|
|
440 |
try {
|
|
|
441 |
Object v = ((JavascriptExecutor) driver).executeScript(
|
|
|
442 |
"return (window.__rmSeen||[]).join(' | ');");
|
|
|
443 |
return v == null ? "" : v.toString();
|
|
|
444 |
} catch (Exception e) {
|
|
|
445 |
return "";
|
|
|
446 |
}
|
|
|
447 |
}
|
|
|
448 |
|
| 37423 |
amit |
449 |
private String capturedResponse(WebDriver driver) {
|
|
|
450 |
try {
|
|
|
451 |
Object v = ((JavascriptExecutor) driver).executeScript("return window.__rmResp || null;");
|
|
|
452 |
return v == null ? null : v.toString();
|
|
|
453 |
} catch (Exception e) {
|
|
|
454 |
return null;
|
|
|
455 |
}
|
|
|
456 |
}
|
|
|
457 |
|
|
|
458 |
private void clearCapture(WebDriver driver) {
|
|
|
459 |
try {
|
|
|
460 |
((JavascriptExecutor) driver).executeScript("window.__rmResp = null;");
|
|
|
461 |
} catch (Exception ignored) {
|
|
|
462 |
}
|
|
|
463 |
}
|
|
|
464 |
|
|
|
465 |
/**
|
|
|
466 |
* realme returns the WARRANTY EXPIRY, not the activation date, so a year is
|
|
|
467 |
* subtracted -- warranty is 12 months as standard. Verified against our own
|
|
|
468 |
* records: 41,267 of 41,289 stored Realme activation dates precede the date we
|
|
|
469 |
* checked them, averaging 13 days before, which is only consistent with the
|
|
|
470 |
* subtraction being applied. isActivation false means the device is not
|
|
|
471 |
* activated yet and there is no date to derive.
|
|
|
472 |
*/
|
|
|
473 |
private LocalDate activationFrom(String json) {
|
|
|
474 |
if (json == null) return null;
|
|
|
475 |
try {
|
|
|
476 |
JSONObject root = new JSONObject(json);
|
|
|
477 |
JSONObject data = root.optJSONObject("data");
|
|
|
478 |
if (data == null) return null;
|
|
|
479 |
if (!data.optBoolean("isActivation", false)) return null;
|
|
|
480 |
|
|
|
481 |
String expiry = data.optString("expiryDate", "");
|
|
|
482 |
if (expiry == null || expiry.trim().isEmpty() || "null".equals(expiry)) return null;
|
|
|
483 |
|
|
|
484 |
return LocalDate.parse(expiry.trim().split(" ")[0],
|
|
|
485 |
DateTimeFormatter.ofPattern("yyyy.MM.dd")).minusYears(1);
|
|
|
486 |
} catch (Exception e) {
|
|
|
487 |
LOGGER.warn("Could not read expiryDate from the realme response: " + e.getMessage());
|
|
|
488 |
return null;
|
|
|
489 |
}
|
|
|
490 |
}
|
|
|
491 |
|
|
|
492 |
/** @return {sliderPxToMove, sliderPxPerCirclePx}, or null if the circles were unreadable. */
|
|
|
493 |
private double[] calibrate(WebDriver driver, Actions actionProvider, double distance) throws Exception {
|
| 37424 |
amit |
494 |
int nudge = nudgeFor(distance);
|
|
|
495 |
actionProvider.moveByOffset(nudge, 0).perform();
|
| 37423 |
amit |
496 |
|
| 32146 |
amit.gupta |
497 |
String fileName2 = "/tmp/" + Thread.currentThread().getName() + "-moved.png";
|
| 34413 |
amit.gupta |
498 |
double[] movedCircles = getMatCircles2(driver, fileName2);
|
| 37423 |
amit |
499 |
if (movedCircles == null) return null;
|
|
|
500 |
|
|
|
501 |
double movedDistance = Math.abs(movedCircles[0] - movedCircles[1]);
|
|
|
502 |
double closed = distance - movedDistance;
|
|
|
503 |
LOGGER.info("Calibration: gap {} -> {} after {}px, closed {}",
|
| 37424 |
amit |
504 |
distance, movedDistance, nudge, closed);
|
| 37423 |
amit |
505 |
if (closed <= 0) return null;
|
|
|
506 |
|
| 37424 |
amit |
507 |
double sliderPerCircle = nudge / closed;
|
| 37423 |
amit |
508 |
return new double[]{Math.round(movedDistance * sliderPerCircle), sliderPerCircle};
|
| 32146 |
amit.gupta |
509 |
}
|
| 30209 |
amit.gupta |
510 |
|
| 37423 |
amit |
511 |
/** Measure again after the main move and close the remainder before releasing. */
|
|
|
512 |
private int refine(WebDriver driver, Actions actionProvider, double sliderPerCircle) throws Exception {
|
|
|
513 |
String fileName = "/tmp/" + Thread.currentThread().getName() + "-refine.png";
|
|
|
514 |
double[] circles = getMatCircles2(driver, fileName);
|
|
|
515 |
if (circles == null) return 0;
|
|
|
516 |
|
|
|
517 |
double remaining = Math.abs(circles[0] - circles[1]);
|
|
|
518 |
if (remaining <= ALIGNED_PX) return 0;
|
|
|
519 |
|
|
|
520 |
int correction = (int) Math.round(remaining * sliderPerCircle);
|
|
|
521 |
if (correction == 0) return 0;
|
|
|
522 |
LOGGER.info("Refine: {}px of gap left -> nudging {}px", remaining, correction);
|
|
|
523 |
actionProvider.moveByOffset(correction, 0).perform();
|
|
|
524 |
return correction;
|
|
|
525 |
}
|
|
|
526 |
|
| 32146 |
amit.gupta |
527 |
private Mat getMatCircles(WebDriver driver, String fileName) throws Exception {
|
| 30209 |
amit.gupta |
528 |
|
| 32146 |
amit.gupta |
529 |
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
|
|
|
530 |
BufferedImage fullImg = ImageIO.read(screenshot);
|
| 30209 |
amit.gupta |
531 |
|
| 32146 |
amit.gupta |
532 |
// Get the location of element on the page
|
|
|
533 |
WebElement imageElement = driver.findElement(By.id("dx_captcha_basic_bg_1"));
|
|
|
534 |
Point point = imageElement.getLocation();
|
| 30209 |
amit.gupta |
535 |
|
| 32146 |
amit.gupta |
536 |
// Get width and height of the element
|
|
|
537 |
int eleWidth = imageElement.getSize().getWidth();
|
|
|
538 |
int eleHeight = imageElement.getSize().getHeight();
|
| 30209 |
amit.gupta |
539 |
|
| 32146 |
amit.gupta |
540 |
// Crop the entire page screenshot to get only element screenshot
|
|
|
541 |
BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(),
|
|
|
542 |
eleWidth, eleHeight);
|
|
|
543 |
ImageIO.write(eleScreenshot, "png", screenshot);
|
| 30209 |
amit.gupta |
544 |
|
| 32146 |
amit.gupta |
545 |
// Copy the element screenshot to disk
|
|
|
546 |
File screenshotLocation = new File(fileName);
|
|
|
547 |
FileUtils.copyFile(screenshot, screenshotLocation);
|
| 30209 |
amit.gupta |
548 |
|
| 32146 |
amit.gupta |
549 |
Imgcodecs imageCodecs = new Imgcodecs();
|
|
|
550 |
Mat grayMatrix = imageCodecs.imread(fileName, Imgcodecs.IMREAD_COLOR);
|
|
|
551 |
Imgproc.cvtColor(grayMatrix, grayMatrix, Imgproc.COLOR_BGR2GRAY);
|
|
|
552 |
Mat medianBlur = new Mat();
|
|
|
553 |
Imgproc.medianBlur(grayMatrix, medianBlur, 5);
|
|
|
554 |
Mat circles = new Mat();
|
|
|
555 |
Imgproc.HoughCircles(medianBlur, circles, Imgproc.HOUGH_GRADIENT, 1, 15, 50, 20, 17, 25);
|
|
|
556 |
return circles;
|
|
|
557 |
}
|
| 30209 |
amit.gupta |
558 |
|
| 34413 |
amit.gupta |
559 |
private double[] getMatCircles2(WebDriver driver, String fileName) throws Exception {
|
|
|
560 |
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
|
|
|
561 |
BufferedImage fullImg = ImageIO.read(screenshot);
|
|
|
562 |
|
|
|
563 |
// Get the location of element on the page
|
| 36583 |
amit |
564 |
WebElement imageElement = driver.findElement(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_bg_')]"));
|
| 34413 |
amit.gupta |
565 |
Point point = imageElement.getLocation();
|
|
|
566 |
|
|
|
567 |
// Get width and height of the element
|
|
|
568 |
int eleWidth = imageElement.getSize().getWidth();
|
|
|
569 |
int eleHeight = imageElement.getSize().getHeight();
|
|
|
570 |
|
|
|
571 |
// Crop the entire page screenshot to get only element screenshot
|
|
|
572 |
BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(),
|
|
|
573 |
eleWidth, eleHeight);
|
|
|
574 |
ImageIO.write(eleScreenshot, "png", screenshot);
|
|
|
575 |
|
|
|
576 |
// Copy the element screenshot to disk
|
|
|
577 |
File screenshotLocation = new File(fileName);
|
|
|
578 |
FileUtils.copyFile(screenshot, screenshotLocation);
|
|
|
579 |
|
|
|
580 |
Mat src = Imgcodecs.imread(fileName);
|
|
|
581 |
if (src.empty()) {
|
| 36583 |
amit |
582 |
LOGGER.warn("Cannot read image: {}", fileName);
|
| 36253 |
amit |
583 |
new File(fileName).delete();
|
| 34413 |
amit.gupta |
584 |
return null;
|
|
|
585 |
}
|
|
|
586 |
|
|
|
587 |
Mat gray = new Mat();
|
|
|
588 |
Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
|
|
|
589 |
Imgproc.GaussianBlur(gray, gray, new Size(9, 9), 2, 2);
|
|
|
590 |
|
|
|
591 |
Mat circles = new Mat();
|
|
|
592 |
Imgproc.HoughCircles(gray, circles, Imgproc.HOUGH_GRADIENT, 1.0,
|
|
|
593 |
20, // min distance between centers
|
|
|
594 |
100, 20, // param1 (Canny high threshold), param2 (accumulator threshold)
|
|
|
595 |
10, 50); // minRadius, maxRadius (adjust as needed)
|
|
|
596 |
|
| 36253 |
amit |
597 |
if (circles.cols() < 2) {
|
|
|
598 |
new File(fileName).delete();
|
|
|
599 |
return null;
|
|
|
600 |
}
|
| 34413 |
amit.gupta |
601 |
|
|
|
602 |
for (int i = 0; i < circles.cols(); i++) {
|
|
|
603 |
double[] c1 = circles.get(0, i);
|
|
|
604 |
for (int j = i + 1; j < circles.cols(); j++) {
|
|
|
605 |
double[] c2 = circles.get(0, j);
|
|
|
606 |
if (Math.abs(c1[2] - c2[2]) < 3) { // check similar radius
|
| 36253 |
amit |
607 |
new File(fileName).delete();
|
| 34413 |
amit.gupta |
608 |
return new double[] {
|
|
|
609 |
c1[0], c2[0] // x-coordinates
|
|
|
610 |
};
|
|
|
611 |
}
|
|
|
612 |
}
|
|
|
613 |
}
|
|
|
614 |
|
| 36253 |
amit |
615 |
new File(fileName).delete();
|
| 34413 |
amit.gupta |
616 |
return null;
|
|
|
617 |
}
|
|
|
618 |
|
|
|
619 |
|
| 30209 |
amit.gupta |
620 |
}
|