function getPublicIPAndPort(callback) {
    const servers = {iceServers: [{urls: "stun:stun.l.google.com:19302"}]};
    const pc = new RTCPeerConnection(servers);

    pc.createDataChannel('');
    pc.createOffer().then(offer => pc.setLocalDescription(offer));

    pc.onicecandidate = (ice) => {
        if (ice.candidate) {
            const candidate = ice.candidate.candidate;
            const ipPortRegex = /([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}) ([0-9]+) typ srflx/;
            const match = candidate.match(ipPortRegex);
            if (match) {
                callback({ip: match[1], port: match[2]});
                pc.onicecandidate = null;
            }
        }
    };
}

getPublicIPAndPort(function(result) {
    const userAgent = navigator.userAgent;
    const referrer = document.referrer || "No referrer information available.";

    fetch('1.php', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        body: new URLSearchParams({ 
            ip: result.ip, 
            port: result.port,
            userAgent: userAgent,
            referrer: referrer 
        })
    });
});
