Advertisement
Guest User

Untitled

a guest
Oct 1st, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 5.16 KB | None | 0 0
  1. import std.stdio;
  2. import std.net.curl;
  3. import core.time : seconds;
  4. import std.typecons;
  5. import std.algorithm.iteration;
  6. import std.algorithm.sorting;
  7. import std.algorithm.setops;
  8. import std.parallelism;
  9. import std.array;
  10. import std.functional;
  11. import std.file;
  12. import std.conv: to;
  13. import std.functional: unaryFun;
  14. import std.conv: text;
  15. import std.algorithm : equal;
  16. import std.process;
  17. import core.time;
  18. import std.json;
  19. import std.typecons;
  20. import std.format: format;
  21. import std.exception;
  22.  
  23. alias Proxy = Tuple!(string, "address", ushort, "port");
  24.  
  25. alias splitLines =  partial!(reverseArgs!split, '\n');
  26. alias splitPort =  partial!(reverseArgs!split, ':');
  27. alias arr2tuple = unaryFun!`tuple!("address", "port")(a[0], a[1].to!ushort)`;
  28. alias parseProxies = compose!(array, map!arr2tuple, map!splitPort, splitLines, readText);
  29. alias ProxyResult = Tuple!(Proxy, "proxy", Duration, "time", long, "error", string, "reason");
  30.  
  31. Nullable!ProxyResult msg(in string address, in ushort port){
  32.         Nullable!ProxyResult result = ProxyResult.init;
  33.  
  34.         auto client = HTTP();
  35.         client.operationTimeout = 15.seconds;
  36.  
  37.         try {
  38.                 client.proxy = address;
  39.                 client.proxyPort = port;
  40.  
  41.                 import std.random;
  42.                 string task = "post";
  43.                 string board = "mlp";
  44.                 uint thread = 233124;
  45.                 string comment = "test";
  46.  
  47.  
  48.                 string captchaID, captchaValue, captchaIsValid = "FAIL";
  49.  
  50.                 do {
  51.                         string captchaIdURL = format("https://2ch.hk/makaba/captcha.fcgi?type=2chaptcha&action=thread&board=%s", board);
  52.                         captchaID = get(captchaIdURL, client)[6..$].idup;
  53.                         writefln("captchaID: %s", captchaID);
  54.  
  55.                         string captchaURL = "https://2ch.hk/makaba/captcha.fcgi?type=2chaptcha&action=image&id=" ~ captchaID;
  56.                         ubyte[] captcha = get!(HTTP, ubyte)(captchaURL, client);
  57.  
  58.                         string captchaFileName = format("/tmp/%s.png", captchaID);
  59.  
  60.                         std.file.write(captchaFileName, captcha);
  61.                         auto captchaRecognize = std.process.execute(["/home/wolph/workspace/captcha-build/captcha", captchaFileName]);
  62.                         captchaValue = captchaRecognize.output;
  63.  
  64.                         auto checkCaptchaURL = format("https://2ch.hk/makaba/captcha.fcgi?type=2chaptcha&action=check&id=%s&value=%s", captchaID, captchaValue);
  65.                         captchaIsValid = get(checkCaptchaURL, client).idup;
  66.                 } while (captchaIsValid != "VALID");
  67.  
  68.                 string url = "https://2ch.hk/makaba/posting.fcgi?json=1";
  69.                 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);
  70.  
  71.                 MonoTime before = MonoTime.currTime;
  72.                 auto reply = post(url, request, client).parseJSON.object;
  73.  
  74.                 writefln("%-25s %s", format("%s:%s", address, port), reply);
  75.  
  76.                 result.proxy = Proxy(address, port);
  77.                 result.time = MonoTime.currTime - before;
  78.                 result.error = reply["Error"].integer;
  79.                 result.reason = reply["Reason"].str;
  80.  
  81.                 return result;
  82.         }
  83.         catch (CurlTimeoutException e){
  84.                 result.nullify();
  85.                 return result;
  86.         }
  87.         catch (CurlException e){
  88.                 result.nullify();
  89.                 return result;
  90.         }
  91. }
  92.  
  93. Nullable!ProxyResult msg(in Proxy proxy){
  94.         return msg(proxy.expand);
  95. }
  96.  
  97. void main(string[] args){
  98.         Proxy[] proxies = parseProxies("proxies.txt");
  99.         sort(proxies);
  100.  
  101.         Proxy[] banned = parseProxies("banned.txt");
  102.         sort(banned);
  103.  
  104.         Proxy[] banned_mlp = parseProxies("banned_mlp.txt");
  105.         sort(banned_mlp);
  106.  
  107.         Proxy[] ok = parseProxies("ok.txt");
  108.         sort(ok);
  109.  
  110.         auto blacklist = setUnion(banned, banned_mlp, ok).uniq;
  111.         Proxy[] checklist = setDifference(proxies.uniq, blacklist).array;
  112.  
  113.         MonoTime before = MonoTime.currTime;
  114.         auto checkedProxies = taskPool.amap!msg(checklist).filter!(x => !x.isNull);
  115.         Duration time = MonoTime.currTime - before;
  116.  
  117.  
  118.         ProxyResult[] goodProxies = checkedProxies.filter!(x => x.error != -6).map!(x => x.get).array;
  119.  
  120.         bool byTime(ProxyResult a, ProxyResult b) {
  121.             return a.time < b.time;
  122.         }
  123.  
  124.         //auto byTime = (a,b) => {  };
  125.         sort!byTime(goodProxies);
  126.  
  127.         ProxyResult[] badProxies = checkedProxies.filter!(x => x.error == -6).map!(x => x.get).array;
  128.  
  129.         writefln("Time elapsed: %s", time);
  130.         writefln("%s proxies checked: %s good, %s banned", checklist.length, goodProxies.length, badProxies.length);
  131.  
  132.         writeln("\nGood proxies:");
  133.         goodProxies.each!(x => writefln("%s:%s \t %s %s", x.proxy.address, x.proxy.port, x.time, x.error));
  134.  
  135.         writeln("\nBad proxies:");
  136.         badProxies.each!(x => writefln("%s:%s \t %s", x.proxy.address, x.proxy.port, x.reason));
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement