与云上的 Asterisk 服务器一起使用时软电话无法连接

Soft phones don't connect when used with Asterisk server on cloud

在 GCP 上设置 Asterisk

我在 Google Cloud Platform 实例上使用过 Asterisk 13,我对 Asterisk 很陌生(几天前开始使用它)。我需要帮助了解如何在我的本地计算机上连接设备(softphone 可以)。

sip.conf

    [general]
    context=public                  ; Default context for incoming calls. Defaults to 'default'
    allowoverlap=no                 ; Disable overlap dialing support. (Default is yes)
    udpbindaddr=0.0.0.0             ; IP address to bind UDP listen socket to (0.0.0.0 binds to all)
    tcpenable=no                    ; Enable server for incoming TCP connections (default is no)
    tcpbindaddr=0.0.0.0             ; IP address for TCP server to bind to (0.0.0.0 binds to all interfaces)
    transport=udp                   ; Set the default transports.  The order determines the primary default transport.
    srvlookup=yes                   ; Enable DNS SRV lookups on outbound calls
    [authentication]
    [basic-options](!)                ; a template
            dtmfmode=rfc2833
            context=from-office
            type=friend
    [natted-phone](!,basic-options)   ; another template inheriting basic-options
            directmedia=no
            host=dynamic
    [public-phone](!,basic-options)   ; another template inheriting basic-options
            directmedia=yes
    [my-codecs](!)                    ; a template for my preferred codecs
            disallow=all
            allow=ilbc
            allow=g729
            allow=gsm
            allow=g723
            allow=ulaw
    [ulaw-phone](!)                   ; and another one for ulaw-only
            disallow=all
            allow=ulaw
    [peer-default]                    ; Added by me, above all is default
            disallow=all
            allow=alaw,ulaw
            type=friend
            host=dynamic
            context=softphone
            secret=<secret>
    [peer-xvhy]
            disallow=all
            allow=alaw,ulaw
            context=softphone
            type=friend
            host=dynamic
            secret=<secret>

地址

这样,我就可以连接我的 phone 了。 在 sip show peers 我看到:

peer-default/peer-default LOCAL_IP                          D  No   No             52629    Unmonitored                                  
peer-xvhy/peer-xvhy       LOCAL_IP                          D  No   No             52629    Unmonitored                                  
2 sip peers [Monitored: 0 online, 0 offline Unmonitored: 2 online, 0 offline]

我觉得很奇怪,两者都有相同的ip和端口。

当我尝试使用 extensions.conf:

调用 phone
[softphone]
exten => 100, 1, Dial(SIP/peer-default)
    same => n, Hangup()

我在 sip set debug on 控制台中看到了这个:

