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. 28
      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 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())

29
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

28
index.js

@ -40,43 +40,43 @@ program
program
.command('static <domain> [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 <domain> <inPort> [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 <domain>')
.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);

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
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)

Loading…
Cancel
Save