|
| 1 | +/* |
| 2 | + * Copyright (c) 2013 IBM Corp. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * http://www.eclipse.org/legal/epl-v10.html |
| 8 | + * |
| 9 | + * Contributors: |
| 10 | + * Seth Hoenig |
| 11 | + * Allan Stockdill-Mander |
| 12 | + * Mike Robertson |
| 13 | + */ |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "crypto/tls" |
| 19 | + "flag" |
| 20 | + "fmt" |
| 21 | + "golang.org/x/net/proxy" |
| 22 | + "log" |
| 23 | + "net/url" |
| 24 | + |
| 25 | + // "log" |
| 26 | + "os" |
| 27 | + "os/signal" |
| 28 | + "strconv" |
| 29 | + "syscall" |
| 30 | + "time" |
| 31 | + |
| 32 | + MQTT "github.com/eclipse/paho.mqtt.golang" |
| 33 | +) |
| 34 | + |
| 35 | +func onMessageReceived(_ MQTT.Client, message MQTT.Message) { |
| 36 | + fmt.Printf("Received message on topic: %s\nMessage: %s\n", message.Topic(), message.Payload()) |
| 37 | +} |
| 38 | + |
| 39 | +func init() { |
| 40 | + // Pre-register custom HTTP proxy dialers for use with proxy.FromEnvironment |
| 41 | + proxy.RegisterDialerType("http", newHTTPProxy) |
| 42 | + proxy.RegisterDialerType("https", newHTTPProxy) |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Illustrates how to make an MQTT connection with HTTP proxy CONNECT support. |
| 47 | + * Specify proxy via environment variable: eg: ALL_PROXY=https://proxy_host:port |
| 48 | + */ |
| 49 | +func main() { |
| 50 | + MQTT.DEBUG = log.New(os.Stdout, "", 0) |
| 51 | + MQTT.ERROR = log.New(os.Stderr, "", 0) |
| 52 | + |
| 53 | + c := make(chan os.Signal, 1) |
| 54 | + signal.Notify(c, os.Interrupt, syscall.SIGTERM) |
| 55 | + |
| 56 | + hostname, _ := os.Hostname() |
| 57 | + |
| 58 | + server := flag.String("server", "tcp://127.0.0.1:1883", "The full URL of the MQTT server to "+ |
| 59 | + "connect to ex: tcp://127.0.0.1:1883") |
| 60 | + topic := flag.String("topic", "#", "Topic to subscribe to") |
| 61 | + qos := flag.Int("qos", 0, "The QoS to subscribe to messages at") |
| 62 | + clientid := flag.String("clientid", hostname+strconv.Itoa(time.Now().Second()), "A clientid for the connection") |
| 63 | + username := flag.String("username", "", "A username to authenticate to the MQTT server") |
| 64 | + password := flag.String("password", "", "Password to match username") |
| 65 | + token := flag.String("token", "", "An optional token credential to authenticate with") |
| 66 | + skipVerify := flag.Bool("skipVerify", false, "Controls whether TLS certificate is verified") |
| 67 | + flag.Parse() |
| 68 | + |
| 69 | + connOpts := MQTT.NewClientOptions().AddBroker(*server). |
| 70 | + SetClientID(*clientid). |
| 71 | + SetCleanSession(true). |
| 72 | + SetProtocolVersion(4) |
| 73 | + |
| 74 | + if *username != "" { |
| 75 | + connOpts.SetUsername(*username) |
| 76 | + if *password != "" { |
| 77 | + connOpts.SetPassword(*password) |
| 78 | + } |
| 79 | + } else if *token != "" { |
| 80 | + connOpts.SetCredentialsProvider(func() (string, string) { |
| 81 | + return "unused", *token |
| 82 | + }) |
| 83 | + } |
| 84 | + |
| 85 | + connOpts.SetTLSConfig(&tls.Config{InsecureSkipVerify: *skipVerify, ClientAuth: tls.NoClientCert}) |
| 86 | + |
| 87 | + connOpts.OnConnect = func(c MQTT.Client) { |
| 88 | + if token := c.Subscribe(*topic, byte(*qos), onMessageReceived); token.Wait() && token.Error() != nil { |
| 89 | + panic(token.Error()) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // Illustrates customized TLS configuration prior to connection attempt |
| 94 | + connOpts.OnConnectAttempt = func(broker *url.URL, tlsCfg *tls.Config) *tls.Config { |
| 95 | + cfg := tlsCfg.Clone() |
| 96 | + cfg.ServerName = broker.Hostname() |
| 97 | + return cfg |
| 98 | + } |
| 99 | + |
| 100 | + client := MQTT.NewClient(connOpts) |
| 101 | + if token := client.Connect(); token.Wait() && token.Error() != nil { |
| 102 | + panic(token.Error()) |
| 103 | + } else { |
| 104 | + fmt.Printf("Connected to %s\n", *server) |
| 105 | + } |
| 106 | + |
| 107 | + <-c |
| 108 | +} |
0 commit comments