Update helperFunctions; update dnsToIPv6SubnetResolver to use new helper function
This commit is contained in:
		
							parent
							
								
									eb1fa8d1c6
								
							
						
					
					
						commit
						3d9c098574
					
				
					 3 changed files with 27 additions and 42 deletions
				
			
		| 
						 | 
				
			
			@ -1,38 +0,0 @@
 | 
			
		|||
# -------------------------------------------------------------------------------
 | 
			
		||||
# IPv6 helper functions
 | 
			
		||||
#
 | 
			
		||||
# by Philip 'ShokiNN' Henning <mail@philip-henning.com>
 | 
			
		||||
# RouterOS compatibility: 7+
 | 
			
		||||
# Version 1.0
 | 
			
		||||
# last update: 20.01.2025
 | 
			
		||||
# License: MIT
 | 
			
		||||
# -------------------------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
# ---------------------------
 | 
			
		||||
# Function: resolveIPv6
 | 
			
		||||
#    - Takes an DNS string (e.g. "example.com")
 | 
			
		||||
#    - Returns a string of and IPv6 address
 | 
			
		||||
# ---------------------------
 | 
			
		||||
:global resolveIPv6 do={
 | 
			
		||||
    :local result [:toarray ""]
 | 
			
		||||
    :local maxwait 5
 | 
			
		||||
    :local cnt 0
 | 
			
		||||
    :local listname "tmp-resolve$cnt"
 | 
			
		||||
    /ipv6/firewall/address-list {
 | 
			
		||||
        :do {
 | 
			
		||||
            :while ([:len [find list=$listname]] > 0) do={
 | 
			
		||||
                :set cnt ($cnt + 1)
 | 
			
		||||
                :set listname "tmp-resolve$cnt"
 | 
			
		||||
            }
 | 
			
		||||
            :set cnt 0
 | 
			
		||||
            add list=$listname address=$1
 | 
			
		||||
            :while ([find list=$listname && dynamic] = "" && $cnt < $maxwait) do={:delay 1;:set cnt ($cnt +1)}
 | 
			
		||||
            :foreach i in=[find list=$listname && dynamic] do={
 | 
			
		||||
                 :local rawip [get $i address]
 | 
			
		||||
                 :set result ($result, [:pick $rawip 0 [:find $rawip "/"]])
 | 
			
		||||
            }
 | 
			
		||||
            remove [find list=$listname && !dynamic]
 | 
			
		||||
        }
 | 
			
		||||
     }
 | 
			
		||||
    :return $result
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -51,9 +51,9 @@
 | 
			
		|||
# Log "run of script"
 | 
			
		||||
:log info "$logPrefix running";
 | 
			
		||||
 | 
			
		||||
[/system/script/run "helperFunctions"; global safelyResolve];
 | 
			
		||||
:local index 0;
 | 
			
		||||
:foreach i in=$domainToIpv6Subnet do={
 | 
			
		||||
    [/system/script/run "ipv6HelperFunctions"; global resolveIPv6];
 | 
			
		||||
 | 
			
		||||
    :local configDomain ("$($i->0)");
 | 
			
		||||
    :local configSubnetLength ("$($i->1)");
 | 
			
		||||
| 
						 | 
				
			
			@ -65,13 +65,12 @@
 | 
			
		|||
    :log info "$startLogMsg $configDomain";
 | 
			
		||||
    /ipv6/firewall/address-list/remove [/ipv6/firewall/address-list/find list="$ipv6AddressList" comment="$configComment"];
 | 
			
		||||
 | 
			
		||||
    :set dnsIp [$resolveIPv6 $configDomain];
 | 
			
		||||
    :set dnsIp [$safelyResolve $configDomain ipv6];
 | 
			
		||||
    :if ($dnsIp != "") do={
 | 
			
		||||
        /ipv6/firewall/address-list/add list="$ipv6AddressList" address="$dnsIp/$configSubnetLength" comment="$configComment";
 | 
			
		||||
        :local addedSubnet [:pick [/ipv6/firewall/address-list/get [/ipv6/firewall/address-list/find list="$ipv6AddressList" comment="$configComment"]] 1];
 | 
			
		||||
        :log info "$logPrefix domain: $configDomain - Set to: $addedSubnet";
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    :log info "$logPrefix domain: $configDomain - Set to: $addedSubnet";
 | 
			
		||||
 | 
			
		||||
    :log info "$endLogMsg $configDomain";
 | 
			
		||||
};
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										24
									
								
								helperFunctions.rsc
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								helperFunctions.rsc
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,24 @@
 | 
			
		|||
# -------------------------------------------------------------------------------
 | 
			
		||||
# Helper functions
 | 
			
		||||
#
 | 
			
		||||
# by Philip 'ShokiNN' Henning <mail@philip-henning.com>
 | 
			
		||||
# RouterOS compatibility: 7+
 | 
			
		||||
# Version 1.0
 | 
			
		||||
# last update: 20.01.2025
 | 
			
		||||
# License: MIT
 | 
			
		||||
# -------------------------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
# ---------------------------
 | 
			
		||||
# Function: safelyResolve
 | 
			
		||||
#    - Takes an DNS string (e.g. "example.com")
 | 
			
		||||
#    - Takes an IP type [ipv4, ipv6]
 | 
			
		||||
#    - Returns a string of and IP address
 | 
			
		||||
# ---------------------------
 | 
			
		||||
:global safelyResolve do={
 | 
			
		||||
    :do {
 | 
			
		||||
        :local ip [:resolve domain-name="$1" type=$2];
 | 
			
		||||
        :return "$ip";
 | 
			
		||||
    } on-error={
 | 
			
		||||
        return "";
 | 
			
		||||
    };
 | 
			
		||||
};
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue