server (1).js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. const express = require('express');
  2. const https = require('https');
  3. const http = require('http');
  4. const tls = require('tls');
  5. const fs = require('fs');
  6. const path = require('path');
  7. const url = require('url');
  8. const { execFile } = require('child_process');
  9. require('dotenv').config({ path: path.join(__dirname, '.env') });
  10. const app = express();
  11. // Parse ALLOWED_DOMAINS environment variable as an array
  12. const allowedDomains = process.env.ALLOWED_DOMAINS ? process.env.ALLOWED_DOMAINS.split(',') : [];
  13. // Middleware to parse JSON request bodies
  14. app.use(express.json());
  15. // Helper function to get SSL options based on hostname
  16. const getSSLOptions = (hostname) => {
  17. const sslPrefix = process.env.SSL_PREFIX || '/etc/letsencrypt/live/';
  18. try {
  19. return {
  20. key: fs.readFileSync(path.join(sslPrefix, hostname, 'privkey.pem')),
  21. cert: fs.readFileSync(path.join(sslPrefix, hostname, 'fullchain.pem')),
  22. };
  23. } catch (error) {
  24. // Fallback to default certificates if domain-specific ones aren't found
  25. console.warn(`Could not load SSL certificates for ${hostname}, using defaults`);
  26. return {
  27. key: fs.readFileSync(process.env.SSL_KEY_FILE),
  28. cert: fs.readFileSync(process.env.SSL_CRT_FILE),
  29. };
  30. }
  31. };
  32. // Redirect HTTP to HTTPS
  33. app.use((req, res, next) => {
  34. if (!req.secure && req.headers['x-forwarded-proto'] !== 'https') {
  35. return res.redirect(`https://${req.headers.host}${req.url}`);
  36. }
  37. next();
  38. });
  39. const forwardRequest = (req, res) => {
  40. const npxRoute = req.params[0];
  41. if (!npxRoute) {
  42. return res.status(400).json({ error: 'Bad Request: No dynamic route found' });
  43. }
  44. const parsedUrl = url.parse(req.url);
  45. const requestOptions = {
  46. hostname: 'localhost',
  47. port: 3000,
  48. path: `/api/v1/${npxRoute}${parsedUrl.search || ''}`, // Include query string
  49. method: req.method,
  50. headers: { ...req.headers },
  51. };
  52. let jsonPayload = null;
  53. if (['POST', 'PUT', 'PATCH', 'DELETE'].includes(req.method)) {
  54. jsonPayload = JSON.stringify(req.body);
  55. requestOptions.headers['Content-Type'] = 'application/json';
  56. requestOptions.headers['Content-Length'] = Buffer.byteLength(jsonPayload);
  57. }
  58. const proxyReq = http.request(requestOptions, (proxyRes) => {
  59. res.writeHead(proxyRes.statusCode, proxyRes.headers);
  60. proxyRes.pipe(res);
  61. });
  62. proxyReq.on('error', (error) => {
  63. console.error(`Error forwarding request: ${error.message}`);
  64. res.status(500).json({ error: 'Internal Server Error' });
  65. });
  66. if (jsonPayload) {
  67. proxyReq.write(jsonPayload);
  68. }
  69. proxyReq.end();
  70. };
  71. // Handle /api/* route forwarding
  72. app.use('/api/v1/*', (req, res) => {
  73. forwardRequest(req, res);
  74. });
  75. // Function for direct PHP execution
  76. const executePhpDirectly = (phpFilePath, domainPath, req, res) => {
  77. // Create environment variables that PHP might expect
  78. const env = {
  79. ...process.env,
  80. DOCUMENT_ROOT: domainPath,
  81. SCRIPT_FILENAME: phpFilePath,
  82. REQUEST_URI: req.url,
  83. QUERY_STRING: url.parse(req.url).query || '',
  84. REQUEST_METHOD: req.method,
  85. HTTP_HOST: req.headers.host,
  86. REMOTE_ADDR: req.ip,
  87. SERVER_NAME: req.headers.host.split(':')[0],
  88. // Additional variables that PHP might expect
  89. GATEWAY_INTERFACE: 'CGI/1.1',
  90. SERVER_PROTOCOL: 'HTTP/1.1',
  91. // Pass HTTP headers as environment variables
  92. ...Object.entries(req.headers).reduce((env, [key, value]) => {
  93. env[`HTTP_${key.toUpperCase().replace(/-/g, '_')}`] = value;
  94. return env;
  95. }, {})
  96. };
  97. execFile('php', [phpFilePath], { env }, (error, stdout, stderr) => {
  98. if (error) {
  99. console.error(`PHP execution error: ${error.message}`);
  100. return res.status(500).send(`PHP Execution Error: ${error.message}`);
  101. }
  102. if (stderr) {
  103. console.error(`PHP stderr output: ${stderr}`);
  104. }
  105. // Set Content-Type to HTML for proper rendering
  106. res.setHeader('Content-Type', 'text/html; charset=utf-8');
  107. res.send(stdout);
  108. });
  109. };
  110. // Helper function to check for index files
  111. const findIndexFile = (directoryPath) => {
  112. // Common index file types to check for
  113. const indexFiles = ['index.html', 'index.htm', 'index.php'];
  114. for (const indexFile of indexFiles) {
  115. const filePath = path.join(directoryPath, indexFile);
  116. if (fs.existsSync(filePath)) {
  117. return { path: filePath, type: indexFile.endsWith('.php') ? 'php' : 'html' };
  118. }
  119. }
  120. return null;
  121. };
  122. // Handle PHP for all domains (both xyz and non-xyz)
  123. app.use((req, res, next) => {
  124. const host = req.headers.host.split(':')[0]; // Remove port if present
  125. const domainPath = path.join('/var/www', host);
  126. // Make sure the domain directory exists
  127. if (!fs.existsSync(domainPath)) {
  128. return next(); // Skip to next middleware if domain directory doesn't exist
  129. }
  130. // Handle direct PHP file requests
  131. if (req.path.endsWith('.php')) {
  132. const phpFilePath = path.join(domainPath, req.path);
  133. if (fs.existsSync(phpFilePath)) {
  134. return executePhpDirectly(phpFilePath, domainPath, req, res);
  135. }
  136. }
  137. // Handle directory requests - check for index files
  138. if (req.path === '/' || req.path.endsWith('/')) {
  139. // First, check for index files in domain root
  140. const indexResult = findIndexFile(path.join(domainPath, req.path));
  141. if (indexResult) {
  142. if (indexResult.type === 'php') {
  143. return executePhpDirectly(indexResult.path, domainPath, req, res);
  144. } else {
  145. return res.sendFile(indexResult.path);
  146. }
  147. }
  148. }
  149. next();
  150. });
  151. // Serve static files from domain root first for all domains
  152. app.use((req, res, next) => {
  153. const host = req.headers.host.split(':')[0]; // Remove port if present
  154. const domainPath = path.join('/var/www', host);
  155. if (!fs.existsSync(domainPath)) {
  156. return next();
  157. }
  158. // Try to serve files from the domain's root directory
  159. express.static(domainPath)(req, res, (err) => {
  160. // Continue to next middleware if file not found
  161. next();
  162. });
  163. });
  164. // Serve static files from build directory as fallback for non-xyz domains
  165. app.use((req, res, next) => {
  166. const host = req.headers.host.split(':')[0]; // Remove port if present
  167. // Handle all domains (both xyz and non-xyz)
  168. const domainPath = path.join('/var/www', host);
  169. const buildPath = path.join(domainPath, 'build');
  170. if (fs.existsSync(buildPath)) {
  171. // Serve static files from build directory
  172. express.static(buildPath)(req, res, next);
  173. } else {
  174. next();
  175. }
  176. });
  177. // Catch-all route handler for all domains
  178. app.get('*', (req, res) => {
  179. const host = req.headers.host.split(':')[0]; // Remove port if present
  180. const domainPath = path.join('/var/www', host);
  181. // First, check for index.html in domain root
  182. const rootIndexPath = path.join(domainPath, 'index.html');
  183. if (fs.existsSync(rootIndexPath)) {
  184. return res.sendFile(rootIndexPath);
  185. }
  186. // Then, check build folder
  187. const buildPath = path.join(domainPath, 'build');
  188. if (fs.existsSync(buildPath)) {
  189. const buildIndexPath = path.join(buildPath, 'index.html');
  190. if (fs.existsSync(buildIndexPath)) {
  191. return res.sendFile(buildIndexPath);
  192. }
  193. }
  194. // No matching file found
  195. res.status(404).send('Not Found');
  196. });
  197. // Create HTTPS server with SNI support for multiple domains
  198. const server = https.createServer({
  199. SNICallback: (hostname, cb) => {
  200. const secureContext = tls.createSecureContext(getSSLOptions(hostname));
  201. if (cb) {
  202. cb(null, secureContext);
  203. } else {
  204. return secureContext;
  205. }
  206. }
  207. }, app);
  208. server.listen(443, '0.0.0.0', () => {
  209. console.log(`Server running on https://0.0.0.0:443`);
  210. });
  211. // Also create a HTTP server that redirects to HTTPS
  212. http.createServer((req, res) => {
  213. res.writeHead(301, { Location: `https://${req.headers.host}${req.url}` });
  214. res.end();
  215. }).listen(80, '0.0.0.0', () => {
  216. console.log('HTTP to HTTPS redirect server running on port 80');
  217. });