Created
August 3, 2025 17:56
-
-
Save m0a0k0s/23e711e66bd6b4fbe006a31df2f86bde to your computer and use it in GitHub Desktop.
OPNsense IDS WAN IP updater service
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
| <?php | |
| # put in /usr/local/etc/inc/plugins.inc.d/idswanupdater.inc | |
| # update 'igc1' with appropriate interface id value | |
| # test: pluginctl -c newwanip | |
| # based on | |
| # https://github.com/opnsense/plugins/blob/master/net/upnp/src/etc/inc/plugins.inc.d/miniupnpd.inc | |
| # https://github.com/opnsense/core/issues/5171 | |
| # https://homenetworkguy.com/how-to/configure-intrusion-detection-opnsense/#comment-5644919182 | |
| # yaml configuration file seems to be auto overwriten in opnsense and not suitable for changes | |
| function idswanupdater_services() | |
| { | |
| $services = []; | |
| $pconfig = []; | |
| $pconfig['name'] = 'idswanupdater'; | |
| $pconfig['description'] = gettext('IDS WAN IP updater'); | |
| $pconfig['php']['restart'] = ['idswanupdater_stop', 'idswanupdater_start']; | |
| $pconfig['php']['start'] = ['idswanupdater_start']; | |
| $pconfig['php']['stop'] = ['idswanupdater_stop']; | |
| $services[] = $pconfig; | |
| return $services; | |
| } | |
| function idswanupdater_start() | |
| { | |
| } | |
| function idswanupdater_stop() | |
| { | |
| } | |
| function idswanupdater_configure() | |
| { | |
| return [ | |
| 'bootup' => ['idswanupdater_configure_do'], | |
| 'newwanip' => ['idswanupdater_configure_do'], | |
| ]; | |
| } | |
| function idswanupdater_configure_do($verbose = false) | |
| { | |
| $config = simplexml_load_file('/conf/config.xml'); | |
| $home_net = $config->OPNsense->IDS->general->homenet; | |
| if (!empty($home_net)) { | |
| $home_net_elements = explode(",", $home_net); | |
| $last_index = count($home_net_elements) - 1; | |
| $new_addr = array_pop(array_reverse(interfaces_primary_address('igc1'))); | |
| if ($home_net_elements[$last_index] != $new_addr) | |
| { | |
| $home_net_elements[$last_index] = $new_addr; | |
| $home_net = implode(",", $home_net_elements); | |
| $config->OPNsense->IDS->general->homenet = $home_net; | |
| $config->asXml('/conf/config.xml'); | |
| mwexec('configctl template reload OPNsense/IDS'); | |
| log_msg("New IP address set: {$new_addr}"); | |
| service_log("New IP address set: {$new_addr}", $verbose); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment