@lzwme/sserver 0.0.5 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/bin/sserver.js +0 -0
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +62 -54
- package/dist/config.d.ts +49 -27
- package/dist/config.js +61 -49
- package/dist/get-logger.d.ts +2 -2
- package/dist/get-logger.js +9 -9
- package/dist/index.d.ts +1 -1
- package/dist/index.js +5 -5
- package/dist/lib/get-cert.d.ts +6 -6
- package/dist/lib/get-cert.js +79 -79
- package/dist/lib/mkcert.d.ts +26 -26
- package/dist/lib/mkcert.js +86 -86
- package/dist/proxy/express-proxy.d.ts +2 -2
- package/dist/proxy/express-proxy.js +24 -24
- package/dist/proxy/php-proxy.d.ts +3 -0
- package/dist/proxy/php-proxy.js +188 -0
- package/dist/proxy/socket-proxy.d.ts +7 -7
- package/dist/proxy/socket-proxy.js +30 -30
- package/dist/static-server.d.ts +3 -3
- package/dist/static-server.js +97 -50
- package/package.json +12 -11
package/dist/lib/mkcert.js
CHANGED
|
@@ -1,86 +1,86 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// @see https://github.com/Subash/mkcert
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.createCert = exports.createCA = exports.generateCert = void 0;
|
|
5
|
-
const node_forge_1 = require("node-forge");
|
|
6
|
-
const util_1 = require("util");
|
|
7
|
-
const generateKeyPair = (0, util_1.promisify)(node_forge_1.pki.rsa.generateKeyPair.bind(node_forge_1.pki.rsa));
|
|
8
|
-
async function generateCert(options) {
|
|
9
|
-
let { subject, issuer, extensions = [], validityDays = 36500, signWith } = options;
|
|
10
|
-
// create serial from and integer between 50000 and 99999
|
|
11
|
-
const serial = Math.floor(Math.random() * 95000 + 50000).toString();
|
|
12
|
-
const keyPair = await generateKeyPair({ bits: 2048, workers: 4 });
|
|
13
|
-
const cert = node_forge_1.pki.createCertificate();
|
|
14
|
-
cert.publicKey = keyPair.publicKey;
|
|
15
|
-
cert.serialNumber = Buffer.from(serial).toString('hex'); // serial number must be hex encoded
|
|
16
|
-
cert.validity.notBefore = new Date();
|
|
17
|
-
cert.validity.notAfter = new Date();
|
|
18
|
-
cert.validity.notAfter.setDate(cert.validity.notAfter.getDate() + validityDays);
|
|
19
|
-
// certificate Attributes: https://git.io/fptna
|
|
20
|
-
cert.setSubject(subject);
|
|
21
|
-
cert.setIssuer(issuer);
|
|
22
|
-
cert.setExtensions(extensions);
|
|
23
|
-
// sign the certificate with it's own private key if no separate signing key is provided
|
|
24
|
-
const signKey = signWith ? node_forge_1.pki.privateKeyFromPem(signWith) : keyPair.privateKey;
|
|
25
|
-
cert.sign(signKey, node_forge_1.md.sha256.create());
|
|
26
|
-
return {
|
|
27
|
-
key: node_forge_1.pki.privateKeyToPem(keyPair.privateKey),
|
|
28
|
-
cert: node_forge_1.pki.certificateToPem(cert),
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
exports.generateCert = generateCert;
|
|
32
|
-
function createCA(options) {
|
|
33
|
-
if (!options.extensions)
|
|
34
|
-
options.extensions = [];
|
|
35
|
-
// required certificate extensions for a certificate authority
|
|
36
|
-
options.extensions = [
|
|
37
|
-
{ name: 'basicConstraints', cA: true, critical: true },
|
|
38
|
-
{ name: 'keyUsage', keyCertSign: true, critical: true },
|
|
39
|
-
...options.extensions,
|
|
40
|
-
];
|
|
41
|
-
return generateCert(options);
|
|
42
|
-
}
|
|
43
|
-
exports.createCA = createCA;
|
|
44
|
-
function createCert(options) {
|
|
45
|
-
// certificate Attributes: https://git.io/fptna
|
|
46
|
-
if (!options.subject)
|
|
47
|
-
options.subject = [];
|
|
48
|
-
if (!options.subject.find(d => d.name === 'commonName')) {
|
|
49
|
-
// use the first address as common name
|
|
50
|
-
options.subject.push({ name: 'commonName', value: options.domains[0] });
|
|
51
|
-
}
|
|
52
|
-
// required certificate extensions for a tls certificate
|
|
53
|
-
const extensions = [
|
|
54
|
-
{ name: 'basicConstraints', cA: false, critical: true },
|
|
55
|
-
{ name: 'keyUsage', digitalSignature: true, keyEncipherment: true, critical: true },
|
|
56
|
-
{ name: 'extKeyUsage', serverAuth: true, clientAuth: true },
|
|
57
|
-
{
|
|
58
|
-
name: 'subjectAltName',
|
|
59
|
-
altNames: options.domains.map(domain => {
|
|
60
|
-
const types = { domain: 2, ip: 7 }; // available Types: https://git.io/fptng
|
|
61
|
-
const isIp = isIP(domain);
|
|
62
|
-
if (isIp)
|
|
63
|
-
return { type: types.ip, ip: domain };
|
|
64
|
-
return { type: types.domain, value: domain };
|
|
65
|
-
}),
|
|
66
|
-
},
|
|
67
|
-
...(options.extensions || []),
|
|
68
|
-
];
|
|
69
|
-
const ca = node_forge_1.pki.certificateFromPem(options.caCert);
|
|
70
|
-
return generateCert({
|
|
71
|
-
...options,
|
|
72
|
-
subject: options.subject,
|
|
73
|
-
issuer: ca.subject.attributes,
|
|
74
|
-
extensions,
|
|
75
|
-
signWith: options.caKey,
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
exports.createCert = createCert;
|
|
79
|
-
function isIP(ip) {
|
|
80
|
-
if (!ip)
|
|
81
|
-
return false;
|
|
82
|
-
if (/::/.test(ip))
|
|
83
|
-
return true; // ipv6
|
|
84
|
-
const v4 = /^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/;
|
|
85
|
-
return v4.test(ip);
|
|
86
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
// @see https://github.com/Subash/mkcert
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.createCert = exports.createCA = exports.generateCert = void 0;
|
|
5
|
+
const node_forge_1 = require("node-forge");
|
|
6
|
+
const util_1 = require("util");
|
|
7
|
+
const generateKeyPair = (0, util_1.promisify)(node_forge_1.pki.rsa.generateKeyPair.bind(node_forge_1.pki.rsa));
|
|
8
|
+
async function generateCert(options) {
|
|
9
|
+
let { subject, issuer, extensions = [], validityDays = 36500, signWith } = options;
|
|
10
|
+
// create serial from and integer between 50000 and 99999
|
|
11
|
+
const serial = Math.floor(Math.random() * 95000 + 50000).toString();
|
|
12
|
+
const keyPair = await generateKeyPair({ bits: 2048, workers: 4 });
|
|
13
|
+
const cert = node_forge_1.pki.createCertificate();
|
|
14
|
+
cert.publicKey = keyPair.publicKey;
|
|
15
|
+
cert.serialNumber = Buffer.from(serial).toString('hex'); // serial number must be hex encoded
|
|
16
|
+
cert.validity.notBefore = new Date();
|
|
17
|
+
cert.validity.notAfter = new Date();
|
|
18
|
+
cert.validity.notAfter.setDate(cert.validity.notAfter.getDate() + validityDays);
|
|
19
|
+
// certificate Attributes: https://git.io/fptna
|
|
20
|
+
cert.setSubject(subject);
|
|
21
|
+
cert.setIssuer(issuer);
|
|
22
|
+
cert.setExtensions(extensions);
|
|
23
|
+
// sign the certificate with it's own private key if no separate signing key is provided
|
|
24
|
+
const signKey = signWith ? node_forge_1.pki.privateKeyFromPem(signWith) : keyPair.privateKey;
|
|
25
|
+
cert.sign(signKey, node_forge_1.md.sha256.create());
|
|
26
|
+
return {
|
|
27
|
+
key: node_forge_1.pki.privateKeyToPem(keyPair.privateKey),
|
|
28
|
+
cert: node_forge_1.pki.certificateToPem(cert),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
exports.generateCert = generateCert;
|
|
32
|
+
function createCA(options) {
|
|
33
|
+
if (!options.extensions)
|
|
34
|
+
options.extensions = [];
|
|
35
|
+
// required certificate extensions for a certificate authority
|
|
36
|
+
options.extensions = [
|
|
37
|
+
{ name: 'basicConstraints', cA: true, critical: true },
|
|
38
|
+
{ name: 'keyUsage', keyCertSign: true, critical: true },
|
|
39
|
+
...options.extensions,
|
|
40
|
+
];
|
|
41
|
+
return generateCert(options);
|
|
42
|
+
}
|
|
43
|
+
exports.createCA = createCA;
|
|
44
|
+
function createCert(options) {
|
|
45
|
+
// certificate Attributes: https://git.io/fptna
|
|
46
|
+
if (!options.subject)
|
|
47
|
+
options.subject = [];
|
|
48
|
+
if (!options.subject.find(d => d.name === 'commonName')) {
|
|
49
|
+
// use the first address as common name
|
|
50
|
+
options.subject.push({ name: 'commonName', value: options.domains[0] });
|
|
51
|
+
}
|
|
52
|
+
// required certificate extensions for a tls certificate
|
|
53
|
+
const extensions = [
|
|
54
|
+
{ name: 'basicConstraints', cA: false, critical: true },
|
|
55
|
+
{ name: 'keyUsage', digitalSignature: true, keyEncipherment: true, critical: true },
|
|
56
|
+
{ name: 'extKeyUsage', serverAuth: true, clientAuth: true },
|
|
57
|
+
{
|
|
58
|
+
name: 'subjectAltName',
|
|
59
|
+
altNames: options.domains.map(domain => {
|
|
60
|
+
const types = { domain: 2, ip: 7 }; // available Types: https://git.io/fptng
|
|
61
|
+
const isIp = isIP(domain);
|
|
62
|
+
if (isIp)
|
|
63
|
+
return { type: types.ip, ip: domain };
|
|
64
|
+
return { type: types.domain, value: domain };
|
|
65
|
+
}),
|
|
66
|
+
},
|
|
67
|
+
...(options.extensions || []),
|
|
68
|
+
];
|
|
69
|
+
const ca = node_forge_1.pki.certificateFromPem(options.caCert);
|
|
70
|
+
return generateCert({
|
|
71
|
+
...options,
|
|
72
|
+
subject: options.subject,
|
|
73
|
+
issuer: ca.subject.attributes,
|
|
74
|
+
extensions,
|
|
75
|
+
signWith: options.caKey,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
exports.createCert = createCert;
|
|
79
|
+
function isIP(ip) {
|
|
80
|
+
if (!ip)
|
|
81
|
+
return false;
|
|
82
|
+
if (/::/.test(ip))
|
|
83
|
+
return true; // ipv6
|
|
84
|
+
const v4 = /^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/;
|
|
85
|
+
return v4.test(ip);
|
|
86
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { type Express } from 'express';
|
|
2
|
-
export declare function proxyByExpress(app: Express): Promise<void>;
|
|
1
|
+
import { type Express } from 'express';
|
|
2
|
+
export declare function proxyByExpress(app: Express): Promise<void>;
|
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.proxyByExpress = void 0;
|
|
4
|
-
const console_log_colors_1 = require("console-log-colors");
|
|
5
|
-
const http_proxy_middleware_1 = require("http-proxy-middleware");
|
|
6
|
-
const config_1 = require("../config");
|
|
7
|
-
const get_logger_1 = require("../get-logger");
|
|
8
|
-
async function proxyByExpress(app) {
|
|
9
|
-
const { proxyConfig } = await (0, config_1.getConfig)();
|
|
10
|
-
if (!proxyConfig?.length)
|
|
11
|
-
return;
|
|
12
|
-
proxyConfig.forEach(({ api, config }) => {
|
|
13
|
-
const onProxyReq = config.onProxyReq;
|
|
14
|
-
config.onProxyReq = (pReq, req, res, options) => {
|
|
15
|
-
const now = new Date();
|
|
16
|
-
get_logger_1.logger.log(now.toTimeString().slice(0, 8) + '.' + String(now.getMilliseconds()).padStart(3, '0'), console_log_colors_1.color.gray('[proxyReq]'), console_log_colors_1.color.cyan(req.originalUrl), '=>', console_log_colors_1.color.cyanBright(`${pReq.protocol}${pReq.host}${pReq.path}`));
|
|
17
|
-
if (onProxyReq)
|
|
18
|
-
onProxyReq(pReq, req, res, options);
|
|
19
|
-
};
|
|
20
|
-
app.use(api, (0, http_proxy_middleware_1.createProxyMiddleware)(config));
|
|
21
|
-
get_logger_1.logger.log(`Init proxy for ${console_log_colors_1.color.cyanBright(api)}`);
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
exports.proxyByExpress = proxyByExpress;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.proxyByExpress = void 0;
|
|
4
|
+
const console_log_colors_1 = require("console-log-colors");
|
|
5
|
+
const http_proxy_middleware_1 = require("http-proxy-middleware");
|
|
6
|
+
const config_1 = require("../config");
|
|
7
|
+
const get_logger_1 = require("../get-logger");
|
|
8
|
+
async function proxyByExpress(app) {
|
|
9
|
+
const { proxyConfig } = await (0, config_1.getConfig)();
|
|
10
|
+
if (!proxyConfig?.length)
|
|
11
|
+
return;
|
|
12
|
+
proxyConfig.forEach(({ api, config }) => {
|
|
13
|
+
const onProxyReq = config.onProxyReq;
|
|
14
|
+
config.onProxyReq = (pReq, req, res, options) => {
|
|
15
|
+
const now = new Date();
|
|
16
|
+
get_logger_1.logger.log(now.toTimeString().slice(0, 8) + '.' + String(now.getMilliseconds()).padStart(3, '0'), console_log_colors_1.color.gray('[proxyReq]'), console_log_colors_1.color.cyan(req.originalUrl), '=>', console_log_colors_1.color.cyanBright(`${pReq.protocol}${pReq.host}${pReq.path}`));
|
|
17
|
+
if (onProxyReq)
|
|
18
|
+
onProxyReq(pReq, req, res, options);
|
|
19
|
+
};
|
|
20
|
+
app.use(api, (0, http_proxy_middleware_1.createProxyMiddleware)(config));
|
|
21
|
+
get_logger_1.logger.log(`Init proxy for ${console_log_colors_1.color.cyanBright(api)}`);
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
exports.proxyByExpress = proxyByExpress;
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// @see https://www.npmjs.com/package/phpcgijs
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.phpProxy = void 0;
|
|
5
|
+
const node_child_process_1 = require("node:child_process");
|
|
6
|
+
const node_url_1 = require("node:url");
|
|
7
|
+
const node_fs_1 = require("node:fs");
|
|
8
|
+
const node_path_1 = require("node:path");
|
|
9
|
+
const fe_utils_1 = require("@lzwme/fe-utils");
|
|
10
|
+
const get_logger_1 = require("../get-logger");
|
|
11
|
+
async function phpProxy(app, config) {
|
|
12
|
+
return app.use('/', createPhpMiddleware(config.rootDir, config.phpConfig));
|
|
13
|
+
}
|
|
14
|
+
exports.phpProxy = phpProxy;
|
|
15
|
+
let PHP_CGI;
|
|
16
|
+
function getPhpCgi() {
|
|
17
|
+
if (PHP_CGI == null) {
|
|
18
|
+
const which = process.platform === 'win32' ? 'where' : 'which';
|
|
19
|
+
PHP_CGI = (0, fe_utils_1.execSync)(`${which} php-cgi`).stdout.trim() || (0, fe_utils_1.execSync)(`${which} php-cgi`).stdout.trim();
|
|
20
|
+
}
|
|
21
|
+
return PHP_CGI;
|
|
22
|
+
}
|
|
23
|
+
async function findPhpFile(pathname, rootDir) {
|
|
24
|
+
let filepath = (0, node_path_1.join)(rootDir, pathname);
|
|
25
|
+
if (!filepath.includes(rootDir) || !(0, node_fs_1.existsSync)(filepath))
|
|
26
|
+
return false;
|
|
27
|
+
let stat = await node_fs_1.promises.stat(filepath);
|
|
28
|
+
if (stat.isDirectory()) {
|
|
29
|
+
filepath = (0, node_path_1.join)(filepath, 'index.php');
|
|
30
|
+
if (!(0, node_fs_1.existsSync)(filepath))
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
return filepath;
|
|
34
|
+
}
|
|
35
|
+
function formatArgs(args) {
|
|
36
|
+
return Object.entries(args).map(([key, value]) => `${key.startsWith('-') ? key : `-${key}`} ${value}`);
|
|
37
|
+
}
|
|
38
|
+
function runPhpCgi(req, response, url, filepath, phpConfig, rootDir) {
|
|
39
|
+
const pathname = url.pathname || req.url;
|
|
40
|
+
const pathinfo = pathname.includes('.php') ? pathname.split('.php')[0] + '.php' : pathname;
|
|
41
|
+
if (!phpConfig)
|
|
42
|
+
return;
|
|
43
|
+
const env = {
|
|
44
|
+
SERVER_SIGNATURE: 'NodeJS server at localhost',
|
|
45
|
+
// The extra path information, as given in the requested URL. In fact, scripts can be accessed by their virtual path, followed by extra information at the end of this path. The extra information is sent in PATH_INFO.
|
|
46
|
+
PATH_INFO: pathinfo,
|
|
47
|
+
// The virtual-to-real mapped version of PATH_INFO.
|
|
48
|
+
PATH_TRANSLATED: '',
|
|
49
|
+
// The virtual path of the script being executed.
|
|
50
|
+
SCRIPT_NAME: pathname,
|
|
51
|
+
SCRIPT_FILENAME: filepath,
|
|
52
|
+
// The real path of the script being executed.
|
|
53
|
+
REQUEST_FILENAME: filepath,
|
|
54
|
+
// The full URL to the current object requested by the client.
|
|
55
|
+
SCRIPT_URI: req.url,
|
|
56
|
+
// The full URI of the current request. It is made of the concatenation of SCRIPT_NAME and PATH_INFO (if available.)
|
|
57
|
+
URL: req.url,
|
|
58
|
+
SCRIPT_URL: req.url,
|
|
59
|
+
// The original request URI sent by the client.
|
|
60
|
+
REQUEST_URI: req.url,
|
|
61
|
+
// The method used by the current request; usually set to GET or POST.
|
|
62
|
+
REQUEST_METHOD: req.method,
|
|
63
|
+
// The information which follows the ? character in the requested URL.
|
|
64
|
+
QUERY_STRING: url.query || '',
|
|
65
|
+
// 'multipart/form-data', //'application/x-www-form-urlencoded', //The MIME type of the request body; set only for POST or PUT requests.
|
|
66
|
+
CONTENT_TYPE: req.get('Content-Type') || '',
|
|
67
|
+
// The length in bytes of the request body; set only for POST or PUT requests.
|
|
68
|
+
CONTENT_LENGTH: req.get('Content-Length') || '0',
|
|
69
|
+
// The authentication type if the client has authenticated itself to access the script.
|
|
70
|
+
AUTH_TYPE: '',
|
|
71
|
+
AUTH_USER: '',
|
|
72
|
+
// The name of the user as issued by the client when authenticating itself to access the script.
|
|
73
|
+
REMOTE_USER: '',
|
|
74
|
+
// All HTTP headers sent by the client. Headers are separated by carriage return characters (ASCII 13 - \n) and each header name is prefixed by HTTP_, transformed to upper cases, and - characters it contains are replaced by _ characters.
|
|
75
|
+
ALL_HTTP: Object.entries(req.headers)
|
|
76
|
+
.map(([key, value]) => `HTTP__${key.toUpperCase().replace('-', '_')}: ${value}`)
|
|
77
|
+
.reduce((a, b) => a + b + '\n', ''),
|
|
78
|
+
// All HTTP headers as sent by the client in raw form. No transformation on the header names is applied.
|
|
79
|
+
ALL_RAW: Object.entries(req.headers)
|
|
80
|
+
.map(([key, value]) => `${key}: value`)
|
|
81
|
+
.reduce((a, b) => a + b + '\n', ''),
|
|
82
|
+
// The web server's software identity.
|
|
83
|
+
SERVER_SOFTWARE: 'NodeJS SServer',
|
|
84
|
+
// The host name or the IP address of the computer running the web server as given in the requested URL.
|
|
85
|
+
SERVER_NAME: 'localhost',
|
|
86
|
+
// The IP address of the computer running the web server.
|
|
87
|
+
SERVER_ADDR: '127.0.0.1',
|
|
88
|
+
// The port to which the request was sent.
|
|
89
|
+
SERVER_PORT: String(phpConfig.port || '9010'),
|
|
90
|
+
// The CGI Specification version supported by the web server; always set to CGI/1.1.
|
|
91
|
+
GATEWAY_INTERFACE: 'CGI/1.1',
|
|
92
|
+
// The HTTP protocol version used by the current request.
|
|
93
|
+
SERVER_PROTOCOL: '',
|
|
94
|
+
// The IP address of the computer that sent the request.
|
|
95
|
+
REMOTE_ADDR: req.ip || '',
|
|
96
|
+
// The port from which the request was sent.
|
|
97
|
+
REMOTE_PORT: '',
|
|
98
|
+
// The absolute path of the web site files. It has the same value as Documents Path.
|
|
99
|
+
DOCUMENT_ROOT: rootDir,
|
|
100
|
+
// The numerical identifier of the host which served the request. On Abyss Web Server X1, it is always set to 1 since there is only a single host.
|
|
101
|
+
INSTANCE_ID: '',
|
|
102
|
+
// The virtual path of the deepest alias which contains the request URI. If no alias contains the request URI, the variable is set to /.
|
|
103
|
+
APPL_MD_PATH: '',
|
|
104
|
+
// The real path of the deepest alias which contains the request URI. If no alias contains the request URI, the variable is set to the same value as DOCUMENT_ROOT.
|
|
105
|
+
APPL_PHYSICAL_PATH: '',
|
|
106
|
+
// It is set to true if the current request is a subrequest, i.e. a request not directly invoked by a client. Otherwise, it is set to true. Subrequests are generated by the server for internal processing. XSSI includes for example result in subrequests.
|
|
107
|
+
IS_SUBREQ: '',
|
|
108
|
+
REDIRECT_STATUS: '1',
|
|
109
|
+
};
|
|
110
|
+
Object.entries(req.headers).forEach(function ([key, value]) {
|
|
111
|
+
env['HTTP_' + key.toUpperCase().replace('-', '_')] = value;
|
|
112
|
+
});
|
|
113
|
+
if (filepath.endsWith('.php')) {
|
|
114
|
+
let childProc;
|
|
115
|
+
const cgiArgs = formatArgs(phpConfig.args);
|
|
116
|
+
let cgipath = phpConfig.cgiPath || getPhpCgi();
|
|
117
|
+
let res = '';
|
|
118
|
+
let err = '';
|
|
119
|
+
if (!cgipath || !(0, node_fs_1.existsSync)(cgipath))
|
|
120
|
+
throw new Error('"php-cgi" cannot be found');
|
|
121
|
+
childProc = (0, node_child_process_1.spawn)(cgipath, cgiArgs, { env });
|
|
122
|
+
// php.stdin.resume();
|
|
123
|
+
// logger.debug(req.rawBody);
|
|
124
|
+
// (new Stream(req.rawBody)).pipe(php.stdin);
|
|
125
|
+
// php.stdin.write('\n');
|
|
126
|
+
// php.stdin.end();
|
|
127
|
+
// pipe request stream directly into the php process
|
|
128
|
+
req.pipe(childProc.stdin);
|
|
129
|
+
childProc.stdin.on('error', () => {
|
|
130
|
+
get_logger_1.logger.error('[php]Error from server');
|
|
131
|
+
});
|
|
132
|
+
childProc.stdout.on('data', (data) => {
|
|
133
|
+
res += data.toString();
|
|
134
|
+
});
|
|
135
|
+
childProc.stderr.on('data', (data) => {
|
|
136
|
+
err += data.toString();
|
|
137
|
+
});
|
|
138
|
+
childProc.on('error', (err) => {
|
|
139
|
+
get_logger_1.logger.error('error', err);
|
|
140
|
+
});
|
|
141
|
+
childProc.on('exit', () => {
|
|
142
|
+
childProc.stdin.end();
|
|
143
|
+
const lines = res ? res.split('\r\n') : [];
|
|
144
|
+
let line = 0;
|
|
145
|
+
let body = '';
|
|
146
|
+
if (lines.length) {
|
|
147
|
+
do {
|
|
148
|
+
const m = lines[line].split(': ');
|
|
149
|
+
if (m[0] === '')
|
|
150
|
+
break;
|
|
151
|
+
if (m[0] == 'Status') {
|
|
152
|
+
response.statusCode = parseInt(m[1]);
|
|
153
|
+
}
|
|
154
|
+
if (m.length == 2) {
|
|
155
|
+
response.setHeader(m[0], m[1]);
|
|
156
|
+
}
|
|
157
|
+
line++;
|
|
158
|
+
} while (lines[line] !== '');
|
|
159
|
+
body = lines.splice(line + 1).join('\n');
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
body = res || err;
|
|
163
|
+
}
|
|
164
|
+
get_logger_1.logger.debug('[php]STATUS: ', response.statusCode);
|
|
165
|
+
response.status(response.statusCode).end(body);
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
response.sendFile(filepath);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
function createPhpMiddleware(rootDir, phpConfig = {}) {
|
|
173
|
+
return async function phpMiddleware(req, res, next) {
|
|
174
|
+
// stop stream until child-process is opened
|
|
175
|
+
req.pause();
|
|
176
|
+
const url = (0, node_url_1.parse)(req.url);
|
|
177
|
+
const filepath = await findPhpFile(url.pathname || req.path, rootDir);
|
|
178
|
+
if (filepath && (filepath.includes('.php') || phpConfig.onlyPhpFile === false)) {
|
|
179
|
+
get_logger_1.logger.info('[php][exec]', fe_utils_1.color.green(req.path), '=>', fe_utils_1.color.yellow(filepath));
|
|
180
|
+
runPhpCgi(req, res, url, filepath, phpConfig, rootDir);
|
|
181
|
+
req.resume();
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
req.resume();
|
|
185
|
+
next();
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import net from 'node:net';
|
|
3
|
-
export declare function socketProxy(config?: {
|
|
4
|
-
localPort: number;
|
|
5
|
-
remotePort: number;
|
|
6
|
-
remoteHost: string;
|
|
7
|
-
}): net.Server;
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import net from 'node:net';
|
|
3
|
+
export declare function socketProxy(config?: {
|
|
4
|
+
localPort: number;
|
|
5
|
+
remotePort: number;
|
|
6
|
+
remoteHost: string;
|
|
7
|
+
}): net.Server;
|
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.socketProxy = void 0;
|
|
7
|
-
const node_net_1 = __importDefault(require("node:net"));
|
|
8
|
-
const console_log_colors_1 = require("console-log-colors");
|
|
9
|
-
function socketProxy(config = {
|
|
10
|
-
// 外网访问的端口
|
|
11
|
-
localPort: 3717,
|
|
12
|
-
// 代理转发的端口
|
|
13
|
-
remotePort: 27017,
|
|
14
|
-
//代理转发的 IP 地址
|
|
15
|
-
remoteHost: '127.0.0.1',
|
|
16
|
-
}) {
|
|
17
|
-
const server = node_net_1.default
|
|
18
|
-
.createServer(socket => {
|
|
19
|
-
console_log_colors_1.log.green('proxy for address', socket.address());
|
|
20
|
-
let to = node_net_1.default.createConnection({
|
|
21
|
-
host: config.remoteHost,
|
|
22
|
-
port: config.remotePort,
|
|
23
|
-
});
|
|
24
|
-
socket.pipe(to);
|
|
25
|
-
to.pipe(socket);
|
|
26
|
-
})
|
|
27
|
-
.listen(config.localPort);
|
|
28
|
-
return server;
|
|
29
|
-
}
|
|
30
|
-
exports.socketProxy = socketProxy;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.socketProxy = void 0;
|
|
7
|
+
const node_net_1 = __importDefault(require("node:net"));
|
|
8
|
+
const console_log_colors_1 = require("console-log-colors");
|
|
9
|
+
function socketProxy(config = {
|
|
10
|
+
// 外网访问的端口
|
|
11
|
+
localPort: 3717,
|
|
12
|
+
// 代理转发的端口
|
|
13
|
+
remotePort: 27017,
|
|
14
|
+
//代理转发的 IP 地址
|
|
15
|
+
remoteHost: '127.0.0.1',
|
|
16
|
+
}) {
|
|
17
|
+
const server = node_net_1.default
|
|
18
|
+
.createServer(socket => {
|
|
19
|
+
console_log_colors_1.log.green('proxy for address', socket.address());
|
|
20
|
+
let to = node_net_1.default.createConnection({
|
|
21
|
+
host: config.remoteHost,
|
|
22
|
+
port: config.remotePort,
|
|
23
|
+
});
|
|
24
|
+
socket.pipe(to);
|
|
25
|
+
to.pipe(socket);
|
|
26
|
+
})
|
|
27
|
+
.listen(config.localPort);
|
|
28
|
+
return server;
|
|
29
|
+
}
|
|
30
|
+
exports.socketProxy = socketProxy;
|
package/dist/static-server.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { type Express } from 'express';
|
|
2
|
-
import { SSConfig } from './config';
|
|
3
|
-
export declare function initServer(
|
|
1
|
+
import { type Express } from 'express';
|
|
2
|
+
import { SSConfig } from './config';
|
|
3
|
+
export declare function initServer(c?: SSConfig): Promise<Express>;
|