diff --git a/config.go b/config.go index 830e122..286a89b 100644 --- a/config.go +++ b/config.go @@ -7,6 +7,7 @@ import ( "os" "path" "runtime" + "strings" MQTT "git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git" log "github.com/Sirupsen/logrus" @@ -16,7 +17,7 @@ const DefaultConfigFile = ".mqttcli.cfg" // Under HOME type Config struct { Host string `json:"host"` - Port string `json:"port"` + Port int `json:"port"` UserName string `json:"username"` Password string `json:"password"` } @@ -48,11 +49,11 @@ func UserHomeDir() string { } func getSettingsFromFile(p string, opts *MQTT.ClientOptions) error { confPath := "" - - if p == "~/.mqtt.cfg" || p == "" { - home := UserHomeDir() + home := UserHomeDir() + // replace home to ~ in order to match + p = strings.Replace(p, home, "~", 1) + if p == "~/.mqttcli.cfg" || p == "" { confPath = path.Join(home, DefaultConfigFile) - _, err := os.Stat(confPath) if os.IsNotExist(err) { return err @@ -63,14 +64,18 @@ func getSettingsFromFile(p string, opts *MQTT.ClientOptions) error { ret, err := readFromConfigFile(confPath) if err != nil { + log.Error(err) return err } if ret.Host != "" { - if ret.Port == "" { - ret.Port = "1883" + if ret.Port == 0 { + ret.Port = 1883 } - scheme := "tcp" // FIXME: - brokerUri := fmt.Sprintf("%s://%s:%s", scheme, ret.Host, ret.Port) + scheme := "tcp" + if ret.Port == 8883 { + scheme = "ssl" + } + brokerUri := fmt.Sprintf("%s://%s:%d", scheme, ret.Host, ret.Port) log.Infof("Broker URI: %s", brokerUri) opts.AddBroker(brokerUri) } diff --git a/main.go b/main.go index 574d29a..c4e328c 100644 --- a/main.go +++ b/main.go @@ -45,7 +45,11 @@ func pubsub(c *cli.Context) { if c.Bool("d") { log.SetLevel(log.DebugLevel) } - opts := NewOption(c) + opts, err := NewOption(c) + if err != nil { + log.Error(err) + os.Exit(1) + } client, err := connect(c, opts) if err != nil { log.Error(err) diff --git a/mqtt.go b/mqtt.go index dc098cc..bc0fb26 100644 --- a/mqtt.go +++ b/mqtt.go @@ -3,7 +3,9 @@ package main import ( "crypto/rand" "crypto/tls" + "crypto/x509" "fmt" + "io/ioutil" "time" MQTT "git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git" @@ -61,6 +63,17 @@ func (m *MQTTClient) Subscribe(topic string, qos int) error { return nil } +func getCertPool(pemPath string) (*x509.CertPool, error) { + certs := x509.NewCertPool() + + pemData, err := ioutil.ReadFile(pemPath) + if err != nil { + return nil, err + } + certs.AppendCertsFromPEM(pemData) + return certs, nil +} + // getRandomClientId returns randomized ClientId. func getRandomClientId() string { const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" @@ -73,7 +86,7 @@ func getRandomClientId() string { } // NewOption returns ClientOptions via parsing command line options. -func NewOption(c *cli.Context) *MQTT.ClientOptions { +func NewOption(c *cli.Context) (*MQTT.ClientOptions, error) { opts := MQTT.NewClientOptions() host := c.String("host") @@ -89,14 +102,20 @@ func NewOption(c *cli.Context) *MQTT.ClientOptions { } opts.SetClientId(clientId) + tlsConfig := &tls.Config{InsecureSkipVerify: false} cafile := c.String("cafile") scheme := "tcp" if cafile != "" { scheme = "ssl" + certPool, err := getCertPool(cafile) + if err != nil { + return nil, err + } + tlsConfig.RootCAs = certPool } insecure := true if insecure { - tlsConfig := &tls.Config{InsecureSkipVerify: true} + tlsConfig.InsecureSkipVerify = true opts.SetTlsConfig(tlsConfig) } @@ -115,5 +134,5 @@ func NewOption(c *cli.Context) *MQTT.ClientOptions { opts.AddBroker(brokerUri) } - return opts + return opts, nil } diff --git a/publish.go b/publish.go index 4339553..5cf085f 100644 --- a/publish.go +++ b/publish.go @@ -13,7 +13,11 @@ func publish(c *cli.Context) { log.SetLevel(log.DebugLevel) } - opts := NewOption(c) + opts, err := NewOption(c) + if err != nil { + log.Error(err) + os.Exit(1) + } client, err := connect(c, opts) if err != nil { log.Error(err) diff --git a/subscribe.go b/subscribe.go index ab7ad2a..c042ebc 100644 --- a/subscribe.go +++ b/subscribe.go @@ -11,7 +11,11 @@ func subscribe(c *cli.Context) { if c.Bool("d") { log.SetLevel(log.DebugLevel) } - opts := NewOption(c) + opts, err := NewOption(c) + if err != nil { + log.Error(err) + os.Exit(1) + } if c.Bool("c") { opts.SetCleanSession(false) }