From 1a2f20cc95ec129698e77d264cae83a6a83fbf08 Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Tue, 31 Oct 2017 16:46:00 +0530 Subject: [PATCH] Fixed port validation and up static --- actions/createProxyServer.js | 32 +++++++++++++++++--------------- actions/createStaticServer.js | 29 +++++++++++++++++++---------- index.js | 28 ++++++++++++++-------------- util/parseToInt.js | 6 ++++++ util/validate.js | 9 ++++++++- 5 files changed, 64 insertions(+), 40 deletions(-) create mode 100644 util/parseToInt.js diff --git a/actions/createProxyServer.js b/actions/createProxyServer.js index fe90de9..dfab7e4 100644 --- a/actions/createProxyServer.js +++ b/actions/createProxyServer.js @@ -3,23 +3,25 @@ var shell = require('shelljs') var npath = require('../util/nginxPath') var conf = require('../util/nginxConf') +var { EOL } = require('os'); + function createProxyServer(domain, inPort, outPort) { fs.outputFileSync((conf(npath.availableSites(), domain)), - "server {" + "\r\n" + - " listen " + outPort + ";" + "\r\n" + - " listen [::]:" + outPort + ";" + "\r\n" + - " root /var/www/" + domain + ";" + "\r\n" + - " index index.html index.htm;" + "\r\n" + - "" + "\r\n" + - " server_name " + domain + ";" + "\r\n" + - " location / {" + "\r\n" + - " proxy_pass http://localhost:" + inPort + ";" + "\r\n" + - " proxy_http_version 1.1;" + "\r\n" + - " proxy_set_header Upgrade $http_upgrade;" + "\r\n" + - " proxy_set_header Connection 'upgrade';" + "\r\n" + - " proxy_set_header Host $host;" + "\r\n" + - " proxy_cache_bypass $http_upgrade;" + "\r\n" + - " }" + "\r\n" + + "server {" + EOL + + " listen " + outPort + ";" + EOL + + " listen [::]:" + outPort + ";" + EOL + + " root /var/www/" + domain + ";" + EOL + + " index index.html index.htm;" + EOL + + "" + EOL + + " server_name " + domain + ";" + EOL + + " location / {" + EOL + + " proxy_pass http://localhost:" + inPort + ";" + EOL + + " proxy_http_version 1.1;" + EOL + + " proxy_set_header Upgrade $http_upgrade;" + EOL + + " proxy_set_header Connection 'upgrade';" + EOL + + " proxy_set_header Host $host;" + EOL + + " proxy_cache_bypass $http_upgrade;" + EOL + + " }" + EOL + "}" ) shell.mkdir('-p', npath.enabledSites()) diff --git a/actions/createStaticServer.js b/actions/createStaticServer.js index 78fdfa2..5567b1a 100644 --- a/actions/createStaticServer.js +++ b/actions/createStaticServer.js @@ -1,18 +1,27 @@ var fs = require('fs-extra') +var shell = require('shelljs') +var npath = require('../util/nginxPath') +var conf = require('../util/nginxConf') + +var { EOL } = require('os'); function createStaticServer(domain, outPort = 80) { - fs.outputFileSync("/test.txt", - "server {" + "\n" + - " listen " + outPort + ";" + "\n" + - " listen [::]:" + outPort + ";" + "\n" + - " root /var/www/" + domain + ";" + "\n" + - " index index.html index.htm;" + "\n" + - "" + "\n" + - " server_name " + domain + "\n" + - " location / {" + "\n" + - " try_files $uri $uri/ =404;" + + fs.outputFileSync((conf(npath.availableSites(), domain)), + "server {" + EOL + + " listen " + outPort + ";" + EOL + + " listen [::]:" + outPort + ";" + EOL + + " root /var/www/" + domain + ";" + EOL + + " index index.html index.htm;" + EOL + + "" + EOL + + " server_name " + domain + EOL + + " location / {" + EOL + + " try_files $uri $uri/ =404;" + EOL + + " }" + EOL + "}" ) + shell.mkdir('-p', npath.enabledSites()) + shell.ln('-sf', conf(npath.availableSites(), domain), conf(npath.enabledSites(), domain)) + shell.ln('-sf', ".", "/var/www" + domain) } module.exports = createStaticServer \ No newline at end of file diff --git a/index.js b/index.js index 7e493f4..813e3d7 100644 --- a/index.js +++ b/index.js @@ -40,43 +40,43 @@ program program .command('static [outPort]') .description('Create a static server at this folder.') - .action(function(domain, outPort=80) { - if(!validate(domain, outPort)) return - console.log('Static server works') + .action(function (domain, outPort = 80) { + if (!validate(domain, outPort)) return //createStaticServer(domain, outPort) + console.log("Done! Your static server has been set up!\nPoint your domain to this server and check " + chalk.cyan(domain) + " to verify!") }) program .command('proxy [outPort]') .description('Create a proxy server, listening at port number.') - .action(function(domain, inPort, outPort = "80") { - if (!validate(domain, inPort, outPort)) return + .action(function (domain, inPort, outPort = "80") { + if (!validate(domain, inPort, outPort)) return createProxyServer(domain, inPort, outPort) - console.log("Done! Your server has been set up!\nPoint your domain to this server and check " + chalk.cyan(domain) + " to verify!") + console.log("Done! Your reverse proxy server has been set up!\nPoint your domain to this server and check " + chalk.cyan(domain) + " to verify!") //} }) - + program .command('list') .description('List all available servers.') - .action(function() { + .action(function () { // Stuff happens here }) - + program .command('kill ') .description('Kill a server.') - .action(function(domain) { + .action(function (domain) { // Stuff happens here }) program .command('*') - .action(function() { + .action(function () { console.log("Invalid command. Type " + chalk.cyan('up --help') + " for help.") }) - -program.on('--help', function(){ + +program.on('--help', function () { console.log(''); console.log(' Usage:'); console.log(''); @@ -86,6 +86,6 @@ program.on('--help', function(){ console.log(' ', chalk.yellow('$ up'), chalk.cyan('proxy'), chalk.blue('domain-name port-number')); console.log(' Set up a proxy server listening at port-number'); console.log(''); - }); +}); program.parse(process.argv); \ No newline at end of file diff --git a/util/parseToInt.js b/util/parseToInt.js new file mode 100644 index 0000000..a072aae --- /dev/null +++ b/util/parseToInt.js @@ -0,0 +1,6 @@ +function parseToInt(inputString) { + var parsing = /^\d+$/.exec(inputString); + return (parsing || [])[0]; +} + +module.exports = parseToInt; \ No newline at end of file diff --git a/util/validate.js b/util/validate.js index ce5db93..783b90c 100644 --- a/util/validate.js +++ b/util/validate.js @@ -1,4 +1,6 @@ -var validator = require('validator') +var validator = require('validator'); +var parseToInt = require('./parseToInt'); + // Using Validator var isDomain = validator.isFQDN @@ -7,7 +9,12 @@ function validate(domain, inPort = undefined, outPort = "80") { var portInvalidMsg = ["\nPort should be a number.", "\nPort should be a number from 1 and 65535."] //var validInPort = /^\d+$/.exec(inPort)[0] //var validOutPort = /^\d+$/.exec(outPort)[0] + //var regex = /^\d+$/.exec(outPort); + //var validInPort = regex ? regex[0] : null; + var validInPort = parseToInt(inPort) + var validOutPort = parseToInt(outPort) + var isValid = true if (!isDomain(domain)) { console.log(domainInvalidMsg)