From af41111458c6e9fd082e5c6935196673ce3fa9b6 Mon Sep 17 00:00:00 2001 From: Iwasaki Yudai Date: Sun, 13 Aug 2017 13:52:10 +0900 Subject: [PATCH] Show alternative URLs when address is 0.0.0.0 --- server/list_address.go | 28 ++++++++++++++++++++++++++++ server/server.go | 28 +++++++++++++++++----------- 2 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 server/list_address.go diff --git a/server/list_address.go b/server/list_address.go new file mode 100644 index 0000000..de2e8b2 --- /dev/null +++ b/server/list_address.go @@ -0,0 +1,28 @@ +package server + +import ( + "net" +) + +func listAddresses() (addresses []string) { + ifaces, err := net.Interfaces() + if err != nil { + return []string{} + } + + addresses = make([]string, 0, len(ifaces)) + + for _, iface := range ifaces { + ifAddrs, _ := iface.Addrs() + for _, ifAddr := range ifAddrs { + switch v := ifAddr.(type) { + case *net.IPNet: + addresses = append(addresses, v.IP.String()) + case *net.IPAddr: + addresses = append(addresses, v.IP.String()) + } + } + } + + return addresses +} diff --git a/server/server.go b/server/server.go index 48d752f..2516279 100644 --- a/server/server.go +++ b/server/server.go @@ -81,18 +81,28 @@ func (server *Server) Run(ctx context.Context, options ...RunOption) error { } counter := newCounter(time.Duration(server.options.Timeout) * time.Second) - url := server.setupURL() + + path := "/" + if server.options.EnableRandomUrl { + path = "/" + randomstring.Generate(server.options.RandomUrlLength) + "/" + } + url := server.setupURL(server.options.Address, path) + handlers := server.setupHandlers(cctx, cancel, url, counter) srv, err := server.setupHTTPServer(handlers, url) if err != nil { return errors.Wrapf(err, "failed to setup an HTTP server") } - log.Printf("URL: %s", url.String()) + log.Printf("GoTTY server is starting at: %s", url.String()) + if server.options.Address == "0.0.0.0" { + for _, address := range listAddresses() { + log.Printf("Alternative URL: %s", server.setupURL(address, path).String()) + } + } if server.options.PermitWrite { log.Printf("Permitting clients to write input to the PTY.") } - if server.options.Once { log.Printf("Once option is provided, accepting only one client") } @@ -143,18 +153,14 @@ func (server *Server) Run(ctx context.Context, options ...RunOption) error { return err } -func (server *Server) setupURL() *url.URL { - host := net.JoinHostPort(server.options.Address, server.options.Port) +func (server *Server) setupURL(ip string, path string) *url.URL { + host := net.JoinHostPort(ip, server.options.Port) + scheme := "http" - path := "/" - - if server.options.EnableRandomUrl { - path = "/" + randomstring.Generate(server.options.RandomUrlLength) + "/" - } - if server.options.EnableTLS { scheme = "https" } + return &url.URL{Scheme: scheme, Host: host, Path: path} }