From 97b1f7c914053155c857ed0d9632ddae96bb1220 Mon Sep 17 00:00:00 2001 From: tiezhu <1130315273@qq.com> Date: Tue, 28 May 2024 09:12:37 +0800 Subject: [PATCH] change server domain to 0.0.0.0 --- cmd/xgo/test-explorer/index.html | 2 +- cmd/xgo/test-explorer/test_explorer.go | 10 ++++++--- cmd/xgo/trace/main.go | 7 +++++-- support/netutil/netutil.go | 28 ++++++++++++++++++++++++-- 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/cmd/xgo/test-explorer/index.html b/cmd/xgo/test-explorer/index.html index 3631463f..00877755 100644 --- a/cmd/xgo/test-explorer/index.html +++ b/cmd/xgo/test-explorer/index.html @@ -10,7 +10,7 @@ window.onload = function(){ const {renderReact,UrlXgoTestingExplorer} = Open renderReact(root,UrlXgoTestingExplorer,{ - apiPrefix:"http://localhost:8080" + apiPrefix:"http://0.0.0.0:8080" }) } diff --git a/cmd/xgo/test-explorer/test_explorer.go b/cmd/xgo/test-explorer/test_explorer.go index 44b1a262..15ebf052 100644 --- a/cmd/xgo/test-explorer/test_explorer.go +++ b/cmd/xgo/test-explorer/test_explorer.go @@ -146,7 +146,7 @@ type RunResult struct { //go:embed index.html var indexHTML string -const apiPlaceholder = "http://localhost:8080" +const apiPlaceholder = "http://0.0.0.0:8080" func compareGoVersion(a *goinfo.GoVersion, b *goinfo.GoVersion, ignorePatch bool) int { if a.Major != b.Major { @@ -249,8 +249,12 @@ func handle(opts *Options) error { setupOpenHandler(server) return netutil.ServePortHTTP(server, 7070, true, 500*time.Millisecond, func(port int) { - url = fmt.Sprintf("http://localhost:%d", port) - fmt.Printf("Server listen at %s\n", url) + localIp := netutil.GetLocalHost() + url = fmt.Sprintf("http://%s:%d", localIp, port) + fmt.Println("Server listen at:") + fmt.Printf("- Local: http://localhost:%d \r\n", port) + fmt.Printf("- Network: http://%s:%d \r\n", localIp, port) + openURL(url) }) } diff --git a/cmd/xgo/trace/main.go b/cmd/xgo/trace/main.go index 67a10a33..219e3565 100644 --- a/cmd/xgo/trace/main.go +++ b/cmd/xgo/trace/main.go @@ -162,8 +162,11 @@ func serveFile(portStr string, file string) error { port = int(parsePort) } err = netutil.ServePortHTTP(server, port, autoIncrPort, 500*time.Millisecond, func(port int) { - url := fmt.Sprintf("http://localhost:%d", port) - fmt.Printf("Server listen at %s\n", url) + localIp := netutil.GetLocalHost() + url := fmt.Sprintf("http://%s:%d", localIp, port) + fmt.Println("Server listen at:") + fmt.Printf("- Local: http://localhost:%d \r\n", port) + fmt.Printf("- Network: http://%s:%d \r\n", localIp, port) openURL(url) }) diff --git a/support/netutil/netutil.go b/support/netutil/netutil.go index 5f78a840..9b0f112b 100644 --- a/support/netutil/netutil.go +++ b/support/netutil/netutil.go @@ -21,14 +21,14 @@ func IsTCPAddrServing(url string, timeout time.Duration) (bool, error) { func ServePortHTTP(server *http.ServeMux, port int, autoIncrPort bool, watchTimeout time.Duration, watch func(port int)) error { return ServePort(port, autoIncrPort, watchTimeout, watch, func(port int) error { - return http.ListenAndServe(fmt.Sprintf("localhost:%d", port), server) + return http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), server) }) } // suggested watch timeout: 500ms func ServePort(port int, autoIncrPort bool, watchTimeout time.Duration, watch func(port int), doWithPort func(port int) error) error { for { - addr := net.JoinHostPort("localhost", strconv.Itoa(port)) + addr := net.JoinHostPort("0.0.0.0", strconv.Itoa(port)) serving, err := IsTCPAddrServing(addr, 20*time.Millisecond) if err != nil { return err @@ -72,3 +72,27 @@ func watchSignalWithinTimeout(timeout time.Duration, errSignal chan struct{}, ac } action() } + +// get the local area network IP address +func GetLocalHost() string { + addresses, err := net.InterfaceAddrs() + if err != nil { + fmt.Printf("net.InterfaceAddrs failed, err: %v", err.Error()) + return "" + } + + for _, address := range addresses { + if ipNet, ok := address.(*net.IPNet); ok && !ipNet.IP.IsLoopback() { + // IPv4 + if ipNet.IP.To4() != nil { + return ipNet.IP.String() + } + // IPv6 + if ipNet.IP.To16() != nil && ipNet.IP.To4() == nil { + return ipNet.IP.String() + } + } + } + fmt.Printf("no network interface found") + return "" +}