Skip to content

Commit 39b5275

Browse files
committed
fix(update): prevent race condition when IP hasn't changed
1 parent 8a1687c commit 39b5275

File tree

4 files changed

+13
-30
lines changed

4 files changed

+13
-30
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# ==============================
22
# Variables
33
# ==============================
4-
VERSION := 0.3.1
4+
VERSION := 0.3.2
55
BINARY_NAME := autoip
66
COMMIT := $(shell git rev-parse --short HEAD)
77
BUILD_DATE := $(shell date -u +%Y-%m-%dT%H:%M:%SZ)

commands/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func RunCreateInteractive() error {
189189

190190
// ── Resumen final ──
191191
printSection("📝 Resumen de configuración")
192-
fmt.Println("Proveedor DDNS :", cfg.DDNS.Provider)
192+
fmt.Println("Proveedor DDNS :", cfg.DDNS.Provider)
193193
switch cfg.DDNS.Provider {
194194
case "noip":
195195
fmt.Println("Hostname :", cfg.DDNS.NoIP.Hostname)

commands/daemon.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,13 @@ func runDaemon(ctx context.Context) error {
5252
select {
5353
case <-ticker.C:
5454
log.Info("Revisando cambios de IP...")
55-
if err := RunUpdate(ctx); err != nil {
56-
log.Error("Error al actualizar IP", "error ", err)
55+
err := RunUpdate(ctx)
56+
if err != nil {
57+
return err
5758
}
5859

5960
case sig := <-sigChan:
60-
log.Info("Señal recibida, cerrando daemon ", "signal ", sig)
61+
log.Warn("Señal recibida, cerrando daemon ", "signal ", sig)
6162
return nil
6263

6364
case <-ctx.Done():

commands/update.go

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ package commands
55

66
import (
77
"context"
8-
"errors"
98
"fmt"
109

1110
"github.com/urfave/cli/v3"
1211
"github.com/vorosdev/autoip/config"
1312
"github.com/vorosdev/autoip/ddns"
14-
"github.com/vorosdev/autoip/ddns/noip"
1513
"github.com/vorosdev/autoip/ip"
1614
"github.com/vorosdev/autoip/log"
1715
)
@@ -37,9 +35,10 @@ func RunUpdate(ctx context.Context) error {
3735
return nil
3836
}
3937

40-
log.Info(fmt.Sprintf("IP nueva detectada: %s (antes: %s)", ipNueva, ipActual))
38+
if ipActual != "" {
39+
log.Info(fmt.Sprintf("IP nueva detectada: %s (antes: %s)", ipNueva, ipActual))
40+
}
4141

42-
// Cargar proveedor DDNS dinámicamente
4342
provider, err := ddns.LoadProvider(cfg.DDNS)
4443
if err != nil {
4544
log.Error("Error al cargar proveedor DDNS: ", err)
@@ -48,28 +47,11 @@ func RunUpdate(ctx context.Context) error {
4847

4948
log.Info("Usando proveedor: ", provider.Name())
5049

51-
// if err := provider.Update(ipNueva); err != nil {
52-
// log.Error(fmt.Sprintf("Error al actualizar proveedor %s: ", provider.Name()), err)
53-
// return err
54-
// }
55-
56-
err = provider.Update(ipNueva)
57-
if err != nil {
58-
// Validación activa ante respuesta sospechosa
59-
if errors.Is(err, noip.ErrNochgSuspicious) {
60-
log.Info("Respuesta 'nochg' sospechosa, realizando validación activa de credenciales")
61-
62-
validateErr := noip.ValidateAuth(cfg.DDNS, "127.0.0.2")
63-
if validateErr != nil {
64-
log.Error("Validación activa fallida: ", validateErr)
65-
return validateErr
66-
}
67-
} else {
68-
log.Error(fmt.Sprintf("Error al actualizar proveedor %s: ", provider.Name()), err)
69-
return err
70-
}
50+
if err := provider.Update(ipNueva); err != nil {
51+
log.Error(fmt.Sprintf("Error al actualizar proveedor %s: ", provider.Name()), err)
52+
return err
7153
}
72-
// Guardar la nueva IP
54+
7355
_ = ip.WriteOldIP(ipActual)
7456
_ = ip.WriteCurrentIP(ipNueva)
7557

0 commit comments

Comments
 (0)