Pickup random port when port option is 0

With upgrading go to go1.9 to use http.Server.ServeTLS()
This commit is contained in:
Iwasaki Yudai 2017-08-26 17:23:04 +09:00
parent b2c2db0764
commit 6ab3093956
3 changed files with 40 additions and 38 deletions

2
Godeps/Godeps.json generated
View File

@ -1,6 +1,6 @@
{ {
"ImportPath": "github.com/yudai/gotty", "ImportPath": "github.com/yudai/gotty",
"GoVersion": "go1.7", "GoVersion": "go1.9",
"GodepVersion": "v79", "GodepVersion": "v79",
"Deps": [ "Deps": [
{ {

View File

@ -9,7 +9,6 @@ import (
"log" "log"
"net" "net"
"net/http" "net/http"
"net/url"
"regexp" "regexp"
noesctmpl "text/template" noesctmpl "text/template"
"time" "time"
@ -100,20 +99,13 @@ func (server *Server) Run(ctx context.Context, options ...RunOption) error {
if server.options.EnableRandomUrl { if server.options.EnableRandomUrl {
path = "/" + randomstring.Generate(server.options.RandomUrlLength) + "/" 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, path, counter)
srv, err := server.setupHTTPServer(handlers, url) srv, err := server.setupHTTPServer(handlers)
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("HTTP server is listening 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.")
} }
@ -121,7 +113,28 @@ func (server *Server) Run(ctx context.Context, options ...RunOption) error {
log.Printf("Once option is provided, accepting only one client") log.Printf("Once option is provided, accepting only one client")
} }
listenErr := make(chan error, 1) if server.options.Port == "0" {
log.Printf("Port number configured to `0`, choosing a random port")
}
hostPort := net.JoinHostPort(server.options.Address, server.options.Port)
listener, err := net.Listen("tcp", hostPort)
if err != nil {
return errors.Wrapf(err, "failed to listen at `%s`", hostPort)
}
scheme := "http"
if server.options.EnableTLS {
scheme = "https"
}
host, port, _ := net.SplitHostPort(listener.Addr().String())
log.Printf("HTTP server is listening at: %s", scheme+"://"+host+":"+port+path)
if server.options.Address == "0.0.0.0" {
for _, address := range listAddresses() {
log.Printf("Alternative URL: %s", scheme+"://"+address+":"+port+path)
}
}
srvErr := make(chan error, 1)
go func() { go func() {
if server.options.EnableTLS { if server.options.EnableTLS {
crtFile := homedir.Expand(server.options.TLSCrtFile) crtFile := homedir.Expand(server.options.TLSCrtFile)
@ -129,12 +142,12 @@ func (server *Server) Run(ctx context.Context, options ...RunOption) error {
log.Printf("TLS crt file: " + crtFile) log.Printf("TLS crt file: " + crtFile)
log.Printf("TLS key file: " + keyFile) log.Printf("TLS key file: " + keyFile)
err = srv.ListenAndServeTLS(crtFile, keyFile) err = srv.ServeTLS(listener, crtFile, keyFile)
} else { } else {
err = srv.ListenAndServe() err = srv.Serve(listener)
} }
if err != nil { if err != nil {
listenErr <- err srvErr <- err
} }
}() }()
@ -147,7 +160,7 @@ func (server *Server) Run(ctx context.Context, options ...RunOption) error {
}() }()
select { select {
case err = <-listenErr: case err = <-srvErr:
if err == http.ErrServerClosed { // by gracefull ctx if err == http.ErrServerClosed { // by gracefull ctx
err = nil err = nil
} else { } else {
@ -167,29 +180,19 @@ func (server *Server) Run(ctx context.Context, options ...RunOption) error {
return err return err
} }
func (server *Server) setupURL(ip string, path string) *url.URL { func (server *Server) setupHandlers(ctx context.Context, cancel context.CancelFunc, pathPrefix string, counter *counter) http.Handler {
host := net.JoinHostPort(ip, server.options.Port)
scheme := "http"
if server.options.EnableTLS {
scheme = "https"
}
return &url.URL{Scheme: scheme, Host: host, Path: path}
}
func (server *Server) setupHandlers(ctx context.Context, cancel context.CancelFunc, url *url.URL, counter *counter) http.Handler {
staticFileHandler := http.FileServer( staticFileHandler := http.FileServer(
&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, Prefix: "static"}, &assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, Prefix: "static"},
) )
var siteMux = http.NewServeMux() var siteMux = http.NewServeMux()
siteMux.HandleFunc(url.Path, server.handleIndex) siteMux.HandleFunc(pathPrefix, server.handleIndex)
siteMux.Handle(url.Path+"js/", http.StripPrefix(url.Path, staticFileHandler)) siteMux.Handle(pathPrefix+"js/", http.StripPrefix(pathPrefix, staticFileHandler))
siteMux.Handle(url.Path+"favicon.png", http.StripPrefix(url.Path, staticFileHandler)) siteMux.Handle(pathPrefix+"favicon.png", http.StripPrefix(pathPrefix, staticFileHandler))
siteMux.Handle(url.Path+"css/", http.StripPrefix(url.Path, staticFileHandler)) siteMux.Handle(pathPrefix+"css/", http.StripPrefix(pathPrefix, staticFileHandler))
siteMux.HandleFunc(url.Path+"auth_token.js", server.handleAuthToken)
siteMux.HandleFunc(url.Path+"config.js", server.handleConfig) siteMux.HandleFunc(pathPrefix+"auth_token.js", server.handleAuthToken)
siteMux.HandleFunc(pathPrefix+"config.js", server.handleConfig)
siteHandler := http.Handler(siteMux) siteHandler := http.Handler(siteMux)
@ -203,15 +206,14 @@ func (server *Server) setupHandlers(ctx context.Context, cancel context.CancelFu
wsMux := http.NewServeMux() wsMux := http.NewServeMux()
wsMux.Handle("/", siteHandler) wsMux.Handle("/", siteHandler)
wsMux.HandleFunc(url.Path+"ws", server.generateHandleWS(ctx, cancel, counter)) wsMux.HandleFunc(pathPrefix+"ws", server.generateHandleWS(ctx, cancel, counter))
siteHandler = http.Handler(wsMux) siteHandler = http.Handler(wsMux)
return siteHandler return siteHandler
} }
func (server *Server) setupHTTPServer(handler http.Handler, url *url.URL) (*http.Server, error) { func (server *Server) setupHTTPServer(handler http.Handler) (*http.Server, error) {
srv := &http.Server{ srv := &http.Server{
Addr: url.Host,
Handler: handler, Handler: handler,
} }

View File

@ -1,4 +1,4 @@
box: golang:1.8.3 box: golang:1.9.0
build: build:
steps: steps: