Browse Source

proxy server almost done

pull/1/head
Muthu Kumar 7 years ago
parent
commit
0dd90ecf3c
  1. 1
      Roadmap.md
  2. 40
      actions/createProxyServer.js
  3. 38
      index.js
  4. 18
      package-lock.json
  5. 4
      package.json
  6. 5
      util/nginxConf.js
  7. 17
      util/nginxPath.js
  8. 39
      util/validate.js

1
Roadmap.md

@ -12,6 +12,7 @@ This living document details our plans for `up`. If you would like to request fe
- [ ] Option for HTTPS.
- MUST add a `-s` flag to enable HTTPS config.
- MAY add a `-c` flag to use `certbot` to automatically generate certificates using letsencrypt.
- [ ] MAY make `up` work on Windows.
Want more features? Raise an [issue!](https://github.com/codefeathers/up-serve/issues)

40
actions/createProxyServer.js

@ -1,23 +1,29 @@
var fs = require('fs-extra')
var shell = require('shelljs')
var npath = require('../util/nginxPath')
var conf = require('../util/nginxConf')
function createProxyServer(domain, inPort) {
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" +
" proxy_pass http://localhost:" + inPort + ";" + "\n" +
" proxy_http_version 1.1;" + "\n" +
" proxy_set_header Upgrade $http_upgrade;" + "\n" +
" proxy_set_header Connection 'upgrade';" + "\n" +
" proxy_set_header Host $host;" + "\n" +
" proxy_cache_bypass $http_upgrade;" + "\n" +
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" +
"}"
)
)
shell.mkdir('-p', npath.enabledSites())
shell.ln('-sf', conf(npath.availableSites(), domain), conf(npath.enabledSites(), domain))
}
module.exports = createProxyServer

38
index.js

@ -5,13 +5,14 @@ var program = require('commander')
var shell = require('shelljs')
var fs = require('fs-extra')
var chalk = require('chalk')
var validator = require('validator')
var validate = require('./util/validate')
//var validator = require('validator')
// Requiring Actions
var createProxyServer = require('./actions/createProxyServer')
// Using Validator
var isDomain = validator.isFQDN
// var isDomain = validator.isFQDN
/* Uncomment in Production!
@ -29,6 +30,7 @@ if (!shell.which('nginx')) {
shell.echo('I need nginx to work. Install nginx first. https://nginx.org/')
shell.exit(1)
}
*/
program
@ -37,32 +39,32 @@ program
program
.command('static <domain> [outPort]')
.description('Create a static server at this folder.')
.action(function(domain, outPort="") {
if(!isDomain(domain)) console.log('\nDomain is not valid. Please use a valid domain name.')
// Stuff happens here
.action(function(domain, outPort="80") {
// stuff happens
createStaticServer(domain, outPort)
})
program
.command('proxy <domain> <inPort> [outPort]')
.description('Create a proxy server, listening at port number.')
.action(function(domain, inPort, outPort = "") {
.action(function(domain, inPort, outPort = "80") {
/* Removing this to make it modular
var validInPort = /^\d+$/.test(inPort)
var validOutPort = /^\d+$/.test(outPort)
if(!isDomain(domain)) {
console.log('\nDomain is not valid. Please use a valid domain name.')
return; }
console.log(domainInvalidMsg)
return }
if(!validInPort || !validOutPort) {
// This part doesn't work yet.
if (!((validInPort > 0 && validInPort <= 65535) && (validOutPort > 0 && validOutPort <= 65535))) {
console.log('\nPort should be a number from 1 and 65535.')
return; }
console.log('\nPort should be a number.')
return; }
else {
createProxyServer(domain, inPort, outPort)
console.log('Done!')
}
console.log(portInvalidMsg[0])
return }
if(!((validInPort > 0 && validInPort <= 65535) && (validOutPort > 0 && validOutPort <= 65535))) {
console.log(portInvalidMsg[1])
return }
else { */
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!")
//}
})
program

18
package-lock.json

@ -36,19 +36,6 @@
"supports-color": "4.5.0"
}
},
"co": {
"version": "4.6.0",
"resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz",
"integrity": "sha1-bqa989hTrlTMuOR7+gvz+QMfsYQ="
},
"co-prompt": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/co-prompt/-/co-prompt-1.0.0.tgz",
"integrity": "sha1-+zcOntrEhXayenMv5dfyHZ/G5vY=",
"requires": {
"keypress": "0.2.1"
}
},
"color-convert": {
"version": "1.9.0",
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.0.tgz",
@ -142,11 +129,6 @@
"graceful-fs": "4.1.11"
}
},
"keypress": {
"version": "0.2.1",
"resolved": "https://registry.npmjs.org/keypress/-/keypress-0.2.1.tgz",
"integrity": "sha1-HoBFQlABjbrUw/6USX1uZ7YmnHc="
},
"minimatch": {
"version": "3.0.4",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz",

4
package.json

@ -20,7 +20,7 @@
"bin": {
"up": "./index.js"
},
"author": "Muthu Kumar (MKRhere)",
"author": "Muthu Kumar (@MKRhere)",
"license": "MIT",
"bugs": {
"url": "https://github.com/codefeathers/up-serve/issues"
@ -28,8 +28,6 @@
"homepage": "https://github.com/codefeathers/up-serve#readme",
"dependencies": {
"chalk": "^2.3.0",
"co": "^4.6.0",
"co-prompt": "^1.0.0",
"commander": "^2.11.0",
"fs-extra": "^4.0.2",
"shelljs": "^0.7.8",

5
util/nginxConf.js

@ -0,0 +1,5 @@
function conf(path, domain) {
return (path + domain + ".conf")
}
module.exports = conf

17
util/nginxPath.js

@ -0,0 +1,17 @@
function availableSites() {
var availableSites = "/etc/nginx/available-sites/"
return availableSites
}
function enabledSites() {
var enabledSites = "/etc/nginx/enabled-sites/"
return enabledSites
}
function conf(path, domain) {
return (path + domain + ".conf")
}
module.exports.conf = conf
module.exports.availableSites = availableSites
module.exports.enabledSites = enabledSites

39
util/validate.js

@ -0,0 +1,39 @@
var validator = require('validator')
// Using Validator
var isDomain = validator.isFQDN
function validate(domain, inPort, outPort = undefined) {
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+$/.test(inPort)
var validOutPort = /^\d+$/.test(outPort)
var isTrue
if (!isDomain(domain)) {
console.log(domainInvalidMsg)
return isTrue = false
}
if (typeof outPort == undefined) {
if (!validInPort) {
console.log(portInvalidMsg[0])
return isTrue = false
}
if (!(validInPort > 0 && validInPort <= 65535)) {
console.log(portInvalidMsg[1])
return isTrue = false
}
}
if (typeof outPort !== undefined) {
if (!validInPort || !validOutPort) {
console.log(portInvalidMsg[0])
return isTrue = false
}
if (!((validInPort > 0 && validInPort <= 65535) && (validOutPort > 0 && validOutPort <= 65535))) {
console.log(portInvalidMsg[1])
return isTrue = false
}
}
return isTrue
}
module.exports = validate
Loading…
Cancel
Save