Skip to content

Commit

Permalink
change server domain to 0.0.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tiezhuli001 committed May 29, 2024
1 parent f52fb37 commit 97b1f7c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cmd/xgo/test-explorer/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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"
})
}
</script>
Expand Down
10 changes: 7 additions & 3 deletions cmd/xgo/test-explorer/test_explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
})
}
Expand Down
7 changes: 5 additions & 2 deletions cmd/xgo/trace/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
28 changes: 26 additions & 2 deletions support/netutil/netutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ""
}

0 comments on commit 97b1f7c

Please sign in to comment.