示例#1
0
        private async Task Connect()
        {
            if (_client.IsLoggedIn)
            {
                return;
            }

            await RetryHelper.RetryOnExceptionAsync(
                async() =>
            {
                if (_client.IsLoggedIn)
                {
                    return;
                }
                if (_loginToken != null)
                {
                    await _client.LoginAsync(_loginToken);
                }
                else if (_authInfos != null)
                {
                    _loginToken = await _client.LoginAsync(_authInfos);
                }
                else
                {
                    await _client.LoginAnonymousAsync();
                }
            }, 2, TimeSpan.FromSeconds(2), CancellationToken.None);
        }
示例#2
0
        private void buttonAcc_Click(object sender, EventArgs e)
        {
            try
            {
                if (_client.IsLoggedIn)
                {
                    _client.Logout();
                }

                _authInfos = _client.GenerateAuthInfos(textBoxLogin.Text, textBoxPassw.Text);
                _token     = _client.Login(_authInfos);

                if (!_client.IsLoggedIn)
                {
                    throw new IOException("Client did not log in");
                }

                DialogResult = DialogResult.OK;
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to log in with the specified login and password. Please make sure that they are correct and you are connected to the internet, then try again.\n\nError message: " + ex.Message,
                                "Log in to mega.nz", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = DialogResult.None;
            }
        }
示例#3
0
        private void buttonAnon_Click(object sender, EventArgs e)
        {
            try
            {
                if (_client.IsLoggedIn)
                {
                    _client.Logout();
                }

                _authInfos = null;
                _token     = null;
                _client.LoginAnonymous();

                if (!_client.IsLoggedIn)
                {
                    throw new IOException("Client did not log in");
                }

                DialogResult = DialogResult.Ignore;
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to log in as an anonymous user. Please make sure that you are connected to the internet, then try again.\n\nError message: " + ex.Message,
                                "Log in to mega.nz", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = DialogResult.None;
            }
        }
示例#4
0
        public async Task <bool> PrepareAsync(CookieAwareWebClient client, CancellationToken cancellation)
        {
            _client = new MegaApiClient(new Options(InternalUtils.GetMegaAppKey().Item1));

            var storage = SettingsHolder.Content.MegaAuthenticationStorage;
            var token   = new MegaApiClient.LogonSessionToken(
                storage.GetEncrypted <string>(KeySession),
                storage.GetEncrypted <byte[]>(KeyToken));

            if (!string.IsNullOrWhiteSpace(token.SessionId) && token.MasterKey != null)
            {
                await _client.LoginAsync(token);
            }
            else
            {
                await _client.LoginAnonymousAsync();
            }

            try {
                var information = await _client.GetNodeFromLinkAsync(_uri);

                TotalSize = information.Size;
                FileName  = information.Name;
                return(true);
            } catch (ApiException e) {
                WindowsHelper.ViewInBrowser(_uri);
                throw new InformativeException("Unsupported link", "Please download it manually.", e);
            }
        }
示例#5
0
        public override async Task ResetAsync(CancellationToken cancellation)
        {
            await base.ResetAsync(cancellation);

            UserEmail    = null;
            UserPassword = null;
            _token       = null;
            Storage.Remove(KeySession);
            Storage.Remove(KeyToken);
        }
