From dec7e98c98e4da74f4c0a101bb382cbcbef2d419 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/runtime_gen/core/version.go | 4 ++-- cmd/xgo/test-explorer/index.html | 2 +- cmd/xgo/test-explorer/test_explorer.go | 10 ++++++--- cmd/xgo/trace/main.go | 7 +++++-- cmd/xgo/version.go | 4 ++-- runtime/core/version.go | 4 ++-- support/netutil/netutil.go | 28 ++++++++++++++++++++++++-- 7 files changed, 45 insertions(+), 14 deletions(-) diff --git a/cmd/xgo/runtime_gen/core/version.go b/cmd/xgo/runtime_gen/core/version.go index 628b7ee8..7a35fcbe 100755 --- a/cmd/xgo/runtime_gen/core/version.go +++ b/cmd/xgo/runtime_gen/core/version.go @@ -7,8 +7,8 @@ import ( ) const VERSION = "1.0.37" -const REVISION = "c60db693248f3308d29cd43a95b71de835346d50+1" -const NUMBER = 237 +const REVISION = "535d9a65afa1c396d4fcd488044fb28c4b5740ea+1" +const NUMBER = 241 // these fields will be filled by compiler const XGO_VERSION = "" diff --git a/cmd/xgo/test-explorer/index.html b/cmd/xgo/test-explorer/index.html index 12d7aada..5cdb7d19 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 60740cb0..29a1c3df 100644 --- a/cmd/xgo/test-explorer/test_explorer.go +++ b/cmd/xgo/test-explorer/test_explorer.go @@ -150,7 +150,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 { @@ -268,8 +268,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/cmd/xgo/version.go b/cmd/xgo/version.go index f5f8af8c..05dc0d46 100644 --- a/cmd/xgo/version.go +++ b/cmd/xgo/version.go @@ -3,8 +3,8 @@ package main import "fmt" const VERSION = "1.0.37" -const REVISION = "c60db693248f3308d29cd43a95b71de835346d50+1" -const NUMBER = 237 +const REVISION = "535d9a65afa1c396d4fcd488044fb28c4b5740ea+1" +const NUMBER = 241 func getRevision() string { revSuffix := "" diff --git a/runtime/core/version.go b/runtime/core/version.go index 628b7ee8..7a35fcbe 100644 --- a/runtime/core/version.go +++ b/runtime/core/version.go @@ -7,8 +7,8 @@ import ( ) const VERSION = "1.0.37" -const REVISION = "c60db693248f3308d29cd43a95b71de835346d50+1" -const NUMBER = 237 +const REVISION = "535d9a65afa1c396d4fcd488044fb28c4b5740ea+1" +const NUMBER = 241 // these fields will be filled by compiler const XGO_VERSION = "" 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 "" +}