| Line 8... |
Line 8... |
| 8 |
import org.springframework.stereotype.Service;
|
8 |
import org.springframework.stereotype.Service;
|
| 9 |
|
9 |
|
| 10 |
import java.io.File;
|
10 |
import java.io.File;
|
| 11 |
import java.util.Base64;
|
11 |
import java.util.Base64;
|
| 12 |
import java.util.HashMap;
|
12 |
import java.util.HashMap;
|
| - |
|
13 |
import java.util.regex.Pattern;
|
| 13 |
|
14 |
|
| 14 |
@Service
|
15 |
@Service
|
| 15 |
public class CaptchaService {
|
16 |
public class CaptchaService {
|
| 16 |
|
17 |
|
| 17 |
@Autowired
|
18 |
@Autowired
|
| 18 |
RestClient restClient;
|
19 |
RestClient restClient;
|
| 19 |
private static final Logger LOGGER = LogManager.getLogger(CaptchaService.class);
|
20 |
private static final Logger LOGGER = LogManager.getLogger(CaptchaService.class);
|
| 20 |
|
21 |
|
| - |
|
22 |
/** The 31 classes the solver model can emit: 123456789ABCDEFGHKMNPQRSTUVWXYZ */
|
| - |
|
23 |
private static final Pattern CAPTCHA_CODE = Pattern.compile("^[1-9A-HK-NP-Z]{4}$");
|
| - |
|
24 |
|
| - |
|
25 |
/**
|
| - |
|
26 |
* @return the 4-character captcha code, or null if the solver did not return
|
| - |
|
27 |
* a usable one. Callers must skip the IMEI when this is null rather
|
| - |
|
28 |
* than submitting the value to Vivo.
|
| - |
|
29 |
*/
|
| 21 |
public String getCaptchaCode(String filePath) throws Exception {
|
30 |
public String getCaptchaCode(String filePath) throws Exception {
|
| 22 |
byte[] fileContent = FileUtils.readFileToByteArray(new File(filePath));
|
31 |
byte[] fileContent = FileUtils.readFileToByteArray(new File(filePath));
|
| 23 |
String encodedString = Base64.getEncoder().encodeToString(fileContent);
|
32 |
String encodedString = Base64.getEncoder().encodeToString(fileContent);
|
| 24 |
System.out.println(encodedString);
|
- |
|
| 25 |
Base64Image base64Image = new Base64Image();
|
33 |
Base64Image base64Image = new Base64Image();
|
| 26 |
base64Image.setImage(encodedString);
|
34 |
base64Image.setImage(encodedString);
|
| - |
|
35 |
|
| 27 |
return restClient.postJson("http://45.79.121.178/uploader", base64Image, new HashMap<>());
|
36 |
String response = restClient.postJson("http://45.79.121.178/uploader", base64Image, new HashMap<>());
|
| - |
|
37 |
|
| - |
|
38 |
// RestClient returns the body for ANY status, so an nginx 502 page or a
|
| - |
|
39 |
// solver error payload arrives here looking like a normal result. Anything
|
| - |
|
40 |
// that is not a real code is rejected instead of being sent to Vivo.
|
| - |
|
41 |
if (response == null || !CAPTCHA_CODE.matcher(response.trim()).matches()) {
|
| - |
|
42 |
LOGGER.error("Captcha solver returned an unusable response: {}",
|
| - |
|
43 |
response == null ? "null" : response.substring(0, Math.min(response.length(), 200)));
|
| - |
|
44 |
return null;
|
| - |
|
45 |
}
|
| - |
|
46 |
return response.trim();
|
| 28 |
}
|
47 |
}
|
| 29 |
|
48 |
|
| 30 |
class Base64Image {
|
49 |
class Base64Image {
|
| 31 |
private String image;
|
50 |
private String image;
|
| 32 |
|
51 |
|