Browse Source

Removed dependency on `validator` and added local module. Working on bugs.

pull/6/head
Muthu Kumar 7 years ago
parent
commit
b97daf1d67
  1. 3
      .eslintrc.json
  2. 2
      actions/killServer.js
  3. 12
      actions/listServers.js
  4. 1542
      assets/tlds.txt
  5. 7
      build/fetchTLDS.js
  6. 10
      index.js
  7. 10
      package-lock.json
  8. 7
      package.json
  9. 1
      servers.up
  10. 61
      utils/isFQDN.js
  11. 73
      utils/listFile.js
  12. 15
      utils/removeFromArray.js
  13. 3
      utils/validate.js

3
.eslintrc.json

@ -1,6 +1,7 @@
{
"env": {
"node": true
"node": true,
"es6": true
},
"extends": "eslint:recommended",
"rules": {

2
actions/killServer.js

@ -10,8 +10,8 @@ function killServer(domain, outPort) {
shell.rm('-rf', conf(npath.confD(), domain, outPort));
shell.rm('-rf', npath.webRootDomain(domain, outPort));
nginxReload();
removeFromList(domain, outPort);
nginxReload();
}
module.exports = killServer;

12
actions/listServers.js

@ -0,0 +1,12 @@
var readServers = require('../utils/listFile').readServers;
var prettyjson = require('prettyjson');
var EOL = require('os').EOL;
function listServers() {
var serversList = readServers();
if(serversList) console.log(EOL + prettyjson.render(serversList) + EOL);
else console.log("\nNo servers were found! Create some using `up`!\n");
}
module.exports = listServers;

1542
assets/tlds.txt

File diff suppressed because it is too large

7
build/fetchTLDS.js

@ -0,0 +1,7 @@
var https = require('https');
var fs = require('fs-extra');
var file = fs.createWriteStream("./assets/tlds.txt");
https.get("https://data.iana.org/TLD/tlds-alpha-by-domain.txt", function(response) {
response.pipe(file);
});

10
index.js

@ -3,21 +3,18 @@
// Requiring npm modules
var program = require('commander');
var chalk = require('chalk');
var fs = require('fs-extra');
var prettyjson = require('prettyjson');
//var fs = require('fs-extra');
// Requiring Actions
var createProxyServer = require('./actions/createProxyServer');
var createStaticServer = require('./actions/createStaticServer');
var killServer = require('./actions/killServer');
var listServers = require('./actions/listServers');
// Requiring utils
var validate = require('./utils/validate');
var requirements = require('./utils/requirements');
var appendToList = require('./utils/listFile').appendToList;
var readServers = require('./utils/listFile').readServers;
var EOL = require('os').EOL;
// Check for requirements such as OS version and nginx install. Throw and exit if requirements not found.
// #Roadmap: Add ability to satisfy any possible requirements.
@ -52,8 +49,7 @@ program
.command('list')
.description('List all available servers.')
.action(function () {
var serversList = readServers();
console.log(EOL + prettyjson.render(serversList) + EOL);
listServers();
});
program

10
package-lock.json

@ -682,11 +682,6 @@
"integrity": "sha512-xYuhvQ7I9PDJIGBWev9xm0+SMSed3ZDBAmvVjbFR1ZRLAF+vlXcQu6cRI9uAlj81rzikElRVteehwV7DuX2ZmQ==",
"dev": true
},
"json-beautify": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/json-beautify/-/json-beautify-1.0.1.tgz",
"integrity": "sha1-WYtQ1Mjqm4/KWru0C34svTrUwvw="
},
"json-schema-traverse": {
"version": "0.3.1",
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.3.1.tgz",
@ -1180,11 +1175,6 @@
"integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=",
"dev": true
},
"validator": {
"version": "9.1.1",
"resolved": "https://registry.npmjs.org/validator/-/validator-9.1.1.tgz",
"integrity": "sha512-1TGGX1GKilfmcEa9rm+9nI9AqIUQK8oj4jZI0DmTGLTPM5jmowBBhyBIHCks73+P1QPZk2i6oOYUq583uOetHQ=="
},
"which": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/which/-/which-1.3.0.tgz",

7
package.json

