asterisk pjsip

2021-04-27 11:20:33

升级 asterisk 至 18 之后,忽然发现 sip 命令无法使用了,查看模块列表看到没有加载 sip 模块,手动加载后可以看到警告,推荐使用 pjsip 模块。在编译配置中能够看到 sip 模块已经是过时状态了,不会默认加载。是时候使用 pjsip 通道了。


pjsip 配置文件是 pjsip.conf,相关的 cli commond 在 pjsip 组内。

配置点类型

就配置上来说,pjsip 的配置更复杂了,也更灵活了,按用途等进行了分类,将每个 sip 终端的不同部分放到了不同的配置点上,比如 账号信息、授权、访问控制等等,每个配置点可以相互组合。

endpoint

endpoint 可以看成是一个终端总的配置文件,包括了其它配置的相互绑定

; endpoint 名称,这里假设 6001
[6001]
;type 指示这个节点的类型,endpoint 类型的直接写 endpoint
type=endpoint

context=default
disallow=all
allow=ulaw
transport=transport-udp
auth=auth6001
aors=6001

transport

transport 是指传输节点,配置传输相关的配置,例如 TCP、UDP 或 WebSockets 等协议的配置选项以及 TLS/SSL 等加密方法。每个传输之间不能重复,需要能够指向不同的传输目标。

; 节点名称,在[6001].transport 中指定的名称一致
[transport-udp]
; 传输的类型要写 transport
type=transport
; 传输协议,例如udp,tcp,tls等
protocol=udp
;绑定的地址,指定端口使用格式: bind=0.0.0.0:port
bind=0.0.0.0

;如果传输协议指定了 tls 可以配置下面的证书类信息
cert_file=
priv_key_file=
ca_list_file=
cipher=
method=

需要注意的是修改了transport需要重启asterisk才能生效。

auth

auth的全称是Authentication ,对入站和出站的节点进行认证,可以多个端点共用。

; 认证节点的名称,可以在 [6001].auth 中使用
[auth6001]
; 认证节点的类型要写 auth
type=auth
;认证的类型,可以是用户名密码模式(userpass),也可以是md5
;可选值:userpass,md5
;下面这是用户名密码类型的示例
auth_type=userpass
password=6001
username=6001
;下面这是md5类型的示例
auth_type=md5
md5_cred=51e63a3da6425a39aecc045ec45f1ae8
username=6001

aor

aor 的全称是 address of record,每一个 endpoint 都要配置一个 aor,而且名称要相同。这个名称一般是 sip 电话使用的用户名。以我的理解,这个节点是 sip 节点与 endpoint 节点之间的关联关系。
具体每个连接会生成一个 contact,如果多个设备使用同一个账号,则会生成多个contact。

; aor 的名称,要与 endpoint 名称相同
[6001]
; aor 类型的配置点类型必须是 aor
type=aor
; 每个 sip 终端使用该 aor 的数量限制。
max_contacts=1
;新的注册时,当连接数不够时是否删除旧的连接
remove_existing=true

其中max_contacts和remove_existing的组合很重要,如果只保留一个终端,就要设置remove_existing=true,否则就要保留足够的 max_contacts 数量。

registration

注册模块用来以一个终端的身份向另外一个 sip 服务器注册,在实际应用中为了响应远端服务器来电,还要配置对应的 endpoint 和 aor。

;因为目前没有用到这种类型,暂时不做详细说明
[mytrunk]
type=registration

暂时未用到这种类型,先占个坑

domain_alias

aor 使用的别名,先占坑

[example2.com]
type=domain_alias

acl

访问控制列表,这是 endpoints 的防火墙,在acl.conf模块中也可以配置类似的功能,不过家里会可以根据不同终端配置的更细致。

;从 acl.conf 文件中直接读取
[acl]
type=acl
acl=example_named_acl1

; 直接定义
[acl]
type=acl
deny=0.0.0.0/0.0.0.0
permit=209.16.236.0
permit=209.16.236.1

;根据contact来配置
[acl]
type=acl
contactdeny=0.0.0.0/0.0.0.0
contactpermit=209.16.236.0
contactpermit=209.16.236.1

这几种格式是可以混合使用的,对于安全有很大帮助,有条件时要深入了解。

identify

确认配置项可以根据来源 IP 绑定到 endpoint

;节点名称,是否与 endpoint 名称有关联需要确认
[6001]
;identify 类型
type=identify
; 对应的目标 endpoint
endpoint=6001
; 来源 ip 地址
match=203.0.113.1

这个配置项看起来比较简单,但具体用还要进一步了解,比如不在 match 内的访问 endpoint ,是否会与 identify 冲突等。

contact

联系对象是个我认为比较难理解的概念,可以认为是一个具体的由 sip 终端与服务器endpoint之间的连接,在连接时由aor自动创建,也可以配置在aor.contact中。

配置模块间关系

ENDPOINT

Many ENDPOINTs can be associated with many AORs
Zero to many ENDPOINTs can be associated with zero to one AUTHs
Zero to many ENDPOINTs can be associated with at least one TRANSPORT
Zero to one ENDPOINTs can be associated with an IDENTIFY

REGISTRATION

Zero to many REGISTRATIONs can be associated with zero to one AUTHs
Zero to many REGISTRATIONs can be associated with at least one TRANSPORT

AOR

Many ENDPOINTs can be associated with many AORs
Many AORs can be associated with many CONTACTs

CONTACT

Many CONTACTs can be associated with many AORs

IDENTIFY

Zero to One ENDPOINTs can be associated with an IDENTIFY object

ACL, DOMAIN_ALIAS

These objects don't have a direct configuration relationship to the other objects.

各个模块间有多种关系和顺序,这张关系图可以加深理解。
微信图片_20210906170315.png

标注的注解图
ERD_key.PNG

建议

这里并没有列出所有的配置项,有必要时看一下pjsip官方列表

Copyright tg-blog 京ICP备15066502号-2