Skip to content

Commit 38eef5b

Browse files
committed
add thumbnail proxy
1 parent 3c34db4 commit 38eef5b

File tree

4 files changed

+105
-56
lines changed

4 files changed

+105
-56
lines changed

auth

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
publi
1+
public

index.js

Lines changed: 101 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,23 @@ import express from 'express';
22
import * as fs from 'fs/promises';
33
import fetch from 'node-fetch';
44
import cors from 'cors';
5-
import { createRequire } from 'module';
6-
const require = createRequire(import.meta.url);
75

86
const app = express();
9-
const packagefile = require('./package.json');
7+
const games = [];
8+
const thumbnails = [];
109

1110
app.use(express.json());
1211
app.use(cors({ origin: '*' }));
1312

1413
app.all('/', async (req, res) => {
15-
res.json({ status: 'ready', version: packagefile.version, website: 'https://gh.retronetwork.ml', description: packagefile.description, repository: packagefile.repository.url.replace('git+', '').replace('.git', '') });
14+
const packageFile = JSON.parse(await fs.readFile('./package.json'));
15+
res.json({
16+
status: 'ready',
17+
version: packageFile.version,
18+
website: 'https://gamehub.dev',
19+
description: packageFile.description,
20+
repository: packageFile.repository.url.replace('git+', '').replace('.git', '')
21+
});
1622
});
1723

