Skip to content

Commit

Permalink
Merge pull request #957 from gravitl/NET-1784-stun-fix
Browse files Browse the repository at this point in the history
Stun TimeOut fixes, Use Ip servers to check ip changes
  • Loading branch information
abhishek9686 authored Dec 28, 2024
2 parents f5ebf09 + 6592a86 commit c143687
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
5 changes: 5 additions & 0 deletions functions/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ func startGoRoutines(wg *sync.WaitGroup) context.CancelFunc {
config.Netclient().EndpointIPv6 = nil
updateConfig = true
}

}

originalDefaultGwIP, err := wireguard.GetDefaultGatewayIp()
Expand Down Expand Up @@ -646,6 +647,10 @@ func holePunchWgPort(proto, portToStun int) (pubIP net.IP, pubPort int, natType
server.Stun = true
stun.SetDefaultStunServers()
}
_, ipErr := GetPublicIP(uint(proto))
if ipErr != nil {
return
}
if server.Stun {
pubIP, pubPort, natType = stun.HolePunch(portToStun, proto)
} else {
Expand Down
18 changes: 12 additions & 6 deletions functions/mqpublish.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,23 @@ func Checkin(ctx context.Context, wg *sync.WaitGroup) {
// if config.Netclient().CurrGwNmIP is not nil, it's an InetClient, then it skips the network change detection
if !config.Netclient().IsStatic && config.Netclient().CurrGwNmIP == nil {
restart := false
ip4, _, _ := holePunchWgPort(4, 0)
ip4, _ := GetPublicIP(4)
if ip4 != nil && !ip4.IsUnspecified() && !config.HostPublicIP.Equal(ip4) {
slog.Warn("IP CHECKIN", "ipv4", ip4, "HostPublicIP", config.HostPublicIP)
slog.Debug("IP CHECKIN", "ipv4", ip4, "HostPublicIP", config.HostPublicIP)
config.HostPublicIP = ip4
restart = true
} else if ip4 == nil && config.HostPublicIP != nil {
slog.Warn("IP CHECKIN", "ipv4", ip4, "HostPublicIP", config.HostPublicIP)
slog.Debug("IP CHECKIN", "ipv4", ip4, "HostPublicIP", config.HostPublicIP)
config.HostPublicIP = nil
restart = true
}
ip6, _, _ := holePunchWgPort(6, 0)
ip6, _ := GetPublicIP(6)
if ip6 != nil && !ip6.IsUnspecified() && !config.HostPublicIP6.Equal(ip6) {
slog.Warn("IP CHECKIN", "ipv6", ip6, "HostPublicIP6", config.HostPublicIP6)
slog.Debug("IP CHECKIN", "ipv6", ip6, "HostPublicIP6", config.HostPublicIP6)
config.HostPublicIP6 = ip6
restart = true
} else if ip6 == nil && config.HostPublicIP6 != nil {
slog.Warn("IP CHECKIN", "ipv6", ip6, "HostPublicIP6", config.HostPublicIP6)
slog.Debug("IP CHECKIN", "ipv6", ip6, "HostPublicIP6", config.HostPublicIP6)
config.HostPublicIP6 = nil
restart = true
}
Expand All @@ -106,6 +106,12 @@ func Checkin(ctx context.Context, wg *sync.WaitGroup) {
slog.Warn("failed to update host settings", err.Error())
}
logger.Log(0, "restarting netclient due to network changes...")
if ip4 != nil {
logger.Log(0, "new IPv4 detected: ", ip4.String())
}
if ip6 != nil {
logger.Log(0, "new IPv6 detected: ", ip6.String())
}
daemon.HardRestart()
}
}
Expand Down
4 changes: 1 addition & 3 deletions stun/stun.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net"
"strconv"
"strings"
"time"

"github.com/gravitl/netclient/config"
"github.com/gravitl/netmaker/logger"
Expand Down Expand Up @@ -143,7 +142,6 @@ func doStunTransaction(lAddr, rAddr *net.UDPAddr) (publicIP net.IP, publicPort i
logger.Log(1, "failed to dial: ", err.Error())
return
}

re := conn.LocalAddr().String()
lIP := re[0:strings.LastIndex(re, ":")]
if strings.ContainsAny(lIP, "[") {
Expand All @@ -162,7 +160,7 @@ func doStunTransaction(lAddr, rAddr *net.UDPAddr) (publicIP net.IP, publicPort i
}
}()
defer conn.Close()
c, err := stun.NewClient(conn, stun.WithTimeoutRate(time.Second*5))
c, err := stun.NewClient(conn)
if err != nil {
logger.Log(1, "failed to create stun client: ", err.Error())
return
Expand Down

0 comments on commit c143687

Please sign in to comment.