Exemplo n.º 1
0
        public Form1()
        {
            InitializeComponent();

            System.Net.ServicePointManager.DefaultConnectionLimit = 50;
            notifyIcon1.Icon = Properties.Resources.min;
            notifyIcon1.Click += notifyIcon1_MouseClick;

            if (!string.IsNullOrEmpty(Properties.Settings.Default.folderPath))
            {
                textBoxFolder.Text = Properties.Settings.Default.folderPath;
            }
            else
            {
                textBoxFolder.Text = Path.Combine(
                        Environment.GetEnvironmentVariable("USERPROFILE"),
                        "MegaDesktop");
            }

            auth = Mega.LoadAccount(GetUserKeyFilePath());
            if (auth != null)
            {
                textBoxEmail.Text = auth.Email;
                textBoxPassword.Text = "password";
            }
        }
Exemplo n.º 2
0
        public static MegaUser LoadAccount(string filePath)
        {
            MegaUser u = null;

            try
            {
                var encrypted = File.ReadAllBytes(filePath);

                var decrypted = ProtectedData.Unprotect(encrypted, keyStoreSeed,
                                                        DataProtectionScope.CurrentUser);

                // registered user
                if (decrypted[0] == 1)
                {
                    u = new MegaUser(
                        email: decrypted.Skip(24 + 1).Take(decrypted.Length - 24 + 1).ToArray(),
                        userHash: decrypted.Skip(1).Take(8).ToArray(),
                        passKey: decrypted.Skip(8 + 1).Take(16).ToArray()
                        );
                }
                else // anon saved user
                {
                    u = new MegaUser(
                        userId: decrypted.Skip(16 + 1).Take(8).ToArray(),
                        passKey: decrypted.Skip(1).Take(16).ToArray());
                }
            }
            catch
            {
                return(null);
            }
            return(u);
        }
Exemplo n.º 3
0
 public static void Init(MegaUser user, Action<Mega> OnSuccess, Action<int> OnError)
 {
     if (OnSuccess == null || OnError == null)
     {
         throw new ArgumentException("Login handlers can not be empty");
     }
     Util.StartThread(() => ThreadInit(user, OnSuccess, OnError), "mega_api_login");
 }
Exemplo n.º 4
0
 public static void Init(MegaUser user, Action <Mega> OnSuccess, Action <int> OnError)
 {
     if (OnSuccess == null || OnError == null)
     {
         throw new ArgumentException("Login handlers can not be empty");
     }
     Util.StartThread(() => ThreadInit(user, OnSuccess, OnError), "mega_api_login");
 }
Exemplo n.º 5
0
        private static IMegaRequest ThreadInit(MegaUser user, Action <Mega> OnSuccess, Action <int> OnError)
        {
            var initTransport = new Transport();
            var mega          = new Mega(initTransport);

            // mandatory anonymous registration
            if (user == null || (user.Email == null && user.Id == null))
            {
                user = new MegaUser();
                var req   = new MRequestCreateAnon <MResponseCreateAnon>(user);
                int?error = null;
                req.Success += (s, args) => user.Id = args.UserId;
                req.Error   += (s, e) => error = e.Error;
                initTransport.EnqueueRequest(req);
                req.ResetEvent.WaitOne();
                if (error != null)
                {
                    OnError((int)error);
                    // just to have the ResetEvent for async compatibility
                    return(new EmptyRequest());
                }
            }

            // set the credentials
            mega.User = user;

            // the login itself
            var sidRequest = new MRequestGetSid <MResponseGetSid>(mega.User);

            sidRequest.Success += (s, args) =>
            {
                mega.User.Sid = args.SessionId;
                mega.User.SetMasterKey(args.MasterKey.Value);

                var getUserRequest = new MRequestGetUser <MResponseGetUser>(mega.User);
                getUserRequest.Success += (s2, args2) =>
                {
                    mega.User.Status = args2.UserStatus;
                    mega.User.Email  = args2.Email;
                    mega.User.Id     = args2.UserId;
                    OnSuccess(mega);
                };
                getUserRequest.Error += (s2, e) =>
                {
                    OnError(e.Error);
                    sidRequest.ResetEvent.Set();
                };
                initTransport.EnqueueRequest(getUserRequest);
                getUserRequest.ResetEvent.WaitOne();
            };
            sidRequest.Error += (s, a) => OnError(a.Error);

            initTransport.EnqueueRequest(sidRequest);
            return(sidRequest);
        }
