Beispiel #1
0
        private static TelegramSession loadIfExists()
        {
            TelegramSession session;

            lock (typeof(TelegramSession)) {
                try {
                    using (IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication())
                        using (Stream fileStream = new IsolatedStorageFileStream("session.dat", FileMode.Open, fileStorage))
                            using (BinaryReader fileReader = new BinaryReader(fileStream)) {
                                session = new TelegramSession(fileReader);
                                logger.info("loaded telegram session: {0}", session);
                            }
                } catch (Exception e) {
                    logger.info("error loading session, create new...: {0}", e);

                    logger.info("cleaning contacts");
                    CleanContacts();

                    ulong sessionId = Helpers.GenerateRandomUlong();
                    session = new TelegramSession(sessionId, 0);
                    // prod 173.240.5.1
                    // test 173.240.5.253
                    TelegramEndpoint endpoint = new TelegramEndpoint("173.240.5.1", 443);
                    TelegramDC       dc       = new TelegramDC();
                    dc.Endpoints.Add(endpoint);
                    session.Dcs.Add(1, dc);
                    session.SetMainDcId(1);


                    logger.info("created new telegram session: {0}", session);
                }
            }

            return(session);
        }
Beispiel #2
0
        public async Task <TLApi> GetFileSession(int dc)
        {
            logger.debug("Getting file session for dc {0}", dc);
            await             Established;
            ConfigConstructor config = (ConfigConstructor)gateway.Config;

            TelegramDC targetDc;

            if (dcs.ContainsKey(dc))
            {
                targetDc = dcs[dc];
            }
            else
            {
                targetDc = new TelegramDC();
                foreach (var dcOption in config.dc_options)
                {
                    DcOptionConstructor optionConstructor = (DcOptionConstructor)dcOption;
                    if (optionConstructor.id == dc)
                    {
                        TelegramEndpoint endpoint = new TelegramEndpoint(optionConstructor.ip_address, optionConstructor.port);
                        targetDc.Endpoints.Add(endpoint);
                    }
                }

                dcs[dc] = targetDc;
            }

            MTProtoGateway fileGateway = await targetDc.GetFileGateway(gateway.Salt);

            TLApi fileGatewayApi = new TLApi(fileGateway);

            if (targetDc.FileAuthorized || dc == mainDcId)
            {
                return(fileGatewayApi);
            }

            Task <auth_ExportedAuthorization>     exportAuthTask = Api.auth_exportAuthorization(dc);
            Auth_exportedAuthorizationConstructor exportedAuth   = (Auth_exportedAuthorizationConstructor)await exportAuthTask;
            auth_Authorization authorization = await fileGatewayApi.auth_importAuthorization(exportedAuth.id, exportedAuth.bytes);

            targetDc.SaveFileAuthorization(authorization);

            return(fileGatewayApi);
        }
Beispiel #3
0
        public async Task Migrate(int dc)
        {
            if (gateway == null)
            {
                logger.error("gateway not found, migration impossible");
                return;
            }

            if (gateway.Config == null)
            {
                logger.error("config in gateway not found, migration impossible");
                return;
            }

            ConfigConstructor config = (ConfigConstructor)gateway.Config;

            if (config.this_dc == dc)
            {
                logger.warning("migration to same dc: {0}", dc);
                return;
            }

            TelegramDC newDc = new TelegramDC();

            foreach (var dcOption in config.dc_options)
            {
                DcOptionConstructor optionConstructor = (DcOptionConstructor)dcOption;
                if (optionConstructor.id == dc)
                {
                    TelegramEndpoint endpoint = new TelegramEndpoint(optionConstructor.ip_address, optionConstructor.port);
                    newDc.Endpoints.Add(endpoint);
                }
            }

            dcs[dc]  = newDc;
            mainDcId = dc;

            gateway.Dispose();
            gateway         = null;
            establishedTask = new TaskCompletionSource <object>();
            ConnectAsync();
            await Established;
        }
