Esempio n. 1
0
    def connect(self, reconnect=False):
        try:
            if not self.session.auth_key or (reconnect
                                             and self.sender is not None):
                self.session.auth_key, self.session.time_offset = \
                    authenticator.do_authentication(self.transport)

                self.session.save()

            self.sender = MtProtoSender(self.transport, self.session)

            query = InitConnectionRequest(api_id=self.api_id,
                                          device_model=platform.node(),
                                          system_version=platform.system(),
                                          app_version=self.__version__,
                                          lang_code="en",
                                          query=GetConfigRequest())

            result = self.invoke(
                InvokeWithLayerRequest(layer=layer, query=query))

            self.dc_options = result.dc_options

            self.sender.ack_requests_confirm = True

            self.signed_in = self.is_user_authorized()

            return True
        except RPCError as error:
            print(f"Could not stabilise initial connection: {error}.")
            return False
Esempio n. 2
0
    def connect(self, reconnect=False):
        """Connects to the Telegram servers, executing authentication if required.
           Note that authenticating to the Telegram servers is not the same as authenticating
           the app, which requires to send a code first."""
        if self.transport is None:
            self.transport = TcpTransport(self.session.server_address,
                                          self.session.port,
                                          proxy=self.proxy)

        try:
            if not self.session.auth_key or (reconnect
                                             and self.sender is not None):
                self.session.auth_key, self.session.time_offset = \
                    authenticator.do_authentication(self.transport)

                self.session.save()

            self.sender = MtProtoSender(self.transport, self.session)

            # Now it's time to send an InitConnectionRequest
            # This must always be invoked with the layer we'll be using
            query = InitConnectionRequest(api_id=self.api_id,
                                          device_model=platform.node(),
                                          system_version=platform.system(),
                                          app_version=self.__version__,
                                          lang_code='en',
                                          query=GetConfigRequest())

            result = self.invoke(
                InvokeWithLayerRequest(layer=layer, query=query))

            # We're only interested in the DC options,
            # although many other options are available!
            self.dc_options = result.dc_options

            # We're signed in if we're authorized
            self.signed_in = self.is_user_authorized()
            return True
        except RPCError as error:
            print('Could not stabilise initial connection: {}'.format(error))
            return False
Esempio n. 3
0
 def test_authenticator():
     transport = TcpTransport('149.154.167.91', 443)
     authenticator.do_authentication(transport)
     transport.close()
Esempio n. 4
0
 def test_authenticator():
     transport = TcpTransport('149.154.167.91', 443)
     authenticator.do_authentication(transport)
     transport.close()
Esempio n. 5
0
 def test_authenticator(self):
     transport = Connection('149.154.167.91', 443)
     self.assertTrue(authenticator.do_authentication(transport))
     transport.close()
Esempio n. 6
0
    def connect(self, reconnect=False,
                device_model=None, system_version=None,
                app_version=None, lang_code=None):
        """Connects to the Telegram servers, executing authentication if
           required. Note that authenticating to the Telegram servers is
           not the same as authenticating the desired user itself, which
           may require a call (or several) to 'sign_in' for the first time.

           Default values for the optional parameters if left as None are:
             device_model   = platform.node()
             system_version = platform.system()
             app_version    = TelegramClient.__version__
             lang_code      = 'en'
        """
        if self.transport is None:
            self.transport = TcpTransport(self.session.server_address,
                                          self.session.port, proxy=self.proxy)

        try:
            if not self.session.auth_key or (reconnect and self.sender is not None):
                self.session.auth_key, self.session.time_offset = \
                    authenticator.do_authentication(self.transport)

                self.session.save()

            self.sender = MtProtoSender(self.transport, self.session)
            self.sender.connect()

            # Set the default parameters if left unspecified
            if not device_model:
                device_model = platform.node()
            if not system_version:
                system_version = platform.system()
            if not app_version:
                app_version = self.__version__
            if not lang_code:
                lang_code = 'en'

            # Now it's time to send an InitConnectionRequest
            # This must always be invoked with the layer we'll be using
            query = InitConnectionRequest(
                api_id=self.api_id,
                device_model=device_model,
                system_version=system_version,
                app_version=app_version,
                lang_code=lang_code,
                query=GetConfigRequest())

            result = self.invoke(
                InvokeWithLayerRequest(
                    layer=layer, query=query))

            # We're only interested in the DC options,
            # although many other options are available!
            self.dc_options = result.dc_options

            self.login_success = True
            return True
        except (RPCError, ConnectionError) as error:
            # Probably errors from the previous session, ignore them
            self._logger.warning('Could not stabilise initial connection: {}'
                                 .format(error))
            return False