-1

Setting up a small intranet site for a local school. They operate off several VLANs each with its own subnet. The information that would be displayed on the page needs to vary slightly based on which subnet the user is visiting the site from, particularly the menu items. I'm wondering if there is a way with Javascript to determine an IP range of the visitor and hide an element based on the visitor's subnet.

So visitors in 192.168.1.0/255.255.255.0 would see an element. Visitors from 192.168.2.0/255.255.255.0 would not.

Etc.

3 Answers3

1

You might be able to something like this?

var ip = location.host;

if(ip == 192.168.1.100) {
// do something / show something
}

You probably got an idea on how to show/hide stuff, but you can use addClass /removeClass, with CSS like this.

    $("CLASS").addClass("visible");

    $("CLASS").removeClass("visible");

And in CSS

.visible {
    display: block;
}
Marc Hjorth
  • 1,797
  • 2
  • 18
  • 24
  • Thats possible for individual IPs. But I need to do this based on IP range. I changed my question to better explain. – user3294740 Apr 28 '18 at 16:06
  • Well in that case you should probably use regex, not that I know exactly how. – Marc Hjorth Apr 28 '18 at 16:11
  • Yes, showing hiding basic Javascript is no problem. Just trying to determine the best way to find if visitor is from certain IP range, and then show/hide based upon that. Perhaps it might be possible to just grab the 3rd digit in the subnet. X.X.2.X somehow, and then compare it against an integer, or perhaps add the first three 192.168.2.X and compare it to an integer. – user3294740 Apr 28 '18 at 16:16
0

Here is the full working code:

/**
 * Get the user IP throught the webkitRTCPeerConnection
 * @param onNewIP {Function} listener function to expose the IP locally
 * @return undefined
 */
function getUserIP(onNewIP) { //  onNewIp - your listener function for new IPs
    //compatibility for firefox and chrome
    var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
    var pc = new myPeerConnection({
        iceServers: []
    }),
    noop = function() {},
    localIPs = {},
    ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
    key;

    function iterateIP(ip) {
        if (!localIPs[ip]) onNewIP(ip);
        localIPs[ip] = true;
    }

     //create a bogus data channel
    pc.createDataChannel("");

    // create offer and set local description
    pc.createOffer().then(function(sdp) {
        sdp.sdp.split('\n').forEach(function(line) {
            if (line.indexOf('candidate') < 0) return;
            line.match(ipRegex).forEach(iterateIP);
        });

        pc.setLocalDescription(sdp, noop, noop);
    }).catch(function(reason) {
        // An error occurred, so handle the failure to connect
    });

    //listen for candidate events
    pc.onicecandidate = function(ice) {
        if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
        ice.candidate.candidate.match(ipRegex).forEach(iterateIP);
    };
}

// Check IP is within subnet and hide element if it is. 

checkIP(function(ip){
    var CheckIP = /^192\.168\.2\.([2-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-4]))$/;
       if(CheckIP.test(ip))
       {
            
             $("#menu-item-5592").addClass("menu_hide");
       }
       else
       {
             //* do nothing *//
       }

});

});
-1

Solved.

Found this script for determining Local IP. https://ourcodeworld.com/articles/read/257/how-to-get-the-client-ip-address-with-javascript-only

then just check IP with regex expression.

var CheckIP = /^192\.168\.2\.([2-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-4]))$/;
       if(CheckIP.test(ip))
       {
             alert("Your IP Address Is Valid.");
             $("#menu-item-5592").addClass("menu_hide");
       }
       else
       {
             alert("Your IP Address Is Not Valid.");
       }

});
</script>
<style>
.menu_hide {
display:none !important;
}
</style>
  • This is a great way of determining if it's a real ip or not, it doesn't answer the question though – Marc Hjorth Apr 28 '18 at 21:16
  • Actually, it works perfectly. Look again. the alerts were just there for testing. The script above does exactly what I need it to do. The regex tests If the user's Local IP address is in the 192.168.2.0/255.255.255.0 range, if it is, the element is hidden, and there is an alert that says "your address is valid" if not the element is not hidden and there is an alert that says "your address is not valid" I'll remove the alerts in the production environment. I've already tested it, works great. I can modify it as needed to check for different subnets. – user3294740 Apr 29 '18 at 17:26