Exemplo n.º 6
0
        private static IMegaRequest ThreadInit(MegaUser user, Action<Mega> OnSuccess, Action<int> OnError)
        {
            var initTransport = new Transport();
            var mega = new Mega(initTransport);

            // mandatory anonymous registration
            if (user == null || (user.Email == null && user.Id == null))
            {
                user = new MegaUser();
                var req = new MRequestCreateAnon<MResponseCreateAnon>(user);
                int? error = null;
                req.Success += (s, args) => user.Id = args.UserId;
                req.Error += (s, e) => error = e.Error;
                initTransport.EnqueueRequest(req);
                req.ResetEvent.WaitOne();
                if (error != null)
                {
                    OnError((int)error);
                    // just to have the ResetEvent for async compatibility
                    return new EmptyRequest();
                }
            }

            // set the credentials
            mega.User = user;

            // the login itself
            var sidRequest = new MRequestGetSid<MResponseGetSid>(mega.User);
            sidRequest.Success += (s, args) =>
            {
                mega.User.Sid = args.SessionId;
                mega.User.SetMasterKey(args.MasterKey.Value);

                var getUserRequest = new MRequestGetUser<MResponseGetUser>(mega.User);
                getUserRequest.Success += (s2, args2) =>
                {
                    mega.User.Status = args2.UserStatus;
                    mega.User.Email = args2.Email;
                    mega.User.Id = args2.UserId;
                    OnSuccess(mega);
                };
                getUserRequest.Error += (s2, e) =>
                {
                    OnError(e.Error);
                    sidRequest.ResetEvent.Set();
                };
                initTransport.EnqueueRequest(getUserRequest);
                getUserRequest.ResetEvent.WaitOne();
            };
            sidRequest.Error += (s, a) => OnError(a.Error);

            initTransport.EnqueueRequest(sidRequest);
            return sidRequest;
        }
Exemplo n.º 7
0
 public Mega InitSync(MegaUser user)
 {
     Mega api = null;
     int? error = null;
     ThreadInit(user, (m) => api = m, (i) => error = i).ResetEvent.WaitOne();
     if (api == null || error != null)
     {
         throw new MegaApiException((int)error, "Login error");
     }
     return api;
 }
Exemplo n.º 8
0
        public Mega InitSync(MegaUser user)
        {
            Mega api   = null;
            int? error = null;

            ThreadInit(user, (m) => api = m, (i) => error = i).ResetEvent.WaitOne();
            if (api == null || error != null)
            {
                throw new MegaApiException((int)error, "Login error");
            }
            return(api);
        }
Exemplo n.º 9
0
        public static MegaUser LoadAccount(string filePath)
        {
            MegaUser u = null;
            try
            {
                var encrypted = File.ReadAllBytes(filePath);

                var decrypted = ProtectedData.Unprotect(encrypted, keyStoreSeed,
                    DataProtectionScope.CurrentUser);

                // registered user
                if (decrypted[0] == 1)
                {
                    u = new MegaUser(
                        email: decrypted.Skip(24 + 1).Take(decrypted.Length - 24 + 1).ToArray(),
                        userHash: decrypted.Skip(1).Take(8).ToArray(),
                        passKey: decrypted.Skip(8 + 1).Take(16).ToArray()
                        );
                }
                else // anon saved user
                {
                    u = new MegaUser(
                        userId: decrypted.Skip(16 + 1).Take(8).ToArray(),
                        passKey: decrypted.Skip(1).Take(16).ToArray());
                }
            }
            catch
            {
                return null;
            }
            return u;
        }
Exemplo n.º 10
0
        private void buttonStart_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxEmail.Text) || string.IsNullOrEmpty(textBoxPassword.Text))
            {
                return;
            }
            buttonBrowse.Enabled = false;
            textBoxFolder.Enabled = false;
            buttonStart.Enabled = false;
            buttonExit.Enabled = false;
            if (!Directory.Exists(textBoxFolder.Text)) { Directory.CreateDirectory(textBoxFolder.Text); }

            if (Properties.Settings.Default.folderPath != textBoxFolder.Text && File.Exists(GetMetadataPath()))
            {
                File.Delete(GetMetadataPath());
            }

            Properties.Settings.Default.folderPath = textBoxFolder.Text;
            Properties.Settings.Default.Save();

            textBoxLoginStatus.Text = "Connecting...";
            textBoxEmail.Enabled = false;
            textBoxPassword.Enabled = false;

            if ((auth != null && textBoxEmail.Text != auth.Email)
                || textBoxPassword.Text != "password"
                || auth == null)
            {
                auth = new MegaUser(textBoxEmail.Text, textBoxPassword.Text);
            }

            Mega.Init(auth, (m) =>
                {
                    Invoke(new Action(() =>
                        { textBoxLoginStatus.Text = "Working."; }));
                    StartSync(m);
                }, (i) =>
                {
                    Invoke(new Action(() =>
                        {
                            textBoxLoginStatus.Text = "Invalid login or password";
                            textBoxEmail.Enabled = true;
                            textBoxPassword.Enabled = true;
                            buttonStart.Enabled = true;
                            buttonExit.Enabled = true;
                            textBoxFolder.Enabled = true;
                            buttonStart.Enabled = true;
                        }));
                });
        }