astoeckel / femtosip

Minimal Python SIP implementation ringing phones as a door bell replacement
GNU Affero General Public License v3.0
48 stars 15 forks source link

AttributeError: 'NoneType' object has no attribute 'group' #6

Closed rktech-de closed 1 year ago

rktech-de commented 2 years ago

Thx for this code it is exactly what I was looking for.

but with a first test to connect to asterisk I got this error:

root@python# python3 femtosip.py --gateway 192.168.10.105 --user 2001 --password 1234 --call '1003' --delay 15.0 --displayname MyTest
2022-05-20 08:51:11,786 request: INVITE sip:1003@192.168.10.105
2022-05-20 08:51:11,787 response: SIP/2.0 401 Unauthorized
Traceback (most recent call last):
  File "/home/bin/femtosip.py", line 558, in <module>
    sip.call(args.call, args.delay)
  File "/home/bin/femtosip.py", line 514, in call
    ResponseParser().feed(readbuf, handle_response)
  File "/home/bin/femtosip.py", line 241, in feed
    call_callback()
  File "/home/bin/femtosip.py", line 134, in call_callback
    callback(self)
  File "/home/bin/femtosip.py", line 439, in handle_response
    state['realm'] = match.group(1)
AttributeError: 'NoneType' object has no attribute 'group'

in my case the content of auth is Digest algorithm=MD5, realm="asterisk", nonce="0afa88e9" which will not give a regex match (testet with regex101.com) https://github.com/astoeckel/femtosip/blob/9cf92572014e77ed42cd1a6910aed00baeeca604/femtosip.py#L434-L436

Changing line 435 to

    r'^[Dd]igest.+realm="([^"]*)"\s*,\s*nonce="([^"]*)".*$',

will solve the issue in my case

astoeckel commented 2 years ago

Thank you for the bug report!

Admittedly, using a regex here is not the right tool; really, the string should be parsed. I'll see if I get around to fixing that in the next few days; if not, I'll just apply your patch.

astoeckel commented 1 year ago

Sorry for the delay; just got around to implementing this properly.

See https://github.com/astoeckel/femtosip/commit/f6d8e3c6b3170a5a85e228b0bd034a8d9b2e6139

Unfortunately, adding (more or less) proper parsing for the WWW-authenticate field added quite a bit of code, but I'm feeling that this is a better solution compared to just changing the RegEx.