<------------>
-- Executing [100@softphone:1] Dial("SIP/peer-default-00000021", "SIP/peer-default") in new stack
      == Using SIP RTP CoS mark 5
    Audio is at 16760
    Adding codec alaw to SDP
    Adding codec ulaw to SDP
    Adding non-codec 0x1 (telephone-event) to SDP
    Reliably Transmitting (no NAT) to <LOCAL_IP>:52629:
    INVITE sip:peer-default@<LOCAL_IP>:52629;ob SIP/2.0
    Via: SIP/2.0/UDP <LOCAL_IP>:5060;branch=z9hG4bK5664fbba
    Max-Forwards: 70
    From: "jacken-hagar" <sip:peer-default@<LOCAL_IP>>;tag=as3175e9a7
    To: <sip:peer-default@<LOCAL_IP>:52629;ob>
    Contact: <sip:peer-default@<LOCAL_IP>:5060>
    Call-ID: 0e6f6ecb04a151372a04d3e12eea8eae@<LOCAL_IP>:5060
    To: <sip:100@<GCP_IP>>;tag=as78507fab
    CSeq: 102 INVITE
    User-Agent: Asterisk PBX certified/13.21-cert2
    Date: Fri, 06 Jul 2018 17:38:06 GMT
    Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, MESSAGE
    Supported: replaces, timer
    Content-Type: application/sdp
    Content-Length: 282

    v=0
    o=root 886654089 886654089 IN IP4 <LOCAL_IP>
    s=Asterisk PBX certified/13.21-cert2
    c=IN IP4 <LOCAL_IP>
    t=0 0
    m=audio 16760 RTP/AVP 8 0 101
    a=rtpmap:8 PCMA/8000
    a=rtpmap:0 PCMU/8000
    a=rtpmap:101 telephone-event/8000
    a=fmtp:101 0-16
    a=maxptime:150
    a=sendrecv

    ---
        -- Called SIP/peer-default

    <--- SIP read from UDP:<LOCAL_IP>:52629 --->
    SIP/2.0 100 Trying
    Via: SIP/2.0/UDP <LOCAL_IP>:5060;received=<GCP_IP>;branch=z9hG4bK5664fbba
    Call-ID: 0e6f6ecb04a151372a04d3e12eea8eae@<LOCAL_IP>:5060
    From: "jacken-hagar" <sip:peer-default@<LOCAL_IP>>;tag=as3175e9a7
    To: <sip:peer-default@192.168.3.227;ob>
    CSeq: 102 INVITE
    Content-Length: 0

    <------------->
    --- (7 headers 0 lines) ---

    <--- SIP read from UDP:<LOCAL_IP>:52629 --->
    SIP/2.0 180 Ringing
    Via: SIP/2.0/UDP <LOCAL_IP>:5060;received=<GCP_IP>;branch=z9hG4bK5664fbba
    Call-ID: 0e6f6ecb04a151372a04d3e12eea8eae@<LOCAL_IP>:5060
    From: "jacken-hagar" <sip:peer-default@<LOCAL_IP>>;tag=as3175e9a7
    To: <sip:peer-default@192.168.3.227;ob>;tag=ERo8E-rMSwEcwRC31GLgFSGyuafT0zMp
    CSeq: 102 INVITE
    Contact: "jacken-hagar" <sip:peer-default@<LOCAL_IP>:52629;ob>
    Allow: PRACK, INVITE, ACK, BYE, CANCEL, UPDATE, INFO, SUBSCRIBE, NOTIFY, REFER, MESSAGE, OPTIONS
    Content-Length: 0

    <------------->
    --- (9 headers 0 lines) ---
    sip_route_dump: route/path hop: <sip:peer-default@<LOCAL_IP>:52629;ob>
        -- SIP/peer-default-00000022 is ringing

    <--- Transmitting (no NAT) to <LOCAL_IP>:52629 --->
    SIP/2.0 180 Ringing
    Via: SIP/2.0/UDP <LOCAL_IP>:52629;branch=z9hG4bKPjgVfIZ2GZvjLmd.liWqt5Kx4tzfqul90b;received=<LOCAL_IP>;rport=52629
    From: "jacken-hagar" <sip:peer-default@<GCP_IP>>;tag=YdEWucbUlrPjEvqWtnSNwzAamrQraB2D
    To: <sip:100@<GCP_IP>>;tag=as78507fab
    Call-ID: VbDA043j0pr7mzU-WCYMOfp5PS0YGPUR
    CSeq: 26560 INVITE
    Server: Asterisk PBX certified/13.21-cert2
    Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, MESSAGE
    Supported: replaces, timer
    Contact: <sip:100@<LOCAL_IP>:5060>
    Content-Length: 0


    <------------>

    <--- SIP read from UDP:<LOCAL_IP>:52629 --->
    SIP/2.0 200 OK
    Via: SIP/2.0/UDP <LOCAL_IP>:5060;received=<GCP_IP>;branch=z9hG4bK5664fbba
    Call-ID: 0e6f6ecb04a151372a04d3e12eea8eae@<LOCAL_IP>:5060
    From: "jacken-hagar" <sip:peer-default@<LOCAL_IP>>;tag=as3175e9a7
    To: <sip:peer-default@192.168.3.227;ob>;tag=ERo8E-rMSwEcwRC31GLgFSGyuafT0zMp
    CSeq: 102 INVITE
    Allow: PRACK, INVITE, ACK, BYE, CANCEL, UPDATE, INFO, SUBSCRIBE, NOTIFY, REFER, MESSAGE, OPTIONS
    Contact: "jacken-hagar" <sip:peer-default@<LOCAL_IP>:52629;ob>
    Supported: replaces, 100rel, norefersub
    Content-Type: application/sdp
    Content-Length: 282

    v=0
    o=- 3739887487 3739887488 IN IP4 <LOCAL_IP>
    s=pjmedia
    b=AS:117
    t=0 0
    a=X-nat:0
    m=audio 4010 RTP/AVP 8 101
    c=IN IP4 <LOCAL_IP>
    b=TIAS:96000
    a=rtcp:4011 IN IP4 192.168.3.227
    a=sendrecv
    a=rtpmap:8 PCMA/8000
    a=rtpmap:101 telephone-event/8000
    a=fmtp:101 0-16
    <------------->
    --- (11 headers 14 lines) ---
    Found RTP audio format 8
    Found RTP audio format 101
    Found audio description format PCMA for ID 8
    Found audio description format telephone-event for ID 101
    Capabilities: us - (alaw|ulaw), peer - audio=(alaw)/video=(nothing)/text=(nothing), combined - (alaw)
    Non-codec capabilities (dtmf): us - 0x1 (telephone-event|), peer - 0x1 (telephone-event|), combined - 0x1 (telephone-event|)
    Peer audio RTP is at port <LOCAL_IP>:4010
    sip_route_dump: route/path hop: <sip:peer-default@<LOCAL_IP>:52629;ob>
    set_destination: Parsing <sip:peer-default@<LOCAL_IP>:52629;ob> for address/port to send to
    set_destination: set destination to <LOCAL_IP>:52629
    Transmitting (no NAT) to <LOCAL_IP>:52629:
    ACK sip:peer-default@<LOCAL_IP>:52629;ob SIP/2.0
    Via: SIP/2.0/UDP <LOCAL_IP>:5060;branch=z9hG4bK7fa42c02
    Max-Forwards: 70
    From: "jacken-hagar" <sip:peer-default@<LOCAL_IP>>;tag=as3175e9a7
    To: <sip:peer-default@<LOCAL_IP>:52629;ob>;tag=ERo8E-rMSwEcwRC31GLgFSGyuafT0zMp
    Contact: <sip:peer-default@<LOCAL_IP>:5060>
    Call-ID: 0e6f6ecb04a151372a04d3e12eea8eae@<LOCAL_IP>:5060
    CSeq: 102 ACK
    User-Agent: Asterisk PBX certified/13.21-cert2
    Content-Length: 0


    ---
        -- SIP/peer-default-00000022 answered SIP/peer-default-00000021
    Audio is at 19568
    Adding codec alaw to SDP
    Adding codec ulaw to SDP
    Adding non-codec 0x1 (telephone-event) to SDP

    <--- Reliably Transmitting (no NAT) to <LOCAL_IP>:52629 --->
    SIP/2.0 200 OK
    Via: SIP/2.0/UDP <LOCAL_IP>:52629;branch=z9hG4bKPjgVfIZ2GZvjLmd.liWqt5Kx4tzfqul90b;received=<LOCAL_IP>;rport=52629
    From: "jacken-hagar" <sip:peer-default@<GCP_IP>>;tag=YdEWucbUlrPjEvqWtnSNwzAamrQraB2D
    To: <sip:100@<GCP_IP>>;tag=as78507fab
    Call-ID: VbDA043j0pr7mzU-WCYMOfp5PS0YGPUR
    CSeq: 26560 INVITE
    Server: Asterisk PBX certified/13.21-cert2
    Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, MESSAGE
    Supported: replaces, timer
    Contact: <sip:100@<LOCAL_IP>:5060>
    Content-Type: application/sdp
    Content-Length: 282

    v=0
    o=root 401214028 401214028 IN IP4 <LOCAL_IP>
    s=Asterisk PBX certified/13.21-cert2
    c=IN IP4 <LOCAL_IP>
    t=0 0
    m=audio 19568 RTP/AVP 8 0 101
    a=rtpmap:8 PCMA/8000
    a=rtpmap:0 PCMU/8000
    a=rtpmap:101 telephone-event/8000
    a=fmtp:101 0-16
    a=maxptime:150
    a=sendrecv

    <------------>
        -- Channel SIP/peer-default-00000022 joined 'simple_bridge' basic-bridge <f1c38edc-c2d7-4681-ad97-7bcb7f7b19b1>
        -- Channel SIP/peer-default-00000021 joined 'simple_bridge' basic-bridge <f1c38edc-c2d7-4681-ad97-7bcb7f7b19b1>
    set_destination: Parsing <sip:peer-default@<LOCAL_IP>:52629;ob> for address/port to send to
    set_destination: set destination to <LOCAL_IP>:52629
    Audio is at 16760
    Adding codec alaw to SDP
    Adding codec ulaw to SDP
    Adding non-codec 0x1 (telephone-event) to SDP
    Reliably Transmitting (no NAT) to <LOCAL_IP>:52629:
    INVITE sip:peer-default@<LOCAL_IP>:52629;ob SIP/2.0
    Via: SIP/2.0/UDP <LOCAL_IP>:5060;branch=z9hG4bK71beb7ff
    Max-Forwards: 70
    From: "jacken-hagar" <sip:peer-default@<LOCAL_IP>>;tag=as3175e9a7
    To: <sip:peer-default@<LOCAL_IP>:52629;ob>;tag=ERo8E-rMSwEcwRC31GLgFSGyuafT0zMp
    Contact: <sip:peer-default@<LOCAL_IP>:5060>
    Call-ID: 0e6f6ecb04a151372a04d3e12eea8eae@<LOCAL_IP>:5060
    CSeq: 103 INVITE
    User-Agent: Asterisk PBX certified/13.21-cert2
    Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, MESSAGE
    Supported: replaces, timer
    X-asterisk-Info: SIP re-invite (External RTP bridge)
    Content-Type: application/sdp
    Content-Length: 281

    v=0
    o=root 886654089 886654090 IN IP4 <LOCAL_IP>
    s=Asterisk PBX certified/13.21-cert2
    c=IN IP4 <LOCAL_IP>
    t=0 0
    m=audio 4008 RTP/AVP 8 0 101
    a=rtpmap:8 PCMA/8000
    a=rtpmap:0 PCMU/8000
    a=rtpmap:101 telephone-event/8000
    a=fmtp:101 0-16
    a=maxptime:150
    a=sendrecv

    ---

    <--- SIP read from UDP:<LOCAL_IP>:52629 --->
    SIP/2.0 200 OK
    Via: SIP/2.0/UDP <LOCAL_IP>:5060;received=<GCP_IP>;branch=z9hG4bK71beb7ff
    Call-ID: 0e6f6ecb04a151372a04d3e12eea8eae@<LOCAL_IP>:5060
    From: "jacken-hagar" <sip:peer-default@<LOCAL_IP>>;tag=as3175e9a7
    To: <sip:peer-default@192.168.3.227;ob>;tag=ERo8E-rMSwEcwRC31GLgFSGyuafT0zMp
    CSeq: 103 INVITE
    Contact: "jacken-hagar" <sip:peer-default@<LOCAL_IP>:52629;ob>
    Allow: PRACK, INVITE, ACK, BYE, CANCEL, UPDATE, INFO, SUBSCRIBE, NOTIFY, REFER, MESSAGE, OPTIONS
    Supported: replaces, 100rel, timer, norefersub
    Content-Type: application/sdp
    Content-Length: 282

    v=0
    o=- 3739887487 3739887489 IN IP4 <LOCAL_IP>
    s=pjmedia
    b=AS:117
    t=0 0
    a=X-nat:0
    m=audio 4010 RTP/AVP 8 101
    c=IN IP4 <LOCAL_IP>
    b=TIAS:96000
    a=rtcp:4011 IN IP4 192.168.3.227
    a=sendrecv
    a=rtpmap:8 PCMA/8000
    a=rtpmap:101 telephone-event/8000
    a=fmtp:101 0-16
    <------------->
    --- (11 headers 14 lines) ---
    Found RTP audio format 8
    Found RTP audio format 101
    Found audio description format PCMA for ID 8
    Found audio description format telephone-event for ID 101
    Capabilities: us - (alaw|ulaw), peer - audio=(alaw)/video=(nothing)/text=(nothing), combined - (alaw)
    Non-codec capabilities (dtmf): us - 0x1 (telephone-event|), peer - 0x1 (telephone-event|), combined - 0x1 (telephone-event|)
    Peer audio RTP is at port <LOCAL_IP>:4010
    set_destination: Parsing <sip:peer-default@<LOCAL_IP>:52629;ob> for address/port to send to
    set_destination: set destination to <LOCAL_IP>:52629
    Transmitting (no NAT) to <LOCAL_IP>:52629:
    ACK sip:peer-default@<LOCAL_IP>:52629;ob SIP/2.0
    Via: SIP/2.0/UDP <LOCAL_IP>:5060;branch=z9hG4bK3f0206ce
    Max-Forwards: 70
    From: "jacken-hagar" <sip:peer-default@<LOCAL_IP>>;tag=as3175e9a7
    To: <sip:peer-default@<LOCAL_IP>:52629;ob>;tag=ERo8E-rMSwEcwRC31GLgFSGyuafT0zMp
    Contact: <sip:peer-default@<LOCAL_IP>:5060>
    Call-ID: 0e6f6ecb04a151372a04d3e12eea8eae@<LOCAL_IP>:5060
    CSeq: 103 ACK
    User-Agent: Asterisk PBX certified/13.21-cert2
    Content-Length: 0


    ---
    Retransmitting #1 (no NAT) to <LOCAL_IP>:52629:
    SIP/2.0 200 OK
    Via: SIP/2.0/UDP <LOCAL_IP>:52629;branch=z9hG4bKPjgVfIZ2GZvjLmd.liWqt5Kx4tzfqul90b;received=<LOCAL_IP>;rport=52629
    From: "jacken-hagar" <sip:peer-default@<GCP_IP>>;tag=YdEWucbUlrPjEvqWtnSNwzAamrQraB2D
    To: <sip:100@<GCP_IP>>;tag=as78507fab
    Call-ID: VbDA043j0pr7mzU-WCYMOfp5PS0YGPUR
    CSeq: 26560 INVITE
    Server: Asterisk PBX certified/13.21-cert2
    Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, MESSAGE
    Supported: replaces, timer
    Contact: <sip:100@<LOCAL_IP>:5060>
    Content-Type: application/sdp
    Content-Length: 282

    v=0
    o=root 401214028 401214028 IN IP4 <LOCAL_IP>
    s=Asterisk PBX certified/13.21-cert2
    c=IN IP4 <LOCAL_IP>
    t=0 0
    m=audio 19568 RTP/AVP 8 0 101
    a=rtpmap:8 PCMA/8000
    a=rtpmap:0 PCMU/8000
    a=rtpmap:101 telephone-event/8000
    a=fmtp:101 0-16
    a=maxptime:150
    a=sendrecv
    ```
    The retransmit log appears 10 times and then the call disconnects
    this happens within 32 seconds every time.

    with this error:
    ```
    WARNING[25825]: chan_sip.c:4069 retrans_pkt: Retransmission timeout reached on transmission f60f2b6a32c0d48cc30f2e0f9c5174c8 for seqno 1 (Critical Response) -- See https://wiki.asterisk.org/wiki/display/AST/SIP+Retransmissions
    Packet timed out after 32000ms with no response

