prevent running on custom ports for clients

This commit is contained in:
Alex
2025-02-24 18:36:18 +07:00
committed by Cuong Manh Le
parent a0c5062e3a
commit 62fe14f76b
+15 -2
View File
@@ -1185,6 +1185,7 @@ func tryUpdateListenerConfig(cfg *ctrld.Config, infoLogger *zerolog.Logger, fata
nextdnsMode := nextdns != "" nextdnsMode := nextdns != ""
// For Windows server with local Dns server running, we can only try on random local IP. // For Windows server with local Dns server running, we can only try on random local IP.
hasLocalDnsServer := hasLocalDnsServerRunning() hasLocalDnsServer := hasLocalDnsServerRunning()
notRouter := router.Name() == ""
for n, listener := range cfg.Listener { for n, listener := range cfg.Listener {
lcc[n] = &listenerConfigCheck{} lcc[n] = &listenerConfigCheck{}
if listener.IP == "" { if listener.IP == "" {
@@ -1214,6 +1215,7 @@ func tryUpdateListenerConfig(cfg *ctrld.Config, infoLogger *zerolog.Logger, fata
} }
updated = updated || lcc[n].IP || lcc[n].Port updated = updated || lcc[n].IP || lcc[n].Port
} }
il := mainLog.Load() il := mainLog.Load()
if infoLogger != nil { if infoLogger != nil {
il = infoLogger il = infoLogger
@@ -1298,6 +1300,12 @@ func tryUpdateListenerConfig(cfg *ctrld.Config, infoLogger *zerolog.Logger, fata
tryOldIPPort5354 = false tryOldIPPort5354 = false
tryPort5354 = false tryPort5354 = false
} }
// if not running on a router, we should not try to listen on any port other than 53
// if we do, this will break the dns resolution for the system.
if notRouter {
tryOldIPPort5354 = false
tryPort5354 = false
}
attempts := 0 attempts := 0
maxAttempts := 10 maxAttempts := 10
for { for {
@@ -1309,6 +1317,9 @@ func tryUpdateListenerConfig(cfg *ctrld.Config, infoLogger *zerolog.Logger, fata
if err == nil { if err == nil {
break break
} }
logMsg(il.Info(), n, "error listening on address: %s, error: %v", addr, err)
if !check.IP && !check.Port { if !check.IP && !check.Port {
if fatal { if fatal {
logMsg(mainLog.Load().Fatal(), n, "failed to listen: %v", err) logMsg(mainLog.Load().Fatal(), n, "failed to listen: %v", err)
@@ -1369,14 +1380,16 @@ func tryUpdateListenerConfig(cfg *ctrld.Config, infoLogger *zerolog.Logger, fata
} else { } else {
listener.IP = oldIP listener.IP = oldIP
} }
if check.Port { // if we are not running on a router, we should not try to listen on any port other than 53
// if we do, this will break the dns resolution for the system.
if check.Port && !notRouter {
listener.Port = randomPort() listener.Port = randomPort()
} else { } else {
listener.Port = oldPort listener.Port = oldPort
} }
if listener.IP == oldIP && listener.Port == oldPort { if listener.IP == oldIP && listener.Port == oldPort {
if fatal { if fatal {
logMsg(mainLog.Load().Fatal(), n, "could not listener on %s: %v", net.JoinHostPort(listener.IP, strconv.Itoa(listener.Port)), err) logMsg(mainLog.Load().Fatal(), n, "could not listen on %s: %v", net.JoinHostPort(listener.IP, strconv.Itoa(listener.Port)), err)
} }
ok = false ok = false
break break