如何从KEMI脚本访问Kamailio配置文件?

2024-04-27 14:19:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在Python脚本中访问Kamailio配置文件中的定义和变量。到目前为止,我只能通过self.my_var = int(KSR.pv.get("$sel(cfg_get.my_group.my_var)"))访问全局变量,其中该变量在配置文件中定义为my_group.my_var = 664.。我如何访问定义(要知道#!ifdef MY_DEFINE成功与否)?或者至少是自己读取它们的配置文件

我在official documentation上什么也没找到,甚至$sel(cfg_get.my_group.my_var)我在别处也找到了

更新这些值在运行时(预处理)通常不可用,因此本机代码可以这样使用它们:

   #!define WITH_SIPTRACE
   #!substdef "!SERVER_ID!654!g"
   ...
   request_route {
    
    #!ifdef WITH_SIPTRACE
        xlog("L_NOTICE", "$C(yx)========= server_id=SERVER_ID NEW $rm $pr:$si:$sp$C(xx)\n");
    #!endif
    ...

在Python中可以实现同样的行为吗

更新2KSR.kx.get_def()KSR.kx.get_defn()中找到了部分答案(如下)


Tags: idget定义servervarmy配置文件with
2条回答

我不喜欢这个解决方案,因为它为每个有趣的定义添加了额外的变量

在配置方面:

####### Custom Parameters #########
/*
    Is there a better method to access these flags (!define)?
    For the constants (!substdef) there are KSR.kx.get_def(), KSR.kx.get_defn().
*/

#!ifdef WITH_OPTIONA
my_group.option_a = yes
#!else
my_group.option_a = no
#!endif

 #!ifdef WITH_OPTIONB
my_group.option_b = yes
#!else
my_group.option_b = no
#!endif

在Python方面:

def __init__(self):
    self.my_domain = KSR.kx.get_def("MY_DOMAIN")
    self.server_id = KSR.kx.get_defn("SERVER_ID")
    assert self.my_domain and self.server_id

    self.flags = None
    self.initialized = False

def real_init(self):
    '''
        Object data is initialized here, dynamically,
            because__init__ and child_init are called too early.
    '''
    def read_flags():
        flags = {}
        for i in ['option_a', 'option_b']:
            value = bool(int(KSR.pv.get("$sel(cfg_get.my_group.{})".format(i))))
            if value:
                flags[i[len('option_'):]] = True
        return flags
    self.flags = read_flags()

    self.initialized = True

更新找到部分解决方案:!substdef个常量,但不是!define个常量

您不应该更改定义。这是没有机制的

要生成可配置参数,请通过htable模块使用数据库或curl module+内存缓存

例如,下面是针对auth的htable优化

AUTH WITH CACHING
# authentication with password caching using htable
modparam("htable", "htable", "auth=>size=10;autoexpire=300;")
modparam("auth_db", "load_credentials", "$avp(password)=password")
route[AUTHCACHE] {
if($sht(auth=>$au::passwd)!=$null) {
if (!pv_auth_check("$fd", "$sht(auth=>$au::passwd)", "0", "1")) {
auth_challenge("$fd", “1”);
exit;
}
} else {
# authenticate requests
if (!auth_check("$fd", "subscriber", "1")) {
auth_challenge("$fd", "0");
exit;
}
$sht(auth=>$au::passwd) = $avp(password);
}
# user authenticated - remove auth header
if(!is_method("REGISTER|PUBLISH"))
consume_credentials();
}

下面是如何使用sql从db获取信息

https://kamailio.org/docs/modules/5.0.x/modules/avpops.html#avpops.f.avp_db_query

avp_db_query("select password, ha1 from subscriber where username='$tu'",
    "$avp(i:678);$avp(i:679)");

作为此类优化的开源示例,您可以看到kazoo项目的代码

相关问题 更多 >