mirror of
https://github.com/sorenisanerd/gotty.git
synced 2024-12-22 23:17:29 +00:00
Show alternative URLs when address is 0.0.0.0
This commit is contained in:
parent
2a2a034788
commit
af41111458
28
server/list_address.go
Normal file
28
server/list_address.go
Normal 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
|
||||
}
|
@ -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}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user