import env from "../../env"; const getIP = () => { return new Promise((res, rej) => { let timedOut = false const peerConnection = new RTCPeerConnection({ iceServers: [ { "urls": ['turn:ideadesignmedia.com:3331'], username: 'IDM', credential: 'TURNME' }, { "urls": ['turn:ideadesignmedia.com:3332'], username: 'IDM', credential: 'TURNME' } , { urls: "stun:stun.l.google.com:19302" }] }); peerConnection.createDataChannel(""); // Create a data channel peerConnection.createOffer().then(offer => peerConnection.setLocalDescription(offer)); const timeout = setTimeout(() => { timedOut = true peerConnection.close(); rej(new Error('Timeout')) }, 30000) peerConnection.onicecandidate = (event) => { if (event && event.candidate && event.candidate.candidate) { const candidate = event.candidate.candidate; const ipRegex = /([0-9]{1,3}\.){3}[0-9]{1,3}/; const ipAddress = candidate.match(ipRegex); if (ipAddress) { if (!timedOut) { clearTimeout(timeout) peerConnection.close(); res(ipAddress[0]); } } } }; }) } export function getZip() { return getIP().then(ip => { if (ip) { return fetch(`${env.API_HOST}/get-zip?ip=${ip}`) .then(response => response.json()) .then(data => { return data.zip }) } }) }