Commit af411114 by Iwasaki Yudai

Show alternative URLs when address is 0.0.0.0

1 parent 2a2a0347
Showing with 45 additions and 11 deletions
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
}
...@@ -81,18 +81,28 @@ func (server *Server) Run(ctx context.Context, options ...RunOption) error { ...@@ -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 { ...@@ -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"
path := "/"
if server.options.EnableRandomUrl {
path = "/" + randomstring.Generate(server.options.RandomUrlLength) + "/"
}
scheme := "http"
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}
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!