Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 4.33 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.            
  54.             string captchaURL = "https://2ch.hk/makaba/captcha.fcgi?type=2chaptcha&action=image&id=" ~ captchaID;
  55.             ubyte[] captcha = get!(HTTP, ubyte)(captchaURL, client);
  56.            
  57.             string captchaFileName = format("/tmp/%s.png", captchaID);
  58.            
  59.             std.file.write(captchaFileName, captcha);
  60.             auto captchaRecognize = std.process.execute(["/home/wolph/workspace/captcha-build/captcha", captchaFileName]);
  61.             captchaValue = captchaRecognize.output;
  62.            
  63.             auto checkCaptchaURL = format("https://2ch.hk/makaba/captcha.fcgi?type=2chaptcha&action=check&id=%s&value=%s", captchaID, captchaValue);
  64.             captchaIsValid = get(checkCaptchaURL, client).idup;
  65.         } while (captchaIsValid != "VALID");
  66.  
  67.         string url = "https://2ch.hk/makaba/posting.fcgi?json=1";
  68.         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);
  69.  
  70.         MonoTime before = MonoTime.currTime;
  71.         auto reply = post(url, request, client).parseJSON.object;
  72.  
  73.         writefln("%-25s %s", format("%s:%s", address, port), reply);
  74.  
  75.         result.proxy = Proxy(address, port);
  76.         result.time = MonoTime.currTime - before;
  77.         result.error = reply["Error"].integer;
  78.         result.reason = reply["Reason"].str;
  79.  
  80.         return result;
  81.     }
  82.     catch (CurlTimeoutException e){
  83.         result.nullify();
  84.         return result;
  85.     }
  86.     catch (CurlException e){
  87.         result.nullify();
  88.         return result;
  89.     }
  90. }
  91.  
  92. Nullable!ProxyResult msg(in Proxy proxy){
  93.     return msg(proxy.expand);
  94. }
  95.  
  96. void main(string[] args){
  97.     Proxy[] proxies = parseProxies("proxies.txt");
  98.     sort(proxies);
  99.  
  100.     Proxy[] banned = parseProxies("banned.txt");
  101.     sort(banned);
  102.  
  103.     Proxy[] banned_mlp = parseProxies("banned_mlp.txt");
  104.     sort(banned_mlp);
  105.  
  106.     Proxy[] ok = parseProxies("ok.txt");
  107.     sort(ok);
  108.  
  109.     auto blacklist = setUnion(banned, banned_mlp, ok).uniq;
  110.     Proxy[] checklist = setDifference(proxies.uniq, blacklist).array;
  111.  
  112.     MonoTime before = MonoTime.currTime;
  113.     auto checkedProxies = taskPool.amap!msg(checklist).filter!(x => !x.isNull);
  114.     Duration time = MonoTime.currTime - before;
  115.  
  116.  
  117.     ProxyResult[] goodProxies = checkedProxies.filter!(x => x.error != -6).map!(x => x.get).array;
  118.  
  119.     bool byTime(ProxyResult a, ProxyResult b) {
  120.         return a.time < b.time;
  121.     }
  122.  
  123.     //auto byTime = (a,b) => {  };
  124.     sort!byTime(goodProxies);
  125.  
  126.     ProxyResult[] badProxies = checkedProxies.filter!(x => x.error == -6).map!(x => x.get).array;
  127.  
  128.     writefln("Time elapsed: %s", time);
  129.     writefln("%s proxies checked: %s good, %s banned", checklist.length, goodProxies.length, badProxies.length);
  130.  
  131.     writeln("\nGood proxies:");
  132.     goodProxies.each!(x => writefln("%s:%s \t %s %s", x.proxy.address, x.proxy.port, x.time, x.error));
  133.  
  134.     writeln("\nBad proxies:");
  135.     badProxies.each!(x => writefln("%s:%s \t %s", x.proxy.address, x.proxy.port, x.reason));
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement