示例#1
0
        public static Session FromBytes(byte [] buffer, ISessionStore store, string sessionUserId)
        {
            using (var stream = new MemoryStream(buffer))
                using (var reader = new BinaryReader(stream)) {
                    var id       = reader.ReadUInt64();
                    var sequence = reader.ReadInt32();

                    // we do this in CI when running tests so that the they can always use a
                    // higher sequence than previous run
#if CI
                    sequence = Session.CurrentTime();
#endif

                    var salt          = reader.ReadUInt64();
                    var lastMessageId = reader.ReadInt64();
                    var timeOffset    = reader.ReadInt32();
                    var serverAddress = Serializers.String.Read(reader);
                    var port          = reader.ReadInt32();

                    var    doesAuthExist  = reader.ReadInt32() == 1;
                    int    sessionExpires = 0;
                    TLUser TLUser         = null;
                    if (doesAuthExist)
                    {
                        sessionExpires = reader.ReadInt32();
                        TLUser         = (TLUser)ObjectUtils.DeserializeObject(reader);
                    }

                    var authData          = Serializers.Bytes.Read(reader);
                    var defaultDataCenter = new DataCenter(serverAddress, port);

                    return(new Session()
                    {
                        AuthKey = new AuthKey(authData),
                        Id = id,
                        Salt = salt,
                        Sequence = sequence,
                        LastMessageId = lastMessageId,
                        TimeOffset = timeOffset,
                        SessionExpires = sessionExpires,
                        TLUser = TLUser,
                        SessionUserId = sessionUserId,
                        DataCenter = defaultDataCenter,
                    });
                }
        }
示例#2
0
        public static Session FromBytes(byte[] buffer, ISessionStore store, string sessionUserId)
        {
            using (var stream = new MemoryStream(buffer))
                using (var reader = new BinaryReader(stream))
                {
                    var id            = reader.ReadUInt64();
                    var sequence      = reader.ReadInt32();
                    var salt          = reader.ReadUInt64();
                    var lastMessageId = reader.ReadInt64();
                    var timeOffset    = reader.ReadInt32();
                    var serverAddress = Serializers.String.Read(reader);
                    var port          = reader.ReadInt32();

                    var    isAuthExsist   = reader.ReadInt32() == 1;
                    int    sessionExpires = 0;
                    TLUser TLUser         = null;
                    if (isAuthExsist)
                    {
                        sessionExpires = reader.ReadInt32();
                        TLUser         = (TLUser)ObjectUtils.DeserializeObject(reader);
                    }

                    var authData          = Serializers.Bytes.Read(reader);
                    var defaultDataCenter = new DataCenter(serverAddress, port);

                    return(new Session(store)
                    {
                        AuthKey = new AuthKey(authData),
                        Id = id,
                        Salt = salt,
                        Sequence = sequence,
                        LastMessageId = lastMessageId,
                        TimeOffset = timeOffset,
                        SessionExpires = sessionExpires,
                        TLUser = TLUser,
                        SessionUserId = sessionUserId,
                        DataCenter = defaultDataCenter,
                    });
                }
        }
示例#3
0
        private async Task ReconnectToDcAsync(int dcId, CancellationToken token = default(CancellationToken))
        {
            token.ThrowIfCancellationRequested();

            if (dcOptions == null || !dcOptions.Any())
            {
                throw new InvalidOperationException($"Can't reconnect. Establish initial connection first.");
            }

            TLExportedAuthorization exported = null;

            if (Session.TLUser != null)
            {
                TLRequestExportAuthorization exportAuthorization = new TLRequestExportAuthorization()
                {
                    DcId = dcId
                };
                exported = await SendRequestAsync <TLExportedAuthorization>(exportAuthorization, token).ConfigureAwait(false);
            }

            IEnumerable <TLDcOption> dcs;

            if (dcIpVersion == DataCenterIPVersion.OnlyIPv6)
            {
                dcs = dcOptions.Where(d => d.Id == dcId && d.Ipv6); // selects only ipv6 addresses
            }
            else if (dcIpVersion == DataCenterIPVersion.OnlyIPv4)
            {
                dcs = dcOptions.Where(d => d.Id == dcId && !d.Ipv6); // selects only ipv4 addresses
            }
            else
            {
                dcs = dcOptions.Where(d => d.Id == dcId); // any
            }
            dcs = dcs.Where(d => !d.MediaOnly);

            TLDcOption dc;

            if (dcIpVersion != DataCenterIPVersion.Default)
            {
                if (!dcs.Any())
                {
                    throw new Exception($"Telegram server didn't provide us with any IPAddress that matches your preferences. If you chose OnlyIPvX, try switch to PreferIPvX instead.");
                }
                dcs = dcs.OrderBy(d => d.Ipv6);
                dc  = dcIpVersion == DataCenterIPVersion.PreferIPv4 ? dcs.First() : dcs.Last(); // ipv4 addresses are at the beginning of the list because it was ordered
            }
            else
            {
                dc = dcs.First();
            }

            var dataCenter = new DataCenter(dcId, dc.IpAddress, dc.Port);

            Session.DataCenter = dataCenter;
            this.store.Save(Session);

            await ConnectInternalAsync(true, token).ConfigureAwait(false);

            if (Session.TLUser != null)
            {
                TLRequestImportAuthorization importAuthorization = new TLRequestImportAuthorization()
                {
                    Id = exported.Id, Bytes = exported.Bytes
                };
                var imported = await SendRequestAsync <TLAuthorization>(importAuthorization, token).ConfigureAwait(false);

                OnUserAuthenticated((TLUser)imported.User);
            }
        }