mirror of
https://github.com/fhmq/hmq.git
synced 2026-05-02 14:28:34 +00:00
modify
This commit is contained in:
93
plugins/authhttp/authhttp.go
Normal file
93
plugins/authhttp/authhttp.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package authhttp
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/fhmq/hmq/logger"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
const (
|
||||
//AuthHTTP plugin name
|
||||
AuthHTTP = "authhttp"
|
||||
)
|
||||
|
||||
var (
|
||||
config Config
|
||||
log = logger.Get().Named("http")
|
||||
)
|
||||
|
||||
//Config device kafka config
|
||||
type Config struct {
|
||||
AuthURL string `json:"auth"`
|
||||
ACLURL string `json:"onSubscribe"`
|
||||
SuperURL string `json:"onPublish"`
|
||||
}
|
||||
|
||||
//Init init kafak client
|
||||
func Init() {
|
||||
content, err := ioutil.ReadFile("../../plugins/kafka/conf.json")
|
||||
if err != nil {
|
||||
log.Fatal("Read config file error: ", zap.Error(err))
|
||||
}
|
||||
// log.Info(string(content))
|
||||
|
||||
err = json.Unmarshal(content, &config)
|
||||
if err != nil {
|
||||
log.Fatal("Unmarshal config file error: ", zap.Error(err))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//CheckAuth check mqtt connect
|
||||
func CheckAuth(clientID, username, password string) bool {
|
||||
payload := fmt.Sprintf("username=%s&password=%s&clientid=%s", username, password, clientID)
|
||||
resp, err := http.Post(config.AuthURL,
|
||||
"application/x-www-form-urlencoded",
|
||||
strings.NewReader(payload))
|
||||
if err != nil {
|
||||
log.Error("request acl: ", zap.Error(err))
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
//CheckSuper check mqtt connect
|
||||
func CheckSuper(clientID, username, password string) bool {
|
||||
payload := fmt.Sprintf("username=%s&password=%s&clientid=%s", username, password, clientID)
|
||||
resp, err := http.Post(config.SuperURL,
|
||||
"application/x-www-form-urlencoded",
|
||||
strings.NewReader(payload))
|
||||
if err != nil {
|
||||
log.Error("request acl: ", zap.Error(err))
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
//CheckACL check mqtt connect
|
||||
func CheckACL(username, access, topic string) bool {
|
||||
url := fmt.Sprintf(config.ACLURL+"?username=%s&access=%s&topic=%s", username, access, topic)
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
// handle error
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
5
plugins/authhttp/conf.json
Normal file
5
plugins/authhttp/conf.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"auth": "http://127.0.0.1:9090/mqtt/auth",
|
||||
"acl": "http://127.0.0.1:9090/mqtt/acl",
|
||||
"super": "http://127.0.0.1:9090/mqtt/superuser"
|
||||
}
|
||||
25
plugins/elements.go
Normal file
25
plugins/elements.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package plugins
|
||||
|
||||
const (
|
||||
//Connect mqtt connect
|
||||
Connect = "connect"
|
||||
//Publish mqtt publish
|
||||
Publish = "publish"
|
||||
//Subscribe mqtt sub
|
||||
Subscribe = "subscribe"
|
||||
//Unsubscribe mqtt sub
|
||||
Unsubscribe = "unsubscribe"
|
||||
//Disconnect mqtt disconenct
|
||||
Disconnect = "disconnect"
|
||||
)
|
||||
|
||||
//Elements kafka publish elements
|
||||
type Elements struct {
|
||||
ClientID string `json:"clientid"`
|
||||
Username string `json:"username"`
|
||||
Topic string `json:"topic"`
|
||||
Payload string `json:"payload"`
|
||||
Timestamp int64 `json:"ts"`
|
||||
Size int32 `json:"size"`
|
||||
Action string `json:"action"`
|
||||
}
|
||||
10
plugins/kafka/conf.json
Normal file
10
plugins/kafka/conf.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"addr": [
|
||||
"127.0.0.1:9090"
|
||||
],
|
||||
"onConnect": "onConnect",
|
||||
"onPublish": "onPublish",
|
||||
"onSubscribe": "onSubscribe",
|
||||
"onDisconnect": "onDisconnect",
|
||||
"onUnsubscribe": "onUnsubscribe"
|
||||
}
|
||||
106
plugins/kafka/kafka.go
Normal file
106
plugins/kafka/kafka.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package kafka
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"time"
|
||||
|
||||
"github.com/Shopify/sarama"
|
||||
"github.com/fhmq/hmq/logger"
|
||||
"github.com/fhmq/hmq/plugins"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
const (
|
||||
//Kafka plugin name
|
||||
Kafka = "kafka"
|
||||
)
|
||||
|
||||
var (
|
||||
kafkaClient sarama.AsyncProducer
|
||||
config Config
|
||||
log = logger.Get().Named("kafka")
|
||||
)
|
||||
|
||||
//Config device kafka config
|
||||
type Config struct {
|
||||
Addr []string `json:"addr"`
|
||||
ConnectTopic string `json:"onConnect"`
|
||||
SubscribeTopic string `json:"onSubscribe"`
|
||||
PublishTopic string `json:"onPublish"`
|
||||
UnsubscribeTopic string `json:"onUnsubscribe"`
|
||||
DisconnectTopic string `json:"onDisconnect"`
|
||||
}
|
||||
|
||||
//Init init kafak client
|
||||
func Init() {
|
||||
content, err := ioutil.ReadFile("../../plugins/kafka/conf.json")
|
||||
if err != nil {
|
||||
log.Fatal("Read config file error: ", zap.Error(err))
|
||||
}
|
||||
// log.Info(string(content))
|
||||
|
||||
err = json.Unmarshal(content, &config)
|
||||
if err != nil {
|
||||
log.Fatal("Unmarshal config file error: ", zap.Error(err))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//connect
|
||||
func connect() {
|
||||
var err error
|
||||
conf := sarama.NewConfig()
|
||||
kafkaClient, err = sarama.NewAsyncProducer(config.Addr, conf)
|
||||
if err != nil {
|
||||
log.Fatal("create kafka async producer failed: ", zap.Error(err))
|
||||
}
|
||||
|
||||
go func() {
|
||||
for err := range kafkaClient.Errors() {
|
||||
log.Error("send msg to kafka failed: ", zap.Error(err))
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
//Publish publish to kafka
|
||||
func Publish(e *plugins.Elements) {
|
||||
topic, key := "", ""
|
||||
switch e.Action {
|
||||
case plugins.Connect:
|
||||
topic = config.ConnectTopic
|
||||
case plugins.Publish:
|
||||
topic = config.PublishTopic
|
||||
case plugins.Subscribe:
|
||||
topic = config.SubscribeTopic
|
||||
case plugins.Unsubscribe:
|
||||
topic = config.UnsubscribeTopic
|
||||
case plugins.Disconnect:
|
||||
topic = config.DisconnectTopic
|
||||
default:
|
||||
log.Error("error action: ", zap.String("action", e.Action))
|
||||
return
|
||||
}
|
||||
key = e.Username
|
||||
err := publish(topic, key, e)
|
||||
if err != nil {
|
||||
log.Error("publish kafka error: ", zap.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
func publish(topic, key string, msg *plugins.Elements) error {
|
||||
payload, err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
select {
|
||||
case kafkaClient.Input() <- &sarama.ProducerMessage{
|
||||
Topic: topic,
|
||||
Key: sarama.ByteEncoder(key),
|
||||
Value: sarama.ByteEncoder(payload)}:
|
||||
return nil
|
||||
case <-time.After(1 * time.Minute):
|
||||
return errors.New("send to kafka time out")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user