Host Forgejo under git subpath and document public repository copies
This commit is contained in:
parent
1631b6bbe4
commit
97a6167bf5
4 changed files with 45 additions and 1 deletions
13
network/forgejo/README.md
Normal file
13
network/forgejo/README.md
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
# DuneRealms Forgejo
|
||||
|
||||
Installed 18 October 2026 under the existing `dunerealms` hosting user. Public URL: https://www.dunerealms.com/git/. Forgejo 17.0.0 official amd64 binary was checked against its SHA256 and verified with the official release-signing key fingerprint EB114F5E6C0DC2BCDD183550A4B61A2DC5923710.
|
||||
|
||||
The installation lives in `/home/dunerealms/apps/forgejo`: binary in `bin/`, SQLite database in `data/`, bare Git repositories in `repositories/`, and private configuration in `custom/conf/app.ini`. No credentials, signing keys or database are stored in this source repository. Administrator username is `unimatrix`; public registration is disabled. Email delivery and Actions runners are not configured. Git transport is HTTPS; Forgejo SSH and LFS are disabled in this initial installation.
|
||||
|
||||
Forgejo binds to localhost:3001. The website's Node host streams `/git/` to that service, preserving Git requests and Forgejo cookies without exposing website session cookies or accepting forged reverse-proxy identity headers. Ordinary website requests retain their bounded body limit. The public Nginx Vhost redirects apex and www1 requests to `https://www.dunerealms.com$request_uri`; www is the serving hostname. The website's private environment sets `PUBLIC_ORIGIN=https://www.dunerealms.com` and `FORGEJO_URL=http://127.0.0.1:3001`. Forgejo config uses `DOMAIN=www.dunerealms.com` and `ROOT_URL=https://www.dunerealms.com/git/`.
|
||||
|
||||
PM2 manages `forgejo` and `dunerealms-web` as the site user, with the saved process list restored by its existing reboot entry. Website release is `20261018-forgejo`, cloned from the working status-panel release. The previous release is preserved. Back up the entire Forgejo directory, including its SQLite database and private config, consistently while Forgejo is stopped or with the supported Forgejo backup tooling.
|
||||
|
||||
The owner explicitly requested public copies of both private GitHub repositories under the Forgejo account. `MC` was imported with its complete history and four branches; `A3` was empty on GitHub and is an empty repository here as well. GitHub visibility was not changed. These are independent copies, not automatically scheduled mirrors.
|
||||
|
||||
Verification: public HTTPS pages, anonymous repository listing and Git ref advertisements worked; MC branch hashes matched the source. The proxy test covers a Git payload above the website's normal body limit, subpath routing, auth forwarding and separation from website cookies/identity headers. Website health and the live world-status API remained available after the canonical hostname change.
|
||||
20
website/node/forgejo-proxy.mjs
Normal file
20
website/node/forgejo-proxy.mjs
Normal 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;
|
||||
};
|
||||
}
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
7
website/tests/forgejo-proxy.test.mjs
Normal file
7
website/tests/forgejo-proxy.test.mjs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import test from 'node:test';import assert from 'node:assert/strict';import {createServer,request} from 'node:http';import {createForgejoProxy} from '../node/forgejo-proxy.mjs';
|
||||
const listen=s=>new Promise(r=>s.listen(0,'127.0.0.1',r));const close=s=>new Promise(r=>s.close(r));
|
||||
test('Forgejo subpath streams git bodies, preserves auth, strips website identity and cookies',async()=>{
|
||||
const upstream=createServer((req,res)=>{let bytes=0;req.on('data',c=>bytes+=c.length);req.on('end',()=>{res.setHeader('Set-Cookie',['forgejo_session=test; Path=/git/; Secure']);res.end(JSON.stringify({path:req.url,auth:req.headers.authorization,cookie:req.headers.cookie,forged:req.headers['x-webauth-user'],bytes}));});});await listen(upstream);
|
||||
const proxy=createForgejoProxy({FORGEJO_URL:'http://127.0.0.1:'+upstream.address().port,PUBLIC_ORIGIN:'https://dunerealms.com'});const server=createServer((req,res)=>{if(!proxy(req,res)){res.statusCode=404;res.end();}});await listen(server);
|
||||
try{const base='http://127.0.0.1:'+server.address().port;const redirected=await fetch(base+'/git',{redirect:'manual'});assert.equal(redirected.status,308);assert.equal(redirected.headers.get('location'),'/git/');const r=await fetch(base+'/git/unimatrix/MC.git/git-receive-pack',{method:'POST',headers:{Authorization:'Basic test',Cookie:'__Host-dune_session=private; forgejo_session=abc','X-Webauth-User':'admin'},body:Buffer.alloc(90000)});const data=await r.json();assert.equal(data.path,'/unimatrix/MC.git/git-receive-pack');assert.equal(data.auth,'Basic test');assert.ok(!data.cookie.includes('private'));assert.equal(data.forged,undefined);assert.equal(data.bytes,90000);assert.match(r.headers.get('set-cookie'),/Path=\/git\//);}finally{await close(server);await close(upstream);}
|
||||
});
|
||||
Loading…
Reference in a new issue

Staff