Muthu Kumar
7 years ago
8 changed files with 119 additions and 76 deletions
@ -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; |
@ -1,5 +1,7 @@ |
|||
// Simple function that takes a path and domain name, concatenates them with ".conf" and returns it.
|
|||
|
|||
function conf(path, domain) { |
|||
return (path + domain + ".conf") |
|||
return (path + domain + ".conf"); |
|||
} |
|||
|
|||
module.exports = conf |
|||
module.exports = conf; |
@ -1,17 +1,20 @@ |
|||
// These functions just return paths. Later, these should be modified to poll from nginx's config.
|
|||
|
|||
function availableSites() { |
|||
var availableSites = "/etc/nginx/available-sites/"; |
|||
var availableSites = "/etc/nginx/sites-available/"; |
|||
return availableSites; |
|||
} |
|||
|
|||
function enabledSites() { |
|||
var enabledSites = "/etc/nginx/enabled-sites/"; |
|||
var enabledSites = "/etc/nginx/sites-enabled/"; |
|||
return enabledSites; |
|||
} |
|||
|
|||
function homeDir() { |
|||
return "/var/www/"; |
|||
function webRoot() { |
|||
var webRoot = "/var/www/"; |
|||
return webRoot; |
|||
} |
|||
|
|||
module.exports.availableSites = availableSites; |
|||
module.exports.enabledSites = enabledSites; |
|||
module.exports.homeDir = homeDir; |
|||
module.exports.webRoot = webRoot; |
@ -1,48 +1,61 @@ |
|||
var validator = require('validator'); |
|||
var parseToInt = require('./parseToInt'); |
|||
var isIP = require('./isIP'); |
|||
|
|||
// Using Validator
|
|||
var isDomain = validator.isFQDN |
|||
var isDomain = validator.isFQDN; |
|||
|
|||
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;
|
|||
|
|||
// Error messages
|
|||
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 validInPort = parseToInt(inPort) |
|||
var validOutPort = parseToInt(outPort) |
|||
// 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; |
|||
} |
|||
|
|||
var isValid = true |
|||
// Throw if input is not a Fully Qualified Domain Name (FQDN)
|
|||
if (!isDomain(domain)) { |
|||
console.log(domainInvalidMsg) |
|||
return isValid = false |
|||
console.log(domainInvalidMsg[1]); |
|||
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 (!validOutPort) { |
|||
console.log(portInvalidMsg[0]) |
|||
return isValid = false |
|||
console.log(portInvalidMsg[0]); // `outPort` is not an integer.
|
|||
return isValid = false; |
|||
} |
|||
if (!(validOutPort > 0 && validOutPort <= 65535)) { |
|||
console.log(portInvalidMsg[1]) |
|||
return isValid = false |
|||
console.log(portInvalidMsg[1]); // `outPort` is not within port range.
|
|||
return isValid = false; |
|||
} |
|||
} |
|||
|
|||
// Enter if `inPort` is defined. This happens for `up proxy` where inbound port is required.
|
|||
if (typeof inPort !== undefined) { |
|||
if (!validInPort || !validOutPort) { |
|||
console.log(portInvalidMsg[0]) |
|||
return isValid = false |
|||
console.log(portInvalidMsg[0]); // Either `inPort` or `outPort` is not an integer.
|
|||
return isValid = false; |
|||
} |
|||
if (typeof outPort !== undefined) { |
|||
if (!((validInPort > 0 && validInPort <= 65535) && (validOutPort > 0 && validOutPort <= 65535))) { |
|||
console.log(portInvalidMsg[1]) |
|||
return isValid = false |
|||
console.log(portInvalidMsg[1]); // Either `inPort` or `outPort` are not within port range.
|
|||
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…
Reference in new issue