OpenWRT 通过 3G Modem 加 asterisk 将 GSM 通话转为 SIP

2019-12-31 11:28:02 +08:00
 hiplon

本文主要实现 OpenWRT 系统通过 Huawei 3G Modem 加 asterisk 套件将 GSM 通话转为 SIP 通话

安装 openwrt 下的 asterisk16 套件

opkg update
opkg install asterisk16-app-system asterisk16-chan-dongle asterisk16-pjsip asterisk16-codec-ulaw asterisk16-codec-alaw asterisk16-res-rtp-asterisk asterisk16-bridge-simple

调整 PJSIP 作为默认服务,并且新增几个 PJSIP 账户,用以测试内线通

/etc/asterisk/pjsip.conf

[transport-udp]                                                        
type=transport   
protocol=udp    ;udp,tcp,tls,ws,wss                                        
bind=0.0.0.0

[6003]                                                                          
type=endpoint                                                                 
transport=transport-udp                                                      
context=from-internal                                 
disallow=all                                
allow=ulaw                                                              
auth=6003-auth                                                                
aors=6003                                                                    
                                                                               
[6003-auth]                                                                             
type=auth                                                                       
auth_type=userpass                                                      
password=6003                                                                     
username=6003                                                                 
                                                                            
[6003]                                                                        
type=aor                                                                       
max_contacts=1



[6004]                                                                 
type=endpoint                                                                 
transport=transport-udp                                                        
context=from-internal                                                              
disallow=all                                                                    
allow=ulaw                                                              
auth=6004-auth                                                                     
aors=6004                                                                     
                                                                            
[6004-auth]                                                                   
type=auth                                                                      
auth_type=userpass                                                              
password=6004                                                                   
username=6004                                                                  
                                                                               
[6004]                                                                      
type=aor                                                                              
max_contacts=1 

新增分机拨打模板,

/etc/asterisk/extension.conf

[from-internal]                                                               
exten => _Z.,1,Dial(PJSIP/${EXTEN},60,Trg)                                        
same => n,Hangup() 

用 asterisk 查看分机状态,拨打过程中pjsip show endpoints中显示的状态会从Not in use转换为In use

asterisk -rvvvv
OpenWrt*CLI> pjsip show contacts

  Contact:  <Aor/ContactUri..............................> <Hash....> <Status> <RTT(ms)..>
==========================================================================================

  Contact:  6001/sip:6001@192.168.234.127:53117;transport= 378e2db08b NonQual         nan
  Contact:  6001/sip:6001@192.168.234.127:53117;transport= 378e2db08b NonQual         nan
  Contact:  6003/sip:6003@192.168.234.158:49989;transport= 2ec100f865 NonQual         nan
  Contact:  6003/sip:6003@192.168.234.158:49989;transport= 2ec100f865 NonQual         nan
  Contact:  6004/sip:6004@192.168.104.11:5060              586381001a NonQual         nan
  Contact:  6004/sip:6004@192.168.104.11:5060              586381001a NonQual         nan

Objects found: 6

    -- PJSIP/6001-00000005 is ringing
    -- PJSIP/6001-00000005 is ringing
       > 0x2262b00 -- Strict RTP learning after remote address set to: 192.168.234.127:52518
    -- PJSIP/6001-00000005 answered PJSIP/6004-00000004
       > 0x2270c60 -- Strict RTP learning after remote address set to: 192.168.104.11:11866
    -- Channel PJSIP/6001-00000005 joined 'simple_bridge' basic-bridge <ee120657-8627-4868-b677-cb0d896a2b5a>
    -- Channel PJSIP/6004-00000004 joined 'simple_bridge' basic-bridge <ee120657-8627-4868-b677-cb0d896a2b5a>
       > 0x2262b00 -- Strict RTP switching to RTP target address 192.168.234.127:52518 as source
       > 0x2270c60 -- Strict RTP switching to RTP target address 192.168.104.11:11866 as source
OpenWrt*CLI> pjsip show endpoints

 Endpoint:  <Endpoint/CID.....................................>  <State.....>  <Channels.>
    I/OAuth:  <AuthId/UserName...........................................................>
        Aor:  <Aor............................................>  <MaxContact>
      Contact:  <Aor/ContactUri..........................> <Hash....> <Status> <RTT(ms)..>
  Transport:  <TransportId........>  <Type>  <cos>  <tos>  <BindAddress..................>
   Identify:  <Identify/Endpoint.........................................................>
        Match:  <criteria.........................>
    Channel:  <ChannelId......................................>  <State.....>  <Time.....>
        Exten: <DialedExten...........>  CLCID: <ConnectedLineCID.......>
