Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't read session from sqlserver #304

Open
nguyentuan3691 opened this issue Jan 2, 2025 · 0 comments
Open

Can't read session from sqlserver #304

nguyentuan3691 opened this issue Jan 2, 2025 · 0 comments

Comments

@nguyentuan3691
Copy link

Hi
I saw the "Heroku" example
I followed the "Heroku" example
Here is my code

`public class SqlServerStore : Stream
{
private string _phoneNumber;
private string _fullname;
private string _proxy;
private byte[] _data;
private int _dataLen;
private long _position;
private DateTime _lastWrite;
private Task _delayedWrite;

public override long Length => _dataLen;
public override long Position { get => _position; set { _position = value; } }
public override bool CanSeek => false;
public override bool CanRead => true;
public override bool CanWrite => true;
public override long Seek(long offset, SeekOrigin origin) => 0;
public override void SetLength(long value) { }
public override void Flush() { }

public byte[] Data => _data;

public SqlServerStore(string phoneNumber, string fullName, string proxy)
{
    _phoneNumber = phoneNumber;
    _fullname = fullName;
    _proxy = proxy;

    using var scope = CanawanHubDataTelegramContext.ServiceProvider.ThreadSafeCreateAsyncScope();
    using var dbContext = scope.ServiceProvider.GetRequiredService<HubDataContext>();
    var teleAccount = dbContext.TeleAccounts.FirstOrDefault(x => x.PhoneNumber == _phoneNumber);
    if (teleAccount != null)
    {
        _data = teleAccount.DataSession;
        _dataLen = _data.Length;
    }
}

public override int Read(byte[] buffer, int offset, int count)
{
    Array.Copy(_data, 0, buffer, offset, count);
    return count;
}

public override async void Write(byte[] buffer, int offset, int count)
{
    _data = buffer; _dataLen = count;
    if (_delayedWrite != null) return;
    var left = 1000 - (int)(DateTime.UtcNow - _lastWrite).TotalMilliseconds;
    if (left < 0)
    {
        await using var scope = CanawanHubDataTelegramContext.ServiceProvider.ThreadSafeCreateAsyncScope();
        using var dbContext = scope.ServiceProvider.GetRequiredService<HubDataContext>();
        var teleAccount = dbContext.TeleAccounts.FirstOrDefault(x => x.PhoneNumber == _phoneNumber);
        if (teleAccount != null)
        {
            await dbContext.DoSaveChanges(async () =>
            {
                dbContext.Attach(teleAccount);
                teleAccount.UpdateDateTime = DateTime.Now;
                teleAccount.DataSession = _data;
            }).ConfigureAwait(false);
        }
        else
        {
            teleAccount = new TeleAccount
            {
                PhoneNumber = _phoneNumber,
                FullName = _fullname,
                Proxy = _proxy,
                CountFiles = 0,
                TotalSizeMb = 0,
                DataSession = _data,
                IsBaned = false,
                IsUse = true,
                CreateDateTime = DateTime.Now,
                UpdateDateTime = DateTime.Now
            };

            await dbContext.DoSaveChanges(async () =>
            {
                await dbContext.AddAsync(teleAccount);
            }).ConfigureAwait(false);
        }
        _lastWrite = DateTime.UtcNow;
    }
    else // delay writings for a full second
        _delayedWrite = Task.Delay(left).ContinueWith(t => { lock (this) { _delayedWrite = null; Write(_data, 0, _dataLen); } });
}

public void Update(byte[] data)
{
    _data = data;
    _dataLen = data.Length;

    using var scope = CanawanHubDataTelegramContext.ServiceProvider.ThreadSafeCreateAsyncScope();
    using var dbContext = scope.ServiceProvider.GetRequiredService<HubDataContext>();
    var teleAccount = dbContext.TeleAccounts.FirstOrDefault(x => x.PhoneNumber == _phoneNumber);
    if (teleAccount != null)
    {
        dbContext.DoSaveChanges(async () =>
        {
            dbContext.Attach(teleAccount);
            teleAccount.DataSession = _data;
        }).Wait();
    }
}

}`

When logging in, the code works fine, but when there is data in the database, when reading the session, it gives an error

WTelegram.WTException: 'Exception while reading session file: Padding is invalid and cannot be removed.
Use the correct api_hash/id/key, or delete the file to start a new session'

I tried to fix the error by downloading the project and editing the Session class, I removed the encryption and 8 byte header. But when logging in, telegram did not send the login authentication code to the telegram app. But running with the library installed via nuget, telegram still sent the login authentication code to the telegram app. This is really confusing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant