From aa186f1038690de9766329115008ba1c476d93e2 Mon Sep 17 00:00:00 2001 From: TJ Porter Date: Thu, 18 Jan 2024 13:34:41 -0600 Subject: [PATCH 1/2] Fixed #209 [FIX] Fixed gen_first_register, gen_register, gen_subscribe, and gen_message not using NAT. [FIX] Fixed issue with registering with remote PBXs --- pyVoIP/SIP/client.py | 79 ++++++++++++++++++++++++++++++-------------- 1 file changed, 54 insertions(+), 25 deletions(-) diff --git a/pyVoIP/SIP/client.py b/pyVoIP/SIP/client.py index e45d6cb..83051f7 100644 --- a/pyVoIP/SIP/client.py +++ b/pyVoIP/SIP/client.py @@ -544,19 +544,19 @@ def gen_urn_uuid(self) -> str: def gen_first_request(self, deregister=False) -> str: regRequest = f"REGISTER sip:{self.server}:{self.port} SIP/2.0\r\n" regRequest += self.__gen_via(self.server, self.gen_branch()) - - regRequest += ( - f'From: "{self.user}" ' - + f";tag=" - + f'{self.tagLibrary["register"]}\r\n' - ) - regRequest += ( - f'To: "{self.user}" ' - + f"\r\n" + tag = self.tagLibrary["register"] + method = "sips" if self.transport_mode is TransportMode.TLS else "sip" + regRequest += self.__gen_from_to( + "From", + self.user, + self.nat.get_host(self.server), + method=method, + port=self.bind_port, + header_parms=f";tag={tag}", ) + regRequest += self.__gen_from_to("To", self.user, self.server, method=method, port=self.port) regRequest += f"Call-ID: {self.gen_call_id()}\r\n" regRequest += f"CSeq: {self.registerCounter.next()} REGISTER\r\n" - method = "sips" if self.transport_mode is TransportMode.TLS else "sip" trans_mode = str(self.transport_mode) regRequest += self.__gen_contact( method, @@ -583,12 +583,22 @@ def gen_first_request(self, deregister=False) -> str: def gen_subscribe(self, response: SIPMessage) -> str: subRequest = f"SUBSCRIBE sip:{self.user}@{self.server} SIP/2.0\r\n" subRequest += self.__gen_via(self.server, self.gen_branch()) - subRequest += ( - f'From: "{self.user}" ' - + f";tag=" - + f"{self.gen_tag()}\r\n" + method = "sips" if self.transport_mode is TransportMode.TLS else "sip" + subRequest += self.__gen_from_to( + "From", + self.user, + self.nat.get_host(self.server), + method=method, + port=self.bind_port, + header_parms=f";tag={self.gen_tag()}", + ) + subRequest += self.__gen_from_to( + "To", + self.user, + self.server, + method=method, + port=self.port, ) - subRequest += f"To: \r\n" subRequest += f'Call-ID: {response.headers["Call-ID"]}\r\n' subRequest += f"CSeq: {self.subscribeCounter.next()} SUBSCRIBE\r\n" # TODO: check if transport is needed @@ -615,14 +625,21 @@ def gen_subscribe(self, response: SIPMessage) -> str: def gen_register(self, request: SIPMessage, deregister=False) -> str: regRequest = f"REGISTER sip:{self.server}:{self.port} SIP/2.0\r\n" regRequest += self.__gen_via(self.server, self.gen_branch()) - regRequest += ( - f'From: "{self.user}" ' - + f";tag=" - + f'{self.tagLibrary["register"]}\r\n' + method = "sips" if self.transport_mode is TransportMode.TLS else "sip" + regRequest += self.__gen_from_to( + "From", + self.user, + self.nat.get_host(self.server), + method=method, + port=self.bind_port, + header_parms=f";tag={self.tagLibrary['register']}", ) - regRequest += ( - f'To: "{self.user}" ' - + f"\r\n" + regRequest += self.__gen_from_to( + "To", + self.user, + self.server, + method=method, + port=self.port, ) call_id = request.headers.get("Call-ID", self.gen_call_id()) regRequest += f"Call-ID: {call_id}\r\n" @@ -1133,9 +1150,21 @@ def gen_message( msg = f"MESSAGE sip:{number}@{self.server} SIP/2.0\r\n" msg += self.__gen_via(self.server, branch) msg += "Max-Forwards: 70\r\n" - msg += f"To: \r\n" - msg += ( - f"From: ;tag={self.gen_tag()}\r\n" + method = "sips" if self.transport_mode is TransportMode.TLS else "sip" + regRequest += self.__gen_from_to( + "From", + self.user, + self.nat.get_host(self.server), + method=method, + port=self.bind_port, + header_parms=f";tag={self.gen_tag()}", + ) + regRequest += self.__gen_from_to( + "To", + number, + self.server, + method=method, + port=self.port, ) msg += f"Call-ID: {call_id}\r\n" msg += f"CSeq: {self.messageCounter.next()} MESSAGE\r\n" From ff806f2ac751d3772a40b16aa9a7476475fa5ae9 Mon Sep 17 00:00:00 2001 From: TJ Porter Date: Thu, 18 Jan 2024 13:44:22 -0600 Subject: [PATCH 2/2] [FIX] Fixed flake8 error [FIX] Fixed black error --- pyVoIP/SIP/client.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pyVoIP/SIP/client.py b/pyVoIP/SIP/client.py index 83051f7..71d8ca6 100644 --- a/pyVoIP/SIP/client.py +++ b/pyVoIP/SIP/client.py @@ -554,7 +554,9 @@ def gen_first_request(self, deregister=False) -> str: port=self.bind_port, header_parms=f";tag={tag}", ) - regRequest += self.__gen_from_to("To", self.user, self.server, method=method, port=self.port) + regRequest += self.__gen_from_to( + "To", self.user, self.server, method=method, port=self.port + ) regRequest += f"Call-ID: {self.gen_call_id()}\r\n" regRequest += f"CSeq: {self.registerCounter.next()} REGISTER\r\n" trans_mode = str(self.transport_mode) @@ -1151,7 +1153,7 @@ def gen_message( msg += self.__gen_via(self.server, branch) msg += "Max-Forwards: 70\r\n" method = "sips" if self.transport_mode is TransportMode.TLS else "sip" - regRequest += self.__gen_from_to( + msg += self.__gen_from_to( "From", self.user, self.nat.get_host(self.server), @@ -1159,7 +1161,7 @@ def gen_message( port=self.bind_port, header_parms=f";tag={self.gen_tag()}", ) - regRequest += self.__gen_from_to( + msg += self.__gen_from_to( "To", number, self.server,