Host Forgejo under git subpath and document public repository copies

This commit is contained in:
unimatrixgit 2026-10-18 20:54:30 +02:00
commit 97a6167bf5
2 changed files with 45 additions and 1 deletions

View file

@ -0,0 +1,20 @@
import {request as httpRequest} from 'node:http';
export function createForgejoProxy(settings){
if(!settings.FORGEJO_URL)return ()=>false;
const upstream=new URL(settings.FORGEJO_URL),origin=new URL(settings.PUBLIC_ORIGIN||'https://dunerealms.com');
if(upstream.protocol!=='http:'||!['127.0.0.1','localhost'].includes(upstream.hostname))throw Error('Forgejo must bind to localhost');
return (req,res)=>{
if(req.url==='/git'||req.url?.startsWith('/git?')){res.writeHead(308,{Location:'/git/'+(req.url.slice(4)||''),'Cache-Control':'no-store'});res.end();return true;}
if(!req.url?.startsWith('/git/'))return false;
const headers={...req.headers,host:origin.host,'x-forwarded-proto':origin.protocol.slice(0,-1),'x-forwarded-host':origin.host,'x-forwarded-for':req.socket.remoteAddress};
for(const key of Object.keys(headers))if(key.startsWith('oai-')||key.startsWith('x-internal-')||key.startsWith('x-webauth-')||['connection','upgrade','proxy-authorization'].includes(key))delete headers[key];
if(headers.cookie)headers.cookie=headers.cookie.split(';').filter(c=>!c.trim().startsWith('__Host-dune_session=')).join(';');
req.setTimeout(300000);
const request=httpRequest({hostname:upstream.hostname,port:upstream.port,method:req.method,path:req.url.slice(4),headers},response=>{
const out={...response.headers};delete out.connection;res.writeHead(response.statusCode||502,out);response.pipe(res);response.on('error',()=>res.destroy());
});
request.setTimeout(300000,()=>request.destroy());
request.on('error',()=>{if(!res.headersSent)res.writeHead(502,{'Content-Type':'text/plain','Cache-Control':'no-store'});res.end('The code forge is temporarily unavailable.');});
res.on('close',()=>{if(!res.writableEnded)request.destroy();});req.on('aborted',()=>request.destroy());req.pipe(request);return true;
};
}

View file

@ -5,6 +5,7 @@ import worker from '../dist/server/index.js';
import {openDatabase} from './mysql.mjs';
import {createAuth,identity,safeReturn} from './auth.mjs';
import {createWorldStatus} from './world-status.mjs';
import {createForgejoProxy} from './forgejo-proxy.mjs';
const root=new URL('./public/',import.meta.url);
const security={'X-Content-Type-Options':'nosniff','Referrer-Policy':'strict-origin-when-cross-origin','Content-Security-Policy':"default-src 'self'; img-src 'self' data:; style-src 'self' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; script-src 'self'; connect-src 'self'; base-uri 'self'; object-src 'none'; frame-ancestors 'self'"};
export function createApplication(db,settings={}) {
@ -40,15 +41,18 @@ export function createApplication(db,settings={}) {
}
export async function start(settings=process.env) {
const db=await openDatabase(settings),handle=createApplication(db,settings),origin=new URL(settings.PUBLIC_ORIGIN||'https://dunerealms.com').origin;
const forgejo=createForgejoProxy(settings);
const server=createServer(async(req,res)=>{
try{
if(forgejo(req,res))return;
req.setTimeout(15000);
let bytes=0;const chunks=[];for await(const chunk of req){bytes+=chunk.length;if(bytes>65536){res.writeHead(413);res.end('Request too large');return;}chunks.push(chunk);}
const path=req.url||'/';if(!path.startsWith('/')||path.startsWith('//')){res.writeHead(400);res.end('Bad request');return;}
const request=new Request(origin+path,{method:req.method,headers:req.headers,body:['GET','HEAD'].includes(req.method)?undefined:Buffer.concat(chunks)});
const result=await handle(request);res.writeHead(result.status,{...security,...Object.fromEntries(result.headers)});res.end(Buffer.from(await result.arrayBuffer()));
}catch(error){console.error('Request failed:',error.code||error.name);if(!res.headersSent)res.writeHead(503,{'Content-Type':'application/json','Cache-Control':'no-store'});res.end(JSON.stringify({message:'The website is temporarily unavailable.'}));}
});
server.requestTimeout=15000;server.headersTimeout=10000;
server.requestTimeout=300000;server.headersTimeout=10000;
await new Promise((resolve,reject)=>{server.once('error',reject);server.listen(Number(settings.PORT||3000),'127.0.0.1',resolve);});
console.log('DuneRealms website running on localhost:'+Number(settings.PORT||3000)+'; MySQL connected.');
const shutdown=()=>server.close(async()=>{await db.close();process.exit(0);});process.once('SIGTERM',shutdown);process.once('SIGINT',shutdown);