NeoProxy
NeoProxy is a node.js proxy server based on express.js and request modules, that can be customized with plugins.
For the cli with basic proxy operations you can use NeoProxy-cli.
Installation
API
At your project's home directory:
npm install --save neoproxy
below)
Plugins (examplesPlugins are used to customize neoproxy behavior. It is called on every request in series. It is a json object consisting of two functions
- condition: this is an optional function that returns true or false specifying whether this plugin will be used for the request.
- middleware: this is the function that have the real logic, it is called once for each request that passes the request. It returns a function that is called for every chunk of data that is available in the response. the latter function just pushes the data (or any other thing) to the next plugin until it is transmitted to the client. Each plugin is considered finished when it pushes null to the next plugin.
Plugins API
Plugin object
It consists of two attributes as explained above
- condition
- arguments:
- req: request came from the client
- res: response came from the server (if it exists)
- returns:
- boolean: weather or not this plugin will be used for this request
- arguments:
- middleware
- arguments:
- req: request came from the client
- res: response came from the server (if it exists)
- push: the function to push each chunk of data when it is finished.
- returns:
- a function: accepts the chunk of data that is pushed to this plugin, or null as the last chunk in the request.
- arguments:
Request object:
It is an express request object with added attributes
-
req.is_resolved
: Specifies if the req is resolved or not, as the request is used on every plugin twice, once before it's resolved and once after. -
req.retry()
: It's a function that to be called if the plugin wants the proxy server to resolve the request again, It can be useful in cases of changing the request based on the response. this method should be called before this plugin call push for this request. -
req.body
: By default, this contains the body of the request that came from the client as a buffer, you can use any body parser middleware that is supported by express instead, for exampleproxy.app.use(require('body-parser').urlencoded({ extended: false }))
.if it is a buffer or a string it will be passed to the server without modification, else it will be stringified as a json object using
JSON.stringify
. -
req.abandon()
: It's a function that tells the proxy server to abandon the connection.
Response object
It is a response object of the module require, although you shouldn't unless you know what you are doing bind it to another stream or add an event listener to it, as this is done internally in the proxy.
The proxy instance
The proxy server instance has three methods
use
: this is where you can attach plugins to the proxy using eitherproxy.use({condition:function(){/* condition */},middleware:function(){/* middleware */}})
proxy.use({middleware:function(){/* middleware */}})
proxy.use(function(){/* condition */},function(){/* middleware */})
proxy.use(function(){/* middleware */})
app
: this is the express instance that is used internally within the app, so ypu can add express middle-wares you like.listen
: this is the function called to start the proxy server it takes two arguments port, callback.
Examples
Please note that the first example is the only complete example, other examples will only contain the plugin.
-
Just a proxy
var Proxy = ;var proxy = Proxy;//proxy.use(plugin1);//proxy.use(condition2,middleware2);//proxy.use(middleware3);var server = proxy; -
Log urls to the standard output
proxy; -
Abandoning connections from a specific ip
proxy; -
Just passes the data after it is resolved
proxy; -
Calculating the traffic used by every ip
var traffic = {};proxy;Or simply (but you have to make sure no following plugin will call req.retry)
var traffic = {};proxy; -
Replace something in the body of the response
var defaults = Proxydefaults;// this builtin plugin will concat the whole response in a single chunkproxyproxy; -
Abort the connection if a string was found in the response.
var defaults = Proxydefaults;proxyproxy; -
Block requests to a particular site
proxy; -
Remove or alter a specific header from request or response
proxy;proxy; -
Limit the data passed every second to 10kB to mock slow internet connections. (Please note that this snippet can be used for test purposes only)
var INTERVAL = 1000; // 1svar LIMIT = 1024*10; // 10kBproxy
Contribution
If you like contributing to this project, I am waiting for the pull request. :)
Todo
- Error handling.
- Testing.
- Handling file uploads.
- Accepting optional parameters.
- Seperating public and private methods.
License
MIT © 2016 Mohamed Elawadi