Subversion Repositories SmartDukaan

Rev

Rev 37459 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
37459 amit 1
package com.smartdukaan.cron.scheduled;
2
 
3
import org.apache.logging.log4j.LogManager;
4
import org.apache.logging.log4j.Logger;
5
import org.json.JSONObject;
6
import org.openqa.selenium.By;
7
import org.openqa.selenium.Dimension;
8
import org.openqa.selenium.JavascriptExecutor;
9
import org.openqa.selenium.WebDriver;
10
import org.openqa.selenium.chrome.ChromeDriver;
11
import org.openqa.selenium.chrome.ChromeOptions;
12
import org.openqa.selenium.support.ui.ExpectedConditions;
13
import org.openqa.selenium.support.ui.WebDriverWait;
14
import org.springframework.beans.factory.annotation.Autowired;
15
import org.springframework.stereotype.Component;
16
 
37673 amit 17
import com.spice.profitmandi.common.util.BrowserLane;
18
 
37459 amit 19
import java.time.Instant;
20
import java.time.LocalDate;
21
import java.time.ZoneId;
22
import java.time.format.DateTimeFormatter;
23
import java.util.HashMap;
24
import java.util.Iterator;
25
import java.util.List;
26
import java.util.Map;
27
import java.util.concurrent.TimeUnit;
28
 
29
/**
30
 * Website-based activation-date pull for Motorola, mirroring
31
 * CheckOppoWarrantyTask / CheckRealmeWarrantyTask.
32
 *
33
 * Differs from those two in one important way: there is no slider. The page
34
 * (https://en-in.support.motorola.com/app/warranty/check) is Oracle Service
35
 * Cloud, so there is no DingXiang widget, no canvas, and OpenCV is not needed
36
 * at all. The lookup is a single AJAX call and the whole job is to read its
37
 * JSON -- the same "read the JSON, not the DOM" lesson that realme taught.
38
 *
39
 * What gates it instead is reCAPTCHA v2, raised by the endpoint itself as an
40
 * "==CHALLENGE REQUIRED==" body. See MotorolaChallengeSolver.
41
 */
