Show alternative URLs when address is 0.0.0.0

This commit is contained in:
Iwasaki Yudai 2017-08-13 13:52:10 +09:00
parent 2a2a034788
commit af41111458
2 changed files with 45 additions and 11 deletions

28
server/list_address.go Normal file
View File

@ -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
}

View File

@ -81,18 +81,28 @@ func (server *Server) Run(ctx context.Context, options ...RunOption) error {
} }
counter := newCounter(time.Duration(server.options.Timeout) * time.Second) 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) handlers := server.setupHandlers(cctx, cancel, url, counter)
srv, err := server.setupHTTPServer(handlers, url) srv, err := server.setupHTTPServer(handlers, url)
if err != nil { if err != nil {
return errors.Wrapf(err, "failed to setup an HTTP server") 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 { if server.options.PermitWrite {
log.Printf("Permitting clients to write input to the PTY.") log.Printf("Permitting clients to write input to the PTY.")
} }
if server.options.Once { if server.options.Once {
log.Printf("Once option is provided, accepting only one client") 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 return err
} }
func (server *Server) setupURL() *url.URL { func (server *Server) setupURL(ip string, path string) *url.URL {
host := net.JoinHostPort(server.options.Address, server.options.Port) host := net.JoinHostPort(ip, server.options.Port)
scheme := "http" scheme := "http"
path := "/"
if server.options.EnableRandomUrl {
path = "/" + randomstring.Generate(server.options.RandomUrlLength) + "/"
}
if server.options.EnableTLS { if server.options.EnableTLS {
scheme = "https" scheme = "https"
} }
return &url.URL{Scheme: scheme, Host: host, Path: path} return &url.URL{Scheme: scheme, Host: host, Path: path}
} }