How to prevent direct access to IP Express node.js - node.js

I can't find a resource for this anywhere online, all I see is references for nginx.
I need help with this quickly as my server is live with users accessing it and somehow google indexed my ip address and users are accessing my site through my ip.
I plan to migrate servers tonight and am aware of why my ip was indexed, but in the meantime need a method to prevent direct access via my ip.
This obviously isn't working, and don't have much room to test, unless I stop the server and kick all of my users off for an extended period of time:
app.get('myiphere', function(req, res){
res.redirect('domain.com');
});

You can implement an application-level middleware which checks that a request host-name isn't anything else but your domain. That way an access to your with an IP address wouldn't cause a processing (on application level).
const SITE_ADDRESS = 'yourwebsite.com';
app.use((req,res,next) => {
if (req.hostname.includes(SITE_ADDRESS))
next();
else
res.status(403).end(`Access with ${req.hostname} is restricted. Use ${SITE_ADDRESS} instead.`);
});

To prevent direct access to your site from IP you can set the loopback IP this way:
app.listen(3000, '127.0.0.1', () => console.log('Server running on port 3000'))

Prevent indexing by creating a robots.txt at your server root directory. See https://stackoverflow.com/a/390379/11191351

Related

dns server configuration for node.js

I have developed server application in node.js. Right now I access the application using 128.1.1.5:3000. But i want to use a domain name like abc.net to access the application. How can I do this. Please suggest.
To configure DNS on your local app,you need to do following configuration.
Make an entry of this DNS example abc.net as a host instead of local host while setting up your node server where you are mentioning the localhost host and port detail eg. in app.js file.
Example
const http = require('http');
const hostname = 'abc.net';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Now open command prompt and type
ipconfig -all
It will list all your IPs. Select the ip of your machine which is preferred one.mostly you can locate this ip by finding the ip which is followed by (preferred) keyword in command prompt.
Now copy this IP address and make an entry of this in system host file.Make sure you have an admin rights to make changes in this file.
Path of host file in Windows
C:\Windows\System32\drivers\etc\hosts
Edit this file and scroll to the end and press Enter to copy the ip address corresponding to the DNS which you have configured in node js application as shown below in new line.
IPaddress(fetched in step 3)
abc.net
i.e ipaddress then give space then dns name
Save the file.
Start your node application.
Now try hitting your api from the url abc.net:port/api
You will need a domain that you can edit the DNS settings on and add an A record that is configured to your server's external IP address
then you can access your domain with the port attached
example: mydomain.com:5000
you should refer to your domain record provider's documentation on how to do this.
Beyond that, you may encounter firewall settings, port settings, and possible HTTPS certificates issues but these are separate topics each.

How can I get the correct ip address of the visitor in node.js? (I tried node-ip and it doesn't work)

I'm using a node ip taken from here: https://github.com/indutny/node-ip
In my webservice I did a simple thing:
var ip = require('ip');
module.exports = function(app) {
app.get('/gps', function (req, res) {
console.log(ip.address());
}
}
I deployed it to my amazon aws account and now whoever enters the page - I constantly see the same ip address in my console log - 172.31.46.96. I tried to check what is this ip (possible option is that it is related to my amazon aws service?), but who.is does not bring the answer.
How should I change my code to see every visitor's ip address instead?
You're most likely getting an IP of an internal load balancer/proxy and you'll need to configure express to handle that.
This is a good place to start.
Use req.connection.remoteAddress to get the ip of your user.

Redirect localhost url in node.js and express

i have a project where users connect to my router, and then enter the address http://192.168.1.50:9091 into the address bar to connect to a page. I would like there to be a way to enter something easier than this long ip address.
here is what's happening so far:
var express = require('express');
var app = express()
, http = require('http')
, server = http.createServer(app)
, io = require('socket.io').listen(server)
, fs = require('fs')
server.listen(9093);
var nRequest = 0;
var nConnexs = 0;
app.get('/', function (req, res) {
res.sendfile(__dirname + '/client_app.html');
});
app.get('/2', function (req, res) { res.sendfile(__dirname +
'/client_app2.html'); });
I am not sure what you mean by:
i have a project where users connect to my router
The Node.js application is sitting on your machine. Until somebody accesses your machine (through IP), the application is never accessed.
As such, you must put something between the user and the application that will direct the user to the IP. For real websites, this is accomplished using DNS. You register a domain name and tell the DNS service the IP of the machine to redirect to.
If you have control over the router (internal network, etc.), you can map an alias to your IP address. It all depends on whether your router runs a DNS server or supports DNSMasq. You will need to check your router manufacturer/model/etc.
Finally, if there are a small number of users that will be accessing your website, you could always have them use the hosts file to map a name to the IP. The location of this file is dependent upon the operating system; just Google: edit hosts file
I assume users are initially connecting to your router's public ip address or a DNS representation of that. If that's the case, you can usually configure your router to do port forwarding such that incoming requests on a particular port are automatically routed to a particular private IP address on your network. In that case, the users would connect to your router, but that request would be automatically forwarded to your internal node server and thus the users would end up directly connected to your node server.
This type of configuration comes with the usual security warnings. Doing this means that your node server must be properly configured against internet attack and your node app must be written with appropriately security precautions - particularly because any compromise to the computer the node server is running on has access to your internal network.
If you show us what users are initially connecting to when they connect to your router, we could offer a more explicit example of how you could configure the router to take advantage of port forwarding.

Node.js vhost as user

Very simply, I'm currently using Express's vhost method to route requests to the appropriate script given a domain name. I really like this route since it means I don't need to have separate ports being listened to by separate node.js instances for each virtual host script and I also don't need a process for each virtual host. However, there is a glaring flaw for me using this method. By using this method, anything in the vhost server has root privileges and not merely the privileges of the user whose script it is. I need know find some way of sandboxing or otherwise running the vhost server as the user that it belongs too. Needless to say, I can't have lower privileged users on the server with access to the root.
TL;DR, What method exists by which I can route requests to different domain name's associated apps without the need to designate ports of which the app would need to know and still disable the author of that script from having access beyond their own user account?
In my apps, I use Bouncy:
var bouncy = require( "bouncy" );
var server = bouncy(function( req, res, bounce ) {
var port;
var subdomain = req.headers.host.split( "." )[ 0 ];
switch ( subdomain ) {
case "xyz":
port = 4002;
break;
default:
port = 4001;
break;
}
bounce( port );
});
server.listen( 4000 );
This way, you may have various apps listening on different ports and on different processes. They all will be proxied to work under the port 4000 aswell, so:
xyz.localhost:4002 = xyz.localhost:4000
localhost:4001 = localhost:4000
I hope it helps ;)

headers.host gets hit by lots of domains that are not mine

I am getting many strange requests that have req.headers.host values that are not my domain.
var mc_domain = "mysubdomain.mydomain.com:8888";
var server = require('http').createServer(function (req, res) {
if (req.headers.host !== my_domain) {
console.log("not the host you are looking for " + req.headers.host);
res.end();
return;
}
});
server.listen("8888");
console output
not the host you are looking for abc.advertising.com
not the host you are looking for parkingaddress1.com
not the host you are looking for gotoinfo.info
...
What is going on, and what can I do to stop/reduce this? Is it just "welcome to the (wild) internet (west)", Or "You need a firewall", or some other foolishness.
Eihter someone's DNS is resolving the other domain name to your IP address or domain is not setup properly. You can use a tool like dig to check the DNS settings at the DNS servers responsible for those domains (you can get them using whois).
You can look at this as problem, or as an opportunity. If I were you, I'd create a landing page for those users and try to sell my product/service.

Resources