glitchless-playtest 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +69 -3
- package/bin/glitchless-playtest.js +33 -1
- package/package.json +1 -1
- package/src/check-bot.js +465 -0
- package/src/check-page.js +252 -0
- package/src/check-report.js +327 -0
- package/src/check-site.js +158 -0
- package/src/check.js +549 -0
- package/src/engine.js +75 -12
- package/src/index.js +17 -5
- package/src/roblox.js +13 -9
- package/src/run.js +17 -8
- package/src/stop.js +36 -0
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// Where a game check finds the game behind the address someone posted, and whether the site lets a bot play it.
|
|
2
|
+
// An itch.io page plays its HTML5 game in a frame on html-classic.itch.zone (an address that changes with every
|
|
3
|
+
// upload), so the check reads the page for that frame and plays it at the page's own size. Any other address is
|
|
4
|
+
// played as it is. Before playing, the check reads the site's robots.txt and stops where it asks bots to stay out;
|
|
5
|
+
// while loading, it stops on a refusal (401, 403, 429) or a page that checks for a human (a challenge or CAPTCHA),
|
|
6
|
+
// which it never tries to get past.
|
|
7
|
+
|
|
8
|
+
// How the check names itself to sites: in the User-Agent after the browser's own, and as the robots.txt agent.
|
|
9
|
+
export const CHECK_AGENT = 'GlitchlessCheck';
|
|
10
|
+
export const CHECK_UA_SUFFIX = `${CHECK_AGENT}/1.0 (+https://www.glitchless.gg)`;
|
|
11
|
+
|
|
12
|
+
const decode = (s) =>
|
|
13
|
+
String(s)
|
|
14
|
+
.replace(/"/g, '"')
|
|
15
|
+
.replace(/'|'/g, "'")
|
|
16
|
+
.replace(/</g, '<')
|
|
17
|
+
.replace(/>/g, '>')
|
|
18
|
+
.replace(/&/g, '&')
|
|
19
|
+
.replace(/\\\//g, '/');
|
|
20
|
+
|
|
21
|
+
// An itch.io game page's address: https://<maker>.itch.io/<game> (a draft's ?secret= kept). → URL or null.
|
|
22
|
+
export function itchPage(url) {
|
|
23
|
+
try {
|
|
24
|
+
const u = new URL(url);
|
|
25
|
+
return /^[a-z0-9-]+\.itch\.io$/i.test(u.hostname) && /^\/[^/]+\/?$/.test(u.pathname) ? u : null;
|
|
26
|
+
} catch {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// The game in an itch.io page's HTML → { frame_url, width, height, title } or { error } in words for a person.
|
|
32
|
+
// The frame is an <iframe> in the page, or for a game behind a "Run game" button, the one in its data-iframe, or for
|
|
33
|
+
// one that opens in its own window, the address in the page's script.
|
|
34
|
+
export function itchFrame(html, pageUrl) {
|
|
35
|
+
const title = decode(html.match(/<h1[^>]*class="[^"]*game_title[^"]*"[^>]*>([^<]+)</)?.[1] || html.match(/<title>([^<]+?)(?: by [^<]+)?<\/title>/)?.[1] || '').trim() || null;
|
|
36
|
+
if (/class="[^"]*password_form|This page is password protected/i.test(html)) return { error: `the itch.io page ${pageUrl} is password-protected, so a bot can't open it`, title };
|
|
37
|
+
const frameRe = /(?:https?:)?\/\/html(?:-classic)?\.itch\.zone\/html\/\d+\/[^"'\s<>\\]+/;
|
|
38
|
+
const iframe = html.match(/<iframe[^>]+src="((?:https?:)?\/\/html(?:-classic)?\.itch\.zone\/[^"]+)"/)?.[1];
|
|
39
|
+
const placeholder = html.match(/data-iframe="([^"]+)"/)?.[1];
|
|
40
|
+
const inScript = decode(html).match(frameRe)?.[0];
|
|
41
|
+
let src = iframe || (placeholder && decode(placeholder).match(frameRe)?.[0]) || inScript;
|
|
42
|
+
if (!src) return { error: `the itch.io page ${pageUrl} has nothing that plays in a browser (its game is a download)`, title };
|
|
43
|
+
src = decode(src);
|
|
44
|
+
if (src.startsWith('//')) src = `https:${src}`;
|
|
45
|
+
const frame = html.match(/<div[^>]*class="[^"]*game_frame[^"]*"[^>]*>/)?.[0] || '';
|
|
46
|
+
const num = (k) => Number(frame.match(new RegExp(`data-${k}="(\\d+)"`))?.[1] || html.match(new RegExp(`"${k}":\\s*(\\d+)`))?.[1]) || null;
|
|
47
|
+
return { frame_url: src, width: num('width'), height: num('height'), title };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// robots.txt → whether `agent` may fetch `path`: the group for the agent by name, else `*`; the longest matching
|
|
51
|
+
// rule wins, and Allow wins a tie (RFC 9309). `*` and a final `$` work as they do for search engines.
|
|
52
|
+
export function robotsAllow(text, agent, path) {
|
|
53
|
+
const groups = [];
|
|
54
|
+
let current = null;
|
|
55
|
+
let lastWasAgent = false;
|
|
56
|
+
for (const raw of String(text || '').split(/\r?\n/)) {
|
|
57
|
+
const line = raw.replace(/#.*/, '').trim();
|
|
58
|
+
const m = line.match(/^([a-z-]+)\s*:\s*(.*)$/i);
|
|
59
|
+
if (!m) continue;
|
|
60
|
+
const [key, value] = [m[1].toLowerCase(), m[2].trim()];
|
|
61
|
+
if (key === 'user-agent') {
|
|
62
|
+
if (!lastWasAgent) groups.push((current = { agents: [], rules: [] }));
|
|
63
|
+
current.agents.push(value.toLowerCase());
|
|
64
|
+
lastWasAgent = true;
|
|
65
|
+
} else if ((key === 'allow' || key === 'disallow') && current) {
|
|
66
|
+
lastWasAgent = false;
|
|
67
|
+
if (value) current.rules.push({ allow: key === 'allow', path: value });
|
|
68
|
+
} else lastWasAgent = false;
|
|
69
|
+
}
|
|
70
|
+
const name = agent.toLowerCase();
|
|
71
|
+
const mine = groups.filter((g) => g.agents.some((a) => a !== '*' && name.includes(a)));
|
|
72
|
+
const rules = (mine.length ? mine : groups.filter((g) => g.agents.includes('*'))).flatMap((g) => g.rules);
|
|
73
|
+
const esc = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
74
|
+
let best = null;
|
|
75
|
+
for (const r of rules) {
|
|
76
|
+
const anchored = r.path.endsWith('$');
|
|
77
|
+
const re = new RegExp(`^${(anchored ? r.path.slice(0, -1) : r.path).split('*').map(esc).join('.*')}${anchored ? '$' : ''}`);
|
|
78
|
+
if (!re.test(path)) continue;
|
|
79
|
+
if (!best || r.path.length > best.path.length || (r.path.length === best.path.length && r.allow)) best = r;
|
|
80
|
+
}
|
|
81
|
+
return best ? best.allow : true;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// A response that says the site doesn't want bots: → the reason in words, or null.
|
|
85
|
+
export function refusal({ status, headers = {}, title = '', body = '' }) {
|
|
86
|
+
const h = (k) => headers[k] ?? headers[k.toLowerCase()];
|
|
87
|
+
if (status === 401 || status === 403) return `the site answered ${status} (it refuses this visit)`;
|
|
88
|
+
if (status === 429) return 'the site answered 429 (too many requests): it is limiting bots';
|
|
89
|
+
if (h('cf-mitigated') === 'challenge') return 'the site checks for a human first (a Cloudflare challenge)';
|
|
90
|
+
const words = `${title} ${String(body).slice(0, 4000)}`;
|
|
91
|
+
if (/just a moment\.\.\.|attention required|verify you are (a )?human|are you a robot|captcha|checking your browser/i.test(words)) return 'the site checks for a human first (a challenge or CAPTCHA)';
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// robots.txt of a page's site, asked once per host: → { allowed, rules_url }. A site without one (404) allows.
|
|
96
|
+
export async function robotsCheck(url, { fetch = globalThis.fetch, ua, cache = new Map() } = {}) {
|
|
97
|
+
const u = new URL(url);
|
|
98
|
+
const at = `${u.origin}/robots.txt`;
|
|
99
|
+
if (!cache.has(at)) {
|
|
100
|
+
const text = await fetch(at, { headers: { 'user-agent': ua }, redirect: 'follow', signal: AbortSignal.timeout(10_000) })
|
|
101
|
+
.then(async (r) => (r.ok && /text\/plain/.test(r.headers.get('content-type') || 'text/plain') ? r.text() : ''))
|
|
102
|
+
.catch(() => '');
|
|
103
|
+
cache.set(at, text);
|
|
104
|
+
}
|
|
105
|
+
return { allowed: robotsAllow(cache.get(at), CHECK_AGENT, `${u.pathname}${u.search}`), rules_url: at };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// The address someone posted → what the check plays: { page_url, play_url, title, size: {width, height} | null,
|
|
109
|
+
// site, referer }. An itch.io page is fetched once for its frame; anything else is played as it is.
|
|
110
|
+
export async function resolveGame(url, { fetch = globalThis.fetch, ua, log = () => {}, robots = new Map() } = {}) {
|
|
111
|
+
let u;
|
|
112
|
+
try {
|
|
113
|
+
u = new URL(/^https?:\/\//i.test(url) ? url : `https://${url}`);
|
|
114
|
+
} catch {
|
|
115
|
+
throw new Error(`"${url}" is not a web address: give the game's page, such as https://someone.itch.io/their-game`);
|
|
116
|
+
}
|
|
117
|
+
if (!['http:', 'https:'].includes(u.protocol)) throw new Error(`only http and https addresses can be checked, not ${u.protocol}`);
|
|
118
|
+
const page = u.toString();
|
|
119
|
+
const robotsPage = await robotsCheck(page, { fetch, ua, cache: robots });
|
|
120
|
+
if (!robotsPage.allowed) throw new Error(`${robotsPage.rules_url} asks bots not to visit ${u.pathname}, so the check stops here`);
|
|
121
|
+
const itch = itchPage(page);
|
|
122
|
+
if (!itch) return { page_url: page, play_url: page, title: null, size: null, site: siteOf(u.hostname), referer: null };
|
|
123
|
+
|
|
124
|
+
log(`reading the itch.io page ${page}`);
|
|
125
|
+
const res = await fetch(page, { headers: { 'user-agent': ua }, redirect: 'follow', signal: AbortSignal.timeout(20_000) });
|
|
126
|
+
const html = await res.text();
|
|
127
|
+
const refused = refusal({ status: res.status, headers: Object.fromEntries(res.headers), body: html, title: html.match(/<title>([^<]*)/)?.[1] });
|
|
128
|
+
if (refused) throw new Error(`${page}: ${refused}, so the check stops here`);
|
|
129
|
+
if (res.status === 404) throw new Error(`there is no itch.io page at ${page} (404): is it public, or a draft that needs its secret link?`);
|
|
130
|
+
if (!res.ok) throw new Error(`${page} answered HTTP ${res.status}`);
|
|
131
|
+
const found = itchFrame(html, page);
|
|
132
|
+
if (found.error) throw new Error(found.error);
|
|
133
|
+
const robotsFrame = await robotsCheck(found.frame_url, { fetch, ua, cache: robots });
|
|
134
|
+
if (!robotsFrame.allowed) throw new Error(`${robotsFrame.rules_url} asks bots not to load the game's frame, so the check stops here`);
|
|
135
|
+
return { page_url: page, play_url: found.frame_url, title: found.title, size: found.width && found.height ? { width: found.width, height: found.height } : null, site: 'itch.io', referer: page };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// The kind of place a game was posted, in the words a report uses.
|
|
139
|
+
export function siteOf(host) {
|
|
140
|
+
const h = String(host).toLowerCase();
|
|
141
|
+
const sites = [
|
|
142
|
+
['itch.zone', 'itch.io'],
|
|
143
|
+
['itch.io', 'itch.io'],
|
|
144
|
+
['github.io', 'GitHub Pages'],
|
|
145
|
+
['vercel.app', 'Vercel'],
|
|
146
|
+
['netlify.app', 'Netlify'],
|
|
147
|
+
['pages.dev', 'Cloudflare Pages'],
|
|
148
|
+
['web.app', 'Firebase Hosting'],
|
|
149
|
+
['firebaseapp.com', 'Firebase Hosting'],
|
|
150
|
+
['glitch.me', 'Glitch'],
|
|
151
|
+
['replit.app', 'Replit'],
|
|
152
|
+
['surge.sh', 'Surge'],
|
|
153
|
+
['newgrounds.com', 'Newgrounds'],
|
|
154
|
+
['crazygames.com', 'CrazyGames'],
|
|
155
|
+
];
|
|
156
|
+
if (h === 'localhost' || h === '127.0.0.1' || h === '[::1]' || h.endsWith('.localhost')) return 'this computer';
|
|
157
|
+
return sites.find(([suffix]) => h === suffix || h.endsWith(`.${suffix}`))?.[1] ?? h.replace(/^www\./, '');
|
|
158
|
+
}
|