Rev 37448 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.smartdukaan.cron.scheduled;import com.spice.profitmandi.common.web.client.HttpClientFactory;import com.spice.profitmandi.dao.model.ImeiActivationTimestampModel;import com.spice.profitmandi.dao.repository.fofo.ActivatedImeiRepository;import com.spice.profitmandi.service.inventory.InventoryService;import org.apache.http.NameValuePair;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.BasicCookieStore;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.json.JSONObject;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import org.springframework.transaction.annotation.Propagation;import org.springframework.transaction.annotation.Transactional;import java.io.IOException;import java.nio.charset.StandardCharsets;import java.time.LocalDate;import java.time.format.DateTimeFormatter;import java.util.ArrayList;import java.util.HashSet;import java.util.List;import java.util.Set;import java.util.function.Predicate;import java.util.stream.Collectors;@Componentclass VivoImeiActivationService {@AutowiredInventoryService inventoryService;@AutowiredCaptchaService captchaService;@AutowiredActivatedImeiRepository activatedImeiRepository;private static final Logger LOGGER = LogManager.getLogger(VivoImeiActivationService.class);/** Days an imei rests after an ANSWER. 0 = due again in the next day's pass. */private static final int DAYS = 0;/** Safety stop so a mis-set DAYS cannot pull an unbounded list into memory. */private static final int POOL_CAP = 20000;/*** ONE FULL PASS PER DAY over everything due, then nothing until tomorrow.** This was two @Scheduled jobs running in parallel every 5 minutes, capped at 50* (secondary) and 10 (tertiary) imeis a tick -- so it never finished a pass over a* 9,510-row pool, and any imei whose lookup failed was retried five minutes later* rather than the next day. The pool is snapshotted here and walked to the end; it* is never re-queried mid-pass, so no imei is asked twice inside a run.** The secondary/tertiary split is only two join paths to the same serial, so they* are concatenated into one list. distinct() is insurance: the secondary query* already excludes anything carrying a FofoLineItem.** The 1st-of-month skip is kept -- vivo's own portal is unreliable on that day.*/@Transactional(propagation = Propagation.NOT_SUPPORTED)public void checkImeiActivation() throws Exception {if (LocalDate.now().getDayOfMonth() == 1) return;List<ImeiActivationTimestampModel> pool = new ArrayList<>();pool.addAll(activatedImeiRepository.selectImeiActivationPendingByBrand("Vivo", DAYS, POOL_CAP));pool.addAll(activatedImeiRepository.selectImeiActivationPendingByBrandTertiary("Vivo", DAYS, POOL_CAP));List<ImeiActivationTimestampModel> imeisActivationList = pool.stream().filter(distinctBySerial()).collect(Collectors.toList());LOGGER.info("Vivo daily pass starting: {} imeis due", imeisActivationList.size());processImeis(imeisActivationList);LOGGER.info("Vivo daily pass finished: {} imeis", imeisActivationList.size());}private static Predicate<ImeiActivationTimestampModel> distinctBySerial() {Set<String> seen = new HashSet<>();return m -> seen.add(m.getSerialNumber());}private void processImeis(List<ImeiActivationTimestampModel> imeisActivationList) throws Exception {if (imeisActivationList.isEmpty()) return;BasicCookieStore cookieStore = new BasicCookieStore();try (CloseableHttpClient httpClient = HttpClientFactory.apacheHttpWithCookies(cookieStore)) {// Seed the session cookiesHttpGet seed = new HttpGet("https://www.vivo.com/in/support/IMEI");try (CloseableHttpResponse seedResponse = httpClient.execute(seed)) {EntityUtils.consumeQuietly(seedResponse.getEntity());}for (ImeiActivationTimestampModel imeiActivationTimestampModel : imeisActivationList) {// Never spend a captcha on a line item with no IMEI. Vivo answers// those with {"msg":"参数为空"} ("parameter is empty") and status 0,// which this code used to record as a captcha failure -- so a correct// code looked wrong, the row was never saved, and it came back every// 5 minutes forever. That was 76% of all rejections.String serial = imeiActivationTimestampModel.getSerialNumber();if (serial == null || serial.trim().isEmpty()) {LOGGER.warn("Skipping activation lookup: line item has no serial number");continue;}Captcha captcha = this.getNewCaptcha(httpClient, cookieStore);if (captcha == null) {// Solver returned something that is not a captcha code (it was down// for 46 days returning an nginx 502 page, which used to be submitted// to Vivo verbatim). Skip; this IMEI is picked up on the next run.continue;}this.getImeiActivation(captcha, httpClient, cookieStore, imeiActivationTimestampModel);}}}/** A solved captcha: the code, plus the image it came from so the verdict can be reported. */static final class Captcha {final String code;final byte[] image;final long fetchedAt;Captcha(String code, byte[] image) {this.code = code;this.image = image;this.fetchedAt = System.currentTimeMillis();}}public Captcha getNewCaptcha(CloseableHttpClient httpClient, BasicCookieStore cookieStore) throws Exception {HttpGet request = new HttpGet("https://www.vivo.com/in/support/generatingCodes");// Kept in memory rather than written to a shared /tmp/captcha.jpg: the two// Vivo jobs run concurrently and used to overwrite each other's image. The// bytes are needed for the verdict report anyway.byte[] bytes;try (CloseableHttpResponse response = httpClient.execute(request)) {bytes = EntityUtils.toByteArray(response.getEntity());}String code = captchaService.getCaptchaCode(bytes);return code == null ? null : new Captcha(code, bytes);}public void getImeiActivation(Captcha captcha, CloseableHttpClient httpClient,BasicCookieStore cookieStore,ImeiActivationTimestampModel vivoImeiAndActivationTimeStampModel) throws Exception {String imei = vivoImeiAndActivationTimeStampModel.getSerialNumber();List<NameValuePair> form = new ArrayList<>();form.add(new BasicNameValuePair("imei", imei));form.add(new BasicNameValuePair("code", captcha.code));HttpPost post = new HttpPost("https://www.vivo.com/in/support/checkCode");post.setEntity(new UrlEncodedFormEntity(form, StandardCharsets.UTF_8));JSONObject imeiActivationJson;try (CloseableHttpResponse response2 = httpClient.execute(post)) {imeiActivationJson = new JSONObject(EntityUtils.toString(response2.getEntity()));}JSONObject data = imeiActivationJson.getJSONObject("data");int status = data.getInt("status");// Vivo is the only judge of whether our guess was right; status 0 means the// captcha was wrong. Hand that back so the sample becomes training data.captchaService.reportVerdict(captcha.image, captcha.code, status != 0);if (status == 0) {// Vivo refuses ~56% of submissions in production, but reading the stored// images by hand showed 9 of 10 carried the CORRECT code, and no external// replication reproduces it (IMEI, user-agent, session reuse, concurrency,// source IP and submit delay were all measured and ruled out). The old log// line recorded five words and discarded the evidence, so capture it:// the code submitted, the imei, how long fetch->submit actually took inside// this process, the session cookies held at submit time, and Vivo's reply.LOGGER.info("Vivo rejected captcha code={} imei={} elapsedMs={} cookies={} response={}",captcha.code, imei, System.currentTimeMillis() - captcha.fetchedAt,cookieStore.getCookies(), imeiActivationJson);return;} else if (status == 2) {LOGGER.info("Received status 2 for {}, data {}", imei, data);activatedImeiRepository.saveActivation(imei, null);return;}JSONObject imeiQueryDto = data.getJSONObject("imeiQueryDto");LOGGER.info("imeiQueryDto - {}", imeiQueryDto);String dueTimeString = imeiQueryDto.getString("dueTime");if (dueTimeString.equals("")) {activatedImeiRepository.saveActivation(imei, null);return;}DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");dueTimeString = dueTimeString.split(" ")[0];LocalDate dueDate = LocalDate.parse(dueTimeString, formatter);activatedImeiRepository.saveActivation(imei, dueDate.atStartOfDay().minusYears(1));LOGGER.info("activatedImei saved for {} with dueDate {}", imei, dueDate.atStartOfDay());}}