1824
app.all('*', async (req, res, next) => {
@@ -41,46 +47,124 @@ app.all('*', async (req, res, next) => {
4147
}
4248
})
4349

44-
app.all('*', async (req, res) => {
50+
app.all('/cdn/games/:id/*', async (req, res) => {
51+
const gameIds = [];
52+
games.forEach(game => {
53+
gameIds.push(game.id);
54+
})
55+
56+
try {
57+
const file = await fetch(games[gameIds.indexOf(`${req.params.id}`)].url + req.path.replace(`/cdn/games/${req.params.id}`, ''));
58+
59+
const data = new Buffer.from(await file.arrayBuffer());
60+
61+
if (file.status !== 404) {
62+
res.writeHead(file.status, { 'Content-Type': file.headers.get('content-type').split(';')[0] })
63+
res.end(data);
64+
} else {
65+
return res.sendStatus(404);
66+
}
67+
} catch (e) {
68+
res.sendStatus(404);
69+
}
70+
});
71+
72+
app.all('/cdn/thumbnails/:id', async (req, res) => {
73+
try {
74+
const file = await fetch(thumbnails[req.params.id-1]);
75+
76+
const data = new Buffer.from(await file.arrayBuffer());
77+
78+
if (file.status !== 404) {
79+
res.writeHead(file.status, { 'Content-Type': file.headers.get('content-type').split(';')[0] })
80+
res.end(data);
81+
} else {
82+
return res.sendStatus(404);
83+
}
84+
} catch (e) {
85+
res.sendStatus(404);
86+
}
87+
});
88+
89+
app.all('*', async (req, res, next) => {
4590
try {
4691
const modifiedHeaders = req.headers;
4792
modifiedHeaders.host = req.get('host');
4893
modifiedHeaders.origin = `${req.protocol}://${req.get('host')}`;
4994
modifiedHeaders.auth = await fs.readFile('./auth');
50-
modifiedHeaders.instance = JSON.stringify({ domain: req.hostname, auth: res.locals.authKey });
95+
if (req.get('host').replace(':' + server.address().port, '') === req.hostname) {
96+
modifiedHeaders.instance = JSON.stringify({ domain: req.hostname, auth: res.locals.authKey, protocol: req.protocol });
97+
}
5198

5299
if (req.method == 'GET' || req.method == 'HEAD') {
53-
const file = await fetch(`https://retronetworkapi.onrender.com/GameHub${req.originalUrl}`, {
100+
const file = await fetch(`http://localhost:3000/GameHub${req.originalUrl}`, {
54101
method: req.method,
55102
headers: modifiedHeaders
56103
});
57104

58-
const data = new Buffer.from(await file.arrayBuffer());
105+
if (file.status !== 404) {
106+
if (req.path.replace('/' + req.path.split('\\').pop().split('/').pop(), '') === '/games') {
107+
const gameIds = [];
108+
109+
games.forEach(game => {
110+
gameIds.push(game.id);
111+
})
112+
113+
if (!gameIds.includes(req.path.split('\\').pop().split('/').pop())) {
114+
games.push({
115+
id: req.path.split('\\').pop().split('/').pop(),
116+
url: file.headers.get('gameUrl'),
117+
thumb: file.headers.get('thumbUrl')
118+
});
119+
}
120+
}
121+
122+
const data = new Buffer.from(await file.arrayBuffer());
123+
124+
if (req.path == '/games') {
125+
const moddedData = [];
126+
JSON.parse(data.toString()).forEach(game => {
127+
thumbnails.push(game.thumbnail);
128+
moddedData.push({
129+
thumbnail: `${req.protocol}://${req.get('host')}/cdn/thumbnails/${game.id}`,
130+
name: game.name,
131+
id: game.id
132+
})
133+
});
59134

60-
if (file.headers.get('content-type').split(';')[0] == 'text/plain' && req.path.endsWith('.html') || req.path.endsWith('.htm')) {
61-
res.writeHead(file.status, { 'Content-Type': 'text/html' })
135+
res.json(moddedData);
136+
} else {
137+
res.writeHead(file.status, { 'Content-Type': file.headers.get('content-type').split(';')[0] })
138+
139+
res.end(data);
140+
}
62141
} else {
63-
res.writeHead(file.status, { 'Content-Type': file.headers.get('content-type').split(';')[0] })
142+
next();
64143
}
65-
66-
res.end(data);
67144
} else {
68-
const file = await fetch(`https://retronetworkapi.onrender.com/GameHub${req.originalUrl}`, {
145+
const file = await fetch(`http://localhost:3000/GameHub${req.originalUrl}`, {
69146
method: req.method,
70147
headers: modifiedHeaders,
71148
body: JSON.stringify(req.body)
72149
});
73150

74151
const data = new Buffer.from(await file.arrayBuffer());
152+
75153
res.writeHead(file.status, { 'Content-Type': file.headers.get('content-type').split(';')[0] })
76154

77155
res.end(data);
78156
}
79157
} catch (e) {
80-
res.sendStatus(404);
158+
next();
81159
}
82160
})
83161

84-
app.listen(2000, () => {
85-
console.log(`Your mirror server is running on port 2000 using node ${process.version}`);
162+
app.use((req, res) => {
163+
try {
164+
res.sendStatus(404);
165+
} catch (e) { }
86166
});
167+
168+
const server = app.listen(2000, () => {
169+
console.log(`Your mirror server is running on port 2000 using node ${process.version}`);
170+
});

package-lock.json

Lines changed: 0 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22
"dependencies": {
33
"cors": "^2.8.5",
44
"express": "^4.18.2",
5-
"fetch": "^1.1.0",
65
"node-fetch": "^3.3.0"
76
},
87
"name": "gamehub-api-mirror-server",
98
"description": "A simple proxy server for the GameHub API",
10-
"version": "1.0.39",
9+
"version": "1.1.0",
1110
"main": "index.js",
1211
"type": "module",
1312
"scripts": {
14-
"start": "node index.mjs"
13+
"start": "node index.js"
1514
},
1615
"repository": {
1716
"type": "git",
@@ -21,7 +20,7 @@
2120
"proxy",
2221
"gamehub"
2322
],
24-
"author": "GameHub88",
23+
"author": "Russell2259",
2524
"license": "ISC",
2625
"bugs": {
2726
"url": "https://github.com/GameHub88/Mirror/issues"

0 commit comments

Comments
 (0)