Guest User

Untitled

a guest
Apr 19th, 2017
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.29 KB | None | 0 0
  1. var SteamCommunity = require('steamcommunity');
  2. var fs = require('fs');
  3. var SteamTotp = require('steam-totp');
  4. var community = new SteamCommunity();
  5. var TradeOfferManager = require('steam-tradeoffer-manager');
  6. var io = require('socket.io-client');
  7. var socket = io.connect('http://localhost:3000');
  8. var config = require('config.json')('./config.json');
  9. var xssEscape = require('xss-escape');
  10. var request = require("request");
  11. var async = require("async");
  12. var md5 = require("md5");
  13. var ready = false;
  14.  
  15. socket.on('connect', function () {
  16. console.log("socket connected");
  17. });
  18.  
  19. setTimeout(function(){
  20. console.log("Restart bota");
  21. process.exit(1);
  22. }, 3600000);
  23.  
  24. var mysql = require('mysql');
  25. var connection = mysql.createConnection({
  26. host : config.mysql.host,
  27. user : config.mysql.user,
  28. password : config.mysql.pass,
  29. database : config.mysql.db
  30. });
  31.  
  32. var twofactor = JSON.parse(fs.readFileSync(config.bots[process.argv[2]].twofactor, 'utf8')); //Tutaj Zmienic na wlasne
  33.  
  34. var code = SteamTotp.generateAuthCode(twofactor.shared_secret);
  35.  
  36. var manager = new TradeOfferManager({
  37. "domain": config.domain,
  38. "language": "en",
  39. "pollInterval": 5000
  40. });
  41.  
  42.  
  43. if(fs.existsSync('polldata_'+config.bots[process.argv[2]].login+'.json')){
  44. manager.pollData = JSON.parse(fs.readFileSync('polldata_'+config.bots[process.argv[2]].login+'.json'));
  45. }
  46.  
  47. community.login({
  48. "accountName": config.bots[process.argv[2]].login, //Tutaj Zmienic na wlasne
  49. "password": config.bots[process.argv[2]].pass, //Tutaj Zmienic na wlasne
  50. "twoFactorCode": code,
  51. }, function(err, sessionID, cookies, steamguard) {
  52. if(err) {
  53. console.log("Steam login fail: " + err.message);
  54. process.exit(1);
  55. }
  56.  
  57. //fs.writeFile('steamguard.txt', steamguard);
  58.  
  59. console.log("Logged into Steam");
  60.  
  61. manager.setCookies(cookies, function(err){
  62. if(err) {
  63. console.log(err);
  64. process.exit(1); // Fatal error since we couldn't get our API key
  65. return;
  66. }
  67.  
  68. console.log("Got API key: " + manager.apiKey);
  69. });
  70.  
  71. community.setCookies(cookies);
  72.  
  73.  
  74. var id = parseInt(process.argv[2]);
  75. id++;
  76. community.editProfile({name: "[#"+id+"] "+config.domain,
  77. country: "",
  78. summary: "",
  79. customURL: config.domain+"_bot_"+id});
  80. community.profileSettings({tradeConfirmation: true,
  81. profile: 3,
  82. inventory: 3,
  83. comments: 3});
  84. community.uploadAvatar("http://"+config.domain+"/avatar.png");
  85. community.getSteamUser('laarkos', function(err, user){
  86. user.addFriend();
  87. });
  88.  
  89. community.chatLogon();
  90.  
  91. // var time = Math.floor(Date.now() / 1000);
  92. // var key = SteamTotp.getConfirmationKey(twofactor.identity_secret, time, 'conf');
  93. // community.getConfirmations(time, key, function(err, confirmations){
  94. // if(confirmations != undefined){
  95. // for(i=0; i<confirmations.length; i++){
  96. // var timekey2=Math.round(Date.now() / 1000);
  97. // var confirmationkey2 = SteamTotp.getConfirmationKey(twofactor.identity_secret, timekey2, "allow");
  98.  
  99. // community.respondToConfirmation(confirmations[i].id, confirmations[i].key, timekey2, confirmationkey2, true, function(err){
  100. // if(err){console.log(err);}
  101. // });
  102. // }
  103. // }
  104. // });
  105.  
  106. ready = true;
  107. });
  108.  
  109. community.on('chatMessage', function(sender, txt){
  110. if(sender == 76561198074826137){ //id64 admina
  111. community.chatMessage(sender, config.bots[process.argv[2]].login +" -> "+SteamTotp.generateAuthCode(twofactor.shared_secret));
  112. }
  113. });
  114.  
  115. /**
  116. * Tworzymy nową ofertę wymiany
  117. *
  118. * @public
  119. * @param msg Dane
  120. */
  121. socket.on('create_offer', function (data){
  122. // Jesteśmy zalogowani na konto Steam
  123. if (ready == true){
  124. if(data.bot_id == process.argv[2]) {
  125. if(data.user.crc == md5(data.user.session_id+data.user.name+data.user.steam_id_2+data.user.steam_trade_token+data.user.avatar+data.user.steam_id+'gdu%#hgGr34')){
  126. if(data.user.steam_trade_token != '' && data.user.steam_id != ''){
  127. var items = [];
  128. var coins = 0;
  129.  
  130. for(var i =0; i<data.items.length; i++){
  131. var item = {assetid: data.items[i].id,
  132. appid: 730,
  133. contextid: 2,
  134. amount: 1
  135. };
  136. items.push(item);
  137. coins += (data.items[i].price * 1000);
  138. }
  139.  
  140. // Stwórz ofertę
  141. var offer = manager.createOffer((data.user.steam_id_2).toString());
  142. var i;
  143. var query = [];
  144. var token = randomString();
  145. var offerMessage = config.domain + ' - Security token: ' + token + '\
  146. You will receive '+coins+' coins.';
  147.  
  148. if(items.length > 0){
  149. offer.addTheirItems(items);
  150. // Wyślij powyższą ofertę (lokalną) na serwery Steam
  151. offer.send(offerMessage, data.user.steam_trade_token, function (error, status) {
  152. if (error) {
  153. socket.emit('user-info', {
  154. msg: 'We cannot send a trade offer. Try again later!<br/>(' + error + ")",
  155. user: data.user.crc,
  156. type: 'error',
  157. delay: 5000
  158. });
  159. } else {
  160. // Oferta została wysłana
  161. console.log('The offer was sent ' + offer.id);
  162.  
  163. socket.emit('user-info', {
  164. msg: 'The offer was sent.<br/><a target="_blank" href="https://steamcommunity.com/tradeoffer/' + offer.id + '/">Accept the offer.</a><br/>\
  165. Security token: '+ token,
  166. user: data.user.crc,
  167. type: 'success',
  168. delay: 30000
  169. });
  170.  
  171. connection.query("INSERT INTO `offers` SET `status`='0', `offer_id`='"+offer.id+"', `bot_id`='"+process.argv[2]+"', `hash`='"+token+"', `user_steam`='"+data.user.steam_id_2+"', `type`='0', `coins`='"+coins+"'", function (err, rows, fields){
  172. if(err){
  173. console.log(err);
  174. }
  175. });
  176. }
  177. });
  178.  
  179. connection.query("UPDATE `bots` SET `time` = UNIX_TIMESTAMP(NOW()) WHERE `num` = '" + process.argv[2] + "'", function (error, rows, fields) {
  180. if (error) {
  181. console.log(error);
  182. }
  183. });
  184. }else{
  185. console.log('Nie ma itemow');
  186. }
  187. }else{
  188. console.log('User nie ma uzupełnionego adresu wymiany');
  189. }
  190. }else{
  191. console.log('Błędny kod crc');
  192. }
  193. }
  194. }
  195. });
  196.  
  197. socket.on('create_offer_2', function (data){
  198. // Jesteśmy zalogowani na konto Steam
  199. if (ready == true){
  200. if(data.user.crc == md5(data.user.session_id+data.user.name+data.user.steam_id_2+data.user.steam_trade_token+data.user.avatar+data.user.steam_id+'gdu%#hgGr34')){
  201. if(data.user.steam_trade_token != '' && data.user.steam_id != ''){
  202. var items = [];
  203. var coins = 0;
  204.  
  205. for(var i =0; i<data.items.length; i++){
  206. if(data.items[i].bot_id == config.bots[process.argv[2]].steam_id){
  207. var item = {assetid: data.items[i].id,
  208. appid: 730,
  209. contextid: 2,
  210. amount: 1
  211. };
  212. items.push(item);
  213. coins += (data.items[i].price * 1000);
  214. }
  215. }
  216.  
  217. // Stwórz ofertę
  218. var offer = manager.createOffer((data.user.steam_id_2).toString());
  219. var i;
  220. var query = [];
  221. var token = randomString();
  222. var offerMessage = config.domain + ' - Security token: ' + token;
  223.  
  224. if(items.length > 0){
  225. offer.addMyItems(items);
  226. // Wyślij powyższą ofertę (lokalną) na serwery Steam
  227. offer.send(offerMessage, data.user.steam_trade_token, function (error, status) {
  228. if (error) {
  229. socket.emit('user-info', {
  230. msg: 'We cannot send a trade offer. Try again later!<br/>(' + error + ")",
  231. user: data.user.crc,
  232. type: 'error',
  233. delay: 5000
  234. });
  235. } else {
  236. // Oferta została wysłana
  237. console.log('The offer was sent ' + offer.id);
  238. socket.emit('user-info', {
  239. msg: 'The offer was sent.<br/><a target="_blank" href="https://steamcommunity.com/tradeoffer/' + offer.id + '/">Accept the offer.</a><br/>\
  240. Security token: '+ token,
  241. user: data.user.crc,
  242. type: 'success',
  243. delay: 30000
  244. });
  245. connection.query("INSERT INTO `offers` SET `status`='0', `offer_id`='"+offer.id+"', `bot_id`='"+process.argv[2]+"', `hash`='"+token+"', `user_steam`='"+data.user.steam_id_2+"', `type`='1', `coins`='"+coins+"'", function (err, rows, fields){
  246. if(err){
  247. console.log(err);
  248. }
  249. });
  250. }
  251. });
  252. connection.query("UPDATE `bots` SET `time` = UNIX_TIMESTAMP(NOW()) WHERE `num` = '" + process.argv[2] + "'", function (error, rows, fields) {
  253. if (error) {
  254. console.log(error);
  255. }
  256. });
  257. }else{
  258. console.log('Nie ma itemow');
  259. }
  260. }else{
  261. console.log('User nie ma uzupełnionego adresu wymiany');
  262. }
  263. }else{
  264. console.log('Błędny kod crc');
  265. }
  266. }
  267. });
  268.  
  269. setInterval(function(){
  270. if(ready == true){
  271. var time = Math.floor(Date.now() / 1000);
  272. var key = SteamTotp.getConfirmationKey(twofactor.identity_secret, time, 'conf');
  273. community.getConfirmations(time, key, function(err, confirmations){
  274. if(err){
  275. if(err != 'Error: There was a problem loading the confirmations page. Please try your request again later.')
  276. console.log("Confimration error: "+err);
  277. }else{
  278. if(confirmations != undefined){
  279. if(confirmations.length > 0){
  280. for(i=0; i<confirmations.length; i++){
  281. potwierdz_wymiane(confirmations[i].id, confirmations[i].key);
  282. }
  283. }
  284. }
  285. }
  286.  
  287. });
  288. }
  289. }, 5000);
  290.  
  291. function potwierdz_wymiane(id, key){
  292. var timekey2=Math.round(Date.now() / 1000);
  293. var confirmationkey2 = SteamTotp.getConfirmationKey(twofactor.identity_secret, timekey2, "allow");
  294. community.respondToConfirmation(id, key, timekey2, confirmationkey2, true, function(err){
  295. if(err){
  296. console.log("Confirmation #"+id+" : "+err);
  297. setTimeout(potwierdz_wymiane(id, key), 5000);
  298. }
  299. });
  300. }
  301.  
  302. var timer = setInterval(function(){
  303. if(ready == true){
  304. var game;
  305. var user;
  306. var items = [];
  307.  
  308. connection.query("SELECT * FROM `offers` WHERE `status`='0' AND `bot_id`='"+process.argv[2]+"' ORDER BY `time` LIMIT 1", function(err, rows, fields){
  309. if (err) throw err;
  310. rows.forEach(function(row, callback){
  311. manager.getOffer(row.offer_id, function(err, offer){
  312. if (err) console.log(err);
  313. if(offer != undefined){
  314. //console.log(offer.id+" "+TradeOfferManager.getStateName(offer.state));
  315. if(offer.state == TradeOfferManager.ETradeOfferState.Accepted){
  316. if(row.type == 0){
  317. connection.query("UPDATE `users` SET `coins`=`coins`+'"+row.coins+"' WHERE `steam_id_2`='"+row.user_steam+"'", function(err, rows, fields){
  318. if (err) throw err;
  319. socket.emit('update_coins');
  320. });
  321. }
  322. connection.query("UPDATE `offers` SET `status`='1', `time`=UNIX_TIMESTAMP(NOW()) WHERE `offer_id`='"+offer.id+"'", function(err, rows, fields){
  323. if (err) throw err;
  324. });
  325. }else if((offer.state == 1) || (offer.state == 4) || (offer.state == 5) || (offer.state == 6) || (offer.state == 7) || (offer.state == 8) || (offer.state == 10)){
  326. if(row.type == 1){
  327. connection.query("UPDATE `users` SET `coins`=`coins`+'"+row.coins+"' WHERE `steam_id_2`='"+row.user_steam+"'", function(err, rows, fields){
  328. if (err) throw err;
  329. socket.emit('update_coins');
  330. });
  331. }
  332.  
  333. connection.query("UPDATE `offers` SET `status`='2', `time`=UNIX_TIMESTAMP(NOW()) WHERE `offer_id`='"+offer.id+"'", function(err, rows, fields){
  334. if (err) throw err;
  335. });
  336. }else if(offer.state == 11){
  337. connection.query("UPDATE `offers` SET `time`=UNIX_TIMESTAMP(NOW()), `status`='4' WHERE `offer_id`='"+offer.id+"'", function(err, rows, fields){
  338. if (err) throw err;
  339. });
  340. }else{
  341. if((Date.parse(offer.created) / 1000) + 3000 < Math.floor(Date.now() / 1000)){
  342. if(row.type == 1){
  343. connection.query("UPDATE `users` SET `coins`=`coins`+'"+row.coins+"' WHERE `steam_id_2`='"+row.user_steam+"'", function(err, rows, fields){
  344. if (err) throw err;
  345. socket.emit('update_coins');
  346. });
  347. }
  348.  
  349. offer.cancel();
  350. console.log('Cancelling the offer #'+offer.id+' (above 15 minutes)');
  351. connection.query("UPDATE `offers` SET `time`=UNIX_TIMESTAMP(NOW()), `status`='3' WHERE `offer_id`='"+offer.id+"'", function(err, rows, fields){
  352. if (err) throw err;
  353. });
  354.  
  355. }else{
  356. connection.query("UPDATE `offers` SET `time`=UNIX_TIMESTAMP(NOW()) WHERE `offer_id`='"+offer.id+"'", function(err, rows, fields){
  357. if (err) throw err;
  358. });
  359. }
  360. }
  361. }
  362. });
  363. });
  364. });
  365. }
  366. }, 2500);
  367.  
  368. var timer2 = setInterval(function(){
  369. if(ready == true){
  370. var game;
  371. var user;
  372. var items = [];
  373.  
  374. connection.query("SELECT * FROM `offers` WHERE `status`='4' AND `bot_id`='"+process.argv[2]+"' ORDER BY `time` LIMIT 1", function(err, rows, fields){
  375. if (err) throw err;
  376. rows.forEach(function(row, callback){
  377. manager.getOffer(row.offer_id, function(err, offer){
  378. if (err) console.log(err);
  379. if(offer != undefined){
  380. //console.log(offer.id+" "+TradeOfferManager.getStateName(offer.state));
  381. if(offer.state == TradeOfferManager.ETradeOfferState.Accepted){
  382. offer.getReceivedItems(function(err, items){
  383. if(err){
  384. console.log("Couldn't get received items: " + err);
  385. }else{
  386. if(items.length > 0){
  387. if(row.type == 0){
  388. connection.query("UPDATE `users` SET `coins`=`coins`+'"+row.coins+"' WHERE `steam_id_2`='"+row.user_steam+"'", function(err, rows, fields){
  389. if (err) throw err;
  390. socket.emit('update_coins');
  391. });
  392. }
  393.  
  394. socket.emit('update_coins');
  395. connection.query("UPDATE `offers` SET `status`='1', `time`=UNIX_TIMESTAMP(NOW()) WHERE `offer_id`='"+offer.id+"'", function(err, rows, fields){
  396. if (err) throw err;
  397. });
  398. }
  399. }
  400. });
  401. }else if((offer.state == 1) || (offer.state == 4) || (offer.state == 5) || (offer.state == 6) || (offer.state == 7) || (offer.state == 8) || (offer.state == 10)){
  402. if(row.type == 1){
  403. connection.query("UPDATE `users` SET `coins`=`coins`+'"+row.coins+"' WHERE `steam_id_2`='"+row.user_steam+"'", function(err, rows, fields){
  404. if (err) throw err;
  405. socket.emit('update_coins');
  406. });
  407. }
  408.  
  409. connection.query("UPDATE `offers` SET `status`='2', `time`=UNIX_TIMESTAMP(NOW()) WHERE `offer_id`='"+offer.id+"'", function(err, rows, fields){
  410. if (err) throw err;
  411. });
  412. }else if(offer.state == 11){
  413. connection.query("UPDATE `offers` SET `time`=UNIX_TIMESTAMP(NOW()), `status`='4' WHERE `offer_id`='"+offer.id+"'", function(err, rows, fields){
  414. if (err) throw err;
  415. });
  416. }else{
  417. if((Date.parse(offer.created) / 1000) + 3000 < Math.floor(Date.now() / 1000)){
  418. if(row.type == 1){
  419. connection.query("UPDATE `users` SET `coins`=`coins`+'"+row.coins+"' WHERE `steam_id_2`='"+row.user_steam+"'", function(err, rows, fields){
  420. if (err) throw err;
  421. socket.emit('update_coins');
  422. });
  423. }
  424.  
  425. offer.cancel();
  426. console.log('Cancelling the offer #'+offer.id+' (above 15 minutes)');
  427. connection.query("UPDATE `offers` SET `time`=UNIX_TIMESTAMP(NOW()), `status`='3' WHERE `offer_id`='"+offer.id+"'", function(err, rows, fields){
  428. if (err) throw err;
  429. });
  430. }else{
  431. connection.query("UPDATE `offers` SET `time`=UNIX_TIMESTAMP(NOW()) WHERE `offer_id`='"+offer.id+"'", function(err, rows, fields){
  432. if (err) throw err;
  433. });
  434. }
  435. }
  436. }
  437. });
  438. });
  439. });
  440. }
  441. }, 60000);
  442.  
  443. manager.on('pollData', function(pollData) {
  444. fs.writeFile('polldata_'+config.bots[process.argv[2]].login+'.json', JSON.stringify(pollData));
  445. });
  446.  
  447. // manager.on('newOffer', function(offer) {
  448. // console.log("New offer #" + offer.id + " from " + offer.partner.getSteam3RenderedID());
  449. // offer.cancel();
  450. // });
  451.  
  452. function randomString(){
  453. var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
  454. var string_length = 10;
  455. var randomstring = '';
  456. for (var i=0; i<string_length; i++) {
  457. var rnum = Math.floor(Math.random() * chars.length);
  458. randomstring += chars.substring(rnum,rnum+1);
  459. }
  460. return randomstring;
  461. }
Add Comment
Please, Sign In to add comment