Commit c5782ca4 authored by rockyl's avatar rockyl

mock/serve 支持ssl

parent 436dab90
...@@ -74,3 +74,7 @@ typings/ ...@@ -74,3 +74,7 @@ typings/
# Serverless directories # Serverless directories
.serverless .serverless
/ssl/*.crt
/ssl/*.pem
/ssl/*.key
/ssl/*.cer
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
const program = require('commander'); const program = require('commander');
program program
.version('0.0.1') .version('0.0.2')
.description('game command line interface') .description('game command line interface')
.command('init [name]', 'Initialize a project with template').alias('i') .command('init [name]', 'Initialize a project with template').alias('i')
.command('mock', 'Mock server').alias('m') .command('mock', 'Mock server').alias('m')
......
...@@ -6,6 +6,8 @@ program ...@@ -6,6 +6,8 @@ program
.option('-h, --host [string]', 'server host', 'localhost') .option('-h, --host [string]', 'server host', 'localhost')
.option('-p, --port [number]', 'server port', 3000) .option('-p, --port [number]', 'server port', 3000)
.option('-f, --folder [string]', 'folder of json files', './') .option('-f, --folder [string]', 'folder of json files', './')
.option('-k, --key-file [string]', 'ssl key file')
.option('-c, --cert-file [string]', 'ssl cert file')
.parse(process.argv); .parse(process.argv);
async function execute() { async function execute() {
...@@ -15,10 +17,13 @@ async function execute() { ...@@ -15,10 +17,13 @@ async function execute() {
host: program.host, host: program.host,
port: program.port, port: program.port,
folder: program.folder, folder: program.folder,
keyFile: program.keyFile,
certFile: program.certFile,
}).then( }).then(
({host, port, jsonPath})=>{ ({host, port, jsonPath, isSSL})=>{
console.log(`Json server start at http://${host}:${port}`); const schema = isSSL ? 'https' : 'http';
console.log('Json path: ', jsonPath); console.log(`${schema} server start at ${schema}://${host}:${port}`);
console.log(`${schema} path: ${jsonPath}`);
}, },
(e)=>{ (e)=>{
console.log(e); console.log(e);
......
...@@ -6,6 +6,8 @@ program ...@@ -6,6 +6,8 @@ program
.option('-h, --host [string]', 'server host', 'localhost') .option('-h, --host [string]', 'server host', 'localhost')
.option('-p, --port [number]', 'server port', 3001) .option('-p, --port [number]', 'server port', 3001)
.option('-f, --folder [string]', 'folder of static files', './') .option('-f, --folder [string]', 'folder of static files', './')
.option('-k, --key-file [string]', 'ssl key file')
.option('-c, --cert-file [string]', 'ssl cert file')
.parse(process.argv); .parse(process.argv);
async function execute() { async function execute() {
...@@ -15,10 +17,13 @@ async function execute() { ...@@ -15,10 +17,13 @@ async function execute() {
host: program.host, host: program.host,
port: program.port, port: program.port,
folder: program.folder, folder: program.folder,
keyFile: program.keyFile,
certFile: program.certFile,
}).then( }).then(
({host, port, publicPath})=>{ ({host, port, publicPath, isSSL})=>{
console.log(`Http server start at http://${host}:${port}`); const schema = isSSL ? 'https' : 'http';
console.log('Http path: ', publicPath); console.log(`${schema} server start at ${schema}://${host}:${port}`);
console.log(`${schema} path: ${publicPath}`);
}, },
(e)=>{ (e)=>{
console.log(e); console.log(e);
......
...@@ -3,22 +3,41 @@ ...@@ -3,22 +3,41 @@
* *
* http serve * http serve
*/ */
const path = require('path'); const path = require('path');
const fs = require('fs'); const fs = require('fs');
const handler = require('serve-handler'); const serveHandler = require('serve-handler');
const http = require('http'); const http = require('http');
const https = require('https');
let publicPath;
function handler(request, response){
return serveHandler(request, response, {
public: publicPath,
});
}
exports.start = function (options) { exports.start = function (options) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const {port, host, folder} = options; const {port, host, folder, keyFile, certFile} = options;
const publicPath = path.resolve(folder); publicPath = path.resolve(folder);
if (fs.existsSync(publicPath)) { if (fs.existsSync(publicPath)) {
const server = http.createServer((request, response) => { let sslOpts;
return handler(request, response, { if(keyFile && certFile){
public: publicPath, const keyContent = fs.readFileSync(keyFile, 'utf8'),
}); certContent = fs.readFileSync(certFile, 'utf8');
});
if(keyContent && certContent){
sslOpts = {
key: keyContent,
cert: certContent
};
}
}
const server = sslOpts ? https.createServer(sslOpts, handler) : http.createServer(handler);
server.on('error', (err) => { server.on('error', (err) => {
reject(err.message); reject(err.message);
...@@ -26,7 +45,7 @@ exports.start = function (options) { ...@@ -26,7 +45,7 @@ exports.start = function (options) {
server.listen(port, host, function () { server.listen(port, host, function () {
resolve({ resolve({
host, port, publicPath, host, port, publicPath, isSSL: !!sslOpts
}); });
}); });
} else { } else {
......
...@@ -6,10 +6,12 @@ ...@@ -6,10 +6,12 @@
const path = require('path'); const path = require('path');
const fs = require('fs'); const fs = require('fs');
const http = require('http');
const https = require('https');
let jsonPath; let jsonPath;
function onRequest(request, response) { function handler(request, response) {
const {url} = request; const {url} = request;
if (url === '/favicon.ico') { if (url === '/favicon.ico') {
...@@ -54,21 +56,33 @@ function onRequest(request, response) { ...@@ -54,21 +56,33 @@ function onRequest(request, response) {
response.end(); response.end();
} }
const http = require('http');
const server = http.createServer(onRequest);
exports.start = function (options) { exports.start = function (options) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const {port = 3000, host = 'localhost', folder = './mock'} = options; const {port, host, folder, keyFile, certFile} = options;
jsonPath = path.resolve(folder); jsonPath = path.resolve(folder);
if (fs.existsSync(jsonPath)) { if (fs.existsSync(jsonPath)) {
let sslOpts;
if(keyFile && certFile){
const keyContent = fs.readFileSync(keyFile, 'utf8'),
certContent = fs.readFileSync(certFile, 'utf8');
if(keyContent && certContent){
sslOpts = {
key: keyContent,
cert: certContent
};
}
}
const server = sslOpts ? https.createServer(sslOpts, handler) : http.createServer(handler);
server.on('error', (err) => { server.on('error', (err) => {
reject(err.message); reject(err.message);
}); });
server.listen(port, host, function () { server.listen(port, host, function () {
resolve({ resolve({
host, port, jsonPath, host, port, jsonPath, isSSL: !!sslOpts
}); });
}); });
} else { } else {
......
[ req ]
default_bits = 2048
default_keyfile = server-key.pem
distinguished_name = subject
req_extensions = req_ext
x509_extensions = x509_ext
string_mask = utf8only
[ subject ]
countryName = Country Name (2 letter code)
countryName_default = US
stateOrProvinceName = State or Province Name (full name)
stateOrProvinceName_default = NY
localityName = Locality Name (eg, city)
localityName_default = New York
organizationName = Organization Name (eg, company)
organizationName_default = localhost, LLC
commonName = Common Name (e.g. server FQDN or YOUR name)
commonName_default = localhost
emailAddress = Email Address
emailAddress_default = test@example.com
[ x509_ext ]
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer
basicConstraints = CA:FALSE
keyUsage = digitalSignature, keyEncipherment
subjectAltName = @alternate_names
nsComment = "OpenSSL Generated Certificate"
[ req_ext ]
subjectKeyIdentifier = hash
basicConstraints = CA:FALSE
keyUsage = digitalSignature, keyEncipherment
subjectAltName = @alternate_names
nsComment = "OpenSSL Generated Certificate"
[ alternate_names ]
DNS.1 = localhost
IP.1 = 192.168.95.2
\ No newline at end of file
#!/bin/bash
openssl req -config localhost.conf -new -sha256 -newkey rsa:2048 -nodes -keyout localhost.key -x509 -days 365 -out localhost.crt
cat localhost.crt localhost.key > localhost.pem
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment