diff --git a/package-lock.json b/package-lock.json index ab09e46..5180870 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,7 +16,7 @@ "@testing-library/user-event": "^12.6.3", "@types/ip": "^1.1.3", "@types/jest": "^26.0.20", - "@types/node": "^12.19.15", + "@types/node": "^12.20.55", "@types/react": "^18.0.17", "@types/react-dom": "^18.0.6", "@types/react-router": "^5.1.11", @@ -3864,7 +3864,8 @@ "node_modules/@types/node": { "version": "12.20.55", "resolved": "https://registry.npmjs.org/@types/node/-/node-12.20.55.tgz", - "integrity": "sha512-J8xLz7q2OFulZ2cyGTLE1TbbZcjpno7FaN6zdJNrgAdrJ+DZzh/uFR6YrTb4C+nXakvud8Q4+rbhoIWlYQbUFQ==" + "integrity": "sha512-J8xLz7q2OFulZ2cyGTLE1TbbZcjpno7FaN6zdJNrgAdrJ+DZzh/uFR6YrTb4C+nXakvud8Q4+rbhoIWlYQbUFQ==", + "license": "MIT" }, "node_modules/@types/node-forge": { "version": "1.3.11", diff --git a/package.json b/package.json index 9c55f95..3d7d983 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "@testing-library/user-event": "^12.6.3", "@types/ip": "^1.1.3", "@types/jest": "^26.0.20", - "@types/node": "^12.19.15", + "@types/node": "^12.20.55", "@types/react": "^18.0.17", "@types/react-dom": "^18.0.6", "@types/react-router": "^5.1.11", diff --git a/src/components/ExploreContainer.tsx b/src/components/ExploreContainer.tsx index 68d8d60..e3b2f3b 100644 --- a/src/components/ExploreContainer.tsx +++ b/src/components/ExploreContainer.tsx @@ -7,7 +7,7 @@ import { } from '@ionic/react'; import { call as callIcon, close as hangUpIcon, shirt } from 'ionicons/icons'; import Peer, { MediaConnection } from 'peerjs'; -import * as os from 'os'; +import os from 'os'; const ExploreContainer: React.FC = () => { const [callString, setCallString] = useState(""); @@ -22,30 +22,45 @@ const ExploreContainer: React.FC = () => { const [callInterval, setCallInterval] = useState(null); const [currentCallId, setCurrentCallId] = useState(null); const [showCopyNotification, setShowCopyNotification] = useState(false); + const [ipv5, setIpv5] = useState('') - - function getLocalIPAddress() { - const networkInterfaces = os.networkInterfaces(); - for (const interfaceName of Object.keys(networkInterfaces)) { - const networkInterface = networkInterfaces[interfaceName]; - if (networkInterface) { - for (const net of networkInterface) { - if (net.family === 'IPv4' && !net.internal) { - console.log(`Your local IP Address is: ${net.address}`); - return net.address; - } - } - } - } - return ''; - } + const getLocalIP = async () => { + return new Promise((resolve, reject) => { + const peerConnection = new RTCPeerConnection(); + // const noop = () => {}; - getLocalIPAddress(); + // Обрабатываем события ICE-кандидатов, которые будут содержать IP-адрес + peerConnection.onicecandidate = (event) => { + if (event.candidate) { + const ip = event.candidate.candidate.split(" ")[4]; + resolve(ip); + // peerConnection.onicecandidate = noop; // Завершаем обработку + } + }; + // Создаём пустой канал данных для инициирования связи + peerConnection.createDataChannel(""); + peerConnection.createOffer() + .then((offer) => peerConnection.setLocalDescription(offer)) + .catch((err) => reject(err)); + }); + }; + + // Использование функции + getLocalIP().then((ip) => { + console.log("Local IP Address:", ip); + setIpv5(ip as string) + }).catch((err) => { + setIpv5("error") + console.error("Error getting IP address:", err); + }); + + useEffect(() => { const initializePeer = () => { - const newPeer = new Peer('',{ debug: 3 }); + console.log(ipv5) + const newPeer = new Peer( ipv5 ,{ debug: 3 }); // const newPeer = new Peer(,{ debug: 3 }); setPeer(newPeer);