| 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;
|
| 37420 |
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;
|
| 30373 |
amit.gupta |
18 |
import org.springframework.stereotype.Component;
|
| 30209 |
amit.gupta |
19 |
|
|
|
20 |
import javax.imageio.ImageIO;
|
|
|
21 |
import java.awt.image.BufferedImage;
|
|
|
22 |
import java.io.File;
|
|
|
23 |
import java.time.LocalDate;
|
|
|
24 |
import java.time.format.DateTimeFormatter;
|
| 37420 |
amit |
25 |
import java.time.Instant;
|
|
|
26 |
import java.time.ZoneId;
|
| 30209 |
amit.gupta |
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
|
|
|
34 |
public class CheckOppoWarrantyTask {
|
| 30209 |
amit.gupta |
35 |
|
| 32146 |
amit.gupta |
36 |
private static final Logger LOGGER = LogManager.getLogger(CheckOppoWarrantyTask.class);
|
| 30209 |
amit.gupta |
37 |
|
| 36253 |
amit |
38 |
static {
|
| 36306 |
amit |
39 |
try {
|
|
|
40 |
OpenCV.loadShared();
|
|
|
41 |
} catch (Throwable t) {
|
|
|
42 |
LogManager.getLogger(CheckOppoWarrantyTask.class).warn("OpenCV not available on this platform: {}", t.getMessage());
|
|
|
43 |
}
|
| 36253 |
amit |
44 |
}
|
|
|
45 |
|
| 32146 |
amit.gupta |
46 |
public Map<String, LocalDate> checkWarranty(List<String> imeis) {
|
|
|
47 |
Map<String, LocalDate> dateMap = new HashMap<>();
|
|
|
48 |
LOGGER.info("Initiating webdriver...");
|
| 30209 |
amit.gupta |
49 |
|
| 32146 |
amit.gupta |
50 |
Map<String, Object> prefsMap = new HashMap<>();
|
|
|
51 |
prefsMap.put("profile.default_content_settings.popups", 0);
|
|
|
52 |
prefsMap.put("download.prompt", false);
|
|
|
53 |
prefsMap.put("download.directory_upgrade", true);
|
| 34413 |
amit.gupta |
54 |
System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver");
|
| 32146 |
amit.gupta |
55 |
//I need this
|
| 34413 |
amit.gupta |
56 |
//System.setProperty("webdriver.chrome.driver", "/Users/amit/cft/chromedriver/chromedriver");
|
| 32146 |
amit.gupta |
57 |
ChromeOptions options = new ChromeOptions();
|
| 34413 |
amit.gupta |
58 |
//options.setBinary("/Users/amit/cft/chrome/Google Chrome for Testing.app/Contents/MacOS/Google Chrome For Testing");
|
| 32146 |
amit.gupta |
59 |
options.setExperimentalOption("prefs", prefsMap);
|
|
|
60 |
//options.setExperimentalOption("detach", true);
|
|
|
61 |
options.addArguments("--headless");
|
|
|
62 |
options.addArguments("--user-agent=\"Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; Microsoft; Lumia 640 XL LTE) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Mobile Safari/537.36 Edge/12.10166\"");
|
| 30209 |
amit.gupta |
63 |
|
| 32146 |
amit.gupta |
64 |
options.addArguments("--no-sandbox");
|
|
|
65 |
options.addArguments("start-maximized");
|
|
|
66 |
options.addArguments("disable-infobars");
|
|
|
67 |
options.addArguments("--disable-extensions");
|
|
|
68 |
options.addArguments("--force-device-scale-factor=1");
|
|
|
69 |
options.addArguments("--test-type");
|
| 37446 |
amit |
70 |
// Headless needs no GPU, yet chrome still forks a gpu-process per browser (6 were
|
|
|
71 |
// alive across the fleet). --disable-gpu removes them; --disable-dev-shm-usage
|
|
|
72 |
// keeps chrome off the small /dev/shm on this box. Both are pure overhead removal,
|
|
|
73 |
// no behaviour change. This box co-hosts a 9.4GB tomcat with ~3GB available and
|
|
|
74 |
// has been OOM-killed twice this month, and tomcat was the victim both times.
|
|
|
75 |
options.addArguments("--disable-gpu");
|
|
|
76 |
options.addArguments("--disable-dev-shm-usage");
|
|
|
77 |
options.addArguments("--disable-software-rasterizer");
|
| 30209 |
amit.gupta |
78 |
|
| 34680 |
amit.gupta |
79 |
WebDriver driver = null;
|
| 30209 |
amit.gupta |
80 |
|
| 34680 |
amit.gupta |
81 |
try {
|
|
|
82 |
driver = new ChromeDriver(options);
|
|
|
83 |
driver.manage().timeouts().setScriptTimeout(6, TimeUnit.SECONDS);
|
|
|
84 |
driver.manage().window().setSize(new Dimension(1600, 900));
|
|
|
85 |
driver.manage().window().maximize();
|
|
|
86 |
// Deleting all the cookies
|
|
|
87 |
driver.manage().deleteAllCookies();
|
|
|
88 |
// Specifiying pageLoadTimeout and Implicit wait
|
|
|
89 |
driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
|
|
|
90 |
driver.manage().timeouts().implicitlyWait(9, TimeUnit.SECONDS);
|
| 34415 |
amit.gupta |
91 |
|
| 34680 |
amit.gupta |
92 |
int mainCount = 0;
|
|
|
93 |
List<String> remainingImeis = imeis;
|
|
|
94 |
WebDriverWait wait10Sec = new WebDriverWait(driver, 8);
|
|
|
95 |
WebDriverWait wait5Sec = new WebDriverWait(driver, 4);
|
|
|
96 |
WebElement slideButton;
|
|
|
97 |
Actions actionProvider = new Actions(driver);
|
|
|
98 |
do {
|
|
|
99 |
driver.get("https://support.oppo.com/in/warranty-check/");
|
| 37420 |
amit |
100 |
installResponseCapture(driver);
|
| 32146 |
amit.gupta |
101 |
try {
|
| 34680 |
amit.gupta |
102 |
driver.findElement(By.cssSelector(".cp-cookie-tip a.close")).click();
|
|
|
103 |
} catch (Exception e) {
|
|
|
104 |
}
|
| 30209 |
amit.gupta |
105 |
|
| 34680 |
amit.gupta |
106 |
for (int i = 0; i < remainingImeis.size(); i++) {
|
|
|
107 |
String imei = remainingImeis.get(i);
|
|
|
108 |
try {
|
| 37410 |
amit |
109 |
LOGGER.info("Starting fresh with new IMEI " + imei);
|
| 34680 |
amit.gupta |
110 |
if (driver.findElement(By.className("el-input__inner")).getAttribute("value").length() > 0) {
|
| 37410 |
amit |
111 |
LOGGER.info("Darn.. leaving" + driver.findElement(By.className("el-input__inner")).getAttribute("value"));
|
| 32146 |
amit.gupta |
112 |
driver.get("https://support.oppo.com/in/warranty-check/");
|
| 37432 |
amit |
113 |
installResponseCapture(driver);
|
| 32146 |
amit.gupta |
114 |
break;
|
|
|
115 |
}
|
| 34680 |
amit.gupta |
116 |
driver.findElement(By.className("el-input__inner")).sendKeys(imei);
|
| 30209 |
amit.gupta |
117 |
|
| 34680 |
amit.gupta |
118 |
//driver.findElement(By.className("el-button--primary")).click();
|
|
|
119 |
driver.findElement(By.cssSelector("div.searchBtn")).click();
|
|
|
120 |
|
|
|
121 |
// Perform click-and-hold action on the element
|
|
|
122 |
int counter = 1;
|
|
|
123 |
boolean captchaNotBroken = true;
|
| 37435 |
amit |
124 |
// release() is only reached on the moveByOffset > 0 path, so an attempt
|
|
|
125 |
// that fails detection or calibration leaves the button DOWN. Measured in
|
|
|
126 |
// production: 79% of presses were never released. Actions is one instance
|
|
|
127 |
// per driver, created outside the imei loop, so the stuck press leaks into
|
|
|
128 |
// the next attempt AND the next imei -- the following clickAndHold lands on
|
|
|
129 |
// an already-held button, so the widget never repositions the handle and the
|
|
|
130 |
// drag continues from wherever the pointer was abandoned.
|
|
|
131 |
boolean pressed = false;
|
| 34680 |
amit.gupta |
132 |
do {
|
|
|
133 |
String fileName = "/tmp/oppo-simple-i-" + counter + " " + System.currentTimeMillis() + ".png";
|
|
|
134 |
try {
|
| 37410 |
amit |
135 |
LOGGER.info("Starting Do");
|
| 36583 |
amit |
136 |
wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_wrapper_')]")));
|
|
|
137 |
wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_slider_')]")));
|
| 37437 |
amit |
138 |
By sliderBy = By.xpath("//*[starts-with(@id, 'dx_captcha_basic_slider_')]");
|
|
|
139 |
slideButton = driver.findElement(sliderBy);
|
|
|
140 |
LOGGER.info("Margin Left before- " + marginLeftOf(slideButton));
|
|
|
141 |
try {
|
|
|
142 |
actionProvider.moveToElement(slideButton).clickAndHold()
|
|
|
143 |
.moveByOffset(1, 0)
|
|
|
144 |
.perform();
|
|
|
145 |
} catch (StaleElementReferenceException stale) {
|
|
|
146 |
// The retry refresh (doubleClick at the end of the previous
|
|
|
147 |
// attempt) swaps the widget's DOM, so the handle located at the
|
|
|
148 |
// top of THIS attempt can already be detached by the time we act
|
|
|
149 |
// on it. Re-find and try once, rather than abandoning the imei
|
|
|
150 |
// and reloading the page: this killed 19% of oppo attempts
|
|
|
151 |
// (54 of 284), against 0 on realme.
|
|
|
152 |
LOGGER.info("Slider went stale before the drag - re-finding and retrying once");
|
|
|
153 |
slideButton = driver.findElement(sliderBy);
|
|
|
154 |
actionProvider.moveToElement(slideButton).clickAndHold()
|
|
|
155 |
.moveByOffset(1, 0)
|
|
|
156 |
.perform();
|
|
|
157 |
}
|
| 37435 |
amit |
158 |
pressed = true;
|
| 37437 |
amit |
159 |
LOGGER.info("Margin Left after - " + marginLeftOf(slideButton));
|
| 34680 |
amit.gupta |
160 |
//Thread.sleep(5000);
|
|
|
161 |
} catch (Exception e) {
|
| 37410 |
amit |
162 |
LOGGER.warn("Slider captcha widget (dx_captcha_basic_*) not found or not draggable"
|
|
|
163 |
+ " - reloading. Cause: " + e.getMessage());
|
| 34680 |
amit.gupta |
164 |
driver.get("https://support.oppo.com/in/warranty-check/");
|
| 37432 |
amit |
165 |
installResponseCapture(driver);
|
| 34680 |
amit.gupta |
166 |
break;
|
|
|
167 |
}
|
|
|
168 |
double[] circles = getMatCircles2(driver, fileName);
|
|
|
169 |
if (circles != null) {
|
|
|
170 |
double firstCircleX = circles[0];
|
|
|
171 |
double secondCircleX = circles[1];
|
|
|
172 |
double distance = Math.abs(firstCircleX - secondCircleX);
|
| 37410 |
amit |
173 |
LOGGER.info("Distance is " + distance);
|
| 34680 |
amit.gupta |
174 |
|
| 37438 |
amit |
175 |
// Closed loop on the ring's own position; no assumed ratio.
|
|
|
176 |
int moveByOffset = aim(driver, actionProvider, circles);
|
| 34680 |
amit.gupta |
177 |
if (moveByOffset > 0) {
|
| 37422 |
amit |
178 |
actionProvider.release().perform();
|
| 37435 |
amit |
179 |
pressed = false;
|
| 37422 |
amit |
180 |
LOGGER.info("Move click performed (main {}px)", moveByOffset);
|
| 32146 |
amit.gupta |
181 |
try {
|
| 34680 |
amit.gupta |
182 |
try {
|
|
|
183 |
wait5Sec.until(ExpectedConditions.visibilityOfElementLocated(By.className("dx_captcha_basic_bar-inform")));
|
| 37433 |
amit |
184 |
// realme logs the widget's own verdict here and oppo did not,
|
|
|
185 |
// which is why realme's failures name POSITION_MISMATCH and
|
|
|
186 |
// oppo's 85% say nothing at all. Same line, oppo's variables.
|
|
|
187 |
LOGGER.warn("Failed = {} (captcha said: {})", counter, captchaSignal(driver));
|
| 34680 |
amit.gupta |
188 |
} catch (Exception notFailedException) {
|
| 37410 |
amit |
189 |
LOGGER.info("Success at attempt " + counter);
|
| 37420 |
amit |
190 |
|
|
|
191 |
// Read the JSON the page itself received rather than the
|
|
|
192 |
// rendered text. The DOM value is built from two text nodes -- the
|
|
|
193 |
// date plus a "(UTC+x)" suffix derived from the browser timezone --
|
|
|
194 |
// and the old locator tested only the first, so it matched nothing.
|
|
|
195 |
// The API carries regDate as epoch millis, which has no format or
|
|
|
196 |
// timezone ambiguity at all.
|
| 37429 |
amit |
197 |
String json = awaitResponse(driver, 8000);
|
| 37420 |
amit |
198 |
LocalDate activationDate = regDateFrom(json);
|
|
|
199 |
|
|
|
200 |
if (json == null) {
|
| 37429 |
amit |
201 |
LOGGER.warn("No getDeviceInfo response for imei {} (captcha said: {})",
|
|
|
202 |
imei, captchaSignal(driver));
|
| 34680 |
amit.gupta |
203 |
} else {
|
| 37420 |
amit |
204 |
LOGGER.info("Activation lookup for {} -> {}", imei,
|
|
|
205 |
activationDate != null ? activationDate : "no activation date");
|
| 32146 |
amit.gupta |
206 |
}
|
| 37420 |
amit |
207 |
// Always record the imei, even with a null date: findElement used to
|
|
|
208 |
// throw here, so the row was never written and came back every run.
|
|
|
209 |
dateMap.put(imei, activationDate);
|
|
|
210 |
clearCapture(driver);
|
|
|
211 |
|
| 34680 |
amit.gupta |
212 |
if (dateMap.size() == imeis.size()) {
|
|
|
213 |
return dateMap;
|
|
|
214 |
}
|
|
|
215 |
WebElement checkAgainButton = driver.findElement(By.className("c-btn"));
|
|
|
216 |
captchaNotBroken = false;
|
|
|
217 |
checkAgainButton.click();
|
|
|
218 |
break;
|
| 32146 |
amit.gupta |
219 |
}
|
| 34680 |
amit.gupta |
220 |
} catch (Throwable e) {
|
| 37410 |
amit |
221 |
LOGGER.warn("Failed = " + counter + " ----------" + e.getMessage());
|
| 32146 |
amit.gupta |
222 |
}
|
|
|
223 |
}
|
|
|
224 |
}
|
| 37435 |
amit |
225 |
// Any path that got here without releasing (null circles, unusable
|
|
|
226 |
// calibration) must let go before the next attempt re-presses.
|
|
|
227 |
if (pressed) {
|
|
|
228 |
try {
|
|
|
229 |
actionProvider.release().perform();
|
|
|
230 |
} catch (Exception releaseFailed) {
|
|
|
231 |
LOGGER.warn("Could not release the slider: " + releaseFailed.getMessage());
|
|
|
232 |
}
|
|
|
233 |
pressed = false;
|
|
|
234 |
}
|
| 37410 |
amit |
235 |
LOGGER.info("After circles conditions");
|
| 36583 |
amit |
236 |
wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_wrapper_')]")));
|
|
|
237 |
wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_slider_')]")));
|
|
|
238 |
slideButton = driver.findElement(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_slider_')]"));
|
| 34680 |
amit.gupta |
239 |
actionProvider.moveToElement(slideButton).doubleClick().perform();
|
| 37410 |
amit |
240 |
LOGGER.info("Ending Do" + counter++);
|
| 30209 |
amit.gupta |
241 |
|
| 37445 |
amit |
242 |
} while (captchaNotBroken && counter <= MAX_CAPTCHA_ATTEMPTS);
|
| 37435 |
amit |
243 |
// The widget-not-found path breaks out mid-iteration; do not carry a held
|
|
|
244 |
// button into the next imei.
|
|
|
245 |
if (pressed) {
|
|
|
246 |
try {
|
|
|
247 |
actionProvider.release().perform();
|
|
|
248 |
} catch (Exception releaseFailed) {
|
|
|
249 |
LOGGER.warn("Could not release the slider: " + releaseFailed.getMessage());
|
|
|
250 |
}
|
|
|
251 |
}
|
| 37445 |
amit |
252 |
LOGGER.warn("Gave up after {} captcha attempts - imei stays pending for the next tick", MAX_CAPTCHA_ATTEMPTS);
|
| 34680 |
amit.gupta |
253 |
} catch (Exception e) {
|
| 37410 |
amit |
254 |
LOGGER.error("Oppo warranty check failed for imei " + imei, e);
|
| 34680 |
amit.gupta |
255 |
}
|
|
|
256 |
driver.get("https://support.oppo.com/in/warranty-check/");
|
| 37432 |
amit |
257 |
// Every navigation destroys the JS context, taking the capture hook
|
|
|
258 |
// with it. Miss one of these and the page is blind from the second
|
|
|
259 |
// imei onwards: __oppoResp and __oppoCaptcha both come back null,
|
|
|
260 |
// which reads as "the captcha failed" when it may well have passed.
|
|
|
261 |
installResponseCapture(driver);
|
| 34680 |
amit.gupta |
262 |
}
|
|
|
263 |
mainCount++;
|
|
|
264 |
remainingImeis = imeis.stream().filter(x -> !dateMap.containsKey(x)).collect(Collectors.toList());
|
| 36253 |
amit |
265 |
if (remainingImeis.isEmpty()) break;
|
| 34680 |
amit.gupta |
266 |
} while (mainCount < 5);
|
|
|
267 |
} finally {
|
|
|
268 |
if (driver != null) {
|
|
|
269 |
try {
|
|
|
270 |
driver.quit(); //Ensures cleanup even on return or error
|
| 32146 |
amit.gupta |
271 |
} catch (Exception e) {
|
| 37410 |
amit |
272 |
LOGGER.warn("Error closing driver: " + e.getMessage());
|
| 32146 |
amit.gupta |
273 |
}
|
|
|
274 |
}
|
| 34680 |
amit.gupta |
275 |
}
|
|
|
276 |
|
| 32146 |
amit.gupta |
277 |
return dateMap;
|
|
|
278 |
}
|
| 30352 |
amit.gupta |
279 |
|
| 34680 |
amit.gupta |
280 |
|
| 37420 |
amit |
281 |
/** Stash the getDeviceInfo response on window as the page receives it. */
|
|
|
282 |
private void installResponseCapture(WebDriver driver) {
|
|
|
283 |
try {
|
|
|
284 |
((JavascriptExecutor) driver).executeScript(
|
|
|
285 |
"if (!window.__oppoCapInstalled) {"
|
| 37428 |
amit |
286 |
+ " window.__oppoCapInstalled = true; window.__oppoResp = null; window.__oppoCaptcha = null;"
|
| 37434 |
amit |
287 |
// __oppoCaptcha is last-captcha-call-wins, and the widget refetches a puzzle
|
|
|
288 |
// (/api/a) straight after a rejected verify (/api/v1). We then wait up to 5s
|
|
|
289 |
// for the error element before reading it, which is ample time for the
|
|
|
290 |
// refetch to overwrite the verdict -- so "all failures show /api/a" cannot
|
|
|
291 |
// distinguish "never verified" from "verified, rejected, then overwritten".
|
|
|
292 |
// Keep the verify separately, and a count, so the two are distinguishable.
|
|
|
293 |
+ " window.__oppoVerify = null; window.__oppoVerifyCount = 0;"
|
| 37425 |
amit |
294 |
// axios can sit on either transport, and realme's page uses fetch --
|
|
|
295 |
// an XHR-only hook silently captures nothing, which is exactly what
|
|
|
296 |
// 'no getDeviceInfo response captured' was reporting.
|
| 37420 |
amit |
297 |
+ " var O = XMLHttpRequest.prototype.open;"
|
|
|
298 |
+ " XMLHttpRequest.prototype.open = function(m,u){"
|
|
|
299 |
+ " this.addEventListener('load', function(){"
|
| 37428 |
amit |
300 |
+ " try { var s=String(u);"
|
|
|
301 |
+ " if (s.indexOf('getDeviceInfo') !== -1) window.__oppoResp = this.responseText;"
|
| 37434 |
amit |
302 |
+ " else if (s.indexOf('/api/v1') !== -1) { window.__oppoVerifyCount++; window.__oppoVerify = (this.responseText||'').slice(0,160); window.__oppoCaptcha = s + ' -> ' + (this.responseText||'').slice(0,200); }"
|
| 37428 |
amit |
303 |
+ " else if (s.indexOf('captcha') !== -1) window.__oppoCaptcha = s + ' -> ' + (this.responseText||'').slice(0,200);"
|
|
|
304 |
+ " } catch(e){}"
|
| 37420 |
amit |
305 |
+ " });"
|
|
|
306 |
+ " return O.apply(this, arguments);"
|
|
|
307 |
+ " };"
|
| 37425 |
amit |
308 |
+ " var F = window.fetch;"
|
|
|
309 |
+ " if (F) { window.fetch = function(){"
|
|
|
310 |
+ " var u = arguments[0]; u = (u && u.url) ? u.url : String(u);"
|
|
|
311 |
+ " return F.apply(this, arguments).then(function(r){"
|
|
|
312 |
+ " try { if (u.indexOf('getDeviceInfo') !== -1) { r.clone().text().then(function(t){ window.__oppoResp = t; }); } } catch(e){}"
|
|
|
313 |
+ " return r;"
|
|
|
314 |
+ " });"
|
|
|
315 |
+ " }; }"
|
| 37431 |
amit |
316 |
// A window.fetch wrapper only catches clients that resolve fetch at call
|
|
|
317 |
// time. Anything that captured globalThis.fetch when its bundle loaded --
|
|
|
318 |
// Nuxt's $fetch/ofetch does exactly this, and it is what hid realme's
|
|
|
319 |
// lookup from us -- sails straight past it. Response.prototype is resolved
|
|
|
320 |
// per call, so it catches every client regardless.
|
|
|
321 |
// Read the clone with the ORIGINAL reader; the patched one recurses forever.
|
|
|
322 |
+ " ['json','text'].forEach(function(name){"
|
|
|
323 |
+ " var orig = Response.prototype[name];"
|
|
|
324 |
+ " if (!orig) return;"
|
|
|
325 |
+ " Response.prototype[name] = function(){"
|
|
|
326 |
+ " try { var u = this.url || '';"
|
|
|
327 |
+ " if (u.indexOf('getDeviceInfo') !== -1 && !this.__oppoSeenBody) {"
|
|
|
328 |
+ " var c = this.clone(); c.__oppoSeenBody = 1;"
|
|
|
329 |
+ " orig.call(c).then(function(t){ window.__oppoResp = (typeof t === 'string') ? t : JSON.stringify(t); });"
|
|
|
330 |
+ " } } catch(e){}"
|
|
|
331 |
+ " return orig.apply(this, arguments);"
|
|
|
332 |
+ " };"
|
|
|
333 |
+ " });"
|
| 37420 |
amit |
334 |
+ "}");
|
|
|
335 |
} catch (Exception e) {
|
|
|
336 |
LOGGER.warn("Could not install the response capture hook: " + e.getMessage());
|
|
|
337 |
}
|
|
|
338 |
}
|
|
|
339 |
|
| 37428 |
amit |
340 |
/** Last response from the captcha widget itself -- says WHY a drag was refused. */
|
|
|
341 |
private String captchaSignal(WebDriver driver) {
|
|
|
342 |
try {
|
| 37434 |
amit |
343 |
Object v = ((JavascriptExecutor) driver).executeScript(
|
|
|
344 |
"return 'verifies=' + (window.__oppoVerifyCount||0)"
|
|
|
345 |
+ " + ' lastVerify=' + (window.__oppoVerify || 'NONE')"
|
|
|
346 |
+ " + ' | ' + (window.__oppoCaptcha || 'none');");
|
| 37428 |
amit |
347 |
return v == null ? null : v.toString();
|
|
|
348 |
} catch (Exception e) {
|
|
|
349 |
return null;
|
|
|
350 |
}
|
|
|
351 |
}
|
|
|
352 |
|
| 37429 |
amit |
353 |
/** Poll for the response instead of reading once: the success signal can fire
|
|
|
354 |
* before the lookup request has completed, which loses the payload. */
|
|
|
355 |
private String awaitResponse(WebDriver driver, long timeoutMs) {
|
|
|
356 |
long deadline = System.currentTimeMillis() + timeoutMs;
|
|
|
357 |
while (System.currentTimeMillis() < deadline) {
|
|
|
358 |
String json = capturedResponse(driver);
|
|
|
359 |
if (json != null && !json.trim().isEmpty()) return json;
|
|
|
360 |
try {
|
|
|
361 |
Thread.sleep(300);
|
|
|
362 |
} catch (InterruptedException ie) {
|
|
|
363 |
Thread.currentThread().interrupt();
|
|
|
364 |
return null;
|
|
|
365 |
}
|
|
|
366 |
}
|
|
|
367 |
return null;
|
|
|
368 |
}
|
|
|
369 |
|
| 37420 |
amit |
370 |
private String capturedResponse(WebDriver driver) {
|
|
|
371 |
try {
|
|
|
372 |
Object v = ((JavascriptExecutor) driver).executeScript("return window.__oppoResp || null;");
|
|
|
373 |
return v == null ? null : v.toString();
|
|
|
374 |
} catch (Exception e) {
|
|
|
375 |
return null;
|
|
|
376 |
}
|
|
|
377 |
}
|
|
|
378 |
|
|
|
379 |
private void clearCapture(WebDriver driver) {
|
|
|
380 |
try {
|
|
|
381 |
((JavascriptExecutor) driver).executeScript("window.__oppoResp = null;");
|
|
|
382 |
} catch (Exception ignored) {
|
|
|
383 |
}
|
|
|
384 |
}
|
|
|
385 |
|
|
|
386 |
/** data.regDate is epoch millis; absent or blank means the device is not activated yet. */
|
|
|
387 |
private LocalDate regDateFrom(String json) {
|
|
|
388 |
if (json == null) return null;
|
|
|
389 |
try {
|
|
|
390 |
JSONObject data = new JSONObject(json).optJSONObject("data");
|
|
|
391 |
if (data == null) return null;
|
|
|
392 |
String reg = data.optString("regDate", "");
|
|
|
393 |
if (reg == null || reg.trim().isEmpty() || "null".equals(reg)) return null;
|
|
|
394 |
return Instant.ofEpochMilli(Long.parseLong(reg.trim()))
|
|
|
395 |
.atZone(ZoneId.of("Asia/Kolkata")).toLocalDate();
|
|
|
396 |
} catch (Exception e) {
|
|
|
397 |
LOGGER.warn("Could not read regDate from the Oppo response: " + e.getMessage());
|
|
|
398 |
return null;
|
|
|
399 |
}
|
|
|
400 |
}
|
|
|
401 |
|
| 37422 |
amit |
402 |
/**
|
| 37439 |
amit |
403 |
* How close the ring must get to the hole before we release, in px.
|
| 37422 |
amit |
404 |
*
|
| 37439 |
amit |
405 |
* There is deliberately NO assumed ratio anywhere in this class. px-per-px is derived
|
|
|
406 |
* from real movement on every attempt and re-derived on every step within it -- see
|
|
|
407 |
* aim(). Earlier revisions carried an ASSUMED_RATIO constant, it was tuned twice
|
|
|
408 |
* (1.7 -> 0.67 -> 1.7) against contaminated measurements, and it never helped. Do not
|
|
|
409 |
* reintroduce one: the widget's px-per-px genuinely varies by environment (1.4-1.5 in
|
|
|
410 |
* production, 1.1-1.2 locally) and by brand, which is exactly why it must be measured.
|
| 37422 |
amit |
411 |
*/
|
| 37445 |
amit |
412 |
/**
|
|
|
413 |
* Captcha attempts per imei per tick, before giving up and letting the next tick
|
|
|
414 |
* retry with a FRESH page and session.
|
|
|
415 |
*
|
|
|
416 |
* A captcha failure is a technical failure, not an answer, so the imei is never
|
|
|
417 |
* abandoned -- it stays pending and comes back in ~5 minutes. What this caps is how
|
|
|
418 |
* many times we grind on it inside one already-refused session.
|
|
|
419 |
*
|
|
|
420 |
* 20 was right before the drag was fixed: pre-glide, 33% of oppo's successes came
|
|
|
421 |
* from attempts 8-20. Post-glide (r37440) the drag lands first or second try and the
|
|
|
422 |
* tail stopped paying. Measured over 308 post-glide successes and 279 exhausted imeis:
|
|
|
423 |
* cap 5 -> 95.5% of successes kept, 67.7% of captcha work saved
|
|
|
424 |
* cap 7 -> 97.7% kept, 58.4% saved <- here
|
|
|
425 |
* cap 10 -> 99.0% kept, 44.8% saved
|
|
|
426 |
* Realme keeps its own, higher cap: it has no glide yet, so its successes still
|
|
|
427 |
* spread to attempt 10+ and 7 would cost it 16.5%. Re-check the histogram after a
|
|
|
428 |
* day; if the distribution shifts, so should this.
|
|
|
429 |
*/
|
|
|
430 |
private static final int MAX_CAPTCHA_ATTEMPTS = 7;
|
|
|
431 |
|
| 37422 |
amit |
432 |
private static final double ALIGNED_PX = 2.0;
|
|
|
433 |
|
| 37437 |
amit |
434 |
/**
|
|
|
435 |
* margin-left for logging only. Reading it off a detached element throws, and this is
|
|
|
436 |
* a diagnostic -- it must never be the reason an attempt is abandoned.
|
|
|
437 |
*/
|
|
|
438 |
private String marginLeftOf(WebElement el) {
|
|
|
439 |
try {
|
|
|
440 |
return el.getCssValue("margin-left");
|
|
|
441 |
} catch (Exception e) {
|
|
|
442 |
return "?";
|
|
|
443 |
}
|
| 37424 |
amit |
444 |
}
|
|
|
445 |
|
| 37437 |
amit |
446 |
/** Seed probe, in slider px. Deliberately small: it only has to produce a first
|
|
|
447 |
* measurable movement, and it must be too small to cross the hole. */
|
|
|
448 |
private static final int SEED_NUDGE = 12;
|
| 37440 |
amit |
449 |
/** Sub-moves per leg, and the pause between them. A leg is traversed as many small
|
|
|
450 |
* eased steps rather than one jump, so the pointer describes a continuous path. */
|
|
|
451 |
private static final int MAX_GLIDE_STEPS = 18;
|
|
|
452 |
private static final int GLIDE_PX_PER_STEP = 8;
|
|
|
453 |
private static final long GLIDE_PAUSE_MS = 45;
|
| 37437 |
amit |
454 |
private static final int MAX_AIM_STEPS = 5;
|
|
|
455 |
private static final int MAX_STEP = 140;
|
|
|
456 |
|
| 37440 |
amit |
457 |
/**
|
|
|
458 |
* Traverse one leg as many small eased sub-moves instead of a single jump.
|
|
|
459 |
*
|
|
|
460 |
* Nothing about the distance changes -- it is still whatever the derived ratio says.
|
|
|
461 |
* Only the path does. The widget scores trajectory as well as final position, and
|
|
|
462 |
* returns POSITION_MISMATCH for either, so a drag that teleports in 2 jumps and one
|
|
|
463 |
* that slides in over 15 are not equally credible. Realme holds ~1.93s and converts
|
|
|
464 |
* 54-74%; oppo held 0.91s in 2-3 jumps and converts 15%. That is suggestive, not
|
|
|
465 |
* proof -- this change is the test.
|
|
|
466 |
*
|
|
|
467 |
* Ease-out (1-(1-t)^2) so it starts quickly and settles, which is how a hand moves.
|
|
|
468 |
*/
|
|
|
469 |
private void glide(Actions actionProvider, int distance) throws InterruptedException {
|
|
|
470 |
int steps = Math.abs(distance) / GLIDE_PX_PER_STEP;
|
|
|
471 |
if (steps > MAX_GLIDE_STEPS) steps = MAX_GLIDE_STEPS;
|
|
|
472 |
if (steps <= 1) {
|
|
|
473 |
actionProvider.moveByOffset(distance, 0).perform();
|
|
|
474 |
return;
|
|
|
475 |
}
|
|
|
476 |
int done = 0;
|
|
|
477 |
for (int i = 1; i <= steps; i++) {
|
|
|
478 |
double t = (double) i / steps;
|
|
|
479 |
int cumulative = (int) Math.round(distance * (1 - Math.pow(1 - t, 2)));
|
|
|
480 |
int delta = cumulative - done;
|
|
|
481 |
if (delta != 0) {
|
|
|
482 |
actionProvider.moveByOffset(delta, 0).perform();
|
|
|
483 |
done += delta;
|
|
|
484 |
}
|
|
|
485 |
Thread.sleep(GLIDE_PAUSE_MS);
|
|
|
486 |
}
|
|
|
487 |
if (done != distance) {
|
|
|
488 |
actionProvider.moveByOffset(distance - done, 0).perform();
|
|
|
489 |
}
|
|
|
490 |
}
|
|
|
491 |
|
| 37438 |
amit |
492 |
/** Centre of the draggable ring, in page CSS px, or null if it is not there. */
|
|
|
493 |
private Double pieceCentreX(WebDriver driver) {
|
|
|
494 |
try {
|
|
|
495 |
WebElement piece = driver.findElement(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_sub-slider_')]"));
|
|
|
496 |
return piece.getLocation().getX() + (piece.getSize().getWidth() / 2.0);
|
|
|
497 |
} catch (Exception e) {
|
|
|
498 |
return null;
|
|
|
499 |
}
|
|
|
500 |
}
|
|
|
501 |
|
| 37422 |
amit |
502 |
/**
|
| 37438 |
amit |
503 |
* Drag the ring onto the hole, deriving px-per-px as we go.
|
| 37437 |
amit |
504 |
*
|
| 37438 |
amit |
505 |
* Why this does not re-run circle detection: it cannot. Hough needs to resolve TWO
|
|
|
506 |
* circles of similar radius, and as the ring closes on the hole they overlap and stop
|
|
|
507 |
* being two -- so the measurement dies exactly at convergence. Measured in production
|
|
|
508 |
* after r37437: 25 of 36 attempts lost the circles on the very step after the main
|
|
|
509 |
* move, and NOT ONE ever reported aligned. The old refine() hit the same wall and
|
|
|
510 |
* silently released. Any loop built on "distance between two detected circles" is
|
|
|
511 |
* blind at the only moment that matters.
|
| 37437 |
amit |
512 |
*
|
| 37438 |
amit |
513 |
* So the target is fixed ONCE, from the detection already done while the two are far
|
|
|
514 |
* apart and resolvable, and from then on we track the RING'S OWN ELEMENT, which stays
|
|
|
515 |
* readable however close it gets. Verified locally against the live widget: the ring's
|
|
|
516 |
* rect tracks the drag cleanly the whole way in (+10 mouse -> +11 ring, repeatably).
|
| 37437 |
amit |
517 |
*
|
| 37438 |
amit |
518 |
* The circle x's are already CSS px relative to the bg element -- the crop in
|
|
|
519 |
* getMatCircles2 uses CSS coordinates and chrome runs with
|
|
|
520 |
* --force-device-scale-factor=1 -- so the only conversion needed is the bg offset.
|
|
|
521 |
*
|
|
|
522 |
* Every move is also a measurement: the ratio is re-derived from what that move
|
|
|
523 |
* actually achieved, and it is signed, so overshoot corrects itself instead of needing
|
|
|
524 |
* to be detected.
|
|
|
525 |
*
|
|
|
526 |
* @return total slider px moved, or 0 if the ring could not be located at all.
|
| 37422 |
amit |
527 |
*/
|
| 37438 |
amit |
528 |
private int aim(WebDriver driver, Actions actionProvider, double[] circleXs) throws Exception {
|
|
|
529 |
Double piece = pieceCentreX(driver);
|
|
|
530 |
if (piece == null) {
|
|
|
531 |
LOGGER.warn("Aim: no ring element to track");
|
|
|
532 |
return 0;
|
|
|
533 |
}
|
|
|
534 |
int bgX;
|
|
|
535 |
try {
|
|
|
536 |
bgX = driver.findElement(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_bg_')]")).getLocation().getX();
|
|
|
537 |
} catch (Exception e) {
|
|
|
538 |
LOGGER.warn("Aim: bg element gone - " + e.getMessage());
|
|
|
539 |
return 0;
|
|
|
540 |
}
|
|
|
541 |
|
|
|
542 |
// Of the two detected circles the ring is whichever sits at the ring element; the
|
|
|
543 |
// other one is the hole we are aiming at.
|
|
|
544 |
double a = bgX + circleXs[0], b = bgX + circleXs[1];
|
|
|
545 |
double target = (Math.abs(a - piece) <= Math.abs(b - piece)) ? b : a;
|
|
|
546 |
LOGGER.info("Aim: ring at {} target at {} ({}px to close)",
|
|
|
547 |
Math.round(piece), Math.round(target), Math.round(target - piece));
|
|
|
548 |
|
|
|
549 |
double ratio = 0; // ring px per mouse px, signed, derived from real moves
|
| 37437 |
amit |
550 |
int total = 0;
|
|
|
551 |
int step = SEED_NUDGE;
|
| 37422 |
amit |
552 |
|
| 37437 |
amit |
553 |
for (int i = 0; i < MAX_AIM_STEPS; i++) {
|
| 37440 |
amit |
554 |
glide(actionProvider, step);
|
| 37437 |
amit |
555 |
total += step;
|
| 37422 |
amit |
556 |
|
| 37438 |
amit |
557 |
Double now = pieceCentreX(driver);
|
|
|
558 |
if (now == null) {
|
|
|
559 |
LOGGER.warn("Aim step {}: lost the ring after {}px", i, total);
|
| 37437 |
amit |
560 |
return total;
|
|
|
561 |
}
|
| 37438 |
amit |
562 |
double moved = now - piece;
|
|
|
563 |
if (Math.abs(moved) > 0.5) {
|
|
|
564 |
double observed = moved / step;
|
|
|
565 |
ratio = (ratio == 0) ? observed : (ratio + observed) / 2;
|
| 37437 |
amit |
566 |
}
|
| 37438 |
amit |
567 |
double remaining = target - now;
|
|
|
568 |
LOGGER.info("Aim step {}: mouse {}px -> ring moved {}px, {}px left, ratio {}",
|
|
|
569 |
i, step, Math.round(moved), Math.round(remaining), String.format("%.2f", ratio));
|
| 37422 |
amit |
570 |
|
| 37438 |
amit |
571 |
if (Math.abs(remaining) <= ALIGNED_PX) {
|
|
|
572 |
LOGGER.info("Aim: aligned to {}px after {} step(s), {}px of slider",
|
|
|
573 |
Math.round(remaining), i + 1, total);
|
| 37437 |
amit |
574 |
return total;
|
|
|
575 |
}
|
| 37438 |
amit |
576 |
piece = now;
|
|
|
577 |
if (ratio == 0) { // nothing measurable yet, probe again
|
| 37437 |
amit |
578 |
step = SEED_NUDGE;
|
|
|
579 |
continue;
|
|
|
580 |
}
|
| 37438 |
amit |
581 |
int next = (int) Math.round(remaining / ratio);
|
|
|
582 |
if (next == 0) next = (remaining > 0 ? 1 : -1);
|
| 37437 |
amit |
583 |
if (next > MAX_STEP) next = MAX_STEP;
|
| 37438 |
amit |
584 |
if (next < -MAX_STEP) next = -MAX_STEP;
|
|
|
585 |
step = next;
|
| 37437 |
amit |
586 |
}
|
| 37438 |
amit |
587 |
LOGGER.info("Aim: {} steps used, {}px of slider, still short", MAX_AIM_STEPS, total);
|
| 37437 |
amit |
588 |
return total;
|
| 32146 |
amit.gupta |
589 |
}
|
| 30209 |
amit.gupta |
590 |
|
| 32146 |
amit.gupta |
591 |
private Mat getMatCircles(WebDriver driver, String fileName) throws Exception {
|
| 30209 |
amit.gupta |
592 |
|
| 32146 |
amit.gupta |
593 |
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
|
|
|
594 |
BufferedImage fullImg = ImageIO.read(screenshot);
|
| 30209 |
amit.gupta |
595 |
|
| 32146 |
amit.gupta |
596 |
// Get the location of element on the page
|
| 36583 |
amit |
597 |
WebElement imageElement = driver.findElement(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_bg_')]"));
|
| 32146 |
amit.gupta |
598 |
Point point = imageElement.getLocation();
|
| 30209 |
amit.gupta |
599 |
|
| 32146 |
amit.gupta |
600 |
// Get width and height of the element
|
|
|
601 |
int eleWidth = imageElement.getSize().getWidth();
|
|
|
602 |
int eleHeight = imageElement.getSize().getHeight();
|
| 30209 |
amit.gupta |
603 |
|
| 32146 |
amit.gupta |
604 |
// Crop the entire page screenshot to get only element screenshot
|
|
|
605 |
BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(),
|
|
|
606 |
eleWidth, eleHeight);
|
|
|
607 |
ImageIO.write(eleScreenshot, "png", screenshot);
|
| 30209 |
amit.gupta |
608 |
|
| 32146 |
amit.gupta |
609 |
// Copy the element screenshot to disk
|
|
|
610 |
File screenshotLocation = new File(fileName);
|
|
|
611 |
FileUtils.copyFile(screenshot, screenshotLocation);
|
| 30209 |
amit.gupta |
612 |
|
| 32146 |
amit.gupta |
613 |
Imgcodecs imageCodecs = new Imgcodecs();
|
|
|
614 |
Mat grayMatrix = imageCodecs.imread(fileName, Imgcodecs.IMREAD_COLOR);
|
|
|
615 |
Imgproc.cvtColor(grayMatrix, grayMatrix, Imgproc.COLOR_BGR2GRAY);
|
|
|
616 |
Mat medianBlur = new Mat();
|
|
|
617 |
Imgproc.medianBlur(grayMatrix, medianBlur, 5);
|
|
|
618 |
Mat circles = new Mat();
|
|
|
619 |
Imgproc.HoughCircles(medianBlur, circles, Imgproc.HOUGH_GRADIENT, 1, 15, 50, 20, 17, 25);
|
|
|
620 |
return circles;
|
|
|
621 |
}
|
| 30209 |
amit.gupta |
622 |
|
| 34413 |
amit.gupta |
623 |
private double[] getMatCircles2(WebDriver driver, String fileName) throws Exception {
|
|
|
624 |
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
|
|
|
625 |
BufferedImage fullImg = ImageIO.read(screenshot);
|
|
|
626 |
|
|
|
627 |
// Get the location of element on the page
|
| 36583 |
amit |
628 |
WebElement imageElement = driver.findElement(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_bg_')]"));
|
| 34413 |
amit.gupta |
629 |
Point point = imageElement.getLocation();
|
|
|
630 |
|
|
|
631 |
// Get width and height of the element
|
|
|
632 |
int eleWidth = imageElement.getSize().getWidth();
|
|
|
633 |
int eleHeight = imageElement.getSize().getHeight();
|
|
|
634 |
|
|
|
635 |
// Crop the entire page screenshot to get only element screenshot
|
|
|
636 |
BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(),
|
|
|
637 |
eleWidth, eleHeight);
|
|
|
638 |
ImageIO.write(eleScreenshot, "png", screenshot);
|
|
|
639 |
|
|
|
640 |
// Copy the element screenshot to disk
|
|
|
641 |
File screenshotLocation = new File(fileName);
|
|
|
642 |
FileUtils.copyFile(screenshot, screenshotLocation);
|
|
|
643 |
|
|
|
644 |
Mat src = Imgcodecs.imread(fileName);
|
|
|
645 |
if (src.empty()) {
|
|
|
646 |
System.err.println("Cannot read image: " + fileName);
|
| 36253 |
amit |
647 |
new File(fileName).delete();
|
| 34413 |
amit.gupta |
648 |
return null;
|
|
|
649 |
}
|
|
|
650 |
|
|
|
651 |
Mat gray = new Mat();
|
|
|
652 |
Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
|
|
|
653 |
Imgproc.GaussianBlur(gray, gray, new Size(9, 9), 2, 2);
|
|
|
654 |
|
|
|
655 |
Mat circles = new Mat();
|
|
|
656 |
Imgproc.HoughCircles(gray, circles, Imgproc.HOUGH_GRADIENT, 1.0,
|
|
|
657 |
20, // min distance between centers
|
|
|
658 |
100, 20, // param1 (Canny high threshold), param2 (accumulator threshold)
|
|
|
659 |
10, 50); // minRadius, maxRadius (adjust as needed)
|
|
|
660 |
|
| 36253 |
amit |
661 |
if (circles.cols() < 2) {
|
|
|
662 |
new File(fileName).delete();
|
|
|
663 |
return null;
|
|
|
664 |
}
|
| 34413 |
amit.gupta |
665 |
|
|
|
666 |
for (int i = 0; i < circles.cols(); i++) {
|
|
|
667 |
double[] c1 = circles.get(0, i);
|
|
|
668 |
for (int j = i + 1; j < circles.cols(); j++) {
|
|
|
669 |
double[] c2 = circles.get(0, j);
|
|
|
670 |
if (Math.abs(c1[2] - c2[2]) < 3) { // check similar radius
|
| 36253 |
amit |
671 |
new File(fileName).delete();
|
| 34413 |
amit.gupta |
672 |
return new double[] {
|
|
|
673 |
c1[0], c2[0] // x-coordinates
|
|
|
674 |
};
|
|
|
675 |
}
|
|
|
676 |
}
|
|
|
677 |
}
|
|
|
678 |
|
| 36253 |
amit |
679 |
new File(fileName).delete();
|
| 34413 |
amit.gupta |
680 |
return null;
|
|
|
681 |
}
|
|
|
682 |
|
|
|
683 |
|
| 30209 |
amit.gupta |
684 |
}
|