Drupal 8 utilizes Symfony components. One of them is the service container and the services it has registered. During the building of the container, there is a compiler pass process that allows alterations of the container's services.
First, we will need to register the service in the module's services.yml file. The \Drupal\Core\DependencyInjection\Compiler\StackedKernelPass class provided by the core will automatically load all the services tagged with http_middleware, such as our ip_restrict.middleware service.
Our arguments definition loads items defined in the parameters.ip_restrict that are used for the class's constructor.
With our provided IpRestrictPass class, we are also tapping into the container's compile cycle. We will take a look at the parameter values for the ip_restrict section to check whether they are enabled. If the enabled setting is set to false, we remove our service from the container.