示例#6
0
        public MegaUpdater(Uri serverUri, int discoveryPriority, int downloadPriority = 10, NetworkCredential credentials = null) : base(serverUri.OriginalString, discoveryPriority, downloadPriority)
        {
            if (serverUri == null)
            {
                throw new ArgumentNullException(nameof(serverUri));
            }
            if (serverUri.Host.ToLower() != "mega.nz")
            {
                throw new NotSupportedException("The link doesn't point to mega.nz - " + serverUri);
            }

            _client = new MegaApiClient();
            _client.ApiRequestFailed += (sender, args) => Console.WriteLine($@"MEGA API ERROR: {args.ApiResult}   {args.Exception}");

            _currentFolderLink = serverUri;

            try
            {
                if (credentials != null)
                {
                    _authInfos = _client.GenerateAuthInfos(credentials.UserName, credentials.Password);
                }
                else if (!string.IsNullOrWhiteSpace(Settings.Default.mega_authInfos))
                {
                    _authInfos = JsonConvert.DeserializeObject <MegaApiClient.AuthInfos>(Settings.Default.mega_authInfos);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed to create MegaApiClient.AuthInfos - " + ex.ToStringDemystified());
            }

            try
            {
                if (!string.IsNullOrWhiteSpace(Settings.Default.mega_sessionToken))
                {
                    _loginToken = JsonConvert.DeserializeObject <MegaApiClient.LogonSessionToken>(Settings.Default.mega_sessionToken);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed to read stored MegaApiClient.LogonSessionToken - " + ex.ToStringDemystified());
            }
        }
示例#7
0
        public override Task PrepareAsync(CancellationToken cancellation)
        {
            if (!IsReady)
            {
                _token = new MegaApiClient.LogonSessionToken(
                    Storage.GetEncrypted <string>(KeySession),
                    Storage.GetEncrypted <byte[]>(KeyToken));
                if (IsTokenValid)
                {
                    IsReady = true;
                }
                else
                {
                    _token = null;
                }
            }

            return(Task.Delay(0));
        }
示例#8
0
        public async Task <bool> PrepareAsync(CookieAwareWebClient client, CancellationToken cancellation)
        {
            _client = new MegaApiClient(new Options(InternalUtils.GetMegaAppKey().Item1));

            var storage = SettingsHolder.Content.MegaAuthenticationStorage;
            var token   = new MegaApiClient.LogonSessionToken(
                storage.GetEncrypted <string>(KeySession),
                storage.GetEncrypted <byte[]>(KeyToken));

            if (!string.IsNullOrWhiteSpace(token.SessionId) && token.MasterKey != null)
            {
                await _client.LoginAsync(token);
            }
            else
            {
                await _client.LoginAnonymousAsync();
            }

            var information = await _client.GetNodeFromLinkAsync(_uri);

            TotalSize = information.Size;
            FileName  = information.Name;
            return(true);
        }
示例#9
0
 public void Login(MegaApiClient.LogonSessionToken logonSessionToken)
 {
     this.UnwrapException(() => this.client.LoginAsync(logonSessionToken).Wait());
 }
示例#10
0
 public Task LoginAsync(MegaApiClient.LogonSessionToken authInfos)
 {
     return(this.client.LoginAsync(authInfos));
 }
 public Task LoginAsync(MegaApiClient.LogonSessionToken authInfos)
 {
     throw new NotImplementedException();
 }
示例#12
0
        private async Task ConnectImpl(bool askToLogin)
        {
            if (_client.IsLoggedIn)
            {
                if (askToLogin && !_anonymous && _loginToken == null)
                {
                    await _client.LogoutAsync();

                    goto retryLoginWithAuth;
                }
                return;
            }

            if (_loginToken != null)
            {
                try
                {
                    await _client.LoginAsync(_loginToken);

                    if (_client.IsLoggedIn)
                    {
                        return;
                    }
                }
                catch (Exception ex)
                {
                    _loginToken = null;
                    Console.WriteLine($"Failed to log in to mega with token, retrying full login - {ex.ToStringDemystified()}");
                }
            }

retryLoginWithAuth:
            if (_authInfos != null)
            {
                _loginToken = await _client.LoginAsync(_authInfos);

                if (_client.IsLoggedIn)
                {
                    return;
                }
            }

            if (_anonymous || !askToLogin)
            {
                await _client.LoginAnonymousAsync();

                if (_client.IsLoggedIn)
                {
                    return;
                }
            }

            if (!askToLogin)
            {
                return;
            }

            var result = MegaLoginWindow.ShowDialog(_authInfos?.Email, _client);

            if (result == null)
            {
                throw new OperationCanceledException();
            }

            _authInfos  = result.Item1;
            _loginToken = result.Item2;
            _anonymous  = _loginToken == null;

            try
            {
                Settings.Default.mega_sessionToken = string.Empty;
                if (_loginToken != null)
                {
                    Settings.Default.mega_sessionToken = JsonConvert.SerializeObject(_loginToken);
                }
                Settings.Default.Save();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed to save MegaApiClient.LogonSessionToken - " + ex.ToStringDemystified());
            }
            try
            {
                Settings.Default.mega_authInfos = string.Empty;
                if (_authInfos != null)
                {
                    Settings.Default.mega_authInfos = JsonConvert.SerializeObject(_authInfos);
                }
                Settings.Default.Save();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed to save new MegaApiClient.AuthInfos - " + ex.ToStringDemystified());
            }

            if (!_client.IsLoggedIn)
            {
                goto retryLoginWithAuth;
            }
        }