Browse Source

Fixed port validation and up static

pull/1/head
Muthu Kumar 7 years ago
parent
commit
1a2f20cc95
  1. 32
      actions/createProxyServer.js
  2. 29
      actions/createStaticServer.js
  3. 20
      index.js
  4. 6
      util/parseToInt.js
  5. 9
      util/validate.js

32
actions/createProxyServer.js

@ -3,23 +3,25 @@ var shell = require('shelljs')
var npath = require('../util/nginxPath') var npath = require('../util/nginxPath')
var conf = require('../util/nginxConf') var conf = require('../util/nginxConf')
var { EOL } = require('os');
function createProxyServer(domain, inPort, outPort) { function createProxyServer(domain, inPort, outPort) {
fs.outputFileSync((conf(npath.availableSites(), domain)), fs.outputFileSync((conf(npath.availableSites(), domain)),
"server {" + "\r\n" + "server {" + EOL +
" listen " + outPort + ";" + "\r\n" + " listen " + outPort + ";" + EOL +
" listen [::]:" + outPort + ";" + "\r\n" + " listen [::]:" + outPort + ";" + EOL +
" root /var/www/" + domain + ";" + "\r\n" + " root /var/www/" + domain + ";" + EOL +
" index index.html index.htm;" + "\r\n" + " index index.html index.htm;" + EOL +
"" + "\r\n" + "" + EOL +
" server_name " + domain + ";" + "\r\n" + " server_name " + domain + ";" + EOL +
" location / {" + "\r\n" + " location / {" + EOL +
" proxy_pass http://localhost:" + inPort + ";" + "\r\n" + " proxy_pass http://localhost:" + inPort + ";" + EOL +
" proxy_http_version 1.1;" + "\r\n" + " proxy_http_version 1.1;" + EOL +
" proxy_set_header Upgrade $http_upgrade;" + "\r\n" + " proxy_set_header Upgrade $http_upgrade;" + EOL +
" proxy_set_header Connection 'upgrade';" + "\r\n" + " proxy_set_header Connection 'upgrade';" + EOL +
" proxy_set_header Host $host;" + "\r\n" + " proxy_set_header Host $host;" + EOL +
" proxy_cache_bypass $http_upgrade;" + "\r\n" + " proxy_cache_bypass $http_upgrade;" + EOL +
" }" + "\r\n" + " }" + EOL +
"}" "}"
) )
shell.mkdir('-p', npath.enabledSites()) shell.mkdir('-p', npath.enabledSites())

29
actions/createStaticServer.js

@ -1,18 +1,27 @@
var fs = require('fs-extra') 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) { function createStaticServer(domain, outPort = 80) {
fs.outputFileSync("/test.txt", fs.outputFileSync((conf(npath.availableSites(), domain)),
"server {" + "\n" + "server {" + EOL +
" listen " + outPort + ";" + "\n" + " listen " + outPort + ";" + EOL +
" listen [::]:" + outPort + ";" + "\n" + " listen [::]:" + outPort + ";" + EOL +
" root /var/www/" + domain + ";" + "\n" + " root /var/www/" + domain + ";" + EOL +
" index index.html index.htm;" + "\n" + " index index.html index.htm;" + EOL +
"" + "\n" + "" + EOL +
" server_name " + domain + "\n" + " server_name " + domain + EOL +
" location / {" + "\n" + " location / {" + EOL +
" try_files $uri $uri/ =404;" + " 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 module.exports = createStaticServer

20
index.js

@ -40,43 +40,43 @@ program
program program
.command('static <domain> [outPort]') .command('static <domain> [outPort]')
.description('Create a static server at this folder.') .description('Create a static server at this folder.')
.action(function(domain, outPort=80) { .action(function (domain, outPort = 80) {
if(!validate(domain, outPort)) return if (!validate(domain, outPort)) return
console.log('Static server works')
//createStaticServer(domain, outPort) //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 program
.command('proxy <domain> <inPort> [outPort]') .command('proxy <domain> <inPort> [outPort]')
.description('Create a proxy server, listening at port number.') .description('Create a proxy server, listening at port number.')
.action(function(domain, inPort, outPort = "80") { .action(function (domain, inPort, outPort = "80") {
if (!validate(domain, inPort, outPort)) return if (!validate(domain, inPort, outPort)) return
createProxyServer(domain, inPort, outPort) 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 program
.command('list') .command('list')
.description('List all available servers.') .description('List all available servers.')
.action(function() { .action(function () {
// Stuff happens here // Stuff happens here
}) })
program program
.command('kill <domain>') .command('kill <domain>')
.description('Kill a server.') .description('Kill a server.')
.action(function(domain) { .action(function (domain) {
// Stuff happens here // Stuff happens here
}) })
program program
.command('*') .command('*')
.action(function() { .action(function () {
console.log("Invalid command. Type " + chalk.cyan('up --help') + " for help.") console.log("Invalid command. Type " + chalk.cyan('up --help') + " for help.")
}) })
program.on('--help', function(){ program.on('--help', function () {
console.log(''); console.log('');
console.log(' Usage:'); console.log(' Usage:');
console.log(''); 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(' ', 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(' Set up a proxy server listening at port-number');
console.log(''); console.log('');
}); });
program.parse(process.argv); program.parse(process.argv);

6
util/parseToInt.js

@ -0,0 +1,6 @@
function parseToInt(inputString) {
var parsing = /^\d+$/.exec(inputString);
return (parsing || [])[0];
}
module.exports = parseToInt;

9
util/validate.js

@ -1,4 +1,6 @@
var validator = require('validator') var validator = require('validator');
var parseToInt = require('./parseToInt');
// Using Validator // Using Validator
var isDomain = validator.isFQDN var isDomain = validator.isFQDN
@ -7,6 +9,11 @@ function validate(domain, inPort = undefined, outPort = "80") {
var portInvalidMsg = ["\nPort should be a number.", "\nPort should be a number from 1 and 65535."] var portInvalidMsg = ["\nPort should be a number.", "\nPort should be a number from 1 and 65535."]
//var validInPort = /^\d+$/.exec(inPort)[0] //var validInPort = /^\d+$/.exec(inPort)[0]
//var validOutPort = /^\d+$/.exec(outPort)[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 var isValid = true
if (!isDomain(domain)) { if (!isDomain(domain)) {

Loading…
Cancel
Save