美文网首页
eclipse-mosquitto编写自定义验证插件

eclipse-mosquitto编写自定义验证插件

作者: 申_9a33 | 来源:发表于2023-12-31 00:47 被阅读0次

    源码

    1.下载安装OpenSSL v1.1.1w

    image.png

    2.下载eclipse/mosquitt源码,并使用Visual Studio 打开

    image.png

    3. 修改mosquitto_payload_modification.c文件

    3.1 修改插件初始化函数mosquitto_plugin_init

    // mosquitto_payload_modification.c
    
    int mosquitto_plugin_init(mosquitto_plugin_id_t *identifier, void **user_data, struct mosquitto_opt *opts, int opt_count)
    {
        UNUSED(user_data);
        UNUSED(opts);
        UNUSED(opt_count);
        int err_code = MOSQ_ERR_SUCCESS;
    
        mosq_pid = identifier;
        mosquitto_log_printf(MOSQ_LOG_INFO, "[mosquitto_plugin_init]");
        paser_opts(opts, opt_count); // 解析配置项
    
        // 注册重启
        err_code = mosquitto_callback_register(mosq_pid, MOSQ_EVT_RELOAD, callback_reload, NULL, NULL);
        if (err_code != MOSQ_ERR_SUCCESS) {
            mosquitto_log_printf(MOSQ_LOG_ERR, "[mosquitto_plugin_init][register] MOSQ_EVT_RELOAD err = %d", err_code);
            return err_code;
        }
        // 注册基础验证
        err_code = mosquitto_callback_register(mosq_pid, MOSQ_EVT_BASIC_AUTH, callback_basic_auth, NULL, NULL);
        if (err_code != MOSQ_ERR_SUCCESS) {
            mosquitto_log_printf(MOSQ_LOG_ERR, "[mosquitto_plugin_init][register] MOSQ_EVT_BASIC_AUTH err = %d", err_code);
            return err_code;
        }
        // 注册ACL验证
        err_code = mosquitto_callback_register(mosq_pid, MOSQ_EVT_ACL_CHECK, callback_acl_check, NULL, NULL);
        if (err_code != MOSQ_ERR_SUCCESS) {
            mosquitto_log_printf(MOSQ_LOG_ERR, "[mosquitto_plugin_init][register] MOSQ_EVT_ACL_CHECK err = %d", err_code);
            return err_code;
        }
    
    
        return err_code;
    
    }
    
    • step1: 解析配置项 (这里是直接将配置项原样打印出来)
    • step2: 注册MOSQ_EVT_RELOAD回调函数
    • step3: 注册MOSQ_EVT_BASIC_AUTH回调函数
    • step4: 注册MOSQ_EVT_ACL_CHECK回调函数

    3.2 修改插件清理函数mosquitto_plugin_cleanup

    // mosquitto_payload_modification.c
    
    int mosquitto_plugin_cleanup(void *user_data, struct mosquitto_opt *opts, int opt_count)
    {
        UNUSED(user_data);
        UNUSED(opts);
        UNUSED(opt_count);
        int err_code = MOSQ_ERR_SUCCESS;
        mosquitto_log_printf(MOSQ_LOG_INFO, "[mosquitto_plugin_cleanup]");
    
        err_code = mosquitto_callback_unregister(mosq_pid, MOSQ_EVT_RELOAD, callback_reload, NULL);
        if (err_code != MOSQ_ERR_SUCCESS) {
            mosquitto_log_printf(MOSQ_LOG_ERR, "[mosquitto_plugin_cleanup][unregister] MOSQ_EVT_RELOAD err = %d", err_code);
        }
        err_code = mosquitto_callback_unregister(mosq_pid, MOSQ_EVT_BASIC_AUTH, callback_basic_auth, NULL);
        if (err_code != MOSQ_ERR_SUCCESS) {
            mosquitto_log_printf(MOSQ_LOG_ERR, "[mosquitto_plugin_cleanup][unregister] MOSQ_EVT_BASIC_AUTH err = %d", err_code);
        }
        err_code = mosquitto_callback_unregister(mosq_pid, MOSQ_EVT_ACL_CHECK, callback_acl_check, NULL);
        if (err_code != MOSQ_ERR_SUCCESS) {
            mosquitto_log_printf(MOSQ_LOG_ERR, "[mosquitto_plugin_cleanup][unregister] MOSQ_EVT_ACL_CHECK err = %d", err_code);
        }
    
        return MOSQ_ERR_SUCCESS;
    }
    
    • 这里清理函数将之前注册的函数全部取消掉

    3.3 实现callback_reload

    // mosquitto_payload_modification.c
    
    static int callback_reload(int event, void* event_data, void* userdata) {
        UNUSED(event);
        UNUSED(userdata);
    
        mosquitto_log_printf(MOSQ_LOG_INFO, "[callback_reload]");
    
        struct mosquitto_evt_reload* ed = event_data;
        paser_opts(ed->options, ed->option_count);
        return MOSQ_ERR_SUCCESS;
    }
    
    • 重启重新解析的一次配置项

    3.4 实现callback_basic_auth函数

    static int callback_basic_auth(int event, void* event_data, void* userdata) {
        UNUSED(event);
        UNUSED(userdata);
    
        struct mosquitto_evt_basic_auth* ed = event_data;
        char* client_id = mosquitto_client_id(ed->client);
        char* client_address = mosquitto_client_address(ed->client);
    
        mosquitto_log_printf(MOSQ_LOG_INFO, "[callback_basic_auth] username=%s, password=%s, client_id=%s, client_address=%s", ed->username, ed->password, client_id, client_address);
    
    
        return MOSQ_ERR_SUCCESS;
    }
    
    • 客户端首次登陆时,会回调这里,来验证客户端的登录信息是否合法
    • 逻辑还没有实现,所以这里打印需要验证的常用信息,然后返回SUCCESS

    3.5 实现callback_acl_check函数

    static int callback_acl_check(int event, void* event_data, void* userdata) {
        UNUSED(event);
        UNUSED(userdata);
    
        struct mosquitto_evt_acl_check* ed = event_data;
        char* username = mosquitto_client_username(ed->client);
        char* client_id = mosquitto_client_id(ed->client);
        char* topic = ed->topic;
        int access = ed->access;
        int qos = ed->qos;
        bool retain = ed->retain;
    
        mosquitto_log_printf(MOSQ_LOG_INFO, "[callback_acl_check] username=%s, client_id=%s, topic=%s, access=%d, qos=%d, retain=%d", username, client_id, topic, access, qos, retain);
        
        return MOSQ_ERR_SUCCESS;
    }
    
    • 客户端需要对某个主题进行收发消息,订阅和取消订阅的回调会到这里
    • access可选的操作有MOSQ_ACL_NONE,MOSQ_ACL_READ,MOSQ_ACL_WRITE,MOSQ_ACL_SUBSCRIBE,MOSQ_ACL_UNSUBSCRIBE
    • 目前逻辑没有实现,只是打印一下客户端访问信息,允许所有客户端的所有操作

    4. 使用自定义插件

    4.1 编译自定义插件

    4.1.1 Windows

    image.png

    4.1.2 ubuntu

    apt-get install build-essential
    apt-get install libssl-dev
    apt-get install xsltproc 
    cmake -B ./cmakebuild
    cd ./cmakebuild/plugins/payload-modification
    make
    
    • step1: 安装gcc等编译工具
    • step2: 安装openssl
    • step3: 安装xsltproc
    • step4: cmake 编译到cmakebuild目录
    • step5: 到对应插件目录
    • step6: 得到对应的插件

    4.2 修改配置文件mosquitto.conf

    image.png

    5.启动查看日志

    屏幕截图 2024-01-01 001614.png
    • 如图启动了两个客户端所有的认证已被我们插件代理

    相关文章

      网友评论

          本文标题:eclipse-mosquitto编写自定义验证插件

          本文链接:https://www.haomeiwen.com/subject/macendtx.html