Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revisionBoth sides next revision
docs:guide-user:advanced:ipset_extras [2023/03/28 02:51] – [Instructions] vgaeteradocs:guide-user:advanced:ipset_extras [2023/10/18 07:56] – consolidate terminology vgaetera
Line 7: Line 7:
  
 ===== Features ===== ===== Features =====
-  * Create and populate IP sets with domains, CIDRs and ASNs.+  * Create and populate IP sets with domains, CIDRsASNs and GeoIP.
   * Populate IP sets automatically at startup.   * Populate IP sets automatically at startup.
  
Line 15: Line 15:
   * Utilize [[packages:pkgdata:resolveip]] to resolve domains.   * Utilize [[packages:pkgdata:resolveip]] to resolve domains.
   * Fetch ASN prefixes using [[https://stat.ripe.net/docs/data_api|RIPEstat Data API]].   * Fetch ASN prefixes using [[https://stat.ripe.net/docs/data_api|RIPEstat Data API]].
-  * Use [[docs:guide-user:base-system:hotplug|Hotplug]] to trigger setup automatically.+  * Fetch GeoIP data using [[https://www.ipdeny.com/ipblocks/|IPdeny GeoIP Data]]. 
 +  * Use [[docs:guide-user:advanced:hotplug_extras|Hotplug extras]] to trigger setup automatically.
  
 ===== Commands ===== ===== Commands =====
 ^ Sub-command ^ Description ^ ^ Sub-command ^ Description ^
-| ''**reset**'' | Reset IP sets. | 
 | ''**setup**'' | Set up IP sets. | | ''**setup**'' | Set up IP sets. |
 | ''**unset**'' | Unset IP sets. | | ''**unset**'' | Unset IP sets. |
Line 31: Line 31:
 local IPSET_CMD="${1}" local IPSET_CMD="${1}"
 case "${IPSET_CMD}" in case "${IPSET_CMD}" in
-(reset+(setup|unsetipset_proc ;; 
-ipset unset +(*) command ipset "${@}" ;; 
-ipset setup ;; +esac 
-(setup|unset)+
 + 
 +ipset_proc() {
 . /lib/functions.sh . /lib/functions.sh
 config_load dhcp config_load dhcp
 config_foreach ipset_proc_"${IPSET_CMD}" ipset config_foreach ipset_proc_"${IPSET_CMD}" ipset
 uci_commit firewall uci_commit firewall
-/etc/init.d/firewall restart ;; +service firewall reload 
-(*) command ipset "${@}" ;; +fw4 reload-sets
-esac+
 } }
  
 ipset_proc_setup() { ipset_proc_setup() {
 local IPSET_CONF="${1}" local IPSET_CONF="${1}"
 +local IPSET_TEMP="$(mktemp -t ipset.XXXXXX)"
 { {
 config_list_foreach "${IPSET_CONF}" domain ipset_domain config_list_foreach "${IPSET_CONF}" domain ipset_domain
 config_list_foreach "${IPSET_CONF}" cidr ipset_cidr config_list_foreach "${IPSET_CONF}" cidr ipset_cidr
 config_list_foreach "${IPSET_CONF}" asn ipset_asn config_list_foreach "${IPSET_CONF}" asn ipset_asn
-} | config_list_foreach "${IPSET_CONF}" name ipset_"${IPSET_CMD}"+config_list_foreach "${IPSET_CONF}" geoip ipset_geoip 
 +} | sort -u > "${IPSET_TEMP}" 
 +config_list_foreach "${IPSET_CONF}" name ipset_"${IPSET_CMD}" 
 +rm -f "${IPSET_TEMP}"
 } }
  
Line 60: Line 65:
 ipset_setup() { ipset_setup() {
 local IPSET_NAME="${1}" local IPSET_NAME="${1}"
-local IPSET_FILE="/etc/nftables.d/${IPSET_NAME}.ipset"+local IPSET_FILE="/var/ipset-${IPSET_NAME}"
 local IPSET_FAMILY local IPSET_FAMILY
 case "${IPSET_NAME}" in case "${IPSET_NAME}" in
 (*6) IPSET_FAMILY="ipv6" (*6) IPSET_FAMILY="ipv6"
-awk -e '!seen[$0]++&&!/\./;;+sed -e "/\./d" ;;
 (*) IPSET_FAMILY="ipv4" (*) IPSET_FAMILY="ipv4"
-awk -e '!seen[$0]++&&!/:/;; +sed -e "/:/d" ;; 
-esac > "${IPSET_FILE}"+esac < "${IPSET_TEMP}" > "${IPSET_FILE}"
 uci -q batch << EOI uci -q batch << EOI
 set firewall.'${IPSET_NAME}'='ipset' set firewall.'${IPSET_NAME}'='ipset'
Line 79: Line 84:
 ipset_unset() { ipset_unset() {
 local IPSET_NAME="${1}" local IPSET_NAME="${1}"
-local IPSET_FILE="/etc/nftables.d/${IPSET_NAME}.ipset"+local IPSET_FILE="/var/ipset-${IPSET_NAME}"
 rm -f "${IPSET_FILE}" rm -f "${IPSET_FILE}"
 +uci -q batch << EOI
 +delete firewall.'${IPSET_NAME}'.loadfile
 +EOI
 } }
  
Line 95: Line 103:
 ipset_asn() { ipset_asn() {
 local IPSET_ENTRY="${1}" local IPSET_ENTRY="${1}"
-uclient-fetch -O - "https://stat.ripe.net/data/\+wget -O - "https://stat.ripe.net/data/\
 announced-prefixes/data.json?resource=${IPSET_ENTRY}" \ announced-prefixes/data.json?resource=${IPSET_ENTRY}" \
 | jsonfilter -e "$['data']['prefixes'][*]['prefix']" | jsonfilter -e "$['data']['prefixes'][*]['prefix']"
 +}
 +
 +ipset_geoip() {
 +local IPSET_ENTRY="${1}"
 +wget -O - "https://www.ipdeny.com/ipblocks/data/\
 +aggregated/${IPSET_ENTRY}-aggregated.zone" \
 +"https://www.ipdeny.com/ipv6/ipaddresses/\
 +aggregated/${IPSET_ENTRY}-aggregated.zone"
 } }
 EOF EOF
Line 105: Line 121:
 mkdir -p /etc/hotplug.d/online mkdir -p /etc/hotplug.d/online
 cat << "EOF" > /etc/hotplug.d/online/70-ipset-setup cat << "EOF" > /etc/hotplug.d/online/70-ipset-setup
-if [ ! -e /var/lock/ipset-setup ] \ +if [ -z "${TERM}" ] \ 
-&& lock -n /var/lock/ipset-setup+&& [ ! -e /var/lock/ipset-setup ] \ 
 +|| [ -n "${TERM}" ] \ 
 +&& lock -n /var/lock/ipset-setup 
 +&& sleep 10
 then . /etc/profile.d/ipset.sh then . /etc/profile.d/ipset.sh
 ipset setup ipset setup
Line 115: Line 134:
 /etc/hotplug.d/online/70-ipset-setup /etc/hotplug.d/online/70-ipset-setup
 EOF EOF
 +
 +# Configure cron
 +cat << "EOF" >> /etc/crontabs/root
 +0 */3 * * * . /etc/hotplug.d/online/70-ipset-setup
 +EOF
 +service cron restart
 </code> </code>
  
Line 133: Line 158:
 uci add_list dhcp.example.asn="2906" uci add_list dhcp.example.asn="2906"
 uci add_list dhcp.example.asn="40027" uci add_list dhcp.example.asn="40027"
 +uci add_list dhcp.example.geoip="cn"
 +uci add_list dhcp.example.geoip="ru"
 uci commit dhcp uci commit dhcp
  
Line 141: Line 168:
 ===== Automated ===== ===== Automated =====
 <code bash> <code bash>
-uclient-fetch -O ipset-extras.sh "https://openwrt.org/_export/code/docs/guide-user/advanced/ipset_extras?codeblock=0"+wget -U "" -O ipset-extras.sh "https://openwrt.org/_export/code/docs/guide-user/advanced/ipset_extras?codeblock=0"
 . ./ipset-extras.sh . ./ipset-extras.sh
 </code> </code>
  
  • Last modified: 2024/04/17 19:58
  • by fail-safe1