PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tor.js

https://gitlab.com/kimvladis/DoubleBotWS
JavaScript | 324 lines | 57 code | 9 blank | 258 comment | 1 complexity | ed9943047243c9affe0ff861096a43c3 MD5 | raw file
  1. var Nightcrawler = require('nightcrawler'),
  2. request = require('request'),
  3. req = request.defaults({ jar: true, json: true }),
  4. crypto = require('crypto'),
  5. redis = require("redis"),
  6. captcha = require('./components/ruCaptcha'),
  7. captchas = [],
  8. lastUpdate = 0,
  9. mongoose = require('mongoose'),
  10. profiler = require('./components/profiler'),
  11. botCount = 0,
  12. unique = require('./components/unique'),
  13. logger = require('./components/logger');
  14. var torLogger = logger.getTorLogger;
  15. var Item = require('./models/item.js');
  16. var Withdraw = require('./models/withdraw.js');
  17. var WithdrawAll = require('./models/withdrawAll.js');
  18. mongoose.connect('mongodb://localhost/test');
  19. var nightcrawler = new Nightcrawler(
  20. {
  21. // Your Tor-Backed HTTP proxy URL
  22. proxy: 'http://localhost:8123',
  23. // Tor's control interface port and password
  24. tor: {
  25. password: 'qawsedrf',
  26. controlPort: 9051
  27. }
  28. }
  29. );
  30. var redisClient = redis.createClient();
  31. var redisSub = redis.createClient();
  32. var redisPub = redis.createClient();
  33. redisPub.publish('reqBotCount', true);
  34. redisSub.on(
  35. "message", function (channel, message) {
  36. switch ( channel ) {
  37. case "captcha":
  38. torLogger.trace('Capthca: ' + message);
  39. captchas.push(message);
  40. break;
  41. case "botCount":
  42. message = JSON.parse(message);
  43. botCount = message;
  44. torLogger.trace('Bots count: ' + botCount);
  45. break;
  46. }
  47. }
  48. );
  49. redisSub.subscribe("captcha");
  50. redisSub.subscribe("botCount");
  51. var j = request.jar();
  52. var cookie = request.cookie('PHPSESSID=nutpfbb242vle854c6ij69tdd6;');
  53. var url = 'https://csgopolygon.com/withdraw.php';
  54. j.setCookie(cookie, url);
  55. //curl 'http://csgopolygon.com/scripts/_get_bank.php?undefined'
  56. //-H 'Cookie: PHPSESSID=pb9pv7cr6jkpf8mv305k76lf06; language=en'
  57. //-H 'Accept-Encoding: gzip, deflate, sdch'
  58. //-H 'Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4'
  59. //-H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106
  60. // Safari/537.36' -H 'Accept: */*' -H 'Referer: http://csgopolygon.com/withdraw.php' -H 'X-Requested-With:
  61. // XMLHttpRequest' -H 'Connection: keep-alive' -H 'Cache-Control: max-age=0' --compressed
  62. var options = {
  63. headers: {
  64. //'Accept-Encoding': 'gzip, deflate, sdch',
  65. 'Accept-Language': 'ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4',
  66. 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
  67. 'Accept': '*/*',
  68. 'Referer': 'https://csgopolygon.com/withdraw.php',
  69. 'X-Requested-With': 'XMLHttpRequest',
  70. 'Connection': 'keep-alive',
  71. 'Cache-Control': 'max-age=0'
  72. },
  73. //proxy: nightcrawler.proxy,
  74. jar: j
  75. };
  76. function checkInList (items) {
  77. return new Promise(function (resolve, reject) {
  78. redisClient.get(
  79. 'list',
  80. function (err, data) {
  81. data = JSON.parse(data);
  82. var necesseryItems = data.items;
  83. var _items = items.filter(
  84. function (item) {
  85. return necesseryItems.indexOf(item.name) >= 0;
  86. }
  87. );
  88. resolve(_items);
  89. }
  90. );
  91. }
  92. );
  93. }
  94. function checkInWithdraw (items) {
  95. return new Promise(function (resolve, reject) {
  96. Withdraw.find(
  97. {},
  98. function (err, withdraws) {
  99. var withdrawed = withdraws.reduce(
  100. function (prev, curr) {
  101. return prev.concat(curr.assetids);
  102. }, []
  103. ).unique();
  104. items = items.filter(
  105. function (item) {
  106. return withdrawed.indexOf(item.assetid) < 0;
  107. }
  108. )
  109. resolve(items);
  110. }
  111. );
  112. }
  113. );
  114. }
  115. function sendItems (items) {
  116. return new Promise(function (resolve, reject) {
  117. var botIds = items.map(
  118. function (item) {
  119. return item.botid;
  120. }
  121. ).unique();
  122. botIds.forEach(
  123. function (botid) {
  124. var botItems = items.filter(
  125. function (item) {
  126. return item.botid === botid;
  127. }
  128. );
  129. botItems = botItems.slice(0, 20);
  130. var response = {
  131. items: botItems.reduce(
  132. function (prev, curr) {
  133. return prev + curr.assetid + ',';
  134. }, ''
  135. ),
  136. sum: botItems.reduce(
  137. function (prev, curr) {
  138. return prev + curr.price;
  139. }, 0
  140. ),
  141. botid: botid
  142. };
  143. if ( botCount > 0 ) {
  144. var obj = {
  145. assetids: botItems.map(
  146. function (T) {
  147. return T.assetid;
  148. }
  149. ),
  150. sum: response.sum,
  151. createdAt: new Date(),
  152. };
  153. var withdraw = new Withdraw(obj);
  154. withdraw.save();
  155. var withdrawAll = new WithdrawAll(obj);
  156. withdrawAll.save();
  157. }
  158. botCount--;
  159. if ( botCount < 0 ) {
  160. botCount = 0;
  161. }
  162. redisPub.publish("withdraw", JSON.stringify(response));
  163. torLogger.trace('Withdraw ' + response.items + ' on sum ' + response.sum);
  164. }
  165. );
  166. resolve(botIds);
  167. }
  168. )
  169. }
  170. function checkItems (items) {
  171. checkInWithdraw(items)
  172. .then(checkInList)
  173. .then(sendItems);
  174. }
  175. function chart (type, y) {
  176. redisPub.publish('chart', JSON.stringify(
  177. {
  178. type: type,
  179. time: Math.floor(new Date() / 1000),
  180. y: y
  181. }
  182. )
  183. );
  184. }
  185. function refresh () {
  186. if ( botCount > 0 ) {
  187. torLogger.trace('Getting captcha');
  188. captcha.getOne(function (captcha) {
  189. torLogger.trace('Captcha - ' + captcha);
  190. options.url = 'https://csgopolygon.com/scripts/_get_bank.php?captcha=' + captcha;
  191. // console.log(options);
  192. profiler.start('reqWithdraw');
  193. try {
  194. req(
  195. options, function (err, res, body) {
  196. if ( body.success ) {
  197. var time = profiler.stop('reqWithdraw');
  198. chart('withdraw', time);
  199. torLogger.trace('api answered successfully. Time: %d', time);
  200. redisPub.publish(
  201. "balance",
  202. JSON.stringify(
  203. {
  204. balance: body.balance,
  205. avail: body.avail
  206. }
  207. )
  208. );
  209. profiler.start('mongo');
  210. checkItems(body.items);
  211. Item.collection.insert(body.items, { ordered: false }, onInsert);
  212. function onInsert (err, docs) {
  213. if ( err ) {
  214. if ( err.code !== 11000 ) {
  215. torLogger.error(err.message);
  216. err.writeErrors.forEach(
  217. function (err) {
  218. if ( err.code !== 11000 ) {
  219. torLogger.error(err.message);
  220. }
  221. }
  222. );
  223. }
  224. }
  225. torLogger.trace('%d items were successfully stored. Time: %d ms', parseInt(docs.nInserted),
  226. profiler.stop('mongo')
  227. );
  228. //Item.find({ name: { $in: necesseryItems }}, function (err, items) {
  229. // checkItems(items);
  230. //});
  231. }
  232. setTimeout(
  233. function () {
  234. refresh();
  235. }, 10000
  236. );
  237. } else {
  238. torLogger.warn(body);
  239. options.url = 'https://csgopolygon.com/banhammer/pid';
  240. req(options, function (err, res, body) {
  241. var cookie = request.cookie('BHC=' + body);
  242. var url = 'https://csgopolygon.com/withdraw.php';
  243. options.jar.setCookie(cookie, url);
  244. refresh();
  245. });
  246. }
  247. }
  248. );
  249. } catch ( e ) {
  250. torLogger.trace(e);
  251. }
  252. });
  253. } else {
  254. setTimeout(refresh, 1000);
  255. }
  256. }
  257. function getRandomInt (min, max) {
  258. return Math.floor(Math.random() * (max - min)) + min;
  259. }
  260. function refreshTest () {
  261. profiler.start('start');
  262. setTimeout(function () {
  263. chart('start', profiler.stop('start'));
  264. profiler.start('withdraw');
  265. setTimeout(function () {
  266. chart('withdraw', profiler.stop('withdraw'));
  267. refreshTest();
  268. }, getRandomInt(500, 1500)
  269. );
  270. }, getRandomInt(100, 500)
  271. );
  272. }
  273. nightcrawler.changeIp().then(
  274. function (ip) {
  275. torLogger.trace('My current IP is: ' + ip);
  276. refresh();
  277. }
  278. );
  279. captcha.fillPull();
  280. torLogger.trace('started');
  281. captcha.checkBalance();
  282. redisClient.get('balance', function (err, data) {
  283. redisPub.publish(
  284. "balance",
  285. data
  286. );
  287. });