42
@Component
43
public class CheckMotorolaWarrantyTask {
44
 
45
    private static final Logger LOGGER = LogManager.getLogger(CheckMotorolaWarrantyTask.class);
46
 
47
    private static final String PAGE_URL = "https://en-in.support.motorola.com/app/warranty/check";
48
 
49
    /** Field and submit, confirmed against the live page. The submit is a
50
     *  type=button; the only type=submit on the page is a FOOTER control. */
51
    private static final String IMEI_INPUT = "#rn_warrantylookup_3_serial";
52
    private static final String SUBMIT_BTN = ".my-products__detect-device-button";
53
 
54
    /** RightNow routes the lookup through this widget endpoint. */
55
    private static final String LOOKUP_ENDPOINT = "/ci/ajax/widget/custom/warranty/warrantylookup";
56
 
57
    private static final String CHALLENGE_MARKER = "==CHALLENGE REQUIRED==";
58
 
59
    /** Optional: no bean supplied means challenges cannot be answered. */
60
    @Autowired(required = false)
61
    private MotorolaChallengeSolver challengeSolver;
62
 
63
    /** Logged once per batch so a first successful payload reveals its shape
64
     *  without spamming the log for every imei. */
65
    private boolean loggedSamplePayload = false;
66
 
67
    public Map<String, LocalDate> checkWarranty(List<String> imeis) {
68
        Map<String, LocalDate> dateMap = new HashMap<>();
69
        if (imeis == null || imeis.isEmpty()) {
70
            return dateMap;
71
        }
72
        loggedSamplePayload = false;
73
 
74
        ChromeOptions options = new ChromeOptions();
75
        Map<String, Object> prefsMap = new HashMap<>();
76
        prefsMap.put("profile.default_content_settings.popups", 0);
77
        prefsMap.put("download.prompt", false);
78
        options.setExperimentalOption("prefs", prefsMap);
79
        // Prod path, same as the oppo/realme drivers. Left overridable so the job
80
        // can be exercised on a dev box against Chrome for Testing without
81
        // editing the file:
82
        //   -Dwebdriver.chrome.driver=/Users/<u>/cft/chromedriver/chromedriver
83
        //   -Dwebdriver.chrome.binary="/Users/<u>/cft/chrome/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing"
84
        if (System.getProperty("webdriver.chrome.driver") == null) {
85
            System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver");
86
        }
87
        String binary = System.getProperty("webdriver.chrome.binary");
88
        if (binary != null && !binary.isEmpty()) {
89
            options.setBinary(binary);
90
        }
91
        options.addArguments("--headless");
92
        options.addArguments("--no-sandbox");
93
        options.addArguments("--disable-extensions");
94
        options.addArguments("--test-type");
95
        // Same memory posture as the oppo/realme drivers: this box co-hosts a
96
        // 9.4GB tomcat with ~3GB available and has been OOM-killed twice, with
97
        // tomcat the victim both times. Each driver tree is roughly 850MB, so
98
        // do not let this job overlap the oppo/realme window.
99
        options.addArguments("--disable-gpu");
100
        options.addArguments("--disable-dev-shm-usage");
101
        options.addArguments("--disable-software-rasterizer");
102
 
103
        WebDriver driver = null;
37673 amit 104
 
105
        // The "do not let this job overlap the oppo/realme window" note above this method was
106
        // only ever a comment -- nothing enforced it. BrowserLane is what makes it true, and it
107
        // is wired here now so that whenever this task does get a trigger it is already safe.
108
        BrowserLane lane = BrowserLane.acquire("Motorola", 5, TimeUnit.MINUTES);
109
        if (lane == null) {
110
            return dateMap;
111
        }
112
 
37459 amit 113
        try {
114
            driver = new ChromeDriver(options);
115
            driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
116
            driver.manage().timeouts().implicitlyWait(8, TimeUnit.SECONDS);
117
            driver.manage().window().setSize(new Dimension(1600, 900));
118
            driver.manage().deleteAllCookies();
119
 
120
            WebDriverWait wait = new WebDriverWait(driver, 12);
121
 
122
            driver.get(PAGE_URL);
123
            installResponseCapture(driver);
124
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(IMEI_INPUT)));
125
 
126
            for (Iterator<String> it = imeis.iterator(); it.hasNext(); ) {
127
                String imei = it.next();
128
                try {
129
                    LocalDate date = lookupOne(driver, wait, imei);
130
                    // A null date is still a result: the caller writes it back
131
                    // as a null activation, which bumps createTimestamp and so
132
                    // defers this imei to the next pool sweep.
133
                    dateMap.put(imei, date);
134
                } catch (ChallengeUnansweredException ce) {
135
                    // Abandon the whole batch. Every subsequent lookup in this
136
                    // session would hit the same wall, and continuing would be
137
                    // pure load on Motorola for no data.
138
                    LOGGER.warn("Motorola raised a reCAPTCHA challenge and no solver is configured "
139
                            + "- abandoning the batch after {} of {} imeis", dateMap.size(), imeis.size());
140
                    break;
141
                } catch (Exception e) {
142
                    LOGGER.warn("Motorola lookup failed for {} - {}", imei, e.getMessage());
143
                }
144
            }
145
        } catch (Exception e) {
146
            LOGGER.error("Motorola warranty run failed", e);
147
        } finally {
148
            if (driver != null) {
149
                try {
150
                    driver.quit();
151
                } catch (Exception ignored) {
152
                }
153
            }
37673 amit 154
            // After quit(), not before: the 850MB is resident for the whole chunk.
155
            lane.close();
37459 amit 156
        }
157
        LOGGER.info("Motorola: resolved {} of {} imeis", dateMap.size(), imeis.size());
158
        return dateMap;
159
    }
160
 
161
    /** One imei: clear, type, submit, wait for the widget's JSON. */
162
    private LocalDate lookupOne(WebDriver driver, WebDriverWait wait, String imei) throws Exception {
163
        clearCapture(driver);
164
 
165
        // Re-navigating per imei is what oppo/realme do, and it is what keeps a
166
        // stale previous result from being read as this imei's answer.
167
        driver.get(PAGE_URL);
168
        installResponseCapture(driver);
169
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(IMEI_INPUT)));
170
 
171
        // The field is React-backed: a plain sendKeys can be discarded on
172
        // re-render, so drive the native setter and fire the events the widget
173
        // actually listens for.
174
        ((JavascriptExecutor) driver).executeScript(
175
                "var f = document.querySelector(arguments[0]);"
176
              + "var s = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype,'value').set;"
177
              + "s.call(f, arguments[1]);"
178
              + "f.dispatchEvent(new Event('input',{bubbles:true}));"
179
              + "f.dispatchEvent(new Event('change',{bubbles:true}));",
180
                IMEI_INPUT, imei);
181
 
182
        driver.findElement(By.cssSelector(SUBMIT_BTN)).click();
183
 
