UnifiApi.Network.PortForward (UnifiApi v0.4.0)

Copy Markdown View Source

UniFi Network API (v1) — port-forward rules.

Manages NAT port-forward rules on the gateway.

Requires cookie + CSRF authentication. See UnifiApi.Auth.Cookie.

Rule fields

  • _id, name, enabled
  • proto"tcp", "udp", "tcp_udp"
  • src — source CIDR / "any"
  • dst_port, fwd_port, fwd (forward IP)
  • pfwd_interface"wan", "wan2", "both"
  • log — boolean, log packets matching this rule

Summary

Functions

Creates a new port-forward rule.

Deletes a port-forward rule.

Returns a specific port-forward rule by id.

Lists port-forward rules on a site.

Updates an existing port-forward rule.

Functions

create(client, site_id, body)

@spec create(Req.Request.t(), String.t(), map()) ::
  {:ok, term()} | {:error, UnifiApi.Error.t()}

Creates a new port-forward rule.

Examples

{:ok, rule} = UnifiApi.Network.PortForward.create(client, "default", %{
  name: "Game server",
  enabled: true,
  proto: "tcp_udp",
  src: "any",
  dst_port: "25565",
  fwd: "192.168.1.50",
  fwd_port: "25565",
  pfwd_interface: "wan"
})

delete(client, site_id, rule_id)

@spec delete(Req.Request.t(), String.t(), String.t()) ::
  {:ok, term()} | {:error, UnifiApi.Error.t()}

Deletes a port-forward rule.

get(client, site_id, rule_id)

@spec get(Req.Request.t(), String.t(), String.t()) ::
  {:ok, term()} | {:error, UnifiApi.Error.t()}

Returns a specific port-forward rule by id.

list(client, site_id, opts \\ [])

@spec list(Req.Request.t(), String.t(), keyword()) ::
  {:ok, term()} | {:error, UnifiApi.Error.t()}

Lists port-forward rules on a site.

Options

Validated with Keyword.validate!/2 — an unknown key raises ArgumentError rather than being silently dropped.

  • :limit — page size, sent as the v1 _limit query param.
  • :start — page offset, sent as the v1 _start query param.
  • :params — extra query params, merged verbatim ahead of _limit / _start.
  • :raw — when true, return the raw response body binary (skips JSON decoding and the v1 envelope unwrap).

Pagination — first page only, and there is no stream/3

/rest/portforward is a v1 collection endpoint: it pages on _start / _limit and the meta envelope carries no total count, so a full page is indistinguishable from a truncated one. list/3 returns the first page only whenever :limit is set, and whatever the controller's own default cap allows otherwise. This module has no stream/3 — rule sets are hand-maintained and small. Page manually if yours is not: request limit: n and walk :start in steps of n until a page returns fewer than n items.

Because a truncated read is indistinguishable from a complete one, never treat list/3 as the authoritative rule set when computing a diff to apply back via update/4 or delete/3.

Examples

{:ok, rules} = UnifiApi.Network.PortForward.list(authed, "default")

# Explicit page of 100
{:ok, rules} = UnifiApi.Network.PortForward.list(authed, "default",
  limit: 100, start: 0)

update(client, site_id, rule_id, body)

@spec update(Req.Request.t(), String.t(), String.t(), map()) ::
  {:ok, term()} | {:error, UnifiApi.Error.t()}

Updates an existing port-forward rule.