Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NET-347): deduce public endpoint on daemon start #474

Merged
merged 9 commits into from
Jun 27, 2023
33 changes: 24 additions & 9 deletions functions/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func closeRoutines(closers []context.CancelFunc, wg *sync.WaitGroup) {
func startGoRoutines(wg *sync.WaitGroup) context.CancelFunc {
ctx, cancel := context.WithCancel(context.Background())
if _, err := config.ReadNetclientConfig(); err != nil {
slog.Error("error reading neclient config file", "error", err)
slog.Error("error reading netclient config file", "error", err)
}
config.UpdateNetclient(*config.Netclient())
if err := config.ReadServerConf(); err != nil {
Expand All @@ -132,6 +132,21 @@ func startGoRoutines(wg *sync.WaitGroup) context.CancelFunc {
config.SetServerCtx()
config.HostPublicIP, config.WgPublicListenPort = holePunchWgPort()
slog.Info("wireguard public listen port: ", "port", config.WgPublicListenPort)

updateConfig := false
if config.Netclient().WgPublicListenPort == 0 {
config.Netclient().WgPublicListenPort = config.WgPublicListenPort
updateConfig = true
}
if config.Netclient().EndpointIP == nil {
config.Netclient().EndpointIP = config.HostPublicIP
updateConfig = true
}
if updateConfig {
if err := config.WriteNetclientConfig(); err != nil {
slog.Error("error writing endpoint/port netclient config file", "error", err)
}
}
setNatInfo()
slog.Info("configuring netmaker wireguard interface")
if len(config.Servers) == 0 {
Expand Down Expand Up @@ -447,14 +462,14 @@ func UpdateKeys() error {
}

func holePunchWgPort() (pubIP net.IP, pubPort int) {
for _, server := range config.Servers {
portToStun := config.Netclient().ListenPort
pubIP, pubPort = stun.HolePunch(server.StunList, portToStun)
if pubPort == 0 || pubIP == nil || pubIP.IsUnspecified() {
continue
}
break
}
stunServers := []models.StunServer{
abhishek9686 marked this conversation as resolved.
Show resolved Hide resolved
{Domain: "stun1.netmaker.io", Port: 3478},
{Domain: "stun2.netmaker.io", Port: 3478},
{Domain: "stun1.l.google.com", Port: 19302},
{Domain: "stun2.l.google.com", Port: 19302},
}
portToStun := config.Netclient().ListenPort
pubIP, pubPort = stun.HolePunch(stunServers, portToStun)
return
}

Expand Down
20 changes: 14 additions & 6 deletions functions/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ func Register(token string) error {
}

func doubleCheck(host *config.Config, apiServer string) (shouldUpdate bool, err error) {
var shouldUpdateHost bool

if len(config.CurrServer) == 0 { // should indicate a first join
// do a double check of name and uuid
logger.Log(1, "performing first join")
var shouldUpdateHost bool
if len(host.Name) == 0 {
if name, err := os.Hostname(); err == nil {
host.Name = name
Expand All @@ -97,11 +97,19 @@ func doubleCheck(host *config.Config, apiServer string) (shouldUpdate bool, err
host.HostPass = logic.RandomString(32)
shouldUpdateHost = true
}
if shouldUpdateHost {
config.UpdateNetclient(*host)
config.WriteNetclientConfig()
return true, nil
}
}

if host.EndpointIP == nil || host.WgPublicListenPort == 0 {
publicIp, publicPort := holePunchWgPort()
host.EndpointIP = publicIp
host.WgPublicListenPort = publicPort
shouldUpdateHost = true
}

if shouldUpdateHost {
config.UpdateNetclient(*host)
config.WriteNetclientConfig()
return true, nil
}
return
}
Expand Down
3 changes: 3 additions & 0 deletions nmproxy/stun/stun.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/gravitl/netclient/nmproxy/models"
"github.com/gravitl/netmaker/logger"
nmmodels "github.com/gravitl/netmaker/models"
"golang.org/x/exp/slog"
"gortc.io/stun"
)

Expand Down Expand Up @@ -55,6 +56,7 @@ func HolePunch(stunList []nmmodels.StunServer, portToStun int) (publicIP net.IP,
IP: net.ParseIP(""),
Port: portToStun,
}
slog.Debug(fmt.Sprintf("hole punching port %d via stun server %s:%d", portToStun, stunServer.Domain, stunServer.Port))
publicIP, publicPort, err = doStunTransaction(l, s)
if err != nil {
logger.Log(0, "stun transaction failed: ", stunServer.Domain, err.Error())
Expand All @@ -65,6 +67,7 @@ func HolePunch(stunList []nmmodels.StunServer, portToStun int) (publicIP net.IP,
}
break
}
slog.Debug("hole punching complete", "public ip", publicIP.String(), "public port", strconv.Itoa(publicPort))
return
}

Expand Down