==========================================================================================

 Endpoint:  6001                                                 In use        1 of inf
     InAuth:  6001-auth/6001
        Aor:  6001                                               1
      Contact:  6001/sip:6001@192.168.234.127:53117;transp 378e2db08b NonQual         nan
  Transport:  transport-udp             udp      0      0  0.0.0.0:5060
    Channel: PJSIP/6001-00000005/AppDial                         Up            00:00:04   
        Exten:                           CLCID: "6004" <6004>

 Endpoint:  6002                                                 Unavailable   0 of inf
     InAuth:  6002-auth/6002
        Aor:  6002                                               1
  Transport:  transport-udp             udp      0      0  0.0.0.0:5060

 Endpoint:  6003                                                 Not in use    0 of inf
     InAuth:  6003-auth/6003
        Aor:  6003                                               1
      Contact:  6003/sip:6003@192.168.234.158:49989;transp 2ec100f865 NonQual         nan
  Transport:  transport-udp             udp      0      0  0.0.0.0:5060

 Endpoint:  6004                                                 In use        1 of inf
     InAuth:  6004-auth/6004
        Aor:  6004                                               1
      Contact:  6004/sip:6004@192.168.104.11:5060          586381001a NonQual         nan
  Transport:  transport-udp             udp      0      0  0.0.0.0:5060
    Channel: PJSIP/6004-00000004/Dial                            Up            00:00:04   
        Exten: 6001                      CLCID: "" <6001>


Objects found: 4

       > 0x2270c60 -- Strict RTP learning complete - Locking on source address 192.168.104.11:11866
       > 0x2262b00 -- Strict RTP learning complete - Locking on source address 192.168.234.127:52518


有条件的情况下建议可以考虑使用 IAX 分机替代 SIP 分机,这样只需要 NAT 打通一个 UDP 端口就能通话,而不用像 SIP 那样要考虑 ALG,ICE,STUN 等方案

下面是新增一个 IAX 分机的用例

opkg update
opkg install asterisk16-chan-iax2

/etc/asterisk/iax.conf

[general]
bindport=4569
bindaddr=0.0.0.0
iaxcompat=yes
nochecksums=yes
disallow=all
allow=ulaw

[6010]
type=friend
username=6010
secret=6010
context=from-internal
host=dynamic
callerid=6010<6010>

/etc/asterisk/extensions.conf

[from-internal]
exten => 6010,1,Dial(IAX2/6010,60,Trg)

由于 3G modem 还没到货,所以目前先更新到这里,等到货后继续配置。

12 月 27 号收货,继续更,用到的型号是 Huawei K3765.

K3765 3G modem

在 openwrt 下配置 dongle 设备,请结合实际数据配置

/etc/asterisk/dongle.conf

[general]
interval=20
[defaults]
context=dongle-in
group=0
exten=+862022221234
[dongle0]
imei=123451234512345

通过 asterisk 控制台查一下设备状态,

asterisk -rvvvv
RiWifi*CLI> dongle show devices
ID           Group State      RSSI Mode Submode Provider Name  Model      Firmware          IMEI             IMSI             Number        
dongle0      0     Free       16   3    3       FFFFFFFFFFFFFF K3765      11.126.03.04.521  123451234512345  123451234512345  Unknown 

接下来新增呼出、呼入的分机配置

/etc/asterisk/extensions.conf

[from-internal]
exten => _1.,1,Dial(Dongle/g0/${EXTEN:1}) ;呼出设置,结合实际,我这边是加了"1"这个前缀,例如我的 SIP 分机要拨打 10011,那么拨号就是 110011
[dongle-in]
exten => +862022221234,1,Dial(IAX2/6010,60,Trg) ;呼入设置,我这边就是配置成所有呼叫直接转到 IAX-6010 分机,复杂点的可以做 IVR,号码本,不过只有一路的电话就不需要搞这么复杂了。

最后,拨号测试

呼入测试

呼出测试

refer: https://www.ppuu.org/2019/12/openwrt-asterisk-dongle-gsm-to-sip/

