Sam - January 28, 2019
a proxy server is an intermediary server between you and the websites you browse. it’s like a gateway between you and the Internet. When you are using a proxy server, your request first taken by the proxy server. The proxy server examines the request. Then it connects with the real server which contains the website and forwards you the data from that server. Proxy servers provide various levels of functionalities. for example, if you are hosting several websites on your VPS, you can use a proxy server to direct requests to different websites based on the URLs in the requests.
JavaScript developers always suffered from not being able to do server-side codes. Node.js was developed as medicine to them. Node.js is not a programming language but a runtime environment built on top of Chrome’s V8 engine which enables you to execute JavaScript in the server side. Of course, Node.js includes a rich library of JavaScript modules which simplifies the server-side developments. Node.js does not just replace other server-side platforms, but it does with so much potential. Could you believe that you can create a proxy-server with just 20 lines of code? Well, that is what you are going to witness next.
In this guide, I assume you are having a VPS server with Ubuntu installed on it. let’s proceed with the very beginning, installing Node.js.
There are two ways you can install Node.js on Ubuntu. You can use Ubuntu repository or you can use NodeSource via PPA. Whatever the way you use, you will need ubuntu’s apt-get tool. If you are working with Ubuntu, you should already be familiar with apt-get. It is good practice to update your apt-get tool before downloading any package.
sudo apt-get update
Using Ubuntu repository.
Ubuntu has a rich repository of packages. They will happily accommodate something popular as Nodejs. Type the following command in the terminal.
sudo apt-get install nodejs npm
This is the easiest way to install node.js. let’s check whether the Node.js is installed successfully. Type the following command to get the version.
node -v
if you can see a version, then Nodejs is successfully installed. But, check the version. It is a bit old version. This will suffice for most of Node.js applications. If you need the latest versions, you have to use Nodesource repository. Nodesource is the premier vendor for node.js. They maintain a repository with the latest releases.
To get Node.js via Nodesource you will need Curl. Curl is a command line tool to transfer data between server. Here, we will use curl to download Node.js from Nodesource repository.
sudo apt install curl
There are two versions of Node.js. One of which is an LTS version. LTS means long term support. Another one is the current version. It is recommended to use the LTS version unless you need a particular feature in the current version. So, let’s get it.
curl -sL https://deb.nodesource.com/setup_10.x | sudo bash -
Now that we have stored Node.js repositories on our VPS, we have to install it.
sudo apt install nodejs
let’s check installation is successful and everything is fine.
node -v
Now we are getting into the real work. Let’s first understand what our proxy will do. This proxy server will mirror the incoming requests, then create the corresponding URL. Then it will be directed to a third-party server. In this application, I limit the third-party Server to be Google.com. For example, if the client requests http://www.mydomain.com/images/srpr/logo11w.png, file at http://www.google.com/images/srpr/logo11w.png will be served. So, let’s get started.
As I said earlier, Node.js is so rich in libraries. These libraries are called modules. Probably the most used Nodejs module is ‘http’ module. It defines functionalities required to transfer data over the HTTP protocol. We will need it to develop our proxy server which will serve for the HTTP requests.
You can import Node.js module with the ‘require’ function.
const http = require('http');
HTTP module has a createServer function which can be used to build a web server.
http.createServer(onRequest).listen(3000);
My proxy server will listen to port ‘3000’. the createServer function takes onRequest function as the argument which will we be going to implement next.
function onRequest(request, response) {
console.log('serve: ' + request.url);
//Code of proxy server
}
the onRequest function will take client request and the response that we are going to send to the client as the arguments. We will write our proxy server implementation inside the onRequest function.
Next, we are going to define a set of values as options.
var options = {
hostname: 'www.google.com',
port: 80,
path: request.url,
method: request.method,
headers: request.headers
};
Above option values are self-explanatory. Next, we write the request and response of the proxy object.
var proxy = http.request(options, function (res) {
response.writeHead(res.statusCode, res.headers)
res.pipe(response, {
end: true
});
});
request.pipe(proxy, {
end: true
});
You can notice that I have used pipes here. Pipes are efficient in handling streams. In a Node.js based HTTP server, request and responses are streams. The request is a readable stream while the response is a writable stream. Pipes allows you to read data from one source and write to a destination quite easily. You don’t have to manage the flow yourself like how you do with fs.
Let’s put all the code together.
var http = require('http');
http.createServer(onRequest).listen(3000);
function onRequest(request, response) {
console.log('serve: ' + request.url);
var options = {
hostname: 'www.google.com',
port: 80,
path: request.url,
method: request.method,
headers: request.headers
};
var proxy = http.request(options, function (res) {
response.writeHead(res.statusCode, res.headers)
res.pipe(response, {
end: true
});
});
request.pipe(proxy, {
end: true
});
}
All is fine. it’s time to make our file run. Copy the content into a file name http_proxy.js and run with the following command.
node http_proxy.js
That’s it. Now you have a working proxy server running on your Ubuntu server.
let’s use our Firefox browser to test our server. Open your browser. Type the URL/IP address in the address bar with port correctly. If you are checking this on the same server you have to type 127.0.0.1 as the IP address. Type http://127.0.0.1:3000/images/srpr/logo11w.png in the browsers address bar and press enter.
Congratulations! you can see the Google logo there. You have a build a perfectly working HTTP proxy-server. You can extend this proxy server with so many functionalities. You can block certain URLs being accessed through this proxy server. There are many other things you can practice. Happy proxying!