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

2023-12-31  本文已影响0人  申_9a33

源码

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;

}

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;
}

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;
}

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

4.2 修改配置文件mosquitto.conf

image.png

5.启动查看日志

屏幕截图 2024-01-01 001614.png
上一篇 下一篇

猜你喜欢

热点阅读