目录

gmqtt是什么

Gmqtt 是一个灵活、高性能的 MQTT broker库,在 golang 中完全实现了 MQTT 协议 V3.x 和 V5

Gmqtt broker库具备以下特点:

  • 提供hook来自定义broker的一些行为(身份验证、ACL 等)。详见server/hooks.go
  • 支持 tls/ssl 和 websocket
  • 提供灵活的可插拔机制。有关详细信息,详见server/plugin.go和/plugin
  • 提供go接口扩展与服务器交互。例如,扩展或插件可以通过函数调用发布消息或添加/删除订阅。有关详细信息,详见server/server.go中的Server接口和admin插件
  • 提供指标数据(通过使用 Prometheus)(插件:prometheus
  • 提供 GRPC 和 REST API 与服务器交互。(插件:admin
  • 提供会话持久化,这意味着broker可以在重启后恢复会话数据。目前,仅支持 redis
  • 提供集群,请参阅federation plugin插件,获取示例和详细信息。

gmqctl介绍

为了简化插件开发,Gmqtt提供了插件模板生成工具,通过命令行可以快速的生成插件模板,令开发者可以更专注于业务实现,gmqctl就是这个命令工具。

$ gmqctl gen plugin --help
code generator

Usage:
  gmqctl gen plugin [flags]

Examples:
The following command will generate a code template for the 'awesome' plugin, which makes use of OnBasicAuth and OnSubscribe hook and enables the configuration in ./plugins directory.

gmqctl gen plugin -n awesome -H OnBasicAuth,OnSubscribe -c true -o ./plugins

Flags:
  -c, --config          Whether the plugin needs a configuration.
  -h, --help            help for plugin
  -H, --hooks string    The hooks use by the plugin, multiple hooks are separated by ','
  -n, --name string     The plugin name.
  -o, --output string   The output directory.

gmqctl生成插件模板

.\gmqctl.exe gen plugin -n jmfortest -H OnBasicAuth -c true -o E:\src\gmqtt\plugin

gmqctl修改已有atuh插件

我们在原有的auth验证方式,补充MD5Twice的验证方式:

  • 在cmd/gmqttd/default_config.yml配置中配置auth验证方式,补充MD5Twice

    auth:
        # Password hash type. (plain | md5 | sha256 | bcrypt | md5twice)
        # Default to MD5.
        hash: md5twice
    
  • plugin/auth/config.go auth插件配置项检查调整

    const (
        Plain  hashType = "plain"
        MD5             = "md5"
        SHA256          = "sha256"
        Bcrypt          = "bcrypt"
        MD5Twice		= "md5twice"
    )
    
    var ValidateHashType = []string{
        Plain, MD5, SHA256, Bcrypt,MD5Twice,
    }
    
  • 补充md5twice的验证行为

    func (a *Auth) validate(username, password string) (permitted bool, err error) {
        if a.config.Hash == MD5Twice{
            return a.validateMD5Twice(username,password)
        }
        .
        .
        .
    }
    func (a *Auth) validateMD5Twice(username string,password string)(permitted bool,err error){
        salt1 := "9ong"
        salt2 := "loadtest"
        str1 := fmt.Sprintf("%s%s",username,salt1)
    
        md51 := md5.Sum([]byte(str1))
        md51str := fmt.Sprintf("%x",md51)
        str2 := fmt.Sprintf("%s%s",md51str,salt2)
        md52 := md5.Sum([]byte(str2))
        token := fmt.Sprintf("%x",md52)
    
        if token== password{
            return true,nil
        }
        return false,errors.New("token failed by 9ong")
    }
    

参考

DrmagicE/gmqtt: Gmqtt is a flexible, high-performance MQTT broker library that fully implements the MQTT protocol V3.x and V5 in golang

gmqtt/README.md at master · DrmagicE/gmqtt

Gmqtt插件机制详解