fix: gracefully handle ipv4-mapped addresses / dual-stack (resolve #860)

This commit is contained in:
Ferdinand Mütsch
2025-10-24 10:33:34 +02:00
parent 63c7427985
commit b9487c1d08
2 changed files with 27 additions and 4 deletions

17
main.go
View File

@@ -17,6 +17,7 @@ import (
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/lpar/gzipped/v2"
"github.com/muety/wakapi/utils"
httpSwagger "github.com/swaggo/http-swagger"
_ "gorm.io/driver/mysql"
_ "gorm.io/driver/postgres"
@@ -378,11 +379,15 @@ func listen(handler http.Handler) {
}
if config.UseTLS() {
if s4 != nil {
if s4 != nil && !utils.IPv4HandledByDualStackHttp(s4, s6) { // https://github.com/muety/wakapi/issues/860
slog.Info("👉 Listening for HTTPS... ✅", "address", s4.Addr)
go func() {
if err := s4.ListenAndServeTLS(config.Server.TlsCertPath, config.Server.TlsKeyPath); err != nil {
conf.Log().Fatal(err.Error())
err := err.Error()
if s6 != nil {
err += " - possibly a dual-stack problem (https://github.com/muety/wakapi/issues/860)?"
}
conf.Log().Fatal(err)
}
}()
}
@@ -410,11 +415,15 @@ func listen(handler http.Handler) {
}()
}
} else {
if s4 != nil {
if s4 != nil && !utils.IPv4HandledByDualStackHttp(s4, s6) { // https://github.com/muety/wakapi/issues/860
slog.Info("👉 Listening for HTTP... ✅", "address", s4.Addr)
go func() {
if err := s4.ListenAndServe(); err != nil {
conf.Log().Fatal(err.Error())
err := err.Error()
if s6 != nil {
err += " - possibly a dual-stack problem (https://github.com/muety/wakapi/issues/860)?"
}
conf.Log().Fatal(err)
}
}()
}

14
utils/net.go Normal file
View File

@@ -0,0 +1,14 @@
package utils
import (
"net/http"
"strings"
)
func IPv4HandledByDualStack(ip4, ip6 string) bool {
return strings.HasPrefix(ip6, "[::]") && strings.HasPrefix(ip4, "0.0.0.0")
}
func IPv4HandledByDualStackHttp(server4, server6 *http.Server) bool {
return server4 == nil || (server6 != nil && IPv4HandledByDualStack(server4.Addr, server6.Addr))
}