5907 次点击
所在节点    宽带症候群
32 条回复
Archeb
2019-12-31 11:51:15 +08:00
好复杂,对我来说转发个短信就够了

不过感谢楼主分享精神
tallest
2019-12-31 15:18:39 +08:00
看着好像很好玩儿的样子!!
privil
2019-12-31 15:39:06 +08:00
优秀!好厉害,之前找过类似的方案
hiplon
2019-12-31 15:56:06 +08:00
@privil #3 多谢,写的比较简约
MidLinn
2019-12-31 16:00:24 +08:00
大佬!真会折腾,佩服一下。
hiplon
2019-12-31 16:02:24 +08:00
@MidLinn #5 哈哈,主要还是因为我这边有张联通固话 SIM 卡,出了所在地就会没有信号了,所以才会想起用这个去解决
cyang
2019-12-31 16:04:09 +08:00
优秀!
tallest
2019-12-31 17:08:28 +08:00
话说,大佬,这玩意儿手机卡能不能玩儿啊?
拨号用的啥拨号诶?系统自带的拨号盘可以嘛?
LiYanHong
2019-12-31 17:19:42 +08:00
之前也想这样,出国把卡放家里,还能通过网络用卡,但没找到现成方案,只好用了双享号。
想问下是虽然是 3g dongle,但是用的 gsm 网络?是否有短信方案,这样就完美了。
hiplon
2019-12-31 17:26:03 +08:00
@LiYanHong #9 用的是 GSM 网络,处理短信的话在 extensions.conf 上面加处理指令就行
下面单纯举个例子,还没测试可用性
[dongle-in-sms]
exten => sms,1,Noop(Incoming SMS from ${CALLERID(num)} ${BASE64_DECODE(${SMS_BASE64})})
exten => sms,n,System(smstodingtalk '${CALLERID(num)}: ${BASE64_DECODE(${SMS_BASE64})} ${STRFTIME(${EPOCH},,%Y-%m-%d %H:%M:%S)} - ${DONGLENAME} -
${CALLERID(num)}: ${BASE64_DECODE(${SMS_BASE64})}' >> /var/log/asterisk/sms.txt) ;调用 smstodingtalk 脚本处理短信内容
exten => sms,n,Hangup()

[dongle-in-ussd]
exten => ussd,1,Noop(Incoming USSD: ${BASE64_DECODE(${USSD_BASE64})})
exten => ussd,n,System(smstodingtalk '${STRFTIME(${EPOCH},,%Y-%m-%d %H:%M:%S)} - ${DONGLENAME}: ${BASE64_DECODE(${USSD_BASE64})}')
exten => ussd,n,Hangup()
hiplon
2019-12-31 17:32:17 +08:00
@tallest #8 移动、联通手机卡可以的,符合制式就行,拨号我用的是支持 IAX 的 zoiper
LiYanHong
2019-12-31 17:32:26 +08:00
我觉得把功能整合下,加个 webui,需求应该很大,国外党的福音。
hiplon
2019-12-31 17:37:06 +08:00
@LiYanHong #12 如果用 WebUI 建议直接买个树莓派刷 RasPBX 就行啦,一般 OpenWRT 性能带不动这么大的 WebUI
NSAgold
2019-12-31 18:53:57 +08:00
近期不是说 2g 准备退网了么...这类 modem 现在应该没有支持 VoLTE 的吧
ericFork
2019-12-31 19:12:54 +08:00
类似方案折腾过一整轮了,我来问个角度奇怪的问题,楼主的联通固话 SIM 是哪里办的?现在还能办吗?
hiplon
2019-12-31 20:23:49 +08:00
@ericFork #15 现在好像比较难找了
ericFork
2019-12-31 20:43:25 +08:00
@hiplon #16 你当年是在营业厅办的么?
ziseyinzi
2019-12-31 20:47:35 +08:00
联通要推 2G 了,有没有 VoHSPA 的方案
isyu
2020-01-01 08:32:40 +08:00
还有类似 iax 这样的语音标准,是易于通过 nat 的吗? SIP 和 h323 之类的,在 nat 环境下都有不少问题。
hiplon
2020-01-01 10:18:48 +08:00
@isyu #19 应该没有了,毕竟 SIP 是事实上的行业标准,IAX 其实也很少会用于 UE 终端上面,平时更多的是做 trunk 用

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/633832

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX