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