测试 Securing Kamailio’s JSON-RPC over HTTP

原文在这里:

https://kaufmania.wordpress.com/2023/09/13/securing-kamailios-json-rpc-over-http/

写的太好了,笔法老练,掌握精准


笔者改了下,kamailio.cfg 全部内容为:

复制代码
#!KAMAILIO

#!defenv RPC_USER
#!defenv RPC_PASS

loadmodule "pv"
loadmodule "ctl"
loadmodule "xlog"
loadmodule "sl"
loadmodule "json"
loadmodule "xhttp"
loadmodule "jsonrpcs"
loadmodule "tcpops"
loadmodule "htable"
loadmodule "auth"
loadmodule "kex"

tcp_accept_no_cl = yes
http_reply_parse = yes

socket_workers = 2
listen = tcp:enp0s3:8081  # 修改成实际网卡
listen = tcp:127.0.0.1:5060
debug = 3
log_stderror = yes
modparam("jsonrpcs", "transport", 0)
modparam("htable", "htable", "rpc_allow=>size=4");

request_route {
    exit;
}

event_route[xhttp:request] {
    route(XHTTP_PORT_CHECK);

    route(XHTTP_RPC_AUTH);

    route(VALIDATE_RPC_METHOD);

    jsonrpc_dispatch();
}

route[XHTTP_PORT_CHECK] {
    if ( $Rp != 8081 ) {
        xcrit("HTTP Request not received on port 8081. reject\n");
        tcp_close_connection();
    }
}

route[XHTTP_RPC_AUTH]{
    if ( $au != $null && $au != $def(RPC_USER) ) {
        xerr("Invalid RPC user : [$au].\n");
        xhttp_reply(
            "403", "Unauthorized", "text/plain", "403 Unauthorized\r\n"
        );
        exit;
    }

    if ( !pv_www_authenticate("$Ri", "$def(RPC_PASS)", "0") ) {
        switch ( $rc ) {
            case -2:
                xerr("Invalid RPC password for user $au\n");
                xhttp_reply(
                    "403", "Unauthorized", "text/plain", "403 Unauthorized\r\n"
                );
                exit;
            case -5:
                xinfo("HTTP request with no crednetials. Send challenge\n");
                www_challenge("$Ri", "0");
                exit;
            default:
                xinfo("Misc. WWW auth failure. $$rc [$rc]\n");
                xhttp_reply(
                    "503", "Server Error", "text/plain", "503 Server Error\r\n"
                );
                exit;
        }
    }

    consume_credentials();
}

route[VALIDATE_RPC_METHOD] {
	json_get_field("$rb", "method", "$avp(rpc_method)");
    $avp(rpc_method) = $(avp(rpc_method){s.unquote});

    if ( $sht(rpc_allow=>$avp(rpc_method)) == 1 ) {
        return;
    }

    xhttp_reply("403", "Not Allowed", "text/plain", "Not Allowed\n");
    exit;
}

event_route[htable:mod-init] {
    $sht(rpc_allow=>stats.fetch)  = 1;
    $sht(rpc_allow=>core.version) = 1;
    $sht(rpc_allow=>mod.stats)    = 1;
}

现在做测试

export RPC_USER=allen

export RPC_PASS=1234

kamailio # 启动 kamailio

curl -X POST -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","method":"stats.fetch","id":"1","params":"all"}' --digest -u allen:1234 http://127.0.0.1:8081 -vvv

输出省略

还是要多动手

相关推荐
一条大祥脚3 天前
26CCPC网络赛 DFS|数学|解方程|异或哈希|通信|二分|倍增|期望DP|最短路计数
通信·倍增·ccpc网络赛·疑惑哈希·期望dp
小贺儿开发5 天前
Unity 局域网遥控幻灯片展示工具 3.0
科技·http·unity·工具·ppt·通信·网页
搞科研的小刘选手8 天前
【中国·海口(线上+线下)】2026年智能感知、通信与海洋应用国际学术会议(ISCMA 2026)
通信·学术会议·会议推荐·智能感知·海洋应用·海口·海南大学
码上有光9 天前
Linux进程通信——共享内存、消息队列和信号量
android·linux·运维·共享内存·通信
Seoyoneh20 天前
呼叫中心系统上云还是本地部署?架构、成本与运维全维度技术对比
人工智能·信息与通信·通信
小贺儿开发21 天前
Unity 家居视频遥控(细节优化)
科技·unity·程序员·udp·视频·工具·通信
Seoyoneh21 天前
智能客服意图识别实战:深度学习与规则引擎融合架构与落地实践
人工智能·信息与通信·通信
物联通信量讯说23 天前
IOTE 2026 释放信号:中国 IoT 出海正在进入“长期运营”阶段
物联网·iot·通信·企业出海
Seoyoneh24 天前
呼叫中心工单系统架构实战:自动建单与闭环流转技术解析
人工智能·信息与通信·通信
Seoyoneh24 天前
智能客服机器人多轮对话与上下文管理:NLU、DST与对话状态机技术实现
人工智能·信息与通信·通信