Subversion Repositories SmartDukaan

Rev

Rev 37393 | Rev 37407 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 37393 Rev 37402
Line 21... Line 21...
21
import org.springframework.transaction.annotation.Propagation;
21
import org.springframework.transaction.annotation.Propagation;
22
import org.springframework.transaction.annotation.Transactional;
22
import org.springframework.transaction.annotation.Transactional;
23
 
23
 
24
import java.io.IOException;
24
import java.io.IOException;
25
import java.nio.charset.StandardCharsets;
25
import java.nio.charset.StandardCharsets;
26
import java.nio.file.Files;
-
 
27
import java.nio.file.Paths;
-
 
28
import java.time.LocalDate;
26
import java.time.LocalDate;
29
import java.time.format.DateTimeFormatter;
27
import java.time.format.DateTimeFormatter;
30
import java.util.ArrayList;
28
import java.util.ArrayList;
31
import java.util.List;
29
import java.util.List;
32
 
30
 
Line 76... Line 74...
76
            try (CloseableHttpResponse seedResponse = httpClient.execute(seed)) {
74
            try (CloseableHttpResponse seedResponse = httpClient.execute(seed)) {
77
                EntityUtils.consumeQuietly(seedResponse.getEntity());
75
                EntityUtils.consumeQuietly(seedResponse.getEntity());
78
            }
76
            }
79
 
77
 
80
            for (ImeiActivationTimestampModel imeiActivationTimestampModel : imeisActivationList) {
78
            for (ImeiActivationTimestampModel imeiActivationTimestampModel : imeisActivationList) {
81
                String captchaBreak = this.getNewCaptcha(httpClient, cookieStore);
79
                Captcha captcha = this.getNewCaptcha(httpClient, cookieStore);
82
                if (captchaBreak == null) {
80
                if (captcha == null) {
83
                    // Solver returned something that is not a captcha code (it was down
81
                    // Solver returned something that is not a captcha code (it was down
84
                    // for 46 days returning an nginx 502 page, which used to be submitted
82
                    // for 46 days returning an nginx 502 page, which used to be submitted
85
                    // to Vivo verbatim). Skip; this IMEI is picked up on the next run.
83
                    // to Vivo verbatim). Skip; this IMEI is picked up on the next run.
86
                    continue;
84
                    continue;
87
                }
85
                }
88
                this.getImeiActivation(captchaBreak, httpClient, imeiActivationTimestampModel);
86
                this.getImeiActivation(captcha, httpClient, imeiActivationTimestampModel);
89
            }
87
            }
90
        }
88
        }
91
    }
89
    }
92
 
90
 
-
 
91
    /** A solved captcha: the code, plus the image it came from so the verdict can be reported. */
-
 
92
    static final class Captcha {
-
 
93
        final String code;
-
 
94
        final byte[] image;
-
 
95
 
-
 
96
        Captcha(String code, byte[] image) {
-
 
97
            this.code = code;
-
 
98
            this.image = image;
-
 
99
        }
-
 
100
    }
-
 
101
 
93
    public String getNewCaptcha(CloseableHttpClient httpClient, BasicCookieStore cookieStore) throws Exception {
102
    public Captcha getNewCaptcha(CloseableHttpClient httpClient, BasicCookieStore cookieStore) throws Exception {
94
        String vivoSupportUrl = "https://www.vivo.com/in/support/generatingCodes";
103
        HttpGet request = new HttpGet("https://www.vivo.com/in/support/generatingCodes");
95
        HttpGet request = new HttpGet(vivoSupportUrl);
-
 
96
 
104
 
97
        String filePath = "/tmp/captcha.jpg";
105
        // Kept in memory rather than written to a shared /tmp/captcha.jpg: the two
-
 
106
        // Vivo jobs run concurrently and used to overwrite each other's image. The
-
 
107
        // bytes are needed for the verdict report anyway.
-
 
108
        byte[] bytes;
98
        try (CloseableHttpResponse response = httpClient.execute(request)) {
109
        try (CloseableHttpResponse response = httpClient.execute(request)) {
99
            LOGGER.info("cookie {}", cookieStore.getCookies());
-
 
100
            LOGGER.info("vivoSupportUrl" + vivoSupportUrl);
-
 
101
            byte[] bytes = EntityUtils.toByteArray(response.getEntity());
110
            bytes = EntityUtils.toByteArray(response.getEntity());
102
            Files.write(Paths.get(filePath), bytes);
-
 
103
        }
111
        }
104
 
112
 
105
        String captchaBreak = captchaService.getCaptchaCode(filePath);
113
        String code = captchaService.getCaptchaCode(bytes);
106
        System.out.println("Captcha is " + captchaBreak);
114
        return code == null ? null : new Captcha(code, bytes);
107
        return captchaBreak;
-
 
108
    }
115
    }
109
 
116
 
110
    public void getImeiActivation(String captchaBreak, CloseableHttpClient httpClient,
117
    public void getImeiActivation(Captcha captcha, CloseableHttpClient httpClient,
111
                                  ImeiActivationTimestampModel vivoImeiAndActivationTimeStampModel) throws Exception {
118
                                  ImeiActivationTimestampModel vivoImeiAndActivationTimeStampModel) throws Exception {
112
 
119
 
113
        String imei = vivoImeiAndActivationTimeStampModel.getSerialNumber();
120
        String imei = vivoImeiAndActivationTimeStampModel.getSerialNumber();
114
 
121
 
115
        List<NameValuePair> form = new ArrayList<>();
122
        List<NameValuePair> form = new ArrayList<>();
116
        form.add(new BasicNameValuePair("imei", imei));
123
        form.add(new BasicNameValuePair("imei", imei));
117
        form.add(new BasicNameValuePair("code", captchaBreak));
124
        form.add(new BasicNameValuePair("code", captcha.code));
118
        HttpPost post = new HttpPost("https://www.vivo.com/in/support/checkCode");
125
        HttpPost post = new HttpPost("https://www.vivo.com/in/support/checkCode");
119
        post.setEntity(new UrlEncodedFormEntity(form, StandardCharsets.UTF_8));
126
        post.setEntity(new UrlEncodedFormEntity(form, StandardCharsets.UTF_8));
120
 
127
 
121
        JSONObject imeiActivationJson;
128
        JSONObject imeiActivationJson;
122
        try (CloseableHttpResponse response2 = httpClient.execute(post)) {
129
        try (CloseableHttpResponse response2 = httpClient.execute(post)) {
123
            imeiActivationJson = new JSONObject(EntityUtils.toString(response2.getEntity()));
130
            imeiActivationJson = new JSONObject(EntityUtils.toString(response2.getEntity()));
124
        }
131
        }
125
 
132
 
126
        JSONObject data = imeiActivationJson.getJSONObject("data");
133
        JSONObject data = imeiActivationJson.getJSONObject("data");
127
        int status = data.getInt("status");
134
        int status = data.getInt("status");
-
 
135
 
-
 
136
        // Vivo is the only judge of whether our guess was right; status 0 means the
-
 
137
        // captcha was wrong. Hand that back so the sample becomes training data.
-
 
138
        captchaService.reportVerdict(captcha.image, captcha.code, status != 0);
-
 
139
 
128
        if (status == 0) {
140
        if (status == 0) {
129
            LOGGER.info("Found invalid captcha");
141
            LOGGER.info("Found invalid captcha");
130
            return;
142
            return;
131
        } else if (status == 2) {
143
        } else if (status == 2) {
132
            LOGGER.info("Received status 2 for {}, data {}", imei, data);
144
            LOGGER.info("Received status 2 for {}, data {}", imei, data);