#!/bin/sh # ===================================================================== # Projet : DARKWALL # Fichier : lib/ipNetworkMaskTools # Auteur : Nicolas "DarkHack" Chapuis (c) 2026 # ================== DARKHACK ================== # [ darkweb.fr - system online ] # Powered by Perplexity, your AI assistant # https://www.perplexity.ai # ============================================== # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ===================================================================== if ! type "shInclude" > /dev/null 2>&1; then racineSh="${racineSh:-/etc/darksh}"; . "$racineSh/lib/shTools"; shDebugEnable="1";fi rootDarkWall="${rootDarkWall:-/etc/darkwall}" [ ! -d "$rootDarkWall" ] && shDebug 0 "exit cause rootDarWall is not dir:$rootDarkWall" && exit 0 requiresCmd="" requiresCmd="printf timeout sleep kill cat echo sh rm mkdir grep md5sum" requiresCmd="${requiresCmd} cut tr rev sort uniq awk head" requiresCmd="${requiresCmd} printf expr fold seq" requiresCmd="${requiresCmd} ip iptables sed" shWhichCmd $requiresCmd >/dev/null shErrorOnMissing $( shWhichCmd $requiresCmd | $cmdTr '_' ' ' ) #requiresCmd="" requiresCmd="wget upnpc" shWhichCmd $requiresCmd >/dev/null shWarnOnMissing $( shWhichCmd $requiresCmd | $cmdTr '_' ' ' ) ipGetLocal() { local log="ipGetLocal:" for ip in $( iptoolsGetIps );do shDebugHidden 2 ""$log"test:$ip" >&2 if ipIsInNetwork $ip "10.0.0.0/8" ;then # is an local ip $cmdEcho $ip fi done } ipGetGlobal() { local log="ipGetGlobal:" # try get ip by http local ip="$( $cmdWget --no-check-certificate -O - -q -t 1 --timeout=10 --dns-timeout=10 --connect-timeout=10 --read-timeout=10 myip.darkweb.fr | $cmdTr -d '\t' | $cmdTr -d ' ' | $cmdTr -d '\n' )" if ipIsGlobal $ip;then $cmdEcho "$ip";else # try get ip by upnp local ip="$( $cmdUpnpc -l | $cmdGrep "ExternalIPAddress" | $cmdCut -d' ' -f3 | $cmdTr -d '\t' | $cmdTr -d ' ' | tr -d '\n')" if ipIsGlobal $ip; then $cmdEcho "$ip";else shDebugHidden 1 ""$log" no global ip :$ip" >&2 fi fi } ipIsValid() { local log="ipIsValid:" local ip="$1" if [ -z "$ip" ] || [ -z "$( $cmdEcho $ip | $cmdGrep -E '^[0-9]{1,3}\.[0-9]{1,3}.[0-9]{1,3}\.[0-9]{1,3}$')" ];then # empty or invalid ip return 1; else # valid ip return 0; fi } ipIsGlobal() { local log="ipIsGlobal:" local ip="$1" # RFC 5735 / 1122 : this network local networks="0.0.0.0/8" # RFC 1918 : Private local networks="$networks 10.0.0.0/8" # RFC 6598 : Carrier Grade Nat share local networks="$networks 100.64.0.0/10" # RFC 1122 : local boucle local networks="$networks 127.0.0.0/8" # RFC 3927 : autoconf ip local networks="$networks 169.254.0.0/16" # RFC 1918 : Private local networks="$networks 172.16.0.0/12" # RFC 5736 : IETF local networks="$networks 192.0.0.0/24" # RFC 5737 : Test Net 1 local networks="$networks 192.0.2.0/24" # RFC 3068 : 6to4 local networks="$networks 192.88.99.0/24" # RFC 1918 : Private local networks="$networks 192.168.0.0/16" # RFC 2544 : Test perf local networks="$networks 198.18.0.0/15" # RFC 5737 : Test Net 2 local networks="$networks 198.51.100.0/24" # RFC 5737 : Test Net 3 local networks="$networks 203.0.113.0/24" # RFC 5771 : Multicast Diffusion local networks="$networks 224.0.0.0/4" # RFC 1112 : Reserved local networks="$networks 240.0.0.0/4" # RFC 919 : Broadcast local networks="$networks 255.255.255.255/32" if ipIsValid $ip;then for network in $networks;do # ip is global if ip is outside if ipIsInNetwork $ip $network ;then # inside return 1; else # outside continue; fi done else # ip invalide return 1; fi # valid ip and not inside private or other not global network return 0; } #ipCidrToNetwork "$ip/$mask" ipCidrToNetwork() { local ipCidr="${1}" local binIpNetwork="" for partIpNetwork in $( $cmdEcho ${ipCidr} | $cmdCut -d'/' -f 1 | $cmdTr '.' ' ');do local binIpNetwork="${binIpNetwork}$(printf '%0.8d\n' $(echo "obase=2;$partIpNetwork" | bc))" done local mask="$( $cmdEcho ${ipCidr} | $cmdCut -d'/' -f 2 )" local binMask="$( $cmdPrintf "%.*d\n" $mask 0 | $cmdTr '0' '1' )$( $cmdPrintf "%.*d\n" $(( 32 - $mask )) 0)" #$cmdEcho "$binIpNetwork => $binMask" local binNetwork="" local cpt=0 for testMask in $( $cmdEcho "$binMask" | $cmdFold -w1 );do local cpt=$(( $cpt + 1 )) if [ 1 -eq $testMask ] && [ 1 -eq $( $cmdExpr substr $binIpNetwork $cpt 1 ) ];then local binNetwork="${binNetwork}1" else local binNetwork="${binNetwork}0" fi done local network="" for partBinNetwork in $( $cmdEcho $binNetwork | $cmdFold -w8 );do local binValue=0 if [ 1 -eq $( $cmdExpr substr $partBinNetwork 8 1) ];then local binValue=$(( $binValue + 1 ));fi if [ 1 -eq $( $cmdExpr substr $partBinNetwork 7 1) ];then local binValue=$(( $binValue + 2 ));fi if [ 1 -eq $( $cmdExpr substr $partBinNetwork 6 1) ];then local binValue=$(( $binValue + 4 ));fi if [ 1 -eq $( $cmdExpr substr $partBinNetwork 5 1) ];then local binValue=$(( $binValue + 8 ));fi if [ 1 -eq $( $cmdExpr substr $partBinNetwork 4 1) ];then local binValue=$(( $binValue + 16 ));fi if [ 1 -eq $( $cmdExpr substr $partBinNetwork 3 1) ];then local binValue=$(( $binValue + 32 ));fi if [ 1 -eq $( $cmdExpr substr $partBinNetwork 2 1) ];then local binValue=$(( $binValue + 64 ));fi if [ 1 -eq $( $cmdExpr substr $partBinNetwork 1 1) ];then local binValue=$(( $binValue + 128 ));fi if [ -z "${network}" ];then local network="${binValue}";else local network="${network}.${binValue}";fi done $cmdEcho "${network}/${mask}" } # ipIsInNetwork "$ip" "$net/$mask" # ipIsInNetwork "$ip" "$net" "$mask" ipIsInNetwork() { local ip="$1" local network="$( $cmdEcho ${2} | $cmdCut -d'/' -f 1 )" if [ -z "$3" ];then local mask="$( $cmdEcho ${2} | $cmdGrep / | $cmdCut -d'/' -f 2 )" if [ -z "$mask" ];then local mask="32";fi local mask="$( printf "%.*d" ${mask} 0 | $cmdTr '0' '1' )$( printf "%.*d" $(( 32 - ${mask} )) 0)" else local mask="" for partMask in $( $cmdEcho "${3}" | $cmdTr '.' ' ');do local mask="${mask}$( printf '%0.8d' $( $cmdEcho "obase=2;$partMask" | bc ) )" done fi #$cmdEcho "$network $mask $ip" local cpt=1 for partNetwork in $(echo "$network" | tr '.' ' ');do local partMask="$( $cmdEcho $mask | $cmdFold -w8 | $cmdTr '\n' ' ' | $cmdCut -d' ' -f $cpt )" local partIp="$( $cmdEcho $ip | $cmdCut -f $cpt -d'.' )" local cpt=$(( $cpt + 1 )) #echo "$partNetwork:$partMask:$partIp" if ipIsInNetworkPart "$partIp" "$partNetwork" "$partMask" ;then continue;else return 1;fi done return 0; } ipIsInNetworkPart() { local partIp="${1}" local partNetwork="${2}" local partMask="${3}" local binPartIp="$(printf '%0.8d' $(echo "obase=2;$partIp" | bc))" local binPartNetwork="$(printf '%0.8d' $(echo "obase=2;$partNetwork" | bc))" local binPartMask="$partMask" local cpt=0 for testMask in $( echo "$binPartMask" | $cmdFold -w1);do local cpt=$(( $cpt + 1 )) if [ 0 -eq $testMask ];then continue;fi binNetwork="$(expr substr $binPartNetwork $cpt 1)" binIp="$(expr substr $binPartIp $cpt 1)" if [ "$binNetwork" -ne "$binIp" ];then return 1;fi done return 0 } #for ip in "192.168.0.1" "192.168.1.1" "192.167.0.1" "193.2.3.1";do # echo "ip:$ip network:192.168.0.0" # if ipIsInNetwork $ip 192.168.0.0 255.255.255.0 ;then echo "24:inside";else echo "24:outside";fi # if ipIsInNetwork $ip 192.168.0.0/16 ;then echo "16:inside";else echo "16:outside";fi # if ipIsInNetwork $ip 192.168.0.0/8 ;then echo "08:inside";else echo "08:outside";fi #done