Browse Source

fix an issue where logging during authtoken reading was never reported. fix an issue where ngrok could not find the user's home directory on linux when cross-compiled from darwin

Alan Shreve 12 years ago
parent
commit
b136b8f1aa
4 changed files with 28 additions and 19 deletions
  1. 2 2
      Makefile
  2. 20 8
      src/ngrok/client/auth.go
  3. 1 9
      src/ngrok/client/cli.go
  4. 5 0
      src/ngrok/client/main.go

+ 2 - 2
Makefile

@@ -17,11 +17,11 @@ client: deps
 	go install -tags '$(BUILDTAGS)' ngrok/main/ngrok
 	go install -tags '$(BUILDTAGS)' ngrok/main/ngrok
 
 
 client-assets:
 client-assets:
-	go get github.com/inconshreveable/go-bindata
+	go install github.com/inconshreveable/go-bindata
 	bin/go-bindata -o src/ngrok/client/assets assets/client
 	bin/go-bindata -o src/ngrok/client/assets assets/client
 
 
 server-assets:
 server-assets:
-	go get github.com/inconshreveable/go-bindata
+	go install github.com/inconshreveable/go-bindata
 	bin/go-bindata -o src/ngrok/server/assets assets/server
 	bin/go-bindata -o src/ngrok/server/assets assets/server
 
 
 release-client: BUILDTAGS=release
 release-client: BUILDTAGS=release

+ 20 - 8
src/ngrok/client/auth.go

@@ -6,6 +6,7 @@ import (
 	"os"
 	"os"
 	"os/user"
 	"os/user"
 	"path"
 	"path"
+	"sync"
 )
 )
 
 
 /*
 /*
@@ -13,27 +14,42 @@ import (
    home directory.
    home directory.
 */
 */
 var (
 var (
+	once             sync.Once
 	currentAuthToken string
 	currentAuthToken string
 	authTokenFile    string
 	authTokenFile    string
 )
 )
 
 
-func init() {
+func Init() {
 	user, err := user.Current()
 	user, err := user.Current()
+
+	// os.Getenv("HOME") hack is here to support osx -> linux cross-compilation
+	// because user.Current() only cross compiles correctly from osx -> windows
+	homeDir := os.Getenv("HOME")
 	if err != nil {
 	if err != nil {
 		log.Warn("Failed to get user's home directory: %s", err.Error())
 		log.Warn("Failed to get user's home directory: %s", err.Error())
-		return
+	} else {
+		homeDir = user.HomeDir
 	}
 	}
 
 
-	authTokenFile = path.Join(user.HomeDir, ".ngrok")
+	authTokenFile = path.Join(homeDir, ".ngrok")
+
+	log.Debug("Reading auth token from file %s", authTokenFile)
 	tokenBytes, err := ioutil.ReadFile(authTokenFile)
 	tokenBytes, err := ioutil.ReadFile(authTokenFile)
 
 
 	if err == nil {
 	if err == nil {
 		currentAuthToken = string(tokenBytes)
 		currentAuthToken = string(tokenBytes)
+	} else {
+		log.Warn("Failed to read ~/.ngrok for auth token: %s", err.Error())
 	}
 	}
 }
 }
 
 
+func LoadAuthToken() string {
+	once.Do(func() { Init() })
+	return currentAuthToken
+}
+
 func SaveAuthToken(token string) {
 func SaveAuthToken(token string) {
-	if token == "" || token == currentAuthToken || authTokenFile == "" {
+	if token == "" || token == LoadAuthToken() || authTokenFile == "" {
 		return
 		return
 	}
 	}
 
 
@@ -43,7 +59,3 @@ func SaveAuthToken(token string) {
 		log.Warn("Failed to write auth token to file %s: %v", authTokenFile, err.Error())
 		log.Warn("Failed to write auth token to file %s: %v", authTokenFile, err.Error())
 	}
 	}
 }
 }
-
-func LoadAuthToken() string {
-	return currentAuthToken
-}

+ 1 - 9
src/ngrok/client/cli.go

@@ -91,14 +91,6 @@ func parseProtocol(proto string) string {
 	panic("unreachable")
 	panic("unreachable")
 }
 }
 
 
-func parseAuthToken(token string) string {
-	if token != "" {
-		return token
-	} else {
-		return LoadAuthToken()
-	}
-}
-
 func parseArgs() *Options {
 func parseArgs() *Options {
 	authtoken := flag.String(
 	authtoken := flag.String(
 		"authtoken",
 		"authtoken",
@@ -160,7 +152,7 @@ func parseArgs() *Options {
 		protocol:  parseProtocol(*protocol),
 		protocol:  parseProtocol(*protocol),
 		webport:   *webport,
 		webport:   *webport,
 		logto:     *logto,
 		logto:     *logto,
-		authtoken: parseAuthToken(*authtoken),
+		authtoken: *authtoken,
 		hostname:  *hostname,
 		hostname:  *hostname,
 	}
 	}
 }
 }

+ 5 - 0
src/ngrok/client/main.go

@@ -264,6 +264,11 @@ func Main() {
 	// set up logging
 	// set up logging
 	log.LogTo(opts.logto)
 	log.LogTo(opts.logto)
 
 
+	// set up auth token
+	if opts.authtoken == "" {
+		opts.authtoken = LoadAuthToken()
+	}
+
 	// init client state
 	// init client state
 	s := &State{
 	s := &State{
 		status: "connecting",
 		status: "connecting",