我从重传日志中可以了解到的(无论多少)是:

From: "jacken-hagar" <sip:peer- 
default@<GCP_IP>>;tag=YdEWucbUlrPjEvqWtnSNwzAamrQraB2D
To: <sip:100@<GCP_IP>>;tag=as78507fab

是它应该发送到 LOCAL_IP 和从 LOCAL_IP 发送的问题,因为那是 sip show peers 所说的关于我的软 phones 的地址。

终于解决了这个问题,希望这对其他人有帮助。

Retransmitting #1 (no NAT) to <LOCAL_IP>:52629:
SIP/2.0 200 OK
Via: SIP/2.0/UDP <LOCAL_IP>:52629;branch=z9hG4bKPjgVfIZ2GZvjLmd.liWqt5Kx4tzfqul90b;received=<LOCAL_IP>;rport=52629
From: "jacken-hagar" <sip:peer-default@<GCP_IP>>;tag=YdEWucbUlrPjEvqWtnSNwzAamrQraB2D
To: <sip:100@<GCP_IP>>;tag=as78507fab
Call-ID: VbDA043j0pr7mzU-WCYMOfp5PS0YGPUR
CSeq: 26560 INVITE
Server: Asterisk PBX certified/13.21-cert2
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, MESSAGE
Supported: replaces, timer
Contact: <sip:100@<LOCAL_IP>:5060>
Content-Type: application/sdp
Content-Length: 282

每次您在 sip set debug on 日志中看到上述内容时,您应该关注联系人 header.

Contact: <sip:100@<LOCAL_IP>:5060>

在我的例子中,<LOCAL_IP> 是我的 GCP 实例的内部地址,而不是 我机器的本地 ip

从这里开始 sip.conf

[general]
context=public                  ; Default context for incoming calls. Defaults to 'default'
allowoverlap=no                 ; Disable overlap dialing support. (Default is yes)
udpbindaddr=0.0.0.0             ; IP address to bind UDP listen socket to (0.0.0.0 binds to all)
tcpenable=no                    ; Enable server for incoming TCP connections (default is no)
tcpbindaddr=0.0.0.0             ; IP address for TCP server to bind to (0.0.0.0 binds to all interfaces)
transport=udp                   ; Set the default transports.  The order determines the primary default transport.
srvlookup=yes                   ; Enable DNS SRV lookups on outbound calls
externip=<PUBLIC_IP_OF_YOUR_INSTANCE>
localnet=<INTERNAL_IP_OF_YOUR_INSTANCE>/<MASK (255.255.255.255)>