Repository navigation
Expand file tree
/
Copy pathuseragent.js
More file actions
42 lines (37 loc) · 1.52 KB
/
Copy pathuseragent.js
File metadata and controls
42 lines (37 loc) · 1.52 KB
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
const { net } = require("electron");
// The official Flash client's update list says which BigPointClient version is current; the site serves the game
// page to that user agent. Asked through the window's session, so it goes out through the same proxy.
const url = "http://darkorbit-22-client.bpsecure.com/bpflashclient/windows.x64/repository/Updates.xml";
// Used when the list can't be read; the site only checks that the agent is the BigPoint client.
const FALLBACK = "BigPointClient/1.6.9";
function fetchText(session) {
return new Promise((resolve, reject) => {
const request = net.request({ url, session, useSessionCookies: false });
const timer = setTimeout(() => request.abort(), 8000);
request.on("response", response => {
let body = "";
response.on("data", chunk => body += chunk);
response.on("end", () => {
clearTimeout(timer);
resolve(body);
});
response.on("error", reject);
});
request.on("error", err => {
clearTimeout(timer);
reject(err);
});
request.on("abort", () => reject(new Error("timeout")));
request.end();
});
}
async function getUserAgent(session) {
try {
const xml = await fetchText(session);
const version = xml.match(/<Version>([0-9.]{1,20})<\/Version>/);
return version ? `BigPointClient/${version[1]}` : FALLBACK;
} catch (err) {
return FALLBACK;
}
}
module.exports = getUserAgent;