Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import std.stdio;
- import std.net.curl;
- import core.time : seconds;
- import std.typecons;
- import std.algorithm.iteration;
- import std.algorithm.sorting;
- import std.algorithm.setops;
- import std.parallelism;
- import std.array;
- import std.functional;
- import std.file;
- import std.conv: to;
- import std.functional: unaryFun;
- import std.conv: text;
- import std.algorithm : equal;
- import std.process;
- import core.time;
- import std.json;
- import std.typecons;
- import std.format: format;
- import std.exception;
- alias Proxy = Tuple!(string, "address", ushort, "port");
- alias splitLines = partial!(reverseArgs!split, '\n');
- alias splitPort = partial!(reverseArgs!split, ':');
- alias arr2tuple = unaryFun!`tuple!("address", "port")(a[0], a[1].to!ushort)`;
- alias parseProxies = compose!(array, map!arr2tuple, map!splitPort, splitLines, readText);
- alias ProxyResult = Tuple!(Proxy, "proxy", Duration, "time", long, "error", string, "reason");
- Nullable!ProxyResult msg(in string address, in ushort port){
- Nullable!ProxyResult result = ProxyResult.init;
- auto client = HTTP();
- client.operationTimeout = 15.seconds;
- try {
- client.proxy = address;
- client.proxyPort = port;
- import std.random;
- string task = "post";
- string board = "mlp";
- uint thread = 233124;
- string comment = "test";
- string captchaID, captchaValue, captchaIsValid = "FAIL";
- do {
- string captchaIdURL = format("https://2ch.hk/makaba/captcha.fcgi?type=2chaptcha&action=thread&board=%s", board);
- captchaID = get(captchaIdURL, client)[6..$].idup;
- string captchaURL = "https://2ch.hk/makaba/captcha.fcgi?type=2chaptcha&action=image&id=" ~ captchaID;
- ubyte[] captcha = get!(HTTP, ubyte)(captchaURL, client);
- string captchaFileName = format("/tmp/%s.png", captchaID);
- std.file.write(captchaFileName, captcha);
- auto captchaRecognize = std.process.execute(["/home/wolph/workspace/captcha-build/captcha", captchaFileName]);
- captchaValue = captchaRecognize.output;
- auto checkCaptchaURL = format("https://2ch.hk/makaba/captcha.fcgi?type=2chaptcha&action=check&id=%s&value=%s", captchaID, captchaValue);
- captchaIsValid = get(checkCaptchaURL, client).idup;
- } while (captchaIsValid != "VALID");
- string url = "https://2ch.hk/makaba/posting.fcgi?json=1";
- string request = format("&task=%s&board=%s&thread=%d&comment=%s&captcha_type=%s&2chaptcha_id=%s&2chaptcha_value=%s", task, board, thread, comment, "2chaptcha", captchaID, captchaValue);
- MonoTime before = MonoTime.currTime;
- auto reply = post(url, request, client).parseJSON.object;
- writefln("%-25s %s", format("%s:%s", address, port), reply);
- result.proxy = Proxy(address, port);
- result.time = MonoTime.currTime - before;
- result.error = reply["Error"].integer;
- result.reason = reply["Reason"].str;
- return result;
- }
- catch (CurlTimeoutException e){
- result.nullify();
- return result;
- }
- catch (CurlException e){
- result.nullify();
- return result;
- }
- }
- Nullable!ProxyResult msg(in Proxy proxy){
- return msg(proxy.expand);
- }
- void main(string[] args){
- Proxy[] proxies = parseProxies("proxies.txt");
- sort(proxies);
- Proxy[] banned = parseProxies("banned.txt");
- sort(banned);
- Proxy[] banned_mlp = parseProxies("banned_mlp.txt");
- sort(banned_mlp);
- Proxy[] ok = parseProxies("ok.txt");
- sort(ok);
- auto blacklist = setUnion(banned, banned_mlp, ok).uniq;
- Proxy[] checklist = setDifference(proxies.uniq, blacklist).array;
- MonoTime before = MonoTime.currTime;
- auto checkedProxies = taskPool.amap!msg(checklist).filter!(x => !x.isNull);
- Duration time = MonoTime.currTime - before;
- ProxyResult[] goodProxies = checkedProxies.filter!(x => x.error != -6).map!(x => x.get).array;
- bool byTime(ProxyResult a, ProxyResult b) {
- return a.time < b.time;
- }
- //auto byTime = (a,b) => { };
- sort!byTime(goodProxies);
- ProxyResult[] badProxies = checkedProxies.filter!(x => x.error == -6).map!(x => x.get).array;
- writefln("Time elapsed: %s", time);
- writefln("%s proxies checked: %s good, %s banned", checklist.length, goodProxies.length, badProxies.length);
- writeln("\nGood proxies:");
- goodProxies.each!(x => writefln("%s:%s \t %s %s", x.proxy.address, x.proxy.port, x.time, x.error));
- writeln("\nBad proxies:");
- badProxies.each!(x => writefln("%s:%s \t %s", x.proxy.address, x.proxy.port, x.reason));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement