Repository navigation
Expand file tree
/
Copy pathlaunch.js
More file actions
92 lines (77 loc) · 3.3 KB
/
Copy pathlaunch.js
File metadata and controls
92 lines (77 loc) · 3.3 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
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
// The request Orbit Helper starts this browser with. It travels in the OHWC_LAUNCH environment variable as JSON -
// never on the command line, which other programs can read:
//
// { "server": "tr5", "dosid": "...", "profile": "123456", "url": "https://...",
// "proxy": { "type": "http" | "socks5", "host": "...", "port": 8080, "username": "...", "password": "..." } }
//
// "proxy" is left out to connect directly. "url" (since 1.1.0) is the page to open instead of the account's start
// page - the shop's payment page of a real-money item the bot's game view asked for; only an https page of the game's
// own sites is taken, anything else is ignored and the start page opens. Started without a request (from the Start menu), the browser opens
// darkorbit.com for a normal login. A request it can't use comes back as { error } (a text in strings/<language>.json): the browser then says so and
// quits rather than open anything - above all never directly when a proxy was asked for.
const VARIABLE = "OHWC_LAUNCH";
// The site's server tags: tr5, int1, gbl3... The URL is built from it, so nothing else is let through.
const SERVER_TAG = /^[a-z]{2,4}[0-9]{1,3}$/;
// Only digits: it becomes a folder name.
const PROFILE = /^[0-9]{1,12}$/;
// Bigpoint's sites: the game, its login and its payment pages (client.js's BIGPOINT_DOMAINS).
const GAME_DOMAINS = ["darkorbit.com", "bpsecure.com", "bigpoint.com", "bigpoint.net"];
function take() {
const raw = process.env[VARIABLE];
delete process.env[VARIABLE];
if (!raw) {
return null;
}
let data;
try {
data = JSON.parse(raw);
} catch (err) {
return { error: "launchUnreadable" };
}
if (!data || typeof data !== "object" || !SERVER_TAG.test(String(data.server)) || typeof data.dosid !== "string" || !/^[A-Za-z0-9]{8,128}$/.test(data.dosid)) {
return { error: "launchNoSession" };
}
const proxy = proxyOf(data.proxy);
if (data.proxy && !proxy) {
return { error: "launchBadProxy" };
}
return {
server: data.server,
dosid: data.dosid,
profile: PROFILE.test(String(data.profile)) ? String(data.profile) : null,
url: startUrlOf(data.url),
proxy
};
}
function startUrlOf(url) {
if (typeof url !== "string" || url.length > 2048) {
return null;
}
try {
const target = new URL(url);
const host = target.hostname.toLowerCase();
return target.protocol === "https:" && GAME_DOMAINS.some(domain => host === domain || host.endsWith(`.${domain}`))
? target.href
: null;
} catch (err) {
return null;
}
}
function proxyOf(proxy) {
if (!proxy || typeof proxy !== "object") {
return null;
}
const type = proxy.type === "socks5" ? "socks5" : proxy.type === "http" ? "http" : null;
const port = Number(proxy.port);
if (!type || typeof proxy.host !== "string" || !/^[A-Za-z0-9.\-:\[\]]{1,255}$/.test(proxy.host) || !Number.isInteger(port) || port < 1 || port > 65535) {
return null;
}
return {
type,
host: proxy.host,
port,
username: typeof proxy.username === "string" && proxy.username ? proxy.username : null,
password: typeof proxy.password === "string" ? proxy.password : null
};
}
module.exports = { take };