@@ -2,17 +2,23 @@ import express from 'express';
22import * as fs from 'fs/promises' ;
33import fetch from 'node-fetch' ;
44import cors from 'cors' ;
5- import { createRequire } from 'module' ;
6- const require = createRequire ( import . meta. url ) ;
75
86const app = express ( ) ;
9- const packagefile = require ( './package.json' ) ;
7+ const games = [ ] ;
8+ const thumbnails = [ ] ;
109
1110app . use ( express . json ( ) ) ;
1211app . use ( cors ( { origin : '*' } ) ) ;
1312
1413app . 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
1824app . 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+ } ) ;
0 commit comments