1
0
mirror of https://github.com/Rogiel/httpchannel synced 2025-12-05 23:22:51 +00:00

Implements CaptchaTrader service

This commit is contained in:
2012-01-17 12:28:22 -02:00
parent e943b08ede
commit 08d22a12fe
81 changed files with 2750 additions and 543 deletions

View File

@@ -48,13 +48,6 @@
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.htmlparser</groupId>
<artifactId>htmlparser</artifactId>

View File

@@ -1,26 +0,0 @@
/**
*
*/
package com.rogiel.httpchannel.captcha;
import java.io.IOException;
import com.rogiel.httpchannel.util.htmlparser.HTMLPage;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public abstract class AbstractHTMLImageCaptchaService<C extends AbstractImageCaptcha>
extends AbstractImageCaptchaService<C> {
@Override
public final C create(String html) {
try {
return create(HTMLPage.parse(html));
} catch (IOException e) {
return null;
}
}
public abstract C create(HTMLPage page) throws IOException;
}

View File

@@ -1,47 +0,0 @@
/**
*
*/
package com.rogiel.httpchannel.captcha;
import java.net.URL;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public abstract class AbstractImageCaptcha implements ImageCaptcha {
private final URL imageURL;
private final String ID;
private String answer;
private boolean automaticallyResolved;
public AbstractImageCaptcha(URL imageURL, String ID) {
this.imageURL = imageURL;
this.ID = ID;
}
@Override
public void setAnswer(String answer) {
this.answer = answer;
}
@Override
public String getAnswer() {
return answer;
}
@Override
public boolean wasAutomaticallyResolved() {
return automaticallyResolved;
}
@Override
public URL getImageURL() {
return imageURL;
}
public String getID() {
return ID;
}
}

View File

@@ -1,41 +0,0 @@
/**
*
*/
package com.rogiel.httpchannel.captcha;
import java.net.URL;
import com.rogiel.httpchannel.http.GetRequest;
import com.rogiel.httpchannel.http.HttpContext;
import com.rogiel.httpchannel.http.PostMultipartRequest;
import com.rogiel.httpchannel.http.PostRequest;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public abstract class AbstractImageCaptchaService<C extends AbstractImageCaptcha>
implements ImageCaptchaService<C> {
protected final HttpContext http = new HttpContext();
@Override
public boolean resolve(C captcha) {
return false;
}
public GetRequest get(String url) {
return http.get(url);
}
public GetRequest get(URL url) {
return http.get(url);
}
public PostRequest post(String url) {
return http.post(url);
}
public PostMultipartRequest multipartPost(String url) {
return http.multipartPost(url);
}
}

View File

@@ -0,0 +1,58 @@
/**
*
*/
package com.rogiel.httpchannel.captcha;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Pattern;
import com.rogiel.httpchannel.http.HttpContext;
import com.rogiel.httpchannel.util.PatternUtils;
import com.rogiel.httpchannel.util.htmlparser.HTMLPage;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class ReCaptchaExtractor {
private static final Pattern CAPTCHA_ID_PATTERN = Pattern
.compile("Recaptcha\\.create\\(\"([0-9A-z|_|\\-]*)\", ");
private static final Pattern CAPTCHA_URL_PATTERN = Pattern
.compile("http://www\\.google\\.com/recaptcha/api/challenge\\?k=([0-9A-z|\\-]*)(&(.*))?");
private static final Pattern CAPTCHA_IMAGE_PATTERN = Pattern
.compile("challenge : '(.*)'");
private static final String CHALLENGE_BASE_URL = "http://www.google.com/recaptcha/api/challenge?ajax=1&k=";
private static final String IMAGE_BASE_URL = "http://www.google.com/recaptcha/api/image?c=";
public static ImageCaptcha extractCaptcha(HTMLPage page, HttpContext ctx) {
final String url = page.findScriptSrc(CAPTCHA_URL_PATTERN);
if (url == null)
return null;
try {
return doExtract(ctx.get(url).asString());
} catch (IOException e) {
return null;
}
}
public static ImageCaptcha extractAjaxCaptcha(HTMLPage page, HttpContext ctx) {
final String siteID = page.findScript(CAPTCHA_ID_PATTERN, 1);
try {
return doExtract(ctx.get(CHALLENGE_BASE_URL + siteID).asString());
} catch (IOException e) {
return null;
}
}
private static ImageCaptcha doExtract(String content) {
final String id = PatternUtils.find(CAPTCHA_IMAGE_PATTERN, content, 1);
try {
return new ImageCaptcha(id, new URL(IMAGE_BASE_URL + id));
} catch (MalformedURLException e) {
return null;
}
}
}

View File

@@ -23,6 +23,8 @@ import org.apache.commons.io.FilenameUtils;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.rogiel.httpchannel.http.Request;
import com.rogiel.httpchannel.service.Downloader.DownloaderConfiguration;
@@ -35,6 +37,8 @@ import com.rogiel.httpchannel.util.ThreadUtils;
*/
public abstract class AbstractHttpDownloader<C extends DownloaderConfiguration>
extends AbstractDownloader<C> implements Downloader<C> {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
protected AbstractHttpDownloader(URL url, C configuration) {
super(url, configuration);
}
@@ -54,6 +58,7 @@ public abstract class AbstractHttpDownloader<C extends DownloaderConfiguration>
if (!listener.timer(timer))
return;
}
logger.debug("Download timer waiting {}", timer);
ThreadUtils.sleep(timer);
}

View File

@@ -38,6 +38,7 @@ public abstract class AbstractHttpService extends AbstractService implements
protected LinkedUploadChannel waitChannelLink(LinkedUploadChannel channel,
Future<?> future) {
logger.debug("Waiting channel {} to link", channel);
while (!channel.isLinked() && !future.isDone()) {
ThreadUtils.sleep(100);
}

View File

@@ -22,6 +22,9 @@ import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.WritableByteChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.rogiel.httpchannel.service.UploadChannel;
import com.rogiel.httpchannel.service.exception.UploadLinkNotFoundException;
@@ -30,6 +33,8 @@ import com.rogiel.httpchannel.service.exception.UploadLinkNotFoundException;
*
*/
public class LinkedUploadChannel implements UploadChannel {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private WritableByteChannel channel;
private final LinkedUploadChannelCloseCallback closeCallback;
@@ -69,6 +74,7 @@ public class LinkedUploadChannel implements UploadChannel {
public void close() throws IOException {
open = false;
final String downloadLink = closeCallback.finish();
logger.debug("Download link returned by service is {}", downloadLink);
if (downloadLink == null)
throw new UploadLinkNotFoundException();
this.downloadLink = new URL(downloadLink);

View File

@@ -0,0 +1,27 @@
/**
*
*/
package com.rogiel.httpchannel.util;
import java.io.IOException;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class ExceptionUtils {
public static void asIOException(Exception e) throws IOException {
final Throwable e2 = e.getCause();
if (e2 == null) {
throw new IOException(e);
} else if (e instanceof IOException) {
throw (IOException) e;
} else if (e2 instanceof IOException) {
throw (IOException) e2;
} else if (e2 instanceof RuntimeException) {
throw (RuntimeException) e2;
} else {
throw new IOException(e);
}
}
}