Last active
October 11, 2024 12:02
-
-
Save zarv1k/e1b37a1a5a2c10936fb532302416bed1 to your computer and use it in GitHub Desktop.
Mikrotik: google ip-range JSON to address-list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:global fGoogleIpRange | |
if (!any $fGoogleIpRange) do={ :global fGoogleIpRange do={ | |
:local addressList $1 | |
:local ipRangeUrl $2 | |
:local exceptionsJsonUrl $3 | |
# load JSON parser - https://github.com/Winand/mikrotik-json-parser | |
/system script run JParseFunctions | |
# read file into JSONIn | |
:global JSONIn ([/tool fetch url=$ipRangeUrl as-value output=user]->"data"); | |
# ensure sync token file exist | |
:local syncTokenValue | |
:local syncTokenFile ($addressList . ".sync-token") | |
:local syncTokenFileName ($syncTokenFile . ".txt") | |
if ([:len [/file find name=$syncTokenFileName]] = 0) do={ | |
:execute script=":put 0" file=$syncTokenFile; | |
:set syncTokenValue "" | |
} else={ | |
:set syncTokenValue [/file get $syncTokenFileName contents] | |
} | |
# ensure JSON parser globals | |
:global JSONUnload | |
:global fJParse; | |
:global fJParsePrint; | |
:global JSONLoad | |
:global JSONLoads | |
# read JSON | |
:global JParseOut [$fJParse]; | |
#$fJParsePrint | |
:if ($syncTokenValue != $JParseOut->"syncToken") do={ | |
# iterate over prefixes if prefixes is not empty | |
:if ([:len ($JParseOut->"prefixes")] > 0) do={ | |
# save syncToken to file | |
:set syncTokenValue ($JParseOut->"syncToken") | |
/file set $syncTokenFileName contents=$syncTokenValue | |
# delete entire address-list | |
/ip firewall address-list remove [/ip firewall address-list find list=$addressList] | |
:foreach prefix in=($JParseOut->"prefixes") do={ | |
:if ([:typeof ($prefix->"ipv4Prefix")] = "str") do={ | |
/ip firewall address-list add list=$addressList address=($prefix->"ipv4Prefix") | |
} | |
} | |
} | |
} else={ | |
:put "ip-range list is already synced" | |
} | |
# read exceptions from URL (when 3rd parameter is set) or from local file and disable each exception address-list | |
:local exceptionsJson | |
:local exceptionsFileName ($addressList . ".exceptions.json") | |
if ([:len ($exceptionsJsonUrl)] > 0) do={ | |
:put "Using exceptions URL" | |
:set exceptionsJson [$JSONLoads ([/tool fetch url=$exceptionsJsonUrl as-value output=user]->"data")] | |
} | |
if ([:len $exceptionsJson] = 0) do={ | |
:put "Using exceptions file" | |
:set exceptionsJson [$JSONLoad $exceptionsFileName] | |
} | |
if ([:len $exceptionsJson] > 0) do={ | |
:foreach k,v in=$exceptionsJson do={ | |
/ip firewall address-list disable [find where list=$addressList address=$k] | |
} | |
} else={ | |
:put "Skip list exceptions" | |
} | |
# cleanup | |
:set JSONIn | |
:set JParseOut | |
# unload JSON library | |
$JSONUnload | |
}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/system script run fGoogleIpRange | |
:global fGoogleIpRange | |
$fGoogleIpRange "google-cloud" "https://www.gstatic.com/ipranges/cloud.json" | |
set $fGoogleIpRange |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/system script run fGoogleIpRange | |
:global fGoogleIpRange | |
$fGoogleIpRange "google" "https://www.gstatic.com/ipranges/goog.json" "https://gist.githubusercontent.com/zarv1k/e1b37a1a5a2c10936fb532302416bed1/raw/9a17fb866208332224dc6f736ed15b819c0bd857/google.exceptions.json" | |
set $fGoogleIpRange |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment