Skip to content

Commit

Permalink
new(all);
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigorodriguez committed Oct 19, 2024
1 parent f135156 commit 2367fe2
Showing 1 changed file with 70 additions and 20 deletions.
90 changes: 70 additions & 20 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,37 +262,63 @@ export class GBServer {

GBServer.globals.webDavServer = await GBCoreService.createWebDavServer(minInstances);

require('dotenv').config(); // Load environment variables
const httpProxy = require('http-proxy');
const auth = require('basic-auth'); // Assuming you're using basic-auth for admin access
const GBSSR = require('./GBSSR'); // Assuming GBSSR is already required elsewhere

// Parse the ROUTE from the .env file
const routeConfig = process.env.ROUTE.split(';').reduce((acc, entry) => {
const [domain, port] = entry.split(':');
acc[domain] = port;
return acc;
}, {});

// Proxy setup
const proxy = httpProxy.createProxyServer({});

server.all('*', async (req, res, next) => {
const host = req.headers.host;

const host = req.headers.host.startsWith('www.') ? req.headers.host.substring(4) : req.headers.host;

// Check if the request is for logs
if (req.originalUrl.startsWith('/logs')) {
if (process.env.ENABLE_WEBLOG === 'true') {
const admins = {
admin: { password: process.env.ADMIN_PASS }
};

// ... some not authenticated middlewares.
// Authenticate the user
const user = auth(req);
if (!user || !admins[user.name] || admins[user.name].password !== user.pass) {
res.set('WWW-Authenticate', 'Basic realm="example"');
return res.status(401).send();
}
} else {
// If logging is not enabled, pass the request to GBSSR filter
await GBSSR.ssrFilter(req, res, next);
}
} else {
// Setups unsecure http redirect.
const proxy = httpProxy.createProxyServer({});

// If the domain is in routeConfig, proxy to the corresponding local service
if (routeConfig[host]) {
const target = `http://localhost:${routeConfig[host]}`;
console.log(`Routing to internal server: ${target}`);
return proxy.web(req, res, { target }, (err) => {
console.error('Proxy error:', err);
res.status(500).send('Internal proxy error.');
});
}

// If the host is the API host, redirect traffic to the API
if (host === process.env.API_HOST) {
GBLogEx.info(0, `Redirecting to API...`);
return proxy.web(req, res, { target: 'http://localhost:1111' }); // Express server
} else {
await GBSSR.ssrFilter(req, res, next);
console.log('Redirecting to API...');
return proxy.web(req, res, { target: 'http://localhost:1111' }); // Express API server
}

// For other cases, pass through the GBSSR filter
await GBSSR.ssrFilter(req, res, next);
}
});

GBLogEx.info(0, `The Bot Server is in RUNNING mode...`);

await minService.startSimpleTest(GBServer.globals.minBoot);
Expand All @@ -310,16 +336,40 @@ export class GBServer {
};

if (process.env.CERTIFICATE_PFX) {
const server1 = http.createServer(async (req, res) => {
const host = req.headers.host.startsWith('www.') ? req.headers.host.substring(4) : req.headers.host;

res
.writeHead(301, {
Location: 'https://' + host + req.url
})
.end();

// Parse the ROUTE from the .env file
const routeConfig = process.env.ROUTE.split(';').reduce((acc, entry) => {
const [domain, port] = entry.split(':');
acc[domain] = port;
return acc;
}, {});

// Create a proxy server for internal routing
const proxy = httpProxy.createProxyServer();

const server1 = http.createServer(async (req, res) => {
let host = req.headers.host.startsWith('www.') ? req.headers.host.substring(4) : req.headers.host;

// If the domain is in the routeConfig, handle internally
if (routeConfig[host]) {
const target = `http://localhost:${routeConfig[host]}`;
proxy.web(req, res, { target }, (err) => {
console.error('Internal routing error:', err);
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Internal routing error.');
});
} else {
// Otherwise, redirect to the HTTPS version
res.writeHead(301, {
Location: `https://${host}${req.url}`
}).end();
}
});

server1.listen(80, () => {
console.log('HTTP Server is listening on port 80');
});
server1.listen(80);

const options1 = {
passphrase: process.env.CERTIFICATE_PASSPHRASE,
Expand Down

0 comments on commit 2367fe2

Please sign in to comment.