184
        String body = awaitResponse(driver, 15000);
185
        if (body == null) {
186
            LOGGER.info("Motorola: no lookup response captured for {}", imei);
187
            return null;
188
        }
189
 
190
        if (body.contains(CHALLENGE_MARKER)) {
191
            String siteKey = extractSiteKey(body);
192
            if (challengeSolver == null) {
193
                throw new ChallengeUnansweredException(siteKey);
194
            }
195
            String token = challengeSolver.solve(siteKey, PAGE_URL);
196
            if (token == null || token.isEmpty()) {
197
                throw new ChallengeUnansweredException(siteKey);
198
            }
199
            body = submitWithToken(driver, wait, token);
200
            if (body == null || body.contains(CHALLENGE_MARKER)) {
201
                LOGGER.warn("Motorola rejected the supplied challenge token for {}", imei);
202
                return null;
203
            }
204
        }
205
 
206
        if (!loggedSamplePayload) {
207
            // The success payload shape is unverified -- every observed response
208
            // so far has been the challenge, so no real warranty body has been
209
            // seen. Log the first one in full so the parser can be tightened to
210
            // the actual field names instead of guessing.
211
            LOGGER.info("Motorola sample lookup payload for {}: {}", imei,
212
                    body.substring(0, Math.min(body.length(), 1500)));
213
            loggedSamplePayload = true;
214
        }
215
        return parseActivationDate(body);
216
    }
217
 
218
    /** Replays the lookup with a challenge token attached. */
219
    private String submitWithToken(WebDriver driver, WebDriverWait wait, String token) {
220
        try {
221
            clearCapture(driver);
222
            ((JavascriptExecutor) driver).executeScript(
223
                    "var el = document.getElementById('g-recaptcha-response');"
224
                  + "if (!el) { el = document.createElement('textarea');"
225
                  + "  el.id = 'g-recaptcha-response'; el.name = 'g-recaptcha-response';"
226
                  + "  el.style.display='none'; document.body.appendChild(el); }"
227
                  + "el.value = arguments[0];", token);
228
            driver.findElement(By.cssSelector(SUBMIT_BTN)).click();
229
            return awaitResponse(driver, 15000);
230
        } catch (Exception e) {
231
            LOGGER.warn("Challenge replay failed - {}", e.getMessage());
232
            return null;
233
        }
234
    }
235
 
236
    /** reCAPTCHA sitekey out of the challenge blob, for the solver. */
237
    private String extractSiteKey(String body) {
238
        try {
239
            int i = body.indexOf("publicKey:");
240
            if (i < 0) return null;
241
            int q1 = body.indexOf('"', i);
242
            int q2 = body.indexOf('"', q1 + 1);
243
            if (q1 < 0 || q2 < 0) return null;
244
            return body.substring(q1 + 1, q2).replace("\\", "");
245
        } catch (Exception e) {
246
            return null;
247
        }
248
    }
249
 
250
    /**
251
     * Pulls an activation/warranty-start date out of the payload.
252
     *
253
     * Deliberately tolerant about field names and formats: the real response
254
     * has never been observed past the challenge, so this tries the keys such
255
     * payloads normally carry and falls back to logging rather than guessing
256
     * wrong. Tighten it once the sample payload above appears in the log.
257
     */
258
    LocalDate parseActivationDate(String json) {
259
        if (json == null || json.trim().isEmpty()) return null;
260
        String[] keys = {"activationDate", "warrantyStartDate", "startDate",
261
                         "purchaseDate", "regDate", "inWarrantyStartDate", "shipDate"};
262
        try {
263
            JSONObject root = new JSONObject(json);
264
            JSONObject scope = root.optJSONObject("data") != null ? root.optJSONObject("data") : root;
265
            for (String key : keys) {
266
                if (!scope.has(key) || scope.isNull(key)) continue;
267
                LocalDate d = toLocalDate(scope.get(key).toString());
268
                if (d != null) return d;
269
            }
270
        } catch (Exception e) {
271
            LOGGER.debug("Motorola payload was not the JSON shape expected - {}", e.getMessage());
272
        }
273
        return null;
274
    }
275
 
276
    /** Epoch millis, or the date formats these portals typically emit. */
