diff --git a/lib/smoke.js b/lib/smoke.js index 17dd9aa..320257e 100644 --- a/lib/smoke.js +++ b/lib/smoke.js @@ -4,6 +4,9 @@ const bodyParser = require('body-parser'); const multer = require('multer'); const proxy = require('express-http-proxy'); const corsMiddleWare = require('cors'); +const http = require('http'); +const https = require('https'); +const fs = require('fs'); const {getMocks} = require('./mock'); const {respondMock} = require('./response'); @@ -16,6 +19,7 @@ function createServer(options) { .disable('x-powered-by') .set('port', options.port) .set('host', options.host) + .set('https', options.https) .use(bodyParser.urlencoded({extended: true})) .use(bodyParser.json()) .use(multer().any()); @@ -70,8 +74,20 @@ function createServer(options) { function startServer(app) { const port = app.get('port'); const host = app.get('host'); - app.listen(port, host, () => { - console.log(`Server started on: http://${host}:${port}`); + const useHttps = app.get('https'); + + let server; + + if (useHttps) { + const key = fs.readFileSync(path.join(__dirname, '/../ssl/selfsigned.key')); + const cert = fs.readFileSync(path.join(__dirname, '/../ssl/selfsigned.crt')); + server = https.createServer({key, cert}, app); + } else { + server = http.createServer(app); + } + + server.listen(port, host, () => { + console.log(`Server started on: http${useHttps ? 's' : ''}://${host}:${port}`); }); } @@ -92,7 +108,8 @@ function getOptions(options) { depth: typeof options.depth === 'number' ? options.depth : 1, saveHeaders: options.saveHeaders || false, saveQueryParams: options.saveQueryParams || false, - cors: options.cors || null + cors: options.cors || null, + https: options.https || false }; } diff --git a/package.json b/package.json index 0017222..665e03d 100644 --- a/package.json +++ b/package.json @@ -98,6 +98,7 @@ "smoke-conv-cli.js", "bin", "lib", - "test/hooks.js" + "test/hooks.js", + "ssl" ] } diff --git a/smoke-cli.js b/smoke-cli.js index 8ced846..dbabd8e 100644 --- a/smoke-cli.js +++ b/smoke-cli.js @@ -29,7 +29,7 @@ function run(args) { const options = minimist(args, { number: ['port', 'depth'], string: ['host', 'set', 'not-found', 'record', 'ignore', 'hooks', 'proxy', 'collection', 'allow-cors'], - boolean: ['help', 'version', 'logs', 'save-headers', 'save-query'], + boolean: ['help', 'version', 'logs', 'save-headers', 'save-query', 'https'], alias: { p: 'port', h: 'host', @@ -70,6 +70,7 @@ function run(args) { record: options.record, collection: options.collection, depth: options.depth, + https: options.https, saveHeaders: options['save-headers'], saveQueryParams: options['save-query'], cors: options['allow-cors'] diff --git a/ssl/selfsigned.crt b/ssl/selfsigned.crt new file mode 100644 index 0000000..0f6b2d7 --- /dev/null +++ b/ssl/selfsigned.crt @@ -0,0 +1,20 @@ +-----BEGIN CERTIFICATE----- +MIIDWzCCAkOgAwIBAgIUdyuGqKugsL4EtNUNkItt9uw/5SIwDQYJKoZIhvcNAQEL +BQAwPTELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNPMSEwHwYDVQQKDBhJbnRlcm5l +dCBXaWRnaXRzIFB0eSBMdGQwHhcNMjQxMDE4MjAyMDU0WhcNMjUxMDE4MjAyMDU0 +WjA9MQswCQYDVQQGEwJVUzELMAkGA1UECAwCQ08xITAfBgNVBAoMGEludGVybmV0 +IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB +AM3CX2BUI7rj0Mg17TMb62bh9wPhGuxlb20FVvSSdxFYEOGvAni/eb5ne1iAmaxD +0QUyB2bUSsDbVbNAn7VMtxUvinVOX2guzDgEMNTsx4sIrfs+NNVcnaINGfLulihe +YoS9DVFiuL7sDzYdXRGUmDJKTkuPFS6PQWXNq8VvT/t4Sd5PyZZBv5lxF7VOsT43 +XlS3e46TcZgUFwwpnBkbLDN5/qu0cc41jBjWRrXtwZb3x4Hk2YBwe1r/HTwDYeue +c2sxw1KFIx6Q1ZM7GX5+KRa9Ha448gvqcCP2gmY9t1GFijdqir8C4g7WJGp65J60 +sf8HU75TKiGRkXexwKP/xw8CAwEAAaNTMFEwHQYDVR0OBBYEFNBlorj46zhJp96r +7UtOEYmv3HKZMB8GA1UdIwQYMBaAFNBlorj46zhJp96r7UtOEYmv3HKZMA8GA1Ud +EwEB/wQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBAKKtO5Et/jVDGZVosIQyrLB/ +tCqpF5MGZgw/PXLdxnahMcZn5oj3PqxfpIap0Y5TiGiQ9wXt4yt5aGEVgv8s54XU +9tzZTZctED5iODK+HPIz35Ya5IjvnKCafHIv1tBrto4Fe9Lcy8llT6DiMTdIM1Pr +emB6qZbsy/JDUOfI/XBsSGga/S11ZS30m/0S6Zs+ynhnzjjWGf6RkcGvtaDawUnU +DOtaoFx5jiT7AA4mYiKfYZ+Zb6ZOOTvn/eZsvbbLTY82TITh2zxNoa7VIwSPJDMm +6PC3rgRnHuEfFm11N3W1/LxsDvUkoGlwfy/f0otE/oZc+BrKRKzBhiZ9IduHU7A= +-----END CERTIFICATE----- diff --git a/ssl/selfsigned.key b/ssl/selfsigned.key new file mode 100644 index 0000000..97c436c --- /dev/null +++ b/ssl/selfsigned.key @@ -0,0 +1,28 @@ +-----BEGIN PRIVATE KEY----- +MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDNwl9gVCO649DI +Ne0zG+tm4fcD4RrsZW9tBVb0kncRWBDhrwJ4v3m+Z3tYgJmsQ9EFMgdm1ErA21Wz +QJ+1TLcVL4p1Tl9oLsw4BDDU7MeLCK37PjTVXJ2iDRny7pYoXmKEvQ1RYri+7A82 +HV0RlJgySk5LjxUuj0FlzavFb0/7eEneT8mWQb+ZcRe1TrE+N15Ut3uOk3GYFBcM +KZwZGywzef6rtHHONYwY1ka17cGW98eB5NmAcHta/x08A2HrnnNrMcNShSMekNWT +Oxl+fikWvR2uOPIL6nAj9oJmPbdRhYo3aoq/AuIO1iRqeuSetLH/B1O+UyohkZF3 +scCj/8cPAgMBAAECggEAJJRQpmxu6bPEk0NbBwIIqIK1imADjFFALO6NFe/P8Spp +3eViGBtaRmpZOFOsfj6UM0tAm5xUKdMUekib2/xmdAK+p7omJfxj2/WKxN/PXb1O +SOM3O1M6xutzWIfn9AdLlnORoY1ecLrAvYsc15pGMHKbVr16M4vOQY394py2aGsZ +JNaANN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