621e8895c9
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2.9 KiB
2.9 KiB
name, kind, lang, domain, version, purity, signature, description, tags, params, output, uses_functions, uses_types, returns, returns_optional, error_type, imports, tested, tests, test_file_path, file_path
| name | kind | lang | domain | version | purity | signature | description | tags | params | output | uses_functions | uses_types | returns | returns_optional | error_type | imports | tested | tests | test_file_path | file_path | |||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| wg_peer_add | function | go | infra | 1.0.0 | impure | func WGPeerAdd(spec WGPeerSpec, configPath, subnetCIDR string) (WGPeerResult, error) | Hub-side: anade peer WireGuard al wg0.conf con IP asignada del pool, syncconf en caliente sin reiniciar interface. Idempotente por PublicKey + DeviceID. Mantiene comentario # DeviceID:<id> sobre cada bloque [Peer] para tracking inverso. |
|
|
WGPeerResult con DeviceID, AssignedIP (pura sin CIDR), ConfigPath y Status ('added'|'already-present'|'reconfigured') |
|
|
false | error_go_core | true |
|
functions/infra/wg_peer_add_test.go | functions/infra/wg_peer_add.go |
Ejemplo
spec := infra.WGPeerSpec{
DeviceID: "pc-aurgi",
PublicKey: "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB=",
}
res, err := infra.WGPeerAdd(spec, "/etc/wireguard/wg0.conf", "10.42.0.0/24")
// res.AssignedIP == "10.42.0.2" (primera IP libre del pool)
// res.Status == "added"
Cuando usarla
Cuando un dispositivo nuevo (PC, contenedor, mobile) se une al mesh WireGuard del hub. Llamar tras generar las claves con wg_keygen_go_infra. Idempotente: si el DeviceID ya existe con la misma clave, devuelve already-present sin tocar el config.
Gotchas
- Race condition: si dos llamadas concurrentes añaden peers simultáneamente, la segunda puede asignar la misma IP libre. Usar file lock (
flock) sobreconfigPathpara serializar en produccion. - WG_SKIP_SYNCCONF=1: en entornos CI sin WireGuard instalado, establecer esta variable para saltarse el exec de
wg syncconf. Los tests ya la activan en elinit(). - syncconf falla → rollback automático: si el
wg syncconfdevuelve error, se restaura el backup.baky se devuelve error. El config queda intacto. - chmod 600: la función hace
chmod 600sobreconfigPathtras cada escritura. Asegúrate de que el proceso tiene permisos sobre el archivo. - Hub IP .1: la función excluye
<red>.1del pool de autoasignación. El hub debe tener esa IP. Si el hub usa otra IP, ajustar la lógica dewgNextFreeIP.