Skip to content

Commit

Permalink
Launcher: if already selected best IP for an announced server, don't …
Browse files Browse the repository at this point in the history
…repeat the work potentially and wrongly selecting another IP.
  • Loading branch information
luskaner committed Aug 16, 2024
1 parent 3f5c7fe commit 39dc711
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
10 changes: 6 additions & 4 deletions launcher/internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ var (
return
}
}
alreadySelectedIp := false
if serverStart == "auto" {
announcePorts := viper.GetStringSlice("Server.AnnouncePorts")
portsInt := make([]int, len(announcePorts))
Expand All @@ -187,11 +188,12 @@ var (
}
}
fmt.Printf("Waiting 15 seconds for server announcements on LAN on port(s) %s (we are v. %d), you might need to allow 'launcher.exe' in the firewall...\n", strings.Join(announcePorts, ", "), common.AnnounceVersionLatest)
errorCode, selectedServerHost := cmdUtils.ListenToServerAnnouncementsAndSelect(portsInt)
errorCode, selectedServerIp := cmdUtils.ListenToServerAnnouncementsAndSelectBestIp(portsInt)
if errorCode != common.ErrSuccess {
return
} else if selectedServerHost != "" {
serverHost = selectedServerHost
} else if selectedServerIp != "" {
serverHost = selectedServerIp
alreadySelectedIp = true
serverStart = "false"
if serverStop == "auto" {
serverStop = "false"
Expand Down Expand Up @@ -224,7 +226,7 @@ var (
return
}
}
errorCode = config.MapHosts(serverHost, canAddHost)
errorCode = config.MapHosts(serverHost, canAddHost, alreadySelectedIp)
if errorCode != common.ErrSuccess {
errorMayBeConfig = true
return
Expand Down
20 changes: 12 additions & 8 deletions launcher/internal/cmdUtils/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func requiresMapCDN() bool {
return (startTimeParsed.Before(upperLimit) && startTimeParsed.After(now)) || (endTimeParsed.Before(upperLimit) && endTimeParsed.After(now)) || (startTimeParsed.Before(now) && endTimeParsed.After(upperLimit))
}

func (c *Config) MapHosts(host string, canMap bool) (errorCode int) {
func (c *Config) MapHosts(host string, canMap bool, alreadySelectedIp bool) (errorCode int) {
var mapCDN bool
ips := mapset.NewSet[string]()
if requiresMapCDN() {
Expand All @@ -86,15 +86,19 @@ func (c *Config) MapHosts(host string, canMap bool) (errorCode int) {
errorCode = internal.ErrConfigIpMap
return
} else {
resolvedIps := launcherCommon.HostOrIpToIps(host)
ok, ip := SelectBestServerIp(resolvedIps.ToSlice())
if !ok {
fmt.Println("Failed to find a reachable IP for " + host + ".")
errorCode = internal.ErrConfigIpMapFind
return
var ip string
if alreadySelectedIp {
ip = host
} else {
ips.Add(ip)
resolvedIps := launcherCommon.HostOrIpToIps(host)
var ok bool
if ok, ip = SelectBestServerIp(resolvedIps.ToSlice()); !ok {
fmt.Println("Failed to find a reachable IP for " + host + ".")
errorCode = internal.ErrConfigIpMapFind
return
}
}
ips.Add(ip)
}
} else if !server.CheckConnectionFromServer(common.Domain, true) {
fmt.Println("serverStart is false and host matches. " + common.Domain + " must be reachable. Review the host is reachable via this domain to TCP port 443 (HTTPS).")
Expand Down
8 changes: 4 additions & 4 deletions launcher/internal/cmdUtils/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func SelectBestServerIp(ips []string) (ok bool, ip string) {
return
}

func ListenToServerAnnouncementsAndSelect(ports []int) (errorCode int, host string) {
func ListenToServerAnnouncementsAndSelectBestIp(ports []int) (errorCode int, ip string) {
errorCode = common.ErrSuccess
servers := server.LanServersAnnounced(ports)
if servers == nil {
Expand Down Expand Up @@ -119,9 +119,9 @@ func ListenToServerAnnouncementsAndSelect(ports []int) (errorCode int, host stri
}
if len(servers) == 1 {
fmt.Printf("Found a single server \"%s\", will connect to it...\n", serverTags[0])
ok, host = SelectBestServerIp(serversStr[0])
ok, ip = SelectBestServerIp(serversStr[0])
if !ok {
fmt.Println("Server is not reachable. Check the client can connect to", host, "on TCP port 443 (HTTPS)")
fmt.Println("Server is not reachable. Check the client can connect to", ip, "on TCP port 443 (HTTPS)")
errorCode = internal.ErrServerUnreachable
return
}
Expand All @@ -142,7 +142,7 @@ func ListenToServerAnnouncementsAndSelect(ports []int) (errorCode int, host stri
break
}
ips := serversStr[option-1]
ok, host = SelectBestServerIp(ips)
ok, ip = SelectBestServerIp(ips)
if ok {
break
} else {
Expand Down

0 comments on commit 39dc711

Please sign in to comment.