-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
111 lines (91 loc) · 2.96 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const Request = require('request');
const SteamID = require('steamid');
const USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36";
module.exports = SteamStore;
/**
* Create a new SteamStore instance.
* @param {object} [options]
* @constructor
*/
function SteamStore(options) {
options = options || {};
this._jar = Request.jar();
let defaults = {
"jar": this._jar,
"timeout": options.timeout || 50000,
"gzip": true,
"headers": {
"User-Agent": options.userAgent || USER_AGENT
}
};
this.request = options.request || Request.defaults({"forever": true}); // "forever" indicates that we want a keep-alive agent
this.request = this.request.defaults(defaults);
// UTC, English
this.setCookie("timezoneOffset=0,0");
this.setCookie("Steam_Language=english");
}
/**
* Set a single cookie on this SteamStore instance.
* @param {string} cookie - In format "cookieName=cookieValue"
*/
SteamStore.prototype.setCookie = function(cookie) {
let cookieName = cookie.match(/(.+)=/)[1];
if (cookieName == 'steamLogin' || cookieName == 'steamLoginSecure') {
this.steamID = new SteamID(cookie.match(/=(\d+)/)[1]);
}
let requestCookie = Request.cookie(cookie);
let isSecure = !!cookieName.match(/(^steamMachineAuth|^steamLoginSecure$)/);
let protocol = isSecure ? 'https' : 'http';
if (requestCookie.domain) {
this._jar.setCookie(requestCookie.clone(), protocol + '://' + requestCookie.domain);
} else {
this._jar.setCookie(Request.cookie(cookie), protocol + "://store.steampowered.com");
this._jar.setCookie(Request.cookie(cookie), protocol + "://steamcommunity.com");
}
};
/**
* Set multiple cookies.
* @param {string[]} cookies
*/
SteamStore.prototype.setCookies = function(cookies) {
cookies.forEach(this.setCookie.bind(this));
};
/**
* Get this SteamStore instance's sessionid CSRF token.
* @returns {string}
*/
SteamStore.prototype.getSessionID = function() {
let cookies = this._jar.getCookieString("http://store.steampowered.com").split(';');
for (let i = 0; i < cookies.length; i++) {
let match = cookies[i].trim().match(/([^=]+)=(.+)/);
if (match[1] == 'sessionid') {
return decodeURIComponent(match[2]);
}
}
let sessionID = generateSessionID();
this.setCookie("sessionid=" + sessionID);
return sessionID;
};
function generateSessionID() {
return Math.floor(Math.random() * 1000000000);
}
SteamStore.prototype._checkHttpError = function(err, response, callback) {
if (err) {
callback(err);
return true;
}
if (response.statusCode >= 300 && response.statusCode <= 399 && response.headers.location.indexOf('/login') != -1) {
callback(new Error("Not Logged In"));
return true;
}
if (response.statusCode >= 400) {
let error = new Error("HTTP error " + response.statusCode);
error.code = response.statusCode;
callback(error);
return true;
}
return false;
};
require('./components/account.js');
require('./components/gifts.js');
require('./components/licenses.js');