Restore embedded DuneGit SSO and move workspace controls below content

This commit is contained in:
Dev 2026-11-13 00:34:38 +01:00
commit 6a9a0ffd67
2 changed files with 105 additions and 17 deletions

View file

@ -1,20 +1,25 @@
import {request as httpRequest} from 'node:http';
export function createForgejoProxy(settings){
export function createForgejoProxy(settings,identityFor=async()=>null){
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;
if(!req.url?.startsWith('/git/')||req.url.split('?')[0].startsWith('/git/sso/'))return false;
const authHeader=settings.FORGEJO_AUTH_HEADER?.toLowerCase();
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(';');
for(const key of Object.keys(headers))if(key===authHeader||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_')).join(';');
req.setTimeout(300000);
const request=httpRequest({hostname:upstream.hostname,port:upstream.port,method:req.method,path:req.url.slice(4),headers},response=>{
const forward=()=>{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;
res.on('close',()=>{if(!res.writableEnded)request.destroy();});req.on('aborted',()=>request.destroy());req.pipe(request);};
if(authHeader&&!req.headers.authorization&&!/\/git-(?:upload|receive)-pack(?:\?|$)|\.git(?:\/|\?|$)|^\/git\/(?:api|assets)\//.test(req.url)){
Promise.resolve().then(()=>identityFor(req)).then(login=>{if(res.destroyed)return;if(login)headers[authHeader]=login;forward();}).catch(()=>{if(!res.headersSent)res.writeHead(503,{'Content-Type':'text/plain','Cache-Control':'no-store'});res.end('DuneGit sign-in is temporarily unavailable.');});
}else forward();
return true;
};
}

View file

@ -1,3 +1,4 @@
import {createGitIdentityRoute,initializeGitIdentities,gitProxyIdentity} from './git-identity.mjs';
import {createPublicExchange} from './exchange-public.mjs';
import {publicBanCards,publicAppealCards,publicBanList,publicAccountBans} from './public-bans.mjs';
import {initializeRanks,can} from './permissions.mjs';
@ -21,6 +22,7 @@ const security={'X-Content-Type-Options':'nosniff','Referrer-Policy':'strict-ori
export function createApplication(db,settings={}) {
const origin=new URL(settings.PUBLIC_ORIGIN||'https://dunerealms.com').origin;
const auth=createAuth(db,origin);
const gitIdentity=createGitIdentityRoute(db,settings,origin);
const worldStatus=createWorldStatus(settings);
const publicRoster=createPublicStaffRoster(db);
const contact=createContact(settings);
@ -29,6 +31,7 @@ export function createApplication(db,settings={}) {
const publicNews=createPublicNews(db);const publicExchange=createPublicExchange(db,settings);
return async function handle(request) {
const url=new URL(request.url);
if(url.pathname.startsWith('/git/sso/')||url.pathname==='/api/git/me')return gitIdentity(request);
if(url.pathname==='/api/exchange/listings')return publicExchange(request);
// Website articles use the editorial register, before the legacy Discord feed.
if(url.pathname==='/api/news'||url.pathname.startsWith('/api/news/')||url.pathname==='/api/home/forum')return publicNews(request);
@ -75,8 +78,11 @@ export function createApplication(db,settings={}) {
};
}
export async function start(settings=process.env) {
const db=await openDatabase(settings);await initializeRanks(db);await initializePublicRoster(db);await initializeCompanyRegistry(db);await initializeShareLedger(db);const handle=createApplication(db,settings),origin=new URL(settings.PUBLIC_ORIGIN||'https://dunerealms.com').origin;
const forgejo=createForgejoProxy(settings);
const db=await openDatabase(settings);await initializeRanks(db);await initializeGitIdentities(db);await initializePublicRoster(db);await initializeCompanyRegistry(db);await initializeShareLedger(db);const handle=createApplication(db,settings),origin=new URL(settings.PUBLIC_ORIGIN||'https://dunerealms.com').origin;
let forgeHeader=settings.FORGEJO_AUTH_HEADER;
if(!forgeHeader&&settings.FORGEJO_URL){const config=await readFile(settings.FORGEJO_CONFIG_FILE||'/home/dunerealms/apps/forgejo/custom/conf/app.ini','utf8');forgeHeader=config.match(/^REVERSE_PROXY_AUTHENTICATION_USER\s*=\s*(.+)$/mi)?.[1]?.trim();}
if(settings.FORGEJO_URL&&!/^[A-Za-z0-9-]+$/.test(forgeHeader||''))throw Error('DuneGit proxy identity header is not configured.');
const forgejo=createForgejoProxy({...settings,FORGEJO_AUTH_HEADER:forgeHeader},gitProxyIdentity(db,settings));
const server=createServer(async(req,res)=>{
try{
if(forgejo(req,res))return;
@ -84,7 +90,7 @@ export async function start(settings=process.env) {
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),outgoing=new Headers(security);for(const [key,value]of result.headers)outgoing.set(key,value);outgoing.set('Content-Security-Policy',security['Content-Security-Policy']);res.writeHead(result.status,Object.fromEntries(outgoing));res.end(Buffer.from(await result.arrayBuffer()));
const result=await handle(request),outgoing=new Headers(security);for(const [key,value]of result.headers)outgoing.set(key,value);outgoing.set('Content-Security-Policy',path.startsWith('/git/sso/')?(result.headers.get('Content-Security-Policy')||security['Content-Security-Policy']):security['Content-Security-Policy']);res.writeHead(result.status,Object.fromEntries(outgoing));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=300000;server.headersTimeout=10000;