반응형

음성 AI 활용을 위한 SIP Client 제작 중 Asterisk SIP Registration 과정을 공유합니다.

https://who.rocq.inria.fr/Philippe.Sultan/Asterisk/asterisk_sip_external_authentication.html

0. 현재 구성 정보

Asterisk 
 - verison 12.8.0
 - IP : 223.130.135.113
 - 설치 참고 https://kev1n.tistory.com/59
SIP Client
 - IP : 13.124.201.154 / 172.31.20.149
 - python 3.10.12
 - Sample APP https://github.com/CheongbinKim/q-phone

0. Asterisk peer 정보 (sip.conf)

[9999]
deny=0.0.0.0/0.0.0.0
disallow=all
secret=3834c7840b10baa98ecad6e6ff9867d3
dtmfmode=rfc2833
canreinvite=no
context=from-internal
host=dynamic
trustrpid=yes
mediaencryption=no
sendrpid=no
type=friend
nat=force_rport,comedia
port=5060
qualify=no
qualifyfreq=60
transport=udp,tcp,tls
avpf=no
force_avp=no
icesupport=no
encryption=no
allow=ulaw
dial=SIP/9999
permit=0.0.0.0/0.0.0.0
callerid=9999 <9999>
callcounter=yes
faxdetect=no

1. REGISTER 요청

REGISTER sip:223.130.135.113:5061 SIP/2.0
Via: SIP/2.0/UDP 172.31.20.149:10001;rport;branch=tzi5f30dvc5z93tczuo8ppawzcq0az50qq9
From: <sip:9999@223.130.135.113:5061>;tag=xk7588628423
To: <sip:9999@223.130.135.113:5061>
Max-Forwards: 70
Call-ID: q05uiz8sjulrd7aa0q04hejbogdml1dqn50
CSeq: 25555 REGISTER
Contact: <sip:9999@172.31.20.149:10001>
Allow: ACK,BYE,CANCEL,INVITE,REGISTER,UPDATE,MESSAGE,INFO,OPTIONS,SUBSCRIBE,NOTIFY,REFER,COMET,PUBLISH,PING,DO,SHAREDFN
Allow-Events: presence,refer,telephone-event,keep-alive,dialog
Supported: replaces, timer
Event: registration
User-Agent: Kev1n 0.1
Expires: 600
Accept: application/sdp,application/dtmf-relay,audio/telephone-event,message/sipfrag,text/plain,text/html
Content-Length: 0

line 1: Request-URI에 Asterisk 서버 정보 입력

line 7: CSeq 는 Request 보낼 때마다 1씩 증가

line 8: Asterisk 에 등록 된 SIP peer 정보 (PJSIP Endpoint), NAT 설정 된 경우 사설IP를 입력

2. 401 응답

SIP/2.0 401 Unauthorized
Via: SIP/2.0/UDP 172.31.20.149:10001;branch=tzi5f30dvc5z93tczuo8ppawzcq0az50qq9;received=13.124.201.154;rport=5080
From: <sip:9999@223.130.135.113:5061>;tag=xk7588628423
To: <sip:9999@223.130.135.113:5061>;tag=as479fbd03
Call-ID: q05uiz8sjulrd7aa0q04hejbogdml1dqn50
CSeq: 25555 REGISTER
Server: FPBX-12.0.76.6(12.8.0)
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, MESSAGE
Supported: replaces, timer
WWW-Authenticate: Digest algorithm=MD5, realm="asterisk", nonce="70648ad6"
Content-Length: 0

Asterisk 서버는 HTTP Digest 인증을 사용한다. REGISTER 요청에 대해 서버는 401로 응답한다. 여기서 WWW-Authenticate 에 반환 된 값들로 인증값을 만들고 다시 REGISTER한다. 알고리즘은로 MD5를 사용한다.

[참조] https://datatracker.ietf.org/doc/html/draft-smith-sipping-auth-examples-01#section-3.1.3

# h1 = 'USERNAME:REALM:PASSWORD'
# h2 = 'METHOD:URI'
# response = 'h1:nonce:h2'

# 위 전문 예제 기준으로
# h1 = '9999:asterisk:3834c7840b10baa98ecad6e6ff9867d3'
# h2 = 'REGISTER:sip:223.130.135.113:5061'
# response = 'h1:70648ad6:h2'

# 파이썬 예제
from hashlib import md5
h1 = self.md5sum('%s:%s:%s' % (username,self.realm,password))
h2 = self.md5sum("REGISTER:%s" % (self.uri))
response = self.md5sum('%s:%s:%s' % (h1,self.__nonce,h2))

3. 인증값과 함께 REGISTER 재 요청

REGISTER sip:223.130.135.113:5061 SIP/2.0
Via: SIP/2.0/UDP 172.31.20.149:10001;rport;branch=tzi5f30dvc5z93tczuo8ppawzcq0az50qq9
From: <sip:9999@223.130.135.113:5061>;tag=xk7588628423
To: <sip:9999@223.130.135.113:5061>
Max-Forwards: 70
Call-ID: q05uiz8sjulrd7aa0q04hejbogdml1dqn50
CSeq: 25556 REGISTER
Contact: <sip:9999@172.31.20.149:10001>
Authorization: Digest username="9999",realm="asterisk",nonce="70648ad6",uri="sip:223.130.135.113:5061",response="00292fe0fbad147c0f1907d3bc5a2920",algorithm=MD5
Allow: ACK,BYE,CANCEL,INVITE,REGISTER,UPDATE,MESSAGE,INFO,OPTIONS,SUBSCRIBE,NOTIFY,REFER,COMET,PUBLISH,PING,DO,SHAREDFN
Allow-Events: presence,refer,telephone-event,keep-alive,dialog
Supported: replaces, timer
Event: registration
User-Agent: Kev1n 0.1
Expires: 600
Accept: application/sdp,application/dtmf-relay,audio/telephone-event,message/sipfrag,text/plain,text/html
Content-Length: 0

4. 200 OK 

SIP/2.0 200 OK
Via: SIP/2.0/UDP 172.31.20.149:10001;branch=tzi5f30dvc5z93tczuo8ppawzcq0az50qq9;received=13.124.201.154;rport=5080
From: <sip:9999@223.130.135.113:5061>;tag=xk7588628423
To: <sip:9999@223.130.135.113:5061>;tag=as479fbd03
Call-ID: q05uiz8sjulrd7aa0q04hejbogdml1dqn50
CSeq: 25556 REGISTER
Server: FPBX-12.0.76.6(12.8.0)
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, MESSAGE
Supported: replaces, timer

 

- Asterisk log 확인

asterisk*CLI> sip show peers

 

이제 Asterisk에 peer로 등록되었고 Inbound 콜을 수신 할  수 있다.

아래는 sngrep을 통해 REGISTER 등록 과정을 캡쳐한 것이다.

 

반응형
Posted by kev1n
,