277
    private LocalDate toLocalDate(String raw) {
278
        if (raw == null) return null;
279
        String v = raw.trim();
280
        if (v.isEmpty() || "null".equalsIgnoreCase(v)) return null;
281
        try {
282
            if (v.matches("\\d{10,13}")) {
283
                long epoch = Long.parseLong(v);
284
                if (v.length() == 10) epoch *= 1000L;
285
                return Instant.ofEpochMilli(epoch).atZone(ZoneId.systemDefault()).toLocalDate();
286
            }
287
        } catch (Exception ignored) {
288
        }
289
        String[] patterns = {"yyyy-MM-dd", "dd-MM-yyyy", "MM/dd/yyyy", "dd/MM/yyyy", "yyyy/MM/dd"};
290
        for (String p : patterns) {
291
            try {
292
                return LocalDate.parse(v.length() > 10 ? v.substring(0, 10) : v,
293
                        DateTimeFormatter.ofPattern(p));
294
            } catch (Exception ignored) {
295
            }
296
        }
297
        return null;
298
    }
299
 
300
    /**
301
     * Captures the widget's AJAX body.
302
     *
303
     * Hooks XHR, fetch AND Response.prototype. The last one matters: a client
304
     * that grabbed globalThis.fetch when its bundle loaded sails straight past
305
     * a window.fetch wrapper, which is exactly what hid realme's lookup.
306
     */
307
    private void installResponseCapture(WebDriver driver) {
308
        try {
309
            ((JavascriptExecutor) driver).executeScript(
310
                "if (!window.__motoCapInstalled) {"
311
              + "  window.__motoCapInstalled = true; window.__motoResp = null;"
312
              + "  var hit = function(u){ return String(u).indexOf('" + LOOKUP_ENDPOINT + "') !== -1; };"
313
              + "  var O = XMLHttpRequest.prototype.open;"
314
              + "  XMLHttpRequest.prototype.open = function(m,u){"
315
              + "    var self=this;"
316
              + "    this.addEventListener('load', function(){"
317
              + "      try { if (hit(u)) window.__motoResp = self.responseText; } catch(e){}"
318
              + "    });"
319
              + "    return O.apply(this, arguments);"
320
              + "  };"
321
              + "  var F = window.fetch;"
322
              + "  if (F) { window.fetch = function(){"
323
              + "    var u = arguments[0]; u = (u && u.url) ? u.url : String(u);"
324
              + "    return F.apply(this, arguments).then(function(r){"
325
              + "      try { if (hit(u)) r.clone().text().then(function(t){ window.__motoResp = t; }); } catch(e){}"
326
              + "      return r;"
327
              + "    });"
328
              + "  }; }"
329
              + "  ['json','text'].forEach(function(name){"
330
              + "    var orig = Response.prototype[name];"
331
              + "    if (!orig) return;"
332
              + "    Response.prototype[name] = function(){"
333
              + "      try { var u = this.url || '';"
334
              + "        if (hit(u) && !this.__motoSeen) {"
335
              + "          var c = this.clone(); this.__motoSeen = 1;"
336
              + "          orig.call(c).then(function(t){"
337
              + "            window.__motoResp = (typeof t === 'string') ? t : JSON.stringify(t); });"
338
              + "        } } catch(e){}"
339
              + "      return orig.apply(this, arguments);"
340
              + "    };"
341
              + "  });"
342
              + "}");
343
        } catch (Exception e) {
344
            LOGGER.warn("Could not install the Motorola response capture hook: {}", e.getMessage());
345
        }
346
    }
347
 
348
    /** Poll rather than read once: the click can return before the AJAX lands. */
349
    private String awaitResponse(WebDriver driver, long timeoutMs) {
350
        long deadline = System.currentTimeMillis() + timeoutMs;
351
        while (System.currentTimeMillis() < deadline) {
352
            try {
353
                Object v = ((JavascriptExecutor) driver).executeScript("return window.__motoResp || null;");
354
                if (v != null && !v.toString().trim().isEmpty()) {
355
                    return v.toString();
356
                }
357
            } catch (Exception ignored) {
358
            }
359
            try {
360
                Thread.sleep(300);
361
            } catch (InterruptedException ie) {
362
                Thread.currentThread().interrupt();
363
                return null;
364
            }
365
        }
366
        return null;
367
    }
368
 
369
    private void clearCapture(WebDriver driver) {
370
        try {
371
            ((JavascriptExecutor) driver).executeScript("window.__motoResp = null;");
372
        } catch (Exception ignored) {
373
        }
374
    }
375
 
376
    /** Raised when the page challenges and nothing can answer it. */
377
    static class ChallengeUnansweredException extends Exception {
378
        ChallengeUnansweredException(String siteKey) {
379
            super("reCAPTCHA challenge raised (sitekey=" + siteKey + ") and no solver is configured");
380
        }
381
    }
382
}