1+ export function getLocalIP ( ) {
2+ return new Promise ( res => {
3+ const RTCPeerConnection = window . RTCPeerConnection || window . webkitRTCPeerConnection || window . mozRTCPeerConnection ;
4+ if ( ! RTCPeerConnection ) res ( null ) ;
5+
6+ const rtc = new RTCPeerConnection ( { iceServers : [ ] } ) ;
7+ rtc . createDataChannel ( '' , { reliable : false } ) ;
8+
9+ rtc . addEventListener ( "icecandidate" , evt => {
10+ if ( evt . candidate && evt . candidate . address ) {
11+ if ( evt . candidate . address . endsWith ( '.local' ) ) {
12+ alert ( `Disable WEB RTC anonymizer: ${ browser ( ) } :/` + `/flags/#enable-webrtc-hide-local-ips-with-mdns` ) ;
13+ } else {
14+ res ( evt . candidate . address ) ;
15+ }
16+ }
17+ res ( null ) ;
18+ } ) ;
19+
20+ rtc . createOffer ( ) . then ( offerDesc => rtc . setLocalDescription ( offerDesc ) ) ;
21+ } ) ;
22+ }
23+
24+ export function getIPs ( ip , netmask = 24 ) {
25+ let ip_a = ip . split ( '.' ) ;
26+ let sum_ip = ( ip_a [ 0 ] << 24 ) | ( ip_a [ 1 ] << 16 ) | ( ip_a [ 2 ] << 8 ) | ip_a [ 3 ] ;
27+ let cidr = Number ( netmask ) ;
28+ let mask = ~ ( 0xffffffff >>> cidr ) ;
29+ let network = 0 , broadcast = 0 , start_ip = 0 , end_ip = 0 ;
30+ if ( cidr === 32 ) {
31+ network = sum_ip ;
32+ broadcast = network ;
33+ start_ip = network ;
34+ end_ip = network ;
35+ } else {
36+ network = sum_ip & mask ;
37+ broadcast = network + ( ~ mask ) ;
38+ if ( cidr === 31 ) {
39+ start_ip = network ;
40+ end_ip = broadcast ;
41+ } else {
42+ start_ip = network + 1 ;
43+ end_ip = broadcast - 1 ;
44+ }
45+ }
46+ let ips = [ '192.168.4.1' ] ; // esp
47+ for ( let ip = start_ip ; ip <= end_ip ; ip ++ ) {
48+ ips . push ( `${ ( ip >>> 24 ) & 0xff } .${ ( ip >>> 16 ) & 0xff } .${ ( ip >>> 8 ) & 0xff } .${ ip & 0xff } ` ) ;
49+ }
50+ return ips ;
51+ }
52+
53+ export function getMaskList ( ) {
54+ const list = [ ] ;
55+ for ( let i = 0 ; i < 33 ; i ++ ) {
56+ let imask ;
57+ if ( i == 32 ) imask = 0xffffffff ;
58+ else imask = ~ ( 0xffffffff >>> i ) ;
59+ list . push ( `${ ( imask >>> 24 ) & 0xff } .${ ( imask >>> 16 ) & 0xff } .${ ( imask >>> 8 ) & 0xff } .${ imask & 0xff } ` ) ;
60+ }
61+ return list ;
62+ }
63+
64+ export function browser ( ) {
65+ if ( navigator . userAgent . includes ( "Opera" ) || navigator . userAgent . includes ( 'OPR' ) ) return 'opera' ;
66+ else if ( navigator . userAgent . includes ( "Edg" ) ) return 'edge' ;
67+ else if ( navigator . userAgent . includes ( "Chrome" ) ) return 'chrome' ;
68+ else if ( navigator . userAgent . includes ( "Safari" ) ) return 'safari' ;
69+ else if ( navigator . userAgent . includes ( "Firefox" ) ) return 'firefox' ;
70+ else if ( ( navigator . userAgent . includes ( "MSIE" ) ) || ( ! ! document . documentMode == true ) ) return 'IE' ;
71+ else return 'unknown' ;
72+ }
0 commit comments