Skip to content

Fix peer IP allocation: fill subnet gaps, sort peers by IP, backup config - #80

Open
leonidorlov-hash wants to merge 1 commit into
PRVTPRO:mainfrom
leonidorlov-hash:fix-peer-ip-allocation
Open

Fix peer IP allocation: fill subnet gaps, sort peers by IP, backup config#80
leonidorlov-hash wants to merge 1 commit into
PRVTPRO:mainfrom
leonidorlov-hash:fix-peer-ip-allocation

Conversation

@leonidorlov-hash

Copy link
Copy Markdown
Contributor

Problem

_get_next_ip() in managers/awg_manager.py and managers/wireguard_manager.py took the last IP in config file order and incremented it. This caused:

  1. Duplicate IPs — if peers in awg0.conf/wg0.conf are not sorted ascending (after manual edits, migrations, deletions), the next client could get an AllowedIPs already used by another peer. WireGuard then applies only the last matching [Peer], breaking one of the clients.
  2. Gaps never reused — deleting a client (e.g. 10.8.1.5) left the address unused forever; new clients kept getting higher IPs.
  3. Octet overflowawg_manager produced invalid octets (254 → 257, 255 → 257).

Fix

  • _get_next_ip() now collects all used addresses into a set and returns the first free IP in the subnet (.2.254), filling gaps. Raises a clear error when the subnet is exhausted.
  • New peers are no longer blindly appended (>> config). A new helper _insert_peer_sorted() rewrites the config with all [Peer] sections sorted by AllowedIPs, keeping the [Interface] header intact.
  • Before overwriting, a timestamped backup (awg0.conf.bak.YYYYMMDD_HHMMSS) is created inside the container.

Notes

  • Existing duplicate peers are not touched — this only fixes allocation for new clients.
  • Tested locally: config reassembly preserves the interface section and sorts peers numerically by IP.

…nfig

- _get_next_ip now returns the first free address in the subnet instead
  of incrementing the last IP found in file order, which could hand out
  duplicate IPs and never reused addresses freed by deleted clients
- new peers are inserted into the server config keeping [Peer] sections
  sorted by AllowedIPs
- a timestamped backup of the config is created inside the container
  before it is overwritten
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant