Merge pull request 'Update server to wirk with NGINX proxy without funky workarounds' (#1) from dev into main

Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
2025-06-25 13:36:09 +10:00
3 changed files with 158 additions and 6 deletions

View File

@@ -1,5 +1,6 @@
# Server settings
PORT=3000
DASHBOARD_HOST=ipfs.hnsproxy.au
# IPFS settings
IPFS_GATEWAY=https://ipfs.io

View File

@@ -84,12 +84,13 @@ To proxy Handshake domains to IPFS, you can use the following nginx configuratio
```nginx
server {
listen 80;
server_name ~^(?<domain>.+)$;
server_name _; # Catch-all for all domains
location / {
proxy_pass http://127.0.0.1:3000/$domain;
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

158
server.js
View File

@@ -14,6 +14,22 @@ app.use(cors());
app.use(morgan('dev'));
app.use(express.json());
// Add a request logger middleware at the very beginning
app.use((req, res, next) => {
console.log('\n----- NEW REQUEST -----');
console.log(`${req.method} ${req.url}`);
console.log(`Headers: ${JSON.stringify({
host: req.get('host'),
referer: req.get('referer'),
'user-agent': req.get('user-agent')
}, null, 2)}`);
next();
});
// Define the main dashboard host
// This should be configurable via environment variable
const DASHBOARD_HOST = process.env.DASHBOARD_HOST || '127.0.0.1:3000';
// Define reserved paths that should not be treated as Handshake domains
const RESERVED_PATHS = [
'api',
@@ -27,7 +43,53 @@ const RESERVED_PATHS = [
'favicon.ico'
];
// Serve static files
// Helper function to normalize host strings for comparison
function normalizeHost(host) {
if (!host) return '';
// Remove port if present and convert to lowercase
const normalized = host.split(':')[0].toLowerCase();
console.log(`Normalizing host: ${host} -> ${normalized}`);
return normalized;
}
// IMPORTANT: Move direct domain access middleware before static file serving
// Middleware to check if request is for direct domain access
app.use(async (req, res, next) => {
const host = req.get('host');
const normalizedHost = normalizeHost(host);
const normalizedDashboardHost = normalizeHost(DASHBOARD_HOST);
console.log(`[HOST CHECK] Request host: ${host}, Normalized: ${normalizedHost}, Dashboard: ${normalizedDashboardHost}`);
// Special handling for curl requests with Host header
if (normalizedHost !== normalizedDashboardHost) {
console.log(`[HOST CHECK] Host ${normalizedHost} doesn't match dashboard ${normalizedDashboardHost}, treating as direct access`);
// Skip direct domain handling for API endpoints
if (req.path.startsWith('/api/')) {
console.log(`[HOST CHECK] Skipping direct domain handling for API endpoint: ${req.path}`);
return next();
}
// Extract domain from the hostname
const domain = normalizedHost;
console.log(`[DIRECT ACCESS] Using domain: ${domain} from host: ${host}`);
try {
return await handleDirectDomainAccess(req, res, domain);
} catch (error) {
console.error(`[HOST CHECK] Error handling direct access, falling back to normal processing: ${error}`);
// Fall back to normal processing if direct access fails
return next();
}
}
console.log(`[HOST CHECK] Host ${normalizedHost} matches dashboard ${normalizedDashboardHost}, continuing with normal processing`);
// Continue with normal processing for dashboard host
next();
});
// Serve static files AFTER checking for direct domain access
app.use(express.static(path.join(__dirname, 'public')));
// Helper function to inject tracking script into HTML content
@@ -54,13 +116,14 @@ function injectTrackingScript(content, mimeType) {
}
// Helper function to make links absolute in HTML content
function makeLinksAbsolute(content, mimeType, domain, subPath = '') {
function makeLinksAbsolute(content, mimeType, domain, subPath = '', isDirectAccess = false) {
if (!mimeType || !mimeType.includes('text/html')) {
return content;
}
let htmlContent = content.toString();
const baseUrl = `/${domain}`;
// If direct access (via subdomain), don't prefix links with domain
const baseUrl = isDirectAccess ? '' : `/${domain}`;
// Create base directory for proper path resolution
let basePath = '/';
@@ -121,6 +184,87 @@ function makeLinksAbsolute(content, mimeType, domain, subPath = '') {
return htmlContent;
}
// Helper function to handle direct domain access
async function handleDirectDomainAccess(req, res, domain) {
console.log(`[DIRECT ACCESS] Starting direct domain access handler for: ${domain}`);
try {
// Remove trailing slash from path for consistency
let subPath = req.path || '';
if (subPath === '/' || subPath === '') {
subPath = '';
}
console.log(`[DIRECT ACCESS] Domain: ${domain}, Path: '${subPath}'`);
// Resolve Handshake domain to get IPFS CID
console.log(`[DIRECT ACCESS] Resolving Handshake domain: ${domain}`);
const cid = await resolveHandshake(domain);
if (!cid) {
console.warn(`[DIRECT ACCESS] No IPFS CID found for domain: ${domain}`);
return res.status(404).json({
error: 'Domain not found or has no IPFS record',
domain: domain
});
}
console.log(`[DIRECT ACCESS] Resolved ${domain} to IPFS CID: ${cid}`);
// Fetch content from IPFS - handle root path specially
const path = subPath === '' ? '' : (subPath.startsWith('/') ? subPath.substring(1) : subPath);
console.log(`[DIRECT ACCESS] Fetching IPFS content for CID: ${cid}, path: '${path}'`);
let content = await fetchFromIpfs(cid, path);
if (!content) {
console.log(`[DIRECT ACCESS] No content found for path: '${path}'`);
// Try index.html for empty paths
if (path === '') {
console.log('[DIRECT ACCESS] Trying index.html for root path');
const indexContent = await fetchFromIpfs(cid, 'index.html');
if (indexContent) {
console.log('[DIRECT ACCESS] Found index.html content, using that instead');
content = indexContent;
} else {
console.log('[DIRECT ACCESS] No index.html found either');
}
}
// If still no content, return 404
if (!content) {
console.log(`[DIRECT ACCESS] Returning 404 for CID: ${cid}, path: '${path}'`);
return res.status(404).json({
error: 'Content not found on IPFS network',
cid: cid,
path: path
});
}
}
// Set appropriate content type
if (content.mimeType) {
console.log(`[DIRECT ACCESS] Setting content type: ${content.mimeType}`);
res.setHeader('Content-Type', content.mimeType);
}
// Process HTML content: make links absolute and inject tracking script
console.log('[DIRECT ACCESS] Processing content');
let processedContent = content.data;
if (content.mimeType && content.mimeType.includes('text/html')) {
console.log('[DIRECT ACCESS] HTML content detected, making links absolute');
processedContent = makeLinksAbsolute(processedContent, content.mimeType, domain, path, true);
console.log('[DIRECT ACCESS] Injecting tracking script');
processedContent = injectTrackingScript(processedContent, content.mimeType);
}
// Return the content
console.log('[DIRECT ACCESS] Sending processed content');
return res.send(processedContent);
} catch (error) {
console.error('[DIRECT ACCESS] Error handling direct domain access:', error);
throw error; // Rethrow so middleware can fall back to normal processing
}
}
// Status endpoint
app.get('/api/status', (req, res) => {
res.json({ status: 'online', version: '1.0.0' });
@@ -130,13 +274,16 @@ app.get('/api/status', (req, res) => {
app.get('/:domain', async (req, res, next) => {
const domain = req.params.domain;
console.log(`[DASHBOARD ROUTE] Processing /:domain request for: ${domain}`);
// Skip this handler for reserved paths
if (RESERVED_PATHS.includes(domain)) {
console.log(`[DASHBOARD ROUTE] Domain '${domain}' is in reserved paths, skipping`);
return next();
}
try {
console.log(`Processing request for domain root: ${domain}`);
console.log(`[DASHBOARD ROUTE] Processing request for domain root: ${domain}`);
// Resolve Handshake domain to get IPFS CID
const cid = await resolveHandshake(domain);
@@ -394,5 +541,8 @@ app.get('*', (req, res) => {
// Start server
app.listen(PORT, () => {
console.log(`\n==================================`);
console.log(`Fire Portal server running on port ${PORT}`);
console.log(`Dashboard host: ${DASHBOARD_HOST}`);
console.log(`==================================\n`);
});