How to prevent executing some JS code in web from my workplace IPs/locations?

Viewed 683

I have some JS code in a webpage that is executed and sends some data gathered when certain events (such as changing pages) are done. I would like to exclude all (or almost all) of the devices in my workplace to execute this function, so as to not gather data that is made in work (99% of it is test data and garbage).

My idea is to use geolocation or IPs in order to exclude this devices (or this location) from executing the JS function, but I don't have a clue how I should do it.

I wouldn't like the webpage to show a popup for asking permission for geolocation, so I prefer to use IP location, but I don't know how that works exactly (once I have the IP, what is next?). I have seen some services but most of them are very limited in the number of requests made by hour/day.

Thank you and best regards.

3 Answers

To find the user location based on the ip, you'll need to use external services such as http://ip-api.com/, https://ipinfo.io or https://geoip-db.com.

Here is a simple js function to put in the console the user location and country.

function ipLookUp () {
  $.ajax('http://ip-api.com/json')
  .then(
      function success(response) {
          console.log('User\'s Location Data is ', response);
          console.log('User\'s Country', response.country);

      },

      function fail(data, status) {
          console.log('Request failed.  Returned status of',
                      status);
      }
  );
}
ipLookUp()

More informations : How To Detect The Location of Your Website’s Visitor Using JavaScript.

I recently discovered Ipregistry (https://ipregistry.co) for a similar use case as you. Here is a code snippet that allows filtering by IP address using the standard fetch API:

var companyIpAddresses = ['1.2.3.4', '2a01:e35:2f23:e3d0::1', '2.3.4.5'];

fetch('https://api.ipregistry.co/?key=tryout')
    .then(function (response) { return response.json(); })
    .then(function (payload) {
        var userIp = payload['ip'];
        if (companyIpAddresses.includes(userIp)) {
            // User is not allowed
        } else {
            // User is allowed
        }
    });

If your organization has its own Autonomous System (AS), you can filter based on its ASN using the field payload['connection']['asn']. However, using IP geolocation to perform the kind of filtering you mention will not be accurate enough. This applies whatever the service you are using.

If you mean work as in "other developers", why not define two environments (like in Angular with the prod environment).

  • When you publish your webpage you can publish it with the prod environment which has a flag sendDiagnostics set to true.
  • When you are developing you use the default environment with sendDiagnostics set to false.

To answer your question:

It is not possible to get the IP addresses location without using an external service (productively). These services just map IPs to locations and keep a database of those behind. If you don't want to do that manually, you need to use one of those services.

Related