check vpn in javascript

To check if a VPN is being used in JavaScript, we can use the WebRTC API to obtain the IP address and check if it matches the user's public IP address or not. Here's an example of how to do that:

index.tsx
function checkVPN() {
  return new Promise(async resolve => {
    const ip = await fetch('https://api.ipify.org?format=json')
      .then(response => response.json())
      .then(data => data.ip)
      .catch(() => '');
    const rtc = new RTCPeerConnection({iceServers: []});
    rtc.createDataChannel('', {reliable: false});
    rtc.onicecandidate = e => resolve(Boolean(e.candidate && e.candidate.candidate.indexOf(ip) < 0));
    rtc.createOffer(offer => rtc.setLocalDescription(offer), () => resolve(false));
  });
}

checkVPN().then(result => console.log(result));
570 chars
15 lines

This code fetches the user's public IP address using an API and creates an instance of the RTCPeerConnection object. It then creates a data channel and listens for ICE candidates. If an ICE candidate is found, we check if its IP address matches the user's public IP address. If it doesn't match, it means that a VPN is being used.

Note that this method of checking for VPN usage is not foolproof and can be circumvented by VPN providers.

gistlibby LogSnag