From 353fd4dcbaec93ab756d694c3e0cca064484faca Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Sun, 30 Jul 2023 21:17:22 +0300 Subject: [PATCH] implement #2175 --- core/host/task.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++- iris.go | 2 +- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/core/host/task.go b/core/host/task.go index c16a98747a..464f65a9d4 100644 --- a/core/host/task.go +++ b/core/host/task.go @@ -9,7 +9,9 @@ import ( "errors" "fmt" "io" + "net" "net/http" + "strings" "time" "github.com/kataras/iris/v12/core/netutil" @@ -26,9 +28,72 @@ func WriteStartupLogOnServe(w io.Writer) func(TaskHost) { if addr == "" { addr = h.Supervisor.Server.Addr } + + var listeningURIs = make([]string, 0, 1) + + if host, port, err := net.SplitHostPort(addr); err == nil { // Improve for the issue #2175. + if host == "" || host == "0.0.0.0" { + if ifaces, err := net.Interfaces(); err == nil { + var ips []string + for _, i := range ifaces { + addrs, err := i.Addrs() + if err != nil { + continue + } + for _, localAddr := range addrs { + var ip net.IP + switch v := localAddr.(type) { + case *net.IPNet: + ip = v.IP + case *net.IPAddr: + ip = v.IP + } + if ip != nil && ip.To4() != nil { + if !ip.IsPrivate() { + // let's don't print ips that are not accessible through browser. + continue + } + ips = append(ips, ip.String()) + } + } + } + + for _, ip := range ips { + listeningURI := netutil.ResolveURL(guessScheme, fmt.Sprintf("%s:%s", ip, port)) + + listeningURI = "> Network: " + listeningURI + listeningURIs = append(listeningURIs, listeningURI) + } + } + } + } + + // if len(listeningURIs) == 0 { + // ^ check no need, we want to print the virtual addr too. listeningURI := netutil.ResolveURL(guessScheme, addr) + if len(listeningURIs) > 0 { + listeningURIs[0] = "\n" + listeningURIs[0] + listeningURI = "> Local: " + listeningURI + } + listeningURIs = append(listeningURIs, listeningURI) + _, _ = fmt.Fprintf(w, "Now listening on: %s\nApplication started. Press CTRL+C to shut down.\n", - listeningURI) + strings.Join(listeningURIs, "\n")) + + /* + When :8080 or 0.0.0.0:8080: + Now listening on: + > Network: http://192.168.1.109:8080 + > Network: http://172.25.224.1:8080 + > Local: http://localhost:8080 + Application started. Press CTRL+C to shut down. + + Otherwise: + Iris Version: 12.2.1 + + Now listening on: http://192.168.1.109:8080 + Application started. Press CTRL+C to shut down. + */ } } diff --git a/iris.go b/iris.go index c2f899fd14..4e6d652c8c 100644 --- a/iris.go +++ b/iris.go @@ -38,7 +38,7 @@ import ( ) // Version is the current version of the Iris Web Framework. -const Version = "12.2.0" +const Version = "12.2.1" // Byte unit helpers. const (