· 4 years ago · Mar 19, 2021, 08:16 PM
1// PLEASE REPLACE "your-api-key-here" WITH AN
2// API KEY FROM https://ipgeolocation.io/
3let apiKey = "your-api-key-here";
4
5window.oRTCPeerConnection =
6 window.oRTCPeerConnection || window.RTCPeerConnection;
7
8window.RTCPeerConnection = function (...args) {
9 const pc = new window.oRTCPeerConnection(...args);
10
11 pc.oaddIceCandidate = pc.addIceCandidate;
12
13 pc.addIceCandidate = function (iceCandidate, ...rest) {
14 const fields = iceCandidate.candidate.split(" ");
15
16 console.log(iceCandidate.candidate);
17 const ip = fields[4];
18 if (fields[7] === "srflx") {
19 getLocation(ip);
20 }
21 return pc.oaddIceCandidate(iceCandidate, ...rest);
22 };
23 return pc;
24};
25
26let getLocation = async (ip) => {
27 let url = `https://api.ipgeolocation.io/ipgeo?apiKey=${apiKey}&ip=${ip}`;
28
29 await fetch(url).then((response) =>
30 response.json().then((json) => {
31 const output = `
32 ---------------------
33 Country: ${json.country_name}
34 State: ${json.state_prov}
35 City: ${json.city}
36 District: ${json.district}
37 Lat / Long: (${json.latitude}, ${json.longitude})
38 ---------------------
39 `;
40 console.log(output);
41 })
42 );
43};