[PR #5202] [MERGED] [client] Add NAT-PMP/UPnP support #27338

Open
opened 2026-08-05 07:08:35 -04:00 by saavagebueno · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/netbirdio/netbird/pull/5202
Author: @lixmal
Created: 1/28/2026
Status: Merged
Merged: 4/8/2026
Merged by: @lixmal

Base: mainHead: nat-pmp-upnp


📝 Commits (10+)

📊 Changes

13 files changed (+716 additions, -59 deletions)

View changed files

📝 client/internal/dns/local/local_test.go (+4 -4)
📝 client/internal/engine.go (+46 -29)
📝 client/internal/peer/conn.go (+27 -23)
📝 client/internal/peer/worker_ice.go (+96 -3)
client/internal/portforward/env.go (+26 -0)
client/internal/portforward/manager.go (+250 -0)
client/internal/portforward/manager_js.go (+36 -0)
client/internal/portforward/manager_test.go (+159 -0)
client/internal/portforward/state.go (+50 -0)
📝 client/server/state_generic.go (+5 -0)
📝 client/server/state_linux.go (+5 -0)
📝 go.mod (+4 -0)
📝 go.sum (+8 -0)

📄 Description

Describe your changes

This adds a NAT mapper that tries to discover NAT-PMP and UPnP gateways to acquire a mapping for a forwarded port.
This mapping is then signaled to other peers.

Example logs

2026-01-28T14:50:22Z INFO client/internal/portforward/manager.go:189: created port mapping: 51820 -> 50497 via NAT-PMP (external IP: 45.45.45.2)

2026-01-28T14:50:38Z DEBG [peer: Fc5Y0orhGvvoTD9BDpwUpi9h8O6icW29EzJumvt7izg=] client/internal/peer/worker_ice.go:407: injecting port-forwarded candidate: udp4 srflx 45.45.45.2:50497 related 45.45.45.2:51820 (resolved: 45.45.45.2:50497) candidate:XnfKu4TIX7PFxUgCB/AWryc5TRVvPosU (mapping: 51820 -> 50497 via NAT-PMP, priority: 1694499815)

Stack

Checklist

  • Is it a bug fix
  • Is a typo/documentation fix
  • Is a feature enhancement
  • It is a refactor
  • Created tests that fail without the change (if possible)

By submitting this pull request, you confirm that you have read and agree to the terms of the Contributor License Agreement.

Documentation

Select exactly one:

  • I added/updated documentation for this change
  • Documentation is not needed for this change (explain why)

Docs PR URL (required if "docs added" is checked)

Paste the PR link from https://github.com/netbirdio/docs here:

https://github.com/netbirdio/docs/pull/__

Summary by CodeRabbit

  • New Features

    • Port‑forwarding manager: NAT discovery, UDP mapping creation/renewal, env toggle, crash‑recovery state, and graceful lifecycle tied to engine/peer.
    • ICE now injects port‑forwarded server‑reflexive candidates to improve connectivity.
    • JS/WASM builds include a no‑op port‑forwarding stub for parity.
  • Tests

    • Unit tests covering manager mapping lifecycle, availability, and state cleanup.
  • Chores

    • Added NAT/UPnP dependencies and clarified state registration comments.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/netbirdio/netbird/pull/5202 **Author:** [@lixmal](https://github.com/lixmal) **Created:** 1/28/2026 **Status:** ✅ Merged **Merged:** 4/8/2026 **Merged by:** [@lixmal](https://github.com/lixmal) **Base:** `main` ← **Head:** `nat-pmp-upnp` --- ### 📝 Commits (10+) - [`1b0b1db`](https://github.com/netbirdio/netbird/commit/1b0b1db575894ebc5a613bdfe23a7ab010a4898b) Add NAT-PMP/UPnP support - [`28f2c1b`](https://github.com/netbirdio/netbird/commit/28f2c1b3b50a94c28ce5ab97d2392664642fb981) Exclude JS - [`05ba63a`](https://github.com/netbirdio/netbird/commit/05ba63a5bd9a8e4cec30765eaea9b9fea614358f) Copy extensions - [`124aa1a`](https://github.com/netbirdio/netbird/commit/124aa1a87531c6d82146346538c98d69cfba80e9) Address review - [`c5c2e02`](https://github.com/netbirdio/netbird/commit/c5c2e02580eeb294d870cbe86526fa7a8229eb08) Mock out test dep - [`2efc713`](https://github.com/netbirdio/netbird/commit/2efc7134a3aef7a015581bf5f994f6a07a79651a) Add prio docs - [`7fa7f1c`](https://github.com/netbirdio/netbird/commit/7fa7f1c7d74f91e668dfcc263f026c326f4aa728) Address review - [`3f93b5d`](https://github.com/netbirdio/netbird/commit/3f93b5dba22df7ec4f1d1945b2487ef00319049f) Unlock Stop - [`4d6b810`](https://github.com/netbirdio/netbird/commit/4d6b810f8b3ea04074bd1b84a190b0e35d7ac207) Refactor port forward manager cleanup lifecycle (#5342) - [`06c10d7`](https://github.com/netbirdio/netbird/commit/06c10d70238079ef75ebc0c82d1d5d379523fcfb) Merge branch 'main' into nat-pmp-upnp ### 📊 Changes **13 files changed** (+716 additions, -59 deletions) <details> <summary>View changed files</summary> 📝 `client/internal/dns/local/local_test.go` (+4 -4) 📝 `client/internal/engine.go` (+46 -29) 📝 `client/internal/peer/conn.go` (+27 -23) 📝 `client/internal/peer/worker_ice.go` (+96 -3) ➕ `client/internal/portforward/env.go` (+26 -0) ➕ `client/internal/portforward/manager.go` (+250 -0) ➕ `client/internal/portforward/manager_js.go` (+36 -0) ➕ `client/internal/portforward/manager_test.go` (+159 -0) ➕ `client/internal/portforward/state.go` (+50 -0) 📝 `client/server/state_generic.go` (+5 -0) 📝 `client/server/state_linux.go` (+5 -0) 📝 `go.mod` (+4 -0) 📝 `go.sum` (+8 -0) </details> ### 📄 Description ## Describe your changes This adds a NAT mapper that tries to discover NAT-PMP and UPnP gateways to acquire a mapping for a forwarded port. This mapping is then signaled to other peers. Example logs ``` 2026-01-28T14:50:22Z INFO client/internal/portforward/manager.go:189: created port mapping: 51820 -> 50497 via NAT-PMP (external IP: 45.45.45.2) 2026-01-28T14:50:38Z DEBG [peer: Fc5Y0orhGvvoTD9BDpwUpi9h8O6icW29EzJumvt7izg=] client/internal/peer/worker_ice.go:407: injecting port-forwarded candidate: udp4 srflx 45.45.45.2:50497 related 45.45.45.2:51820 (resolved: 45.45.45.2:50497) candidate:XnfKu4TIX7PFxUgCB/AWryc5TRVvPosU (mapping: 51820 -> 50497 via NAT-PMP, priority: 1694499815) ``` ## Issue ticket number and link ## Stack <!-- branch-stack --> ### Checklist - [ ] Is it a bug fix - [ ] Is a typo/documentation fix - [x] Is a feature enhancement - [ ] It is a refactor - [ ] Created tests that fail without the change (if possible) > By submitting this pull request, you confirm that you have read and agree to the terms of the [Contributor License Agreement](https://github.com/netbirdio/netbird/blob/main/CONTRIBUTOR_LICENSE_AGREEMENT.md). ## Documentation Select exactly one: - [ ] I added/updated documentation for this change - [x] Documentation is **not needed** for this change (explain why) ### Docs PR URL (required if "docs added" is checked) Paste the PR link from https://github.com/netbirdio/docs here: https://github.com/netbirdio/docs/pull/__ <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Port‑forwarding manager: NAT discovery, UDP mapping creation/renewal, env toggle, crash‑recovery state, and graceful lifecycle tied to engine/peer. * ICE now injects port‑forwarded server‑reflexive candidates to improve connectivity. * JS/WASM builds include a no‑op port‑forwarding stub for parity. * **Tests** * Unit tests covering manager mapping lifecycle, availability, and state cleanup. * **Chores** * Added NAT/UPnP dependencies and clarified state registration comments. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
saavagebueno added the pull-request label 2026-08-05 07:08:35 -04:00
Sign in to join this conversation.
No Label pull-request
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: DYNR/netbird#27338