diff --git a/Makefile b/Makefile index 67c0296..af6a37d 100644 --- a/Makefile +++ b/Makefile @@ -1,44 +1,26 @@ # To create a new release of Teleconsole: # - make your changes # - bump VERSION variable in this Makefile -# - run 'make version' -# - commit & push to git -# - run `make release` to create git tags +# - run 'make' +# - THEN commit & push to git +# - run `make release` to create and push the new git tag # To bump the version, edit this variable and run `make version` -export VERSION=0.1.1-beta - +export VERSION=0.2.0-beta OUT=out/teleconsole GOSRC=$(shell find -name "*.go" -print) -TELEPORT=$(shell find ../../gravitational/teleport/lib -name "*.go" -print) -TARBALL="teleconsole-v$(VERSION)-`go env GOOS`-`go env GOARCH`.tar.bz2" # Default target: out/teleconsole $(OUT): $(GOSRC) Makefile $(MAKE) -C version go build -i -ldflags -w -o $(OUT) -# Runs teleconsole against a server hosted in a local VM running -# as 'teleconsole.local' -.PHONY:dev -dev: clean - $(MAKE) $(OUT) - $(MAKE) -C ../telecast clean - $(MAKE) -C ../telecast dev - sleep 3 - out/teleconsole -s teleconsole.local:5000 -insecure - # Makes a new release (pushes tags to Github) .PHONY:release release: version git tag -f $(VERSION) git push --tags --force -# Make version bumps the version -.PHONY:version -version: - @VERSION=$(VERSION) $(MAKE) -C version - .PHONY:clean clean: rm -rf out diff --git a/clt/api_client.go b/clt/api_client.go index 9329b51..c92bafe 100644 --- a/clt/api_client.go +++ b/clt/api_client.go @@ -39,8 +39,14 @@ func NewAPIClient(config *conf.Config, clientVersion string) *APIClient { Endpoint: config.APIEndpointURL, clientVersion: clientVersion, } + // create cookie storage: client.httpClient.Jar, _ = cookiejar.New(nil) + // disable automatic redirects + client.httpClient.CheckRedirect = func(req *http.Request, via []*http.Request) error { + return http.ErrUseLastResponse + } + if config.InsecureHTTPS { fmt.Println("\033[1mWARNING:\033[0m running in insecure mode!") client.httpClient.Transport = &http.Transport{ @@ -53,10 +59,32 @@ func NewAPIClient(config *conf.Config, clientVersion string) *APIClient { // Sends the version of the client to the server and receives a session // cookie. Every new API conversation must start here func (this *APIClient) CheckVersion() error { - resp, err := this.GET("/api/version") - if err != nil { - log.Error(err) - return trace.Wrap(err) + var ( + resp *http.Response + err error + ) + const maxRedirects = 2 + + for i := 0; i <= maxRedirects; i++ { + log.Infof("Getting version from %s", this.Endpoint) + // Request server's version (and report ours): + resp, err = this.GET("/api/version") + if err != nil { + log.Error(err) + return trace.Wrap(err) + } + // Redirect to another less busy server? + if resp.StatusCode == http.StatusTemporaryRedirect { + ep := resp.Header.Get("Location") + if ep == "" { + return trace.Errorf("Invalid redirect from the server") + } + if this.Endpoint, err = url.Parse(ep); err != nil { + return trace.Errorf("Invalid redirect from the server to '%s'", ep) + } + continue + } + break } // HTTP error? if resp.StatusCode != http.StatusOK { @@ -73,18 +101,6 @@ func (this *APIClient) CheckVersion() error { if sv.WarningMsg != "" { fmt.Println("\033[1mWARNING:\033[0m", sv.WarningMsg) } - // change the endpoint if requested: - if sv.Endpoint != "" { - u := &url.URL{ - Scheme: "https", - Host: this.Endpoint.Host, - Path: "/api", - } - cookies := this.httpClient.Jar.Cookies(u) - u.Host = sv.Endpoint - this.Endpoint.Host = sv.Endpoint - this.httpClient.Jar.SetCookies(u, cookies) - } log.Infof("Connecting to https://%s", this.Endpoint.Host) return nil } diff --git a/lib/session.go b/lib/session.go index 5d163e2..4838a91 100644 --- a/lib/session.go +++ b/lib/session.go @@ -58,10 +58,6 @@ type ServerVersion struct { // clients must show this warning message to users if it's not empty WarningMsg string `json:"warn_msg"` - - // server may "redirect" the client to use this endpoint - // instead: - Endpoint string `json:"endpoint"` } func (s *Session) GetNodeHostPort() (host string, port int, err error) { diff --git a/version/Makefile b/version/Makefile index c589ccf..1489b45 100644 --- a/version/Makefile +++ b/version/Makefile @@ -2,7 +2,7 @@ BUILDDATE=`date +%F` GITREF=`git describe --always --tags --dirty --long` # $(VERSION_GO) will be written to version.go -VERSION_GO="/* DO NOT EDIT THIS FILE. IT IS GENERATED BY 'make setver'*/\n\ +VERSION_GO="/* DO NOT EDIT THIS FILE. IT IS GENERATED BY 'make'*/\n\ package version\n\ const( Version = \"$(VERSION)\"\n)\nvar Gitref, BuildDate string\n" @@ -16,5 +16,5 @@ func init() { Gitref = \"$(GITREF)\"\nBuildDate= \"$(BUILDDATE)\"\n} " # .PHONY:setver setver: - @printf $(VERSION_GO) > version.go - @printf $(GITREF_GO) > git.go + @printf $(VERSION_GO) | gofmt > version.go + @printf $(GITREF_GO) | gofmt > git.go diff --git a/version/version.go b/version/version.go index 7b4ba75..d627088 100644 --- a/version/version.go +++ b/version/version.go @@ -1,5 +1,8 @@ -/* DO NOT EDIT THIS FILE. IT IS GENERATED BY 'make setver'*/ - package version - const( Version = "0.1.1-beta" +/* DO NOT EDIT THIS FILE. IT IS GENERATED BY 'make'*/ +package version + +const ( + Version = "0.2.0-beta" ) + var Gitref, BuildDate string