# UPnP daemon [![badge github]][url github] [![badge crates.io]][url crates.io] [![badge license]][url license] [badge github]: https://img.shields.io/badge/github-FloGa%2Fupnp--daemon-green [badge crates.io]: https://img.shields.io/crates/v/upnp-daemon [badge license]: https://img.shields.io/crates/l/upnp-daemon [url github]: https://github.com/FloGa/upnp-daemon [url crates.io]: https://crates.io/crates/upnp-daemon [url license]: https://github.com/FloGa/upnp-daemon/blob/develop/LICENSE A daemon for continuously opening ports via UPnP. ## Motivation There are quite some programs out there that need certain network ports to be open to work properly, but do not provide the capability for opening them automatically via UPnP. Sure, one could always argue about the security implications that come with UPnP, but if you are willing to take the risk, it is just annoying, that for example your webserver is not reachable from the internet, because you forgot to open port 80, or your router rebooted and cleared the table of open ports. Or your machine does for whatever reason not have a static IP address, so you cannot add a consistent port mapping. Because of this frustration, I created `upnp-daemon`, a small service written in Rust, that will periodically check a file with your defined port mappings and send them to your router. The main usage will be that you start it once and let it run as a background service forever. The file with the port mappings will be newly read in on each iteration, so you can add new mappings on the fly. ## Installation upnp-daemon can be installed easily through Cargo via `crates.io`: ```shell script cargo install --locked upnp-daemon ``` Please note that the `--locked` flag is necessary here to have the exact same dependencies as when the application was tagged and tested. Without it, you might get more up-to-date versions of dependencies, but you have the risk of undefined and unexpected behavior if the dependencies changed some functionalities. The application might even fail to build if the public API of a dependency changed too much. Alternatively, pre-built binaries can be downloaded from the [GitHub releases][gh-releases] page. [gh-releases]: https://github.com/FloGa/upnp-daemon/releases ## Usage ```text Usage: upnp-daemon [OPTIONS] --file Options: -f, --file The file (or "-" for stdin) with the port descriptions --format The format of the configuration file [default: csv] [possible values: csv, json] -d, --csv-delimiter Field delimiter when using CSV files [default: ;] -F, --foreground Run in foreground instead of forking to background -1, --oneshot Run just one time instead of continuously -n, --interval Specify update interval in seconds [default: 60] --close-ports-on-exit Close specified ports on program exit --only-close-ports Only close specified ports and exit --pid-file Absolute path to PID file for daemon mode [default: /tmp/upnp-daemon.pid] -h, --help Print help -V, --version Print version ``` In the most basic case, a call might look like so: ```shell script upnp-daemon --file ports.csv ``` This will start a background process (daemon) that reads in port mappings from a CSV file (see [config file format](#config-file-format)) every minute and ask the appropriate routers to open those ports. The PID of the process will be written to `/tmp/upnp-daemon.pid` by default and locked exclusively, so that only one instance is running at a time. To quit it, kill the PID that is written in this file. Bash can do it like so: ```shell script kill $( - port The port number to open for the given IP address. Note that upnp-daemon is greedy at the moment, if a port mapping is already in place, it will be deleted and re-added with the given IP address. This might be configurable in a future release. - protocol The protocol for which the given port will be opened. Possible values are `UDP` and `TCP`. - duration The lease duration for the port mapping in seconds. Please note that some UPnP capable routers might choose to ignore this value, so do not exclusively rely on this. - comment A comment about the reason for the port mapping. Will be stored together with the mapping in the router.