@ -4,7 +4,8 @@
"description": "A cli tool to quickly create and manage nginx server blocks.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"build": "node ./build/fetchTLDS"
},
"repository": {
"type": "git",
@ -30,10 +31,8 @@
"chalk": "^2.3.0",
"commander": "^2.11.0",
"fs-extra": "^4.0.2",
"json-beautify": "^1.0.1",
"prettyjson": "^1.2.1",
"shelljs": "^0.7.8",
"validator": "^9.1.1"
"shelljs": "^0.7.8"
},
"devDependencies": {
"eslint": "^4.10.0"

1
servers.up

@ -0,0 +1 @@
[]

61
utils/isFQDN.js

@ -0,0 +1,61 @@
// Module courtesy of TRGWII
// Original: https://github.com/trgwii/isFQDN
'use strict';
var fs = require('fs-extra');
// Official list of TLDs should be fetched from:
// https://data.iana.org/TLD/tlds-alpha-by-domain.txt
// You must have received a copy of the list along with `up`
// Run `npm run build` to update the cached list
function isFQDN(domain) {
// Importing and parsing `tlds.txt` file
var tlds = fs.readFileSync('./assets/tlds.txt', 'utf8')
.split(/[\r\n]+/)
.filter(x => !x.startsWith('#'));
if (domain.length > 253) {
return false;
}
var labels = domain.split('.').reverse();
if (labels.length < 2) {
return false;
}
var tld = labels[0];
if (!tlds.includes(tld.toUpperCase())) {
return false;
}
for (var label of labels) {
const len = label.length;
if (len > 63 || len === 0) {
return false;
}
for (let i = 0; i < len; i++) {
const char = label[i];
if ((i === 0 || i === len - 1) && char === '-') {
return false;
}
if (!char.match(/^[a-zA-Z0-9-]$/)) {
return false;
}
}
}
return true;
};
module.exports = isFQDN;

73
utils/listFile.js

@ -1,49 +1,48 @@
var fs = require('fs-extra');
var shell = require('shelljs');
var beautifyJSON = require("json-beautify");
var removeFromArray = require('./removeFromArray');
var EOL = require('os').EOL; // \n if used on Linux, \r\n if used on Windows.
var listFilePath = "/etc/up-serve/servers.up";
var listFilePath = "./servers.up";
function appendToList(domain, outPort, inPort) {
inPort = inPort || undefined;
var jsonFile = {};
var jsonFile = { "domains": [] };
var domBlock;
if (!inPort) {
domBlock = {
"type": "static",
"outPort": outPort,
"inPort": undefined
}
"domain": domain,
"type": "static",
"outPort": outPort
};
} else {
domBlock = {
"type": "proxy",
"outPort": outPort,
"inPort": inPort
}
"domain": domain,
"type": "proxy",
"outPort": outPort,
"inPort": inPort
};
}
if (fs.existsSync(listFilePath)) {
jsonFile = fs.readFileSync(listFilePath);
jsonFile = JSON.parse(jsonFile);
for (block in jsonFile) {
if (block[domain] == domain && block[domain].outPort == outPort) {
delete jsonFile.block;
return;
/*for (name in jsonFile) {
if (name == domain && jsonFile[name][outPort] == outPort) {
delete jsonFile[domain];
console.log('\nDomain was deleted successfully.\n');
}
}
jsonFile[domain] = domBlock;
jsonFile = beautifyJSON(jsonFile, null, 2, 30);
}
else {
jsonFile[domain] = domBlock;
jsonFile = beautifyJSON(jsonFile);
}*/
jsonFile = removeFromArray(jsonFile.domains, domain, outPort);
}
jsonFile.domains.push(domBlock);
jsonFile = JSON.stringify(jsonFile, null, '\t');
fs.writeFileSync(listFilePath, jsonFile);
}
@ -52,25 +51,25 @@ function removeFromList (domain, outPort) {
if (fs.existsSync(listFilePath)) {
jsonFile = fs.readFileSync(listFilePath);
jsonFile = JSON.parse(jsonFile);
// for (name in jsonFile) {
// if (name == domain && jsonFile[domain].outPort == outPort) {
// delete jsonFile[domain];
// console.log('\nDomain was deleted successfully.\n');
// }
// }
jsonFile = removeFromArray(jsonFile.domains, domain, outPort);
for (block in jsonFile) {
if (block[domain] == domain && block[domain].outPort == outPort) {
delete jsonFile.block;
console.log('\nDomain was deleted successfully.\n');
return;
}
}
jsonFile = beautifyJSON(jsonFile, null, 2, 30);
jsonFile = JSON.stringify(jsonFile, null, '\t');
fs.writeFileSync(listFilePath, jsonFile);
}
else {
console.log("\Domain was not in my list. Are you sure?\n")
}
fs.writeFileSync(listFilePath, jsonFile);
else console.log("\nNo servers were created using `up` yet.\n");
}
function readServers () {
return JSON.parse(fs.readFileSync(listFilePath));
var serversList = JSON.parse(fs.readFileSync(listFilePath));
if(!serversList[0]) return undefined;
return serversList;
}
module.exports.appendToList = appendToList;

15
utils/removeFromArray.js

@ -0,0 +1,15 @@
function removeFromArray (arr, dom, port) {
var shouldDelete = [];
for(var i = 0; i < arr.length; i++) if((arr[i].domain == dom) && (arr[i].outPort == port)) shouldDelete = [true, i];
if (shouldDelete[0]) {
arr.splice(shouldDelete[1], 1);
console.log('\nItem was deleted!\n');
} else {
console.log('\nItem was not found!\n');
}
return arr;
}
module.exports = removeFromArray;

3
utils/validate.js

@ -1,9 +1,8 @@
var validator = require('validator');
var parseToInt = require('./parseToInt');
var isIP = require('./isIP');
// Using Validator
var isDomain = validator.isFQDN;
var isDomain = require('./isFQDN');
function validate(domain, inPort, outPort) {
//

Loading…
Cancel
Save