#include #include #include #include extern "C" { #include "stellar/stellar.h" #include "stellar/session.h" #include "stellar/stellar_mq.h" #include "stellar/stellar_exdata.h" } #include "cJSON.h" #include "socks_decoder.h" extern "C" int commit_test_result_json(cJSON *node, const char *name); #define unused(x) ((void)(x)) int g_test_socks_decoder_plugin_id = 0; int g_socks_message_topic_id = 0; void *ctx_new(struct session *session, void *plugin_env) { unused(plugin_env); unused(session); cJSON *root = cJSON_CreateObject(); return (void *)root; } void ctx_free(struct session *sess, void *session_ctx, void *plugin_env) { unused(sess); unused(plugin_env); commit_test_result_json((cJSON *)session_ctx, "socks_decoder_test"); } static void append_json(cJSON *root, struct socks_info *info) { cJSON *json_stream = cJSON_CreateObject(); cJSON_AddStringToObject(json_stream, "version", info->version == SOCKS_VERSION_4 ? "SOCKS4" : "SOCKS5"); char ip_str[INET6_ADDRSTRLEN] = {0}; if (info->dst_addr.type == SOCKS_ADDR_IPV4) { inet_ntop(AF_INET, &info->dst_addr.ipv4, ip_str, INET_ADDRSTRLEN); } else if (info->dst_addr.type == SOCKS_ADDR_IPV6) { inet_ntop(AF_INET6, info->dst_addr.ipv6, ip_str, INET6_ADDRSTRLEN); } else { memcpy(ip_str, info->dst_addr.fqdn.iov_base, info->dst_addr.fqdn.iov_len); } cJSON_AddStringToObject(json_stream, "dst_addr", ip_str); cJSON_AddNumberToObject(json_stream, "dst_port", ntohs(info->dst_addr.port)); cJSON_AddStringToObject(json_stream, "user_name", (char *)info->user_name.iov_base); cJSON_AddStringToObject(json_stream, "password", (char *)info->password.iov_base); cJSON_AddItemToObject(root, "socks_info", json_stream); } void test_socks_decoder_on_message(struct session *session, int topic_id, const void *msg, void *per_session_ctx, void *plugin_env) { unused(plugin_env); unused(session); unused(topic_id); struct socks_info *info = (struct socks_info *)msg; cJSON *json_root = (cJSON *)per_session_ctx; append_json(json_root, info); } extern "C" void *SOCKS_DECODER_TEST_PLUG_INIT(struct stellar *st) { g_test_socks_decoder_plugin_id = stellar_session_plugin_register(st, ctx_new, ctx_free, NULL); g_socks_message_topic_id = stellar_mq_get_topic_id(st, SOCKS_MESSAGE_TOPIC); stellar_session_mq_subscribe(st, g_socks_message_topic_id, test_socks_decoder_on_message, g_test_socks_decoder_plugin_id); return NULL; } extern "C" void SOCKS_DECODER_TEST_PLUG_DESTROY(void *plugin_env) { unused(plugin_env); }