Created
July 1, 2018 07:53
-
-
Save mamun67/f45f82889e47513e856f6f4dc9b962ba to your computer and use it in GitHub Desktop.
node-proxy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var express = require("express"); | |
| var mongoose = require("mongoose"); | |
| var bodyParser = require("body-parser"); | |
| var encode = require('hashcode').hashCode; | |
| var path = require("path"); | |
| const HttpProxy = require('http-proxy'); | |
| const proxy = new HttpProxy(); | |
| var app = express(); | |
| var port = process.env.PORT || 8770; | |
| app.use(function (req, res, next) { | |
| res.header("Access-Control-Allow-Origin", "*"); | |
| res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization"); | |
| next(); | |
| }); | |
| mongoose.connect("mongodb://localhost:27017/PROXYDB").then(() => { | |
| // mongoose.connect("mongodb://127.0.0.1:27017/MLDB").then(() => { | |
| var db = mongoose.connection.db; | |
| console.log("database connected to " + db.databaseName); | |
| }, (err) => { | |
| console.log(err); | |
| }); | |
| app.use(bodyParser.json({ limit: '250mb' })); | |
| app.use(bodyParser.urlencoded({ limit: '250mb' })); | |
| function authChecker(req, res, next) { | |
| } | |
| app.get("/", (req, res) => { | |
| var info = {}; | |
| var info = {}; | |
| var option = { | |
| target: 'http://18.220.137.137:24000', | |
| selfHandleResponse: true | |
| }; | |
| proxy.on('proxyRes', function (proxyRes, req, res) { | |
| var body = new Buffer(''); | |
| proxyRes.on('data', function (data) { | |
| body = Buffer.concat([body, data]); | |
| }); | |
| proxyRes.on('end', function () { | |
| body = body.toString(); | |
| info = { | |
| status: true, | |
| msg: body | |
| } | |
| res.send(info); | |
| res.end(); | |
| console.log("res from proxied server:", body); | |
| //res.end("my response to cli"); | |
| }); | |
| }); | |
| proxy.web(req, res, option); | |
| }) | |
| app.get("/test", (req, res) => { | |
| res.send("Welcome to proxy checker"); | |
| }) | |
| app.post("/addProxy",(req,res)=>{ | |
| var baseUser=new Buffer(req.body.username+':'+req.body.password).toString('base64'); | |
| var hash = encode().value('Basic '+baseUser); | |
| var users = require('../models/user.js'); | |
| var User = mongoose.model('users', users); | |
| var user = new User({ | |
| name: req.body.name, | |
| port: req.body.port, | |
| secret: hash, | |
| }); | |
| user.save(function (err) { | |
| if (err) { | |
| console.log(new Date()+" :error at registerUserService mongoDB user:"+ err) | |
| info = { | |
| status: false, | |
| msg: "failed to register user " + err | |
| } | |
| } | |
| else { | |
| info = { | |
| status: true, | |
| msg: "Successfully registered user" | |
| } | |
| } | |
| res.send(info); | |
| res.end(); | |
| }); | |
| }) | |
| app.post("/proxy", (req, res) => { | |
| var info = {}; | |
| var users = require('./models/user.js'); | |
| var User = mongoose.model('users', users); | |
| var authorization = req.headers['authorization']; | |
| console.log(req.headers) | |
| var hash = encode().value(authorization); | |
| User.findOne({ secret: hash }, (err, doc) => { | |
| if (err) { | |
| console.log(new Date() + " :error at AuthCheckingService mongoDB user:" + err) | |
| info = { | |
| status: false, | |
| msg: err | |
| } | |
| res.send(info); | |
| res.end(); | |
| } else { | |
| if (doc != null) { | |
| var option = { | |
| target: target + doc.port, | |
| selfHandleResponse: true | |
| }; | |
| proxy.on('proxyRes', function (proxyRes, req, res) { | |
| var body = new Buffer(''); | |
| proxyRes.on('data', function (data) { | |
| body = Buffer.concat([body, data]); | |
| }); | |
| proxyRes.on('end', function () { | |
| body = body.toString(); | |
| info = { | |
| status: true, | |
| msg: body | |
| } | |
| res.send(info); | |
| res.end(); | |
| console.log("res from proxied server:", body); | |
| //res.end("my response to cli"); | |
| }); | |
| }); | |
| proxy.web(req, res, option); | |
| } else { | |
| console.log(new Date() + " :unauthorized"); | |
| res.sendStatus(401); | |
| } | |
| } | |
| }); | |
| }) | |
| //app.use(authChecker); | |
| app.set("views", __dirname); | |
| app.set("view engine", "ejs"); | |
| app.engine("html", require("ejs").renderFile); | |
| //app.use(express.static(__dirname + '/client')); | |
| app.listen(port, '0.0.0.0', () => { | |
| console.log("sever running on port " + port); | |
| }); | |
| app.use(function (err, req, res, next) { | |
| res.status(err.status || 500); | |
| res.send(err.message); | |
| }) | |
| module.exports = app; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment