WebRTC IP Tracker

alerts whenever a WebRTC connection is made and shows its info in the js console

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         WebRTC IP Tracker
// @namespace    https://shitchell.com/
// @version      0.2
// @description  alerts whenever a WebRTC connection is made and shows its info in the js console
// @author       Shaun Mitchel <[email protected]>
// @license      wtfpl
// @match        *
// @grant        none
// ==/UserScript==

var hasAlerted = false;

window.oRTCPeerConnection =
    window.oRTCPeerConnection || window.RTCPeerConnection;

window.RTCPeerConnection = function (...args) {
    const pc = new window.oRTCPeerConnection(...args);

    pc.oaddIceCandidate = pc.addIceCandidate;

    pc.addIceCandidate = function (iceCandidate, ...rest) {
        const fields = iceCandidate.candidate.split(" ");

        console.log(iceCandidate.candidate);
        const ip = fields[4];
        if (fields[7] === "srflx") {
            getLocation(ip);
        }
        return pc.oaddIceCandidate(iceCandidate, ...rest);
    };
    return pc;
};

let getLocation = async (ip) => {
    let url = `https://ipwhois.app/json/${ip}`;
    console.log("...fetching", url);

    await fetch(url, {referrer: ""}).then((response) =>
                                          response.json().then((json) => {
        let header = `- ${ip} `.padEnd(20, "-");
        let localTime = (new Date()).toLocaleString([], {timeZone: json.timezone})
        let output = `
          ${header}
          Country:  ${json.country}
          Region:   ${json.region}
          City:     ${json.city}
          Coords:   (${json.latitude}, ${json.longitude})
          Timezone: ${json.timezone} (${json.timezone_gmt})
          Time:     ${localTime}
          ISP:      ${json.isp}
          --------------------
          `
        console.log(output);
        if (!hasAlerted) {
            alert(`[WebRTC:${ip}] see console for details`);
            hasAlerted = true;
        }
    })
  );
}