1

I am trying to get the mac-address of client's machine whenever they click the button. (This is for office purpose only).

I have read multiple SO answer and in that they mentioned we can't do it using JavaScript but still am not satisfied with answer.

However I still managed to get client IP address using the below code but as the IP will be dynamic therefore I have to check for MAC address.

Code to get the client IP address .

 ngOnInit(){
         this.getUserIP(function(ip){
            console.log(ip)
        });
    }

getUserIP(onNewIP) { //  onNewIp - your listener function for new IPs
            //compatibility for firefox and chrome
            // console.log("onNewIP" , onNewIP);
            // (<any>window).mozRTCPeerConnection 
            var myPeerConnection = (<any>window).RTCPeerConnection || (<any>window).mozRTCPeerConnection  || (<any>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(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);
            }, noop); 

            //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);
            };
        }

Why I want to get MAC address

When the user signup in my software , later I want that user to make request from that machine only. Therefore I need the MAC address. Is this not possible?

halfer
  • 19,824
  • 17
  • 99
  • 186
Pushprajsinh Chudasama
  • 7,772
  • 4
  • 20
  • 43
  • 3
    You cannot get the MAC address from the machine where the browser is running on by executing Javascript (and thus also Angular.js) inside the browser. Access to this information is restricted due to the sandbox in which Javascript is executed. – Sunny Parekh Oct 15 '19 at 11:21
  • This is the same answer provided in one of the SO answer . My another question is , there is node package like https://www.npmjs.com/package/getmac . Then how they make it possible and how to use it ? @SunnyParekh – Pushprajsinh Chudasama Oct 15 '19 at 11:23
  • 2
    as far as I know, you can't read the mac address of the computer from js, One possible solution is to generate an encrypted UUID from your server and sign it, you can store that UUID in a cookie and send it to your server on each request – Daniel Oct 15 '19 at 11:30
  • 1
    for your second question, npmjs.com/package/getmac is for node.js, node is an interpreter of js, but node != js. https://www.educba.com/javascript-vs-node-js/ – Daniel Oct 15 '19 at 11:34
  • 3
    Your package _getmac_, first line's description : `Get the mac address of the current machine you are on`. It does __NOT__ get the Mac Address of a client's machine. – Florian Oct 15 '19 at 11:40
  • But i want see that does that user make the request from the machine that is allocated to him/her . Using UUID can it possible . If it is , please can you explain it in detail? @Dan – Pushprajsinh Chudasama Oct 15 '19 at 11:41
  • Ohh yes . Thank for correcting @Florian – Pushprajsinh Chudasama Oct 15 '19 at 11:41
  • Are you asking how to generate a UUID? @PushprajsinhChudasama – Daniel Oct 15 '19 at 11:56
  • UUID is also know as _id ? is that only UUID ? @Dan – Pushprajsinh Chudasama Oct 15 '19 at 12:02
  • https://en.wikipedia.org/wiki/Universally_unique_identifier – Daniel Oct 15 '19 at 13:58
  • You can call it what you want, it's just something that identifies something unequivocally – Daniel Oct 15 '19 at 13:59
  • That will not help me . @Dan – Pushprajsinh Chudasama Oct 16 '19 at 07:01
  • check out this link https://stackoverflow.com/questions/49692073/how-can-i-get-a-unique-device-id-in-javascript – Daniel Oct 16 '19 at 07:16
  • The first thing that came to mind when I read the question was to create the application in Electron, then you can use some node module or similar to read system information. – John Nov 06 '19 at 11:55

0 Answers0