-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·218 lines (197 loc) · 7.14 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env node
import * as readline from 'node:readline/promises';
import * as axl from 'app-xbox-live'
import axios from 'axios';
import https from 'https';
import ora from 'ora';
import logSymbols from 'log-symbols';
import * as fs from 'fs';
import path from 'path'
const spinner = ora();
process.on('SIGINT', () => {
spinner.fail();
spinner.warn("Cancelled by user")
process.exit(0);
});
let attemptCount = 0;
let errorCount = 0;
let authToken;
let xl;
let reservationId;
// Define settings defaults
const settings = {
autoClaim: false,
monitorAvailability: false,
lookupRetryDelaySeconds: 75,
login: "",
password: "",
desiredGamertag: ""
};
// Clear screen and prompt for credentials
DisplayHeader();
// Attempt to load settings from settings.json
LoadSettings();
// Prompt for login and password if not found in settings.json
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
if (!settings.login || !settings.password) {
console.log("Please enter your Microsoft account credentials");
console.log(`${logSymbols.warning} Not compatible with 2FA or passwordless accounts ${logSymbols.warning}\n`);
settings.login = await rl.question('Login: ');
settings.password = await rl.question('Password: ');
}
// Clear screen and prompt for desired gamertag
DisplayHeader();
// Prompt for desired gamertag if not found in settings.json
if (!settings.desiredGamertag) {
settings.desiredGamertag = await rl.question(`${logSymbols.info} Desired gamertag: `);
} else {
console.log(`${logSymbols.info} Desired gamertag: ${settings.desiredGamertag}`);
}
rl.close();
// Attempt to login
await AttemptLogin(settings.login, settings.password);
// Begin gamertag lookup
spinner.start(`Looking up gamertag ${settings.desiredGamertag}...`);
await LookupGamertag(settings.desiredGamertag);
function LoadSettings() {
try {
var settingsJson;
const filePath = path.join(process.cwd(), 'settings.json');
const data = fs.readFileSync(filePath, 'utf8');
settingsJson = JSON.parse(data);
settings.autoClaim = settingsJson?.autoClaim || false;
settings.monitorAvailability = settingsJson?.monitorAvailability || false;
settings.login = settingsJson?.login || "";
settings.password = settingsJson?.password || "";
settings.desiredGamertag = settingsJson?.desiredGamertag || "";
settings.lookupRetryDelaySeconds = settingsJson?.lookupRetryDelaySeconds || 75;
} catch (err) {
return
}
}
function DisplayHeader() {
console.clear();
console.log("\u{0001F3AE}\u{0001F3AE}\u{0001F3AE}\u{0001F3AE}\u{0001F3AE}\u{0001F3AE}\u{0001F3AE}\u{0001F3AE}\u{0001F3AE}\u{0001F3AE}\u{0001F3AE}\u{0001F3AE}\u{0001F3AE}\u{0001F3AE}\u{0001F3AE}\u{0001F3AE}")
console.log("\u{0001F3AE} Xbox Live Gamertag Utility \u{0001F3AE}")
console.log("\u{0001F3AE}\u{0001F3AE}\u{0001F3AE}\u{0001F3AE}\u{0001F3AE}\u{0001F3AE}\u{0001F3AE}\u{0001F3AE}\u{0001F3AE}\u{0001F3AE}\u{0001F3AE}\u{0001F3AE}\u{0001F3AE}\u{0001F3AE}\u{0001F3AE}\u{0001F3AE}\n")
console.log(" (Press CTRL+C to Cancel)\n")
}
async function AttemptLogin(user, password) {
try {
spinner.start("Logging in...");
const result = await axl.Token(user, password);
authToken = `XBL3.0 x=${result[1]};${result[0]}`;
xl = new axl.Account(authToken);
const me = await xl.me.profile.get();
reservationId = `${me.userXuid}`;
spinner.succeed(`Successfully logged in as ${me.gamerTag}`);
} catch (err) {
spinner.fail(`Login failed, check credentials and try again`);
process.exit();
}
}
async function LookupGamertag(gamertag) {
do {
try {
const lookupResponse = await QueryGamertag(gamertag);
errorCount = 0; // Reset request error count
if (lookupResponse?.status == 200) {
attemptCount++;
if (lookupResponse?.data?.composedGamertag == gamertag) {
settings.monitorAvailability = false;
spinner.succeed(`Gamertag ${gamertag} is available`);
if (settings.autoClaim) {
ClaimGamertag(gamertag);
}
} else {
if (settings.monitorAvailability) {
spinner.start(`Gamertag ${gamertag} is unavailable, monitoring... (${attemptCount})`);
await sleep(settings.lookupRetryDelaySeconds * 1000);
} else {
settings.monitorAvailability = false;
spinner.fail(`Gamertag ${gamertag} is unavailable`);
}
}
} else if (lookupResponse?.status === 401 || lookupResponse?.status === 403) {
spinner.warn("Authentication error occurred");
await AttemptLogin(settings.login, settings.password);
} else if (lookupResponse !== null) {
spinner.fail(`Error ${lookupResponse?.data?.code}: ${lookupResponse?.data?.description} (Status: ${lookupResponse?.status})`);
}
} catch (err) {
errorCount++;
spinner.fail("Error: " + err.message);
if (errorCount >= 10)
process.exit(1);
}
} while (settings.monitorAvailability)
}
async function QueryGamertag(gamertag) {
const headers = {
'User-agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'Accept-language' : 'en-US,en;q=0.9',
'x-xbl-contract-version': '1',
'authorization': authToken
};
const requestBody = {
gamertag: gamertag,
reservationId: reservationId,
targetGamertagFields: "gamertag"
};
try {
const response = await axios.post('https://gamertag.xboxlive.com/gamertags/reserve', requestBody, {
headers: headers,
httpAgent: new https.Agent({ rejectUnauthorized: false })
});
return response;
} catch (err) {
if (err.response)
return err.response;
spinner.fail("Error: " + err.message);
return null;
}
}
async function ClaimGamertag(desiredGamertag) {
spinner.start(`Attempting to claim gamertag ${desiredGamertag}...`);
const headers = {
'User-agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
'Accept' : 'application/json, text/plain, */*',
'Accept-Language' : 'en-US,en;q=0.9',
'x-xbl-contract-version': '6',
'authorization': authToken
};
const requestBody = {
reservationId: reservationId,
gamertag: {
gamertag: desiredGamertag,
gamertagSuffix: '',
classicGamertag: desiredGamertag
},
preview: false,
useLegacyEntitlement: false
};
try {
const result = await axios.post('https://accounts.xboxlive.com/users/current/profile/gamertag', requestBody, {
headers: headers,
httpAgent: new https.Agent({ rejectUnauthorized: false })
});
if (result.status === 200) {
spinner.succeed(`Successfully claimed gamertag ${desiredGamertag}! \u{0001F389}`);
} else if (result.status === 401 || result.status === 403) {
spinner.warn("Authentication error occurred");
await AttemptLogin(settings.login, settings.password);
await ClaimGamertag(desiredGamertag);
} else {
spinner.fail(`Unknown error attempting to claim gamertag`);
}
} catch (error) {
// TODO: Log error to log file
spinner.fail(`Failed to claim gamertag due to error:\n\t${error.message}`);
};
}
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}