Browse Source

Minor Refactoring, added check for IP Addresses

pull/1/head
Muthu Kumar 7 years ago
parent
commit
bcd4d5a830
  1. 18
      actions/createProxyServer.js
  2. 24
      actions/createStaticServer.js
  3. 51
      index.js
  4. 24
      util/isIP.js
  5. 6
      util/nginxConf.js
  6. 13
      util/nginxPath.js
  7. 2
      util/parseToInt.js
  8. 57
      util/validate.js

18
actions/createProxyServer.js

@ -1,9 +1,9 @@
var fs = require('fs-extra') var fs = require('fs-extra');
var shell = require('shelljs') 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'); var { EOL } = require('os'); // \n if used on Linux, \r\n if used on Windows.
function createProxyServer(domain, inPort, outPort) { function createProxyServer(domain, inPort, outPort) {
fs.outputFileSync((conf(npath.availableSites(), domain)), fs.outputFileSync((conf(npath.availableSites(), domain)),
@ -24,8 +24,8 @@ function createProxyServer(domain, inPort, outPort) {
" }" + EOL + " }" + EOL +
"}" "}"
) )
shell.mkdir('-p', npath.enabledSites()) shell.mkdir('-p', npath.enabledSites()); // Creates directory if doesn't exist
shell.ln('-sf', conf(npath.availableSites(), domain), conf(npath.enabledSites(), domain)) shell.ln('-sf', conf(npath.availableSites(), domain), conf(npath.enabledSites(), domain)); // Symlink the conf file from sites-available to sites-enabled
} };
module.exports = createProxyServer module.exports = createProxyServer;

24
actions/createStaticServer.js

@ -1,16 +1,16 @@
var fs = require('fs-extra') var fs = require('fs-extra');
var shell = require('shelljs') 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'); var { EOL } = require('os'); // \n if used on Linux, \r\n if used on Windows.
function createStaticServer(domain, outPort = 80) { function createStaticServer(domain, outPort = 80) {
fs.outputFileSync((conf(npath.availableSites(), domain)), fs.outputFileSync((conf(npath.availableSites(), domain)), // Gets nginx's paths from nginxPath.js
"server {" + EOL + "server {" + EOL +
" listen " + outPort + ";" + EOL + " listen " + outPort + ";" + EOL +
" listen [::]:" + outPort + ";" + EOL + " listen [::]:" + outPort + ";" + EOL +
" root /var/www/" + domain + ";" + EOL + " root " + npath.webRoot() + + domain + ";" + EOL +
" index index.html index.htm;" + EOL + " index index.html index.htm;" + EOL +
"" + EOL + "" + EOL +
" server_name " + domain + EOL + " server_name " + domain + EOL +
@ -19,9 +19,9 @@ function createStaticServer(domain, outPort = 80) {
" }" + EOL + " }" + EOL +
"}" "}"
) )
shell.mkdir('-p', npath.enabledSites()) shell.mkdir('-p', npath.enabledSites()) // Creates directory if doesn't exist
shell.ln('-sf', conf(npath.availableSites(), domain), conf(npath.enabledSites(), domain)) shell.ln('-sf', conf(npath.availableSites(), domain), conf(npath.enabledSites(), domain)) // Symlink the conf file from sites-available to sites-enabled
shell.ln('-sf', ".", npath.homeDir() + domain) shell.ln('-sf', ".", npath.webRoot() + domain) // Symlink current directory to nginx's web root
} };
module.exports = createStaticServer module.exports = createStaticServer;

51
index.js

@ -1,35 +1,33 @@
#!/usr/bin/env node --harmony #!/usr/bin/env node
// Requiring npm modules // Requiring npm modules
var program = require('commander') var program = require('commander');
var shell = require('shelljs') var shell = require('shelljs');
var fs = require('fs-extra') var fs = require('fs-extra');
var chalk = require('chalk') var chalk = require('chalk');
var validate = require('./util/validate')
//var validator = require('validator')
// Requiring Actions // Requiring utils
var createProxyServer = require('./actions/createProxyServer') var validate = require('./util/validate');
var createStaticServer = require('./actions/createStaticServer')
// Using Validator // Requiring Actions
// var isDomain = validator.isFQDN var createProxyServer = require('./actions/createProxyServer');
var createStaticServer = require('./actions/createStaticServer');
/* Uncomment in Production! /* Uncomment in Production!
//Detect Linux or BSD // Detect Linux or BSD
var isLin = /^linux|^bsd/.test(process.platform) var isLin = /^linux|^bsd/.test(process.platform);
//Throw if OS is not Linux or BSD // Throw if OS is not Linux or BSD. This should be changed to throw if not Debian based distro. Eventually, we can add more exceptions as `up` handles more cases.
if(!isLin) { if(!isLin) {
shell.echo("\nThis is not a Linux or freeBSD distribution. I'm not written for this distro. Please raise an issue at " + chalk.cyan("https://github.com/codefeathers/up-serve") + " if you want `up` to be ported for your distro") shell.echo("\nThis is not a Linux or freeBSD distribution. I'm not written for this distro. Please raise an issue at " + chalk.cyan("https://github.com/codefeathers/up-serve") + " if you want `up` to be ported for your distro");
shell.exit(1) shell.exit(1);
} }
//Throw if Nginx is not found // Throw if Nginx is not found
if (!shell.which('nginx')) { if (!shell.which('nginx')) {
shell.echo('I need nginx to work. Install nginx first. https://nginx.org/') shell.echo('I need nginx to work. Install nginx first. https://nginx.org/');
shell.exit(1) shell.exit(1);
} }
*/ */
@ -40,20 +38,19 @@ 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 outport is not given, 80 is set as default. Later, change this default to reflect nginx's settings.
if (!validate(domain, outPort)) return if (!validate(domain, outPort)) return //Validates domain and outport, and if invalid, throws and returns.
//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!") 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") { //Inbound port is necessary, but outbound is set to 80 by default. Again, will change this to reflect nginx's settings.
if (!validate(domain, inPort, outPort)) return if (!validate(domain, inPort, outPort)) return
createProxyServer(domain, inPort, outPort) createProxyServer(domain, inPort, outPort)
console.log("Done! Your reverse proxy 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
@ -71,11 +68,12 @@ program
}) })
program program
.command('*') .command('*') // This should pick invalid commands, but it doesn't, yet.
.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.")
}) })
// Adds custom help text to the automatically generated help.
program.on('--help', function () { program.on('--help', function () {
console.log(''); console.log('');
console.log(' Usage:'); console.log(' Usage:');
@ -88,4 +86,5 @@ program.on('--help', function () {
console.log(''); console.log('');
}); });
// Parses commands passed to `up` and chooses one of the above commands.
program.parse(process.argv); program.parse(process.argv);

24
util/isIP.js

@ -0,0 +1,24 @@
// Parses a string, and returns true if it is an IP Address.
function isIP(str) {
var segments = str
.split('.')
.map(Number);
if (!segments.length === 4) {
return false;
}
for (var segment of segments) {
if (Number.isNaN(segment)) {
return false;
}
if (segment < 1 || segment > 255) {
return false;
}
}
if (segments[3] > 254) {
return false;
}
return true;
}
module.exports = isIP;

6
util/nginxConf.js

@ -1,5 +1,7 @@
// Simple function that takes a path and domain name, concatenates them with ".conf" and returns it.
function conf(path, domain) { function conf(path, domain) {
return (path + domain + ".conf") return (path + domain + ".conf");
} }
module.exports = conf module.exports = conf;

13
util/nginxPath.js

@ -1,17 +1,20 @@
// These functions just return paths. Later, these should be modified to poll from nginx's config.
function availableSites() { function availableSites() {
var availableSites = "/etc/nginx/available-sites/"; var availableSites = "/etc/nginx/sites-available/";
return availableSites; return availableSites;
} }
function enabledSites() { function enabledSites() {
var enabledSites = "/etc/nginx/enabled-sites/"; var enabledSites = "/etc/nginx/sites-enabled/";
return enabledSites; return enabledSites;
} }
function homeDir() { function webRoot() {
return "/var/www/"; var webRoot = "/var/www/";
return webRoot;
} }
module.exports.availableSites = availableSites; module.exports.availableSites = availableSites;
module.exports.enabledSites = enabledSites; module.exports.enabledSites = enabledSites;
module.exports.homeDir = homeDir; module.exports.webRoot = webRoot;

2
util/parseToInt.js

@ -1,3 +1,5 @@
// Parse an input string and return a number if it is an integer. If it's a float, string, or array, return undefined.
function parseToInt(inputString) { function parseToInt(inputString) {
var parsing = /^\d+$/.exec(inputString); var parsing = /^\d+$/.exec(inputString);
return (parsing || [])[0]; return (parsing || [])[0];

57
util/validate.js

@ -1,48 +1,61 @@
var validator = require('validator'); var validator = require('validator');
var parseToInt = require('./parseToInt'); var parseToInt = require('./parseToInt');
var isIP = require('./isIP');
// Using Validator // Using Validator
var isDomain = validator.isFQDN var isDomain = validator.isFQDN;
function validate(domain, inPort = undefined, outPort = "80") { function validate(domain, inPort = undefined, outPort = "80") {
var domainInvalidMsg = "\nDomain is not valid. Please use a valid domain name."
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) // Error messages
var validOutPort = parseToInt(outPort) var domainInvalidMsg = ["\nPlease use a domain name instead of an IP address.", "\nDomain is not valid. Please use a valid domain name."];
var portInvalidMsg = ["\nPort should be a number.", "\nPort should be a number from 1 and 65535."];
var isValid = true // ARGV returns a string as input. Port numbers should be parsed to int to validate them. If validation fails, these will return undefined and will fail the subsequent test.
var validInPort = parseToInt(inPort);
var validOutPort = parseToInt(outPort);
// The value of isInvalid will be returned back. If none of the `if`s are true, the default value `true` is returned `domain`, `inPort` and `outPort` are considered validated.
var isValid = true;
// Throw if IP is given instead of domain name.
if (isIP(domain)) {
console.log(domainInvalidMsg[0]);
return isValid = false;
}
// Throw if input is not a Fully Qualified Domain Name (FQDN)
if (!isDomain(domain)) { if (!isDomain(domain)) {
console.log(domainInvalidMsg) console.log(domainInvalidMsg[1]);
return isValid = false return isValid = false;
} }
// Enter if `inPort` is not defined. This happens for `up static` where no inbound ports are required.
if (typeof inPort == undefined) { if (typeof inPort == undefined) {
if (!validOutPort) { if (!validOutPort) {
console.log(portInvalidMsg[0]) console.log(portInvalidMsg[0]); // `outPort` is not an integer.
return isValid = false return isValid = false;
} }
if (!(validOutPort > 0 && validOutPort <= 65535)) { if (!(validOutPort > 0 && validOutPort <= 65535)) {
console.log(portInvalidMsg[1]) console.log(portInvalidMsg[1]); // `outPort` is not within port range.
return isValid = false return isValid = false;
} }
} }
// Enter if `inPort` is defined. This happens for `up proxy` where inbound port is required.
if (typeof inPort !== undefined) { if (typeof inPort !== undefined) {
if (!validInPort || !validOutPort) { if (!validInPort || !validOutPort) {
console.log(portInvalidMsg[0]) console.log(portInvalidMsg[0]); // Either `inPort` or `outPort` is not an integer.
return isValid = false return isValid = false;
} }
if (typeof outPort !== undefined) { if (typeof outPort !== undefined) {
if (!((validInPort > 0 && validInPort <= 65535) && (validOutPort > 0 && validOutPort <= 65535))) { if (!((validInPort > 0 && validInPort <= 65535) && (validOutPort > 0 && validOutPort <= 65535))) {
console.log(portInvalidMsg[1]) console.log(portInvalidMsg[1]); // Either `inPort` or `outPort` are not within port range.
return isValid = false return isValid = false;
} }
} }
return isValid return isValid; // If any of the `if`s were true, `isInvalid = false`. If not, `isInvalid = true`.
} }
} }
module.exports = validate module.exports = validate;
Loading…
Cancel
Save