Skip to content

Commit 6facc02

Browse files
committed
Add 2captchaBasic
1 parent 0e7631c commit 6facc02

File tree

9 files changed

+453
-210
lines changed

9 files changed

+453
-210
lines changed

KinanCity-captcha-2captcha/src/test/java/com/kinancity/core/captcha/twoCaptcha/TwoCaptchaProviderTest.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
package com.kinancity.core.captcha.twoCaptcha;
22

3-
import java.io.File;
4-
import java.io.FileInputStream;
5-
import java.io.IOException;
6-
import java.io.InputStream;
7-
import java.util.Properties;
8-
93
import com.kinancity.core.captcha.CaptchaProvider;
10-
import org.junit.Before;
4+
import junit.framework.TestCase;
115
import org.junit.Ignore;
126
import org.junit.Test;
137
import org.slf4j.Logger;
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>kinancity-parent</artifactId>
7+
<groupId>com.kinancity</groupId>
8+
<version>2.1.17-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>kinancity-captcha-2captcha-basic</artifactId>
13+
<dependencies>
14+
<dependency>
15+
<groupId>com.kinancity</groupId>
16+
<artifactId>kinancity-captcha-api</artifactId>
17+
<version>2.1.17-SNAPSHOT</version>
18+
<scope>compile</scope>
19+
</dependency>
20+
21+
<!-- Lombok -->
22+
<dependency>
23+
<groupId>org.projectlombok</groupId>
24+
<artifactId>lombok</artifactId>
25+
<scope>provided</scope>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>junit</groupId>
30+
<artifactId>junit</artifactId>
31+
<version>4.13.1</version>
32+
<scope>test</scope>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>org.assertj</groupId>
37+
<artifactId>assertj-core</artifactId>
38+
<version>3.6.2</version>
39+
<scope>test</scope>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>com.github.2captcha</groupId>
44+
<artifactId>2captcha-java</artifactId>
45+
<version>1.1.1</version>
46+
</dependency>
47+
</dependencies>
48+
49+
<properties>
50+
<maven.compiler.source>8</maven.compiler.source>
51+
<maven.compiler.target>8</maven.compiler.target>
52+
</properties>
53+
54+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.kinancity.core.captcha.twocaptchabasic;
2+
3+
import com.kinancity.api.errors.TechnicalException;
4+
import com.kinancity.core.captcha.CaptchaException;
5+
import com.kinancity.core.captcha.CaptchaProvider;
6+
import com.kinancity.core.captcha.CaptchaQueue;
7+
import com.kinancity.core.captcha.impl.BaseCaptchaProvider;
8+
import com.kinancity.core.captcha.impl.BaseConfigurationException;
9+
import com.kinancity.core.captcha.ptc.PtcCaptchaData;
10+
import com.twocaptcha.TwoCaptcha;
11+
import com.twocaptcha.captcha.Captcha;
12+
import com.twocaptcha.captcha.ReCaptcha;
13+
import com.twocaptcha.exceptions.ApiException;
14+
import lombok.extern.slf4j.Slf4j;
15+
16+
import java.util.HashMap;
17+
import java.util.Map;
18+
import java.util.Optional;
19+
20+
@Slf4j
21+
public class TwoCaptchaBasicProvider extends BaseCaptchaProvider<TwoCaptchaTask> {
22+
23+
public static final String OPTION_CUSTOM_HOST = "CUSTOM_HOST";
24+
25+
/**
26+
* 2 captcha Soft Id;
27+
*/
28+
private final int KINAN_SOFT_ID = 1816;
29+
30+
private TwoCaptcha solver;
31+
32+
public static CaptchaProvider getInstance(CaptchaQueue queue, String apiKey) throws CaptchaException {
33+
return getInstance(queue, apiKey, Optional.empty());
34+
}
35+
36+
public static CaptchaProvider getInstance(CaptchaQueue queue, String apiKey, Optional<String> altUrl) throws CaptchaException {
37+
return new TwoCaptchaBasicProvider(queue, apiKey, altUrl);
38+
}
39+
40+
public TwoCaptchaBasicProvider(CaptchaQueue queue, String apiKey, Optional<String> altUrl) throws BaseConfigurationException {
41+
super(queue, apiKey);
42+
43+
log.debug("Start using TwoCaptchaBasicProvider with api key starting by {}", apiKey.substring(0, 6));
44+
45+
solver = new TwoCaptcha(apiKey);
46+
solver.setSoftId(KINAN_SOFT_ID);
47+
48+
// Custom Host for 2captcha alt
49+
altUrl.ifPresent(solver::setHost);
50+
altUrl.ifPresent(url -> log.info("TwoCaptchaBasicProvider with custom host : {}", url));
51+
52+
}
53+
54+
@Override
55+
public double getBalance() throws TechnicalException {
56+
try {
57+
return solver.balance();
58+
} catch (Exception e) {
59+
throw new TechnicalException("Error getting balance", e);
60+
}
61+
}
62+
63+
@Override
64+
protected void checkTaskStatusAndProcess(TwoCaptchaTask task) {
65+
66+
try {
67+
// Update Captcha Object
68+
String result = solver.getResult(task.getTaskId());
69+
70+
if (result != null) {
71+
this.onTaskSuccess(task);
72+
this.sendSolutionToQueue(result);
73+
}
74+
// Otherwise, we just wait
75+
76+
} catch (Exception e) {
77+
log.error("Error getting Captcha Info", e);
78+
task.setCanBeRetried(false);
79+
this.onTaskFailed(task);
80+
}
81+
}
82+
83+
@Override
84+
protected void createNewCaptchaTasks(int nbToRequest) {
85+
for (int i = 0; i < nbToRequest; i++) {
86+
if(isHasBalanceLeft()) {
87+
try {
88+
String captchaId = solver.send(getNewCaptchaTask());
89+
this.onTaskCreationSuccess(new TwoCaptchaTask(captchaId));
90+
} catch (Exception e) {
91+
if (e instanceof ApiException && e.getMessage().contains("ERROR_ZERO_BALANCE")) {
92+
this.onZeroBalanceError();
93+
} else {
94+
log.error("Failed to create Captcha Task", e);
95+
}
96+
}
97+
} else {
98+
log.warn("Zero Balance reached, skip remaining captcha tasks creations");
99+
}
100+
}
101+
}
102+
103+
private Captcha getNewCaptchaTask() {
104+
ReCaptcha captcha = new ReCaptcha();
105+
captcha.setSoftId(KINAN_SOFT_ID);
106+
captcha.setSiteKey(PtcCaptchaData.GOOGLE_SITE_KEY);
107+
captcha.setUrl(PtcCaptchaData.PAGE_URL);
108+
return captcha;
109+
}
110+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.kinancity.core.captcha.twocaptchabasic;
2+
3+
import com.kinancity.core.captcha.impl.BaseSolvingTask;
4+
5+
public class TwoCaptchaTask extends BaseSolvingTask {
6+
7+
public TwoCaptchaTask(String taskId) {
8+
super(taskId);
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.kinancity.core.captcha.twocaptchabasic;
2+
3+
import com.kinancity.api.errors.TechnicalException;
4+
import com.kinancity.core.captcha.CaptchaException;
5+
import com.kinancity.core.captcha.CaptchaQueue;
6+
import com.kinancity.core.captcha.CaptchaRequest;
7+
import com.kinancity.core.captcha.impl.LogCaptchaCollector;
8+
import lombok.extern.slf4j.Slf4j;
9+
import org.junit.Test;
10+
11+
import java.util.HashMap;
12+
import java.util.Map;
13+
import java.util.Optional;
14+
15+
@Slf4j
16+
public class TwoCaptchaBasicProviderTest {
17+
18+
@Test
19+
public void solvingTest() throws CaptchaException, TechnicalException, InterruptedException {
20+
21+
CaptchaQueue queue = new CaptchaQueue(new LogCaptchaCollector());
22+
23+
String apiKey = System.getenv("apiKey") ;
24+
String altUrl = System.getenv("altUrl") ;
25+
26+
TwoCaptchaBasicProvider provider = new TwoCaptchaBasicProvider(queue, apiKey, Optional.ofNullable(altUrl));
27+
28+
log.info("Start Provider");
29+
new Thread(provider).start();
30+
31+
log.info("Provider Started, get Balance");
32+
33+
double balance = provider.getBalance();
34+
log.info("Balance is : {}", balance);
35+
36+
CaptchaRequest request = queue.addRequest(new CaptchaRequest("test1"));
37+
CaptchaRequest request2 = queue.addRequest(new CaptchaRequest("test2"));
38+
39+
while (request.getResponse() == null || request2.getResponse() == null) {
40+
Thread.sleep(500);
41+
}
42+
43+
log.info("Response 1 given : {}", request.getResponse());
44+
log.info("Response 2 given : {}", request2.getResponse());
45+
46+
Thread.sleep(2000);
47+
48+
}
49+
}

KinanCity-core/config.example.properties

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Captcha providers deathbycaptcha|imageTypers|2captcha|antiCaptcha|capsolver|captchaai|local
1+
# Captcha providers deathbycaptcha|imageTypers|2captcha|antiCaptcha|capsolver|2captchaBasic|captchaai|local
22
# captcha.provider=deathbycaptcha
33

44
# ImageTypers Access Token
@@ -10,6 +10,9 @@
1010
# Anti Captcha
1111
# captcha.key=[PUT YOUR ACCOUNT KEY HERE]
1212

13+
# 2 captcha alternate host (to be used with 2captchaBasic)
14+
# Use another provide using 2captcha API such as https://ocr.captchaai.com/
15+
# 2captcha.altHost=ocr.captchaai.com
1316

1417

1518
# List of proxies

0 commit comments

Comments
 (0)