Advertisement
Guest User

Untitled

a guest
Sep 18th, 2016
818
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.06 KB | None | 0 0
  1. const Steam = require('steam');
  2. const SteamWebLogon = require('steam-weblogon');
  3. const GetSteamApiKey = require('steam-web-api-key');
  4. const Winston = require('winston');
  5. const SteamTrade = require('steam-trade');
  6. const SteamTradeOffers = require('steam-tradeoffers');
  7. const SteamTotp = require('steam-totp');
  8. const SteamCommunity = require('steamcommunity');
  9.  
  10. const fs = require('fs');
  11. const crypto = require('crypto');
  12. const readline = require('readline');
  13. const sleep = require('sleep');
  14.  
  15. const ParentBot = function (username, password, options) {
  16. const that = this;
  17.  
  18. this.username = username;
  19. this.password = password;
  20. this.options = options || {};
  21.  
  22. this.service = this.options.service || undefined;
  23. this.apikey = this.options.apikey || undefined;
  24. this.sentryfile = this.options.sentryfile || this.username + '.sentry';
  25. this.logfile = this.options.logfile || this.username + '.log';
  26. this.guardCode = this.options.guardCode || undefined;
  27. this.twoFactorCode = this.options.twoFactorCode || undefined;
  28. this.sharedSecret = this.options.sharedSecret || undefined;
  29. this.identitySecret = this.options.identitySecret || undefined;
  30. this.confirmationInterval = this.options.confirmationInterval || undefined;
  31. this.gamePlayed = this.options.gamePlayed || undefined;
  32.  
  33. this.steamClient = new Steam.SteamClient();
  34. this.steamUser = new Steam.SteamUser(this.steamClient);
  35. this.steamFriends = new Steam.SteamFriends(this.steamClient);
  36. this.steamTrading = new Steam.SteamTrading(this.steamClient);
  37. this.steamGameCoordinator = (this.options.gamePlayed ? new Steam.SteamGameCoordinator(this.steamClient, parseInt(this.options.gamePlayed)) : undefined);
  38. this.steamRichPresence = (this.options.richPresenceID ? new Steam.SteamRichPresence(this.steamClient, parseInt(this.options.richPresenceID)) : undefined);
  39. this.steamUnifiedMessages = (this.options.service ? new Steam.SteamUnifiedMessages(this.steamClient, this.options.service) : undefined);
  40. this.steamWebLogon = new SteamWebLogon(this.steamClient, this.steamUser);
  41. this.community = new SteamCommunity();
  42. this.steamTrade = new SteamTrade();
  43. this.offers = new SteamTradeOffers();
  44.  
  45. this.rl = readline.createInterface({
  46. input: process.stdin,
  47. output: process.stdout
  48. });
  49.  
  50. this.logger = new (Winston.Logger)({
  51. transports: [
  52. new (Winston.transports.Console)({
  53. colorize: true,
  54. timestamp: true,
  55. label: that.username,
  56. level: 'silly',
  57. json: false
  58. }),
  59. new (Winston.transports.File)({
  60. level: 'debug',
  61. timestamp: true,
  62. json: false,
  63. filename: that.logfile
  64. })
  65. ]
  66. });
  67.  
  68. if (this.sentryfile) {
  69. fs.existsSync(this.sentryfile)
  70. ? this.logger.info('Using sentry file ' + this.sentryfile)
  71. : this.logger.warn('Sentry defined in options doesn\'t exists and will be created on successful login');
  72. }
  73.  
  74. //SteamClient events
  75. this.steamClient.on('error', () => { this._onError() });
  76. this.steamClient.on('connected', () => { this._onConnected() });
  77. this.steamClient.on('logOnResponse', res => { this._onLogOnResponse(res) });
  78. this.steamClient.on('loggedOff', eresult => { this._onLoggedOff(eresult) });
  79. this.steamClient.on('debug', this.logger.silly);
  80.  
  81. //SteamUser events
  82. this.steamUser.on('updateMachineAuth', (res, callback) => { this._onUpdateMachineAuth(res, callback) });
  83.  
  84. //SteamFriends events
  85. this.steamFriends.on('friendMsg', (steamID, message, type) => { this._onFriendMsg(steamID, message, type) });
  86. this.steamFriends.on('friend', (steamID, relationship) => { this._onFriend(steamID, relationship); });
  87. }
  88.  
  89.  
  90. exports.ParentBot = ParentBot;
  91. exports.ES6 = require('./parentbot-es6.js');
  92. ParentBot.Steam = Steam;
  93. ParentBot.SteamCommunity = SteamCommunity;
  94. ParentBot.SteamWebApiKey = GetSteamApiKey;
  95.  
  96. var prototype = ParentBot.prototype;
  97.  
  98. prototype.connect = function connectCallback() {
  99. //sleep.sleep(180);
  100. this.steamClient.connect();
  101. this.logger.debug('Connecting to Steam...');
  102. }
  103.  
  104. prototype.logOn = function logOnCallback() {
  105. this.logger.debug('Logging in...');
  106. const that = this;
  107. try {
  108. var sha = '';
  109. if (fs.existsSync(this.sentryfile)) {
  110. var file = fs.readFileSync(this.sentryfile);
  111. sha = crypto.createHash('sha1').update(file).digest();
  112. }
  113.  
  114. if (this.options.guardCode) {
  115. this.steamUser.logOn({
  116. account_name: that.username,
  117. password: that.password,
  118. auth_code: that.guardCode,
  119. sha_sentryfile: sha
  120. });
  121. }
  122. else if (this.options.twoFactorCode) {
  123. this.steamUser.logOn({
  124. account_name: that.username,
  125. password: that.password,
  126. two_factor_code: that.twoFactorCode,
  127. sha_sentryfile: sha
  128. })
  129. }
  130. else if (this.options.sharedSecret) {
  131. this.steamUser.logOn({
  132. account_name: that.username,
  133. password: that.password,
  134. two_factor_code: SteamTotp.generateAuthCode(that.options.sharedSecret),
  135. sha_sentryfile: sha
  136. })
  137. }
  138. else {
  139. this.steamUser.logOn({
  140. account_name: that.username,
  141. password: that.password,
  142. sha_sentryfile: sha
  143. });
  144. }
  145. }
  146. catch (err) {
  147. this.logger.error('Error logging in: ' + err);
  148. }
  149. }
  150.  
  151. prototype._onError = function errorCallback() {
  152. this.logger.error('Disconnected from Steam, reconnecting...');
  153. sleep.sleep(180);
  154. this.connect();
  155. }
  156.  
  157. prototype._onConnected = function connectedCallback() {
  158. this.logger.verbose('Connected to Steam, logging in...');
  159. this.logOn();
  160. }
  161.  
  162. prototype._onLogOnResponse = function logOnResponseCallback(response) {
  163. if (response.eresult === Steam.EResult.OK) {
  164. this.logger.info('Logged into Steam!');
  165. this.steamFriends.setPersonaState(Steam.EPersonaState = 1);
  166. this.steamUser.gamesPlayed({ "games_played": [{ "game_id": (this.gamePlayed ? parseInt(this.gamePlayed) : null) }] });
  167. this.steamWebLogon.webLogOn((webSessionID, cookies) => {
  168. this.community.setCookies(cookies);
  169. cookies.forEach(cookie => {
  170. this.steamTrade.setCookie(cookie.trim());
  171. });
  172.  
  173. if (this.confirmationInterval && this.identitySecret) {
  174. this.community.startConfirmationChecker(this.confirmationInterval, this.identitySecret);
  175. }
  176.  
  177. this.community.enableTwoFactor((e, response) => {
  178. if(e) {
  179. if(parseInt(e.eresult) === 2) {
  180. this.logger.error('Failed to enable two factor. Check if you have a phone number enabled for this account.');
  181. this.logger.error(e.stack);
  182. }
  183. else if(parseInt(e.eresult) === 29) {
  184. this.logger.warn('Already have 2FA enabled');
  185. }
  186. else {
  187. this.logger.error(e.stack);
  188. }
  189. }
  190. else {
  191. this.logger.verbose('Writing information to ' + this.username + '.2fa_info. Please add shared_secret and identity_secret ' +
  192. 'to your config as options sharedSecret and identitySecret respectively.');
  193. fs.writeFileSync(this.username + '.2fa_info', JSON.stringify(response, null, 2));
  194. this.finalizeTwoFactor(response);
  195. }
  196. });
  197.  
  198. this.steamTrade.sessionID = webSessionID;
  199. if (!this.apikey) {
  200. GetSteamApiKey({
  201. sessionID: webSessionID,
  202. webCookie: cookies
  203. }, (e, api) => {
  204. if (e) this.logger.error('Error getting API key: ' + e);
  205. else {
  206. this.apikey = api;
  207. this.offers.setup({
  208. sessionID: webSessionID,
  209. webCookie: cookies,
  210. APIKey: this.apikey
  211. });
  212. }
  213. });
  214. }
  215. else {
  216. this.offers.setup({
  217. sessionID: webSessionID,
  218. webCookie: cookies,
  219. APIKey: this.apikey
  220. });
  221. }
  222. this.logger.info('Logged into Steam web');
  223. });
  224. }
  225. else {
  226. this.logger.warn('EResult for logon: ' + response.eresult);
  227. if(response.eresult === 63) {
  228. this.logger.warn('Please provide the steamguard code sent to your email at ' + response.email_domain);
  229. process.exit(63);
  230. }
  231. }
  232. }
  233.  
  234. prototype.finalizeTwoFactor = function finalizeCallback(res) {
  235. this.rl.question('Code received by SMS: ', code => {
  236. this.community.finalizeTwoFactor(res.shared_secret, code, e => {
  237. if(e) {
  238. if(e.message === 'Invalid activation code') {
  239. this.logger.error('Invalid activation code, please try again');
  240. this.finalizeTwoFactor(res);
  241. }
  242. else {
  243. this.logger.error(e.stack);
  244. }
  245. }
  246. else {
  247. this.logger.info('Two factor auth enabled.');
  248. }
  249. });
  250. });
  251. }
  252.  
  253. prototype._onLoggedOff = function logedOffCallback() {
  254. this.logger.error('Logged off of Steam, logging in again...');
  255. this.logOn();
  256. }
  257.  
  258. prototype._onUpdateMachineAuth = function updateMachineAuthCallback(response, callback) {
  259. this.logger.debug('New sentry: ' + response.filename);
  260. fs.writeFileSync(this.sentryfile, response.bytes);
  261. callback({
  262. sha_file: crypto.createHash('sha1').update(response.bytes).digest()
  263. });
  264. }
  265.  
  266. prototype._onFriendMsg = function friendMsgCallback(steamID, message, type) {
  267. if (type === Steam.EChatEntryType.ChatMsg) {
  268. this.logger.info('Message from ' + steamID + ': ' + message);
  269. this.steamFriends.sendMessage(steamID, 'Hi, thanks for messaging me! If you are getting this message, it means that my ' +
  270. 'owner hasn\'t configured me properly. Annoy them with messages until they do!');
  271. }
  272. }
  273.  
  274. prototype._onFriend = function friendCallback(steamID, relationship) {
  275. if (relationship === Steam.EFriendRelationship.RequestRecipient) {
  276. this.steamFriends.addFriend(steamID);
  277. this.steamFriends.sendMessage(steamID, 'Hi, thanks for adding me! If you are getting this message, it means that my ' +
  278. 'owner hasn\'t configured me properly. Annoy them with messages until they do!');
  279. }
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement