Subversion Repositories SmartDukaan

Rev

Rev 37446 | Rev 37472 | 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.List;

@Component
class VivoImeiActivationService {

    @Autowired
    InventoryService inventoryService;

    @Autowired
    CaptchaService captchaService;

    @Autowired
    ActivatedImeiRepository activatedImeiRepository;

    private static final Logger LOGGER = LogManager.getLogger(VivoImeiActivationService.class);

    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void checkImeiActivation() throws Exception {
        if (LocalDate.now().getDayOfMonth() == 1) return;

        List<ImeiActivationTimestampModel> imeisActivationList = activatedImeiRepository
                .selectImeiActivationPendingByBrand("Vivo", 1, 50);
        LOGGER.info("Secondary imeisActivationList = {}, size = {}", imeisActivationList, imeisActivationList.size());

        processImeis(imeisActivationList);
    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void checkImeiActivationTertiary() throws Exception {
        if (LocalDate.now().getDayOfMonth() == 1) return;

        List<ImeiActivationTimestampModel> imeisActivationList = activatedImeiRepository
                .selectImeiActivationPendingByBrandTertiary("Vivo", 1, 10);
        LOGGER.info("Tertiary imeisActivationList = {}, size = {}", imeisActivationList, imeisActivationList.size());

        processImeis(imeisActivationList);
    }

    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 cookies
            HttpGet 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());
    }
}