Browse Source

re-add support to write the configuration file when the server validates a new authtoken works

Alan Shreve 12 years ago
parent
commit
179a76b8cf
2 changed files with 50 additions and 11 deletions
  1. 43 11
      src/ngrok/client/config.go
  2. 7 0
      src/ngrok/client/model.go

+ 43 - 11
src/ngrok/client/config.go

@@ -16,20 +16,21 @@ import (
 )
 
 type Configuration struct {
-	HttpProxy          string                          `yaml:"http_proxy"`
-	ServerAddr         string                          `yaml:"server_addr"`
-	InspectAddr        string                          `yaml:"inspect_addr"`
-	TrustHostRootCerts bool                            `yaml:"trust_host_root_certs"`
-	AuthToken          string                          `yaml:"auth_token"`
-	Tunnels            map[string]*TunnelConfiguration `yaml:"tunnels"`
-	LogTo              string
+	HttpProxy          string                          `yaml:"http_proxy,omitempty"`
+	ServerAddr         string                          `yaml:"server_addr,omitempty"`
+	InspectAddr        string                          `yaml:"inspect_addr,omitempty"`
+	TrustHostRootCerts bool                            `yaml:"trust_host_root_certs,omitempty"`
+	AuthToken          string                          `yaml:"auth_token,omitempty"`
+	Tunnels            map[string]*TunnelConfiguration `yaml:"tunnels,omitempty"`
+	LogTo              string                          `yaml:"-"`
+	Path               string                          `yaml:"-"`
 }
 
 type TunnelConfiguration struct {
-	Subdomain string            `yaml:"subdomain"`
-	Hostname  string            `yaml:"hostname"`
-	Protocols map[string]string `yaml:"proto"`
-	HttpAuth  string            `yaml:"auth"`
+	Subdomain string            `yaml:"subdomain,omitempty"`
+	Hostname  string            `yaml:"hostname,omitempty"`
+	Protocols map[string]string `yaml:"proto,omitempty"`
+	HttpAuth  string            `yaml:"auth,omitempty"`
 }
 
 func LoadConfiguration(opts *Options) (config *Configuration, err error) {
@@ -130,6 +131,7 @@ func LoadConfiguration(opts *Options) (config *Configuration, err error) {
 
 	// override configuration with command-line options
 	config.LogTo = opts.logto
+	config.Path = configPath
 	if opts.authtoken != "" {
 		config.AuthToken = opts.authtoken
 	}
@@ -228,3 +230,33 @@ func validateProtocol(proto, propName string) (err error) {
 
 	return
 }
+
+func SaveAuthToken(configPath, authtoken string) (err error) {
+	// read the configuration
+	oldConfigBytes, err := ioutil.ReadFile(configPath)
+	if err != nil {
+		return
+	}
+
+	c := new(Configuration)
+	if err = goyaml.Unmarshal(oldConfigBytes, c); err != nil {
+		return
+	}
+
+	// no need to save, the authtoken is already the correct value
+	if c.AuthToken == authtoken {
+		return
+	}
+
+	// update auth token
+	c.AuthToken = authtoken
+
+	// rewrite configuration
+	newConfigBytes, err := goyaml.Marshal(c)
+	if err != nil {
+		return
+	}
+
+	err = ioutil.WriteFile(configPath, newConfigBytes, 0600)
+	return
+}

+ 7 - 0
src/ngrok/client/model.go

@@ -49,6 +49,7 @@ type ClientModel struct {
 	authToken     string
 	tlsConfig     *tls.Config
 	tunnelConfig  map[string]*TunnelConfiguration
+	configPath    string
 }
 
 func newClientModel(config *Configuration, ctl mvc.Controller) *ClientModel {
@@ -107,6 +108,9 @@ func newClientModel(config *Configuration, ctl mvc.Controller) *ClientModel {
 
 		// tunnel configuration
 		tunnelConfig: config.Tunnels,
+
+		// config path
+		configPath: config.Path,
 	}
 }
 
@@ -240,6 +244,9 @@ func (c *ClientModel) control() {
 	c.serverVersion = authResp.MmVersion
 	c.Info("Authenticated with server, client id: %v", c.id)
 	c.update()
+	if err = SaveAuthToken(c.configPath, c.authToken); err != nil {
+		c.Error("Failed to save auth token: %v", err)
+	}
 
 	// request tunnels
 	reqIdToTunnelConfig := make(map[string]*TunnelConfiguration)