Beispiel #4
0
        public async Task<TLApi> GetFileSession(int dc) {
            logger.debug("Getting file session for dc {0}", dc);
            await Established;
            ConfigConstructor config = (ConfigConstructor) gateway.Config;

            TelegramDC targetDc;
            if(dcs.ContainsKey(dc)) {
                targetDc = dcs[dc];
            } else {
                targetDc = new TelegramDC();
                foreach(var dcOption in config.dc_options) {
                    DcOptionConstructor optionConstructor = (DcOptionConstructor) dcOption;
                    if(optionConstructor.id == dc) {
                        TelegramEndpoint endpoint = new TelegramEndpoint(optionConstructor.ip_address, optionConstructor.port);
                        targetDc.Endpoints.Add(endpoint);
                    }
                }

                dcs[dc] = targetDc;
            }
            
            MTProtoGateway fileGateway = await targetDc.GetFileGateway(gateway.Salt);
            
            TLApi fileGatewayApi = new TLApi(fileGateway);

            if(targetDc.FileAuthorized || dc == mainDcId) {
                return fileGatewayApi;
            } 

            Task<auth_ExportedAuthorization> exportAuthTask = Api.auth_exportAuthorization(dc);
            Auth_exportedAuthorizationConstructor exportedAuth = (Auth_exportedAuthorizationConstructor) await exportAuthTask;
            auth_Authorization authorization = await fileGatewayApi.auth_importAuthorization(exportedAuth.id, exportedAuth.bytes);
            targetDc.SaveFileAuthorization(authorization);

            return fileGatewayApi;
        }
Beispiel #5
0
        public async Task Migrate(int dc) {
            if(gateway == null) {
                logger.error("gateway not found, migration impossible");
                return;
            }

            if (gateway.Config == null) {
                logger.error("config in gateway not found, migration impossible");
                return;
            }

            ConfigConstructor config = (ConfigConstructor) gateway.Config;
            if(config.this_dc == dc) {
                logger.warning("migration to same dc: {0}", dc);
                return;
            }

            TelegramDC newDc = new TelegramDC();
            foreach(var dcOption in config.dc_options) {
                DcOptionConstructor optionConstructor = (DcOptionConstructor) dcOption;
                if(optionConstructor.id == dc) {
                    TelegramEndpoint endpoint = new TelegramEndpoint(optionConstructor.ip_address, optionConstructor.port);
                    newDc.Endpoints.Add(endpoint);
                }
            }

            dcs[dc] = newDc;
            mainDcId = dc;

            gateway.Dispose();
            gateway = null;
            establishedTask = new TaskCompletionSource<object>();
            ConnectAsync();
            await Established;
        }
Beispiel #6
0
        private static TelegramSession loadIfExists() {
            TelegramSession session;
            lock(typeof(TelegramSession)) {
                try {
                    using(IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication())
                    using(Stream fileStream = new IsolatedStorageFileStream("session.dat", FileMode.Open, fileStorage))
                    using(BinaryReader fileReader = new BinaryReader(fileStream)) {
                        session = new TelegramSession(fileReader);
                        logger.info("loaded telegram session: {0}", session);
                    }
                } catch(Exception e) {
                    logger.info("error loading session, create new...: {0}", e);

                    logger.info("cleaning contacts");
                    CleanContacts();

                    ulong sessionId = Helpers.GenerateRandomUlong();
                    session = new TelegramSession(sessionId, 0);
                    // prod 173.240.5.1 
                    // test 173.240.5.253
                    TelegramEndpoint endpoint = new TelegramEndpoint("173.240.5.1", 443);
                    TelegramDC dc = new TelegramDC();
                    dc.Endpoints.Add(endpoint);
                    session.Dcs.Add(1, dc);
                    session.SetMainDcId(1);


                    logger.info("created new telegram session: {0}", session);
                }
            }

            return session;
        }