|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.IO; |
| 4 | +using System.Threading; |
| 5 | +using System.Windows.Forms; |
| 6 | + |
| 7 | +namespace MegaKeep |
| 8 | +{ |
| 9 | + public partial class MegaKeep : Form |
| 10 | + { |
| 11 | + private string _local = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); |
| 12 | + |
| 13 | + public MegaKeep() |
| 14 | + { |
| 15 | + InitializeComponent(); |
| 16 | + } |
| 17 | + |
| 18 | + private void btnRun_Click(object sender, EventArgs e) |
| 19 | + { |
| 20 | + txtLog.Clear(); |
| 21 | + |
| 22 | + // first make sure megacmd is found |
| 23 | + if (!File.Exists(_local + "\\MEGAcmd\\mega-login.bat")) |
| 24 | + { |
| 25 | + Log("mega-login.bat was not found, please install it to the default dirctory: https://mega.nz/cmd"); |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + // then check to make sure the file exists |
| 30 | + if (!File.Exists(txtPath.Text)) |
| 31 | + { |
| 32 | + MessageBox.Show("The file could not be found"); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + Log("Loading file..."); |
| 37 | + |
| 38 | + // then try to read the text file's contents |
| 39 | + string[] lines; |
| 40 | + try |
| 41 | + { |
| 42 | + lines = File.ReadAllLines(txtPath.Text); |
| 43 | + } |
| 44 | + catch (Exception ex) |
| 45 | + { |
| 46 | + Log("Error: " + ex.ToString()); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + // loop through every line |
| 51 | + foreach (var line in lines) |
| 52 | + { |
| 53 | + var info = line.Split(':'); |
| 54 | + var user = info[0]; |
| 55 | + var pass = info[1]; |
| 56 | + |
| 57 | + var restart = false; |
| 58 | + |
| 59 | + Log("Logging in to " + user + "..."); |
| 60 | + |
| 61 | + Process login = new Process |
| 62 | + { |
| 63 | + StartInfo = |
| 64 | + { |
| 65 | + UseShellExecute = false, |
| 66 | + RedirectStandardOutput = true, |
| 67 | + RedirectStandardError = true, |
| 68 | + CreateNoWindow = true, |
| 69 | + FileName = _local + "\\MEGAcmd\\mega-login.bat", |
| 70 | + Arguments = user + " \"" + pass + "\"" |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | + login.Start(); |
| 75 | + var result = login.StandardOutput.ReadToEnd(); |
| 76 | + login.WaitForExit(); |
| 77 | + |
| 78 | + if (login.HasExited) |
| 79 | + { |
| 80 | + if (result.Contains("Login failed")) |
| 81 | + { |
| 82 | + Log("Failed: " + result); |
| 83 | + continue; // just move on to the next account |
| 84 | + } |
| 85 | + else if (result.Contains("Already logged in")) |
| 86 | + { |
| 87 | + Log("Already logged in. Logging out and restarting..."); |
| 88 | + restart = true; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // wait a sec |
| 93 | + Thread.Sleep(1500); |
| 94 | + |
| 95 | + Process logout = new Process |
| 96 | + { |
| 97 | + StartInfo = |
| 98 | + { |
| 99 | + UseShellExecute = false, |
| 100 | + RedirectStandardOutput = true, |
| 101 | + RedirectStandardError = true, |
| 102 | + CreateNoWindow = true, |
| 103 | + FileName = _local + "\\MEGAcmd\\mega-logout.bat" |
| 104 | + } |
| 105 | + }; |
| 106 | + |
| 107 | + logout.Start(); |
| 108 | + logout.WaitForExit(); |
| 109 | + |
| 110 | + if (logout.HasExited) |
| 111 | + Log(logout.StandardOutput.ReadToEnd()); |
| 112 | + |
| 113 | + if (restart) |
| 114 | + { |
| 115 | + btnRun.PerformClick(); |
| 116 | + return; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + Log("Finished"); |
| 121 | + } |
| 122 | + |
| 123 | + private void btnLocate_Click(object sender, EventArgs e) |
| 124 | + { |
| 125 | + OpenFileDialog openFile = new OpenFileDialog(); |
| 126 | + |
| 127 | + openFile.Multiselect = false; |
| 128 | + openFile.Title = "Mega Keepalive"; |
| 129 | + openFile.Filter = "Text Files (*.txt)|*.txt"; |
| 130 | + |
| 131 | + if (openFile.ShowDialog() == DialogResult.OK) |
| 132 | + { |
| 133 | + txtPath.Text = openFile.FileName; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private void Log(string txt) |
| 138 | + { |
| 139 | + txtLog.Text += txt + Environment.NewLine; |
| 140 | + } |
| 141 | + |
| 142 | + private void MegaKeep_Load(object sender, EventArgs e) |
| 143 | + { |
| 144 | + txtPath.Text = Properties.Settings.Default.Location; |
| 145 | + } |
| 146 | + |
| 147 | + private void txtPath_TextChanged(object sender, EventArgs e) |
| 148 | + { |
| 149 | + Properties.Settings.Default.Location = txtPath.Text; |
| 150 | + Properties.Settings.Default.Save(); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments