Skip to content

Instantly share code, notes, and snippets.

@gdunstone
Last active August 2, 2017 15:55
Show Gist options
  • Save gdunstone/492f35656747fdba02b42b7c9be70079 to your computer and use it in GitHub Desktop.
Save gdunstone/492f35656747fdba02b42b7c9be70079 to your computer and use it in GitHub Desktop.
surgemq simpleserver and chill client
package main
import (
"fmt"
"github.com/surge/glog"
"github.com/surgemq/message"
"github.com/surgemq/surgemq/service"
"time"
)
const NAME string = "test"
func onRecvMsg(msg *message.PublishMessage) error {
glog.Infoln(msg.String())
return nil
}
func main() {
myTopic := fmt.Sprintf("/all/%s", NAME)
c := &service.Client{}
msg := message.NewConnectMessage()
msg.SetWillQos(1)
msg.SetVersion(4)
msg.SetCleanSession(false)
msg.SetClientId([]byte(NAME))
msg.SetKeepAlive(10)
msg.SetWillTopic([]byte(myTopic))
msg.SetWillMessage([]byte(fmt.Sprintf("HELLO I AM %s", NAME)))
msg.SetUsername([]byte(NAME))
msg.SetPassword([]byte("supersecretpassword"))
err := c.Connect("tcp://127.0.0.1:1883", msg)
if err != nil {
glog.Fatalln(err)
}
// Creates a new SUBSCRIBE message to subscribe to topic "abc"
submsg := message.NewSubscribeMessage()
submsg.AddTopic([]byte(myTopic), 0)
subErr := c.Subscribe(submsg, nil, onRecvMessage)
if subErr != nil {
glog.Fatalln(subErr)
}
// Creates a new PUBLISH message with the appropriate contents for publishing
pubmsg := message.NewPublishMessage()
pubmsg.SetPacketId(222)
pubmsg.SetQoS(0)
for {
// Publishes to the server by sending the message
payload := time.Now().String()
glog.Infoln(payload)
pubmsg.SetPacketId(pubmsg.PacketId() + 1)
pubmsg.SetPayload([]byte(payload))
c.Publish(pubmsg, nil)
time.Sleep(time.Second * 5)
}
c.Disconnect()
}
package main
import (
"github.com/surge/glog"
"github.com/surgemq/surgemq/auth"
"github.com/surgemq/surgemq/service"
"golang.org/x/crypto/bcrypt"
)
// hash, _ := GetBcryptHash("supersecretpassword")
// Thes are functionally identical....
const ACCESSHASH string = "$2a$10$pAfSh55f8cDafSFd1wW1QO9dzHeqcduAbjCsomUzZMVK.3An11j26"
func GetBcryptHash(passwd string) (hashedPassword []byte, err error) {
return bcrypt.GenerateFromPassword([]byte(passwd), bcrypt.DefaultCost)
}
type BCryptAuther struct {
hash []byte
}
func (masterCreds BCryptAuther) Authenticate(id string, cred interface{}) error {
inputPasswordString, _ := cred.(string)
inputPassword := []byte(inputPasswordString)
err := bcrypt.CompareHashAndPassword(masterCreds.hash, inputPassword)
if err == nil {
glog.Warningln("AUTHSUCCESS %s:REDACTED", id)
return nil
}
glog.Warningln("AUTHFAIL %s:%s", id, inputPasswordString)
return auth.ErrAuthFailure
}
func main() {
// register the BCrypt Authenticator
auth.Register("bcryptAuth", &BCryptAuther{hash: []byte(ACCESSHASH)})
svr := &service.Server{
KeepAlive: 360,
ConnectTimeout: 60,
AckTimeout: 30,
TimeoutRetries: -1,
SessionsProvider: "mem",
TopicsProvider: "mem",
Authenticator: "bcryptAuth",
}
/* create plain MQTT listener */
err := svr.ListenAndServe("tcp://:1883")
if err != nil {
glog.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment