using System; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Threading; using System.Windows.Forms; using System.IO; using System.Net; using System.Net.Sockets; using System.Security.Cryptography; using System.Diagnostics; using System.Resources; using SharpCompress.Archive; using SharpCompress.Common; namespace Client { public partial class Form1 : Form { public Form1() { InitializeComponent(); } #region Config string versionAddress = @"http://46.20.46.198/patches/LA/version.txt"; string patchAddress = @"http://46.20.46.198/patches/LA/patch/"; string LUAddress = @"http://46.20.46.198/patches/LU/version.txt"; string path = Application.StartupPath + @"\reslauncher\version.clv"; string localPath = Application.StartupPath + @"\"; #endregion private void Form1_Load(object sender, EventArgs e) { if (!Directory.Exists(System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + @"\reslauncher)")) Directory.CreateDirectory(System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + @"\reslauncher"); if ((LU()) < LUversion()) { if (File.Exists("LU.exe")) { Process.Start("LU.exe"); Application.Exit(); } else { MessageBox.Show("Cannot find LU.exe"); Application.Exit(); } } } private void Form1_Shown(object sender, EventArgs e) { toolStripStatusLabel1.Text = "Connecting to the server..."; bw.RunWorkerAsync(); } private void bw_DoWork(object sender, DoWorkEventArgs e) { while ((version()) < checkUpdates()) { downloadUpdate(version()); } } private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e) { toolStripStatusLabel1.Text = "Downloading patch... (" + e.ProgressPercentage + "%)"; //progressBar1.Value = e.ProgressPercentage; //this.Dispatcher.BeginInvoke((Action)(() => { processLabel.Content = "Downloading patch..."; })); } private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { toolStripStatusLabel1.Text = "Patch check completed"; button1.Enabled = true; //processLabel.Content = "Patch check completed"; //button1.IsEnabled = true; //progressBar1.Visibility = Visibility.Hidden; } //Read LU patches available on server int LUversion() { WebClient wc = new WebClient(); using (StreamReader sr = new StreamReader(wc.OpenRead(LUAddress))) { int lines = 0; while (sr.ReadLine() != null) { lines++; } return lines; } } //Check local LU Version int LU() { if (!File.Exists(localPath + @"reslauncher\version.lu")) { using (StreamWriter sw = new StreamWriter(localPath + @"reslauncher\version.lu")) { sw.WriteLine(Encrypt("0", true)); } } using (StreamReader sr = new StreamReader(localPath + @"\reslauncher\version.lu")) { return Convert.ToInt32(Decrypt(sr.ReadLine(), true)); } } int version() { string versionFile = path; int version; if (!File.Exists(versionFile)) { using (StreamWriter sw = new StreamWriter(versionFile)) { sw.WriteLine(Encrypt("0", true)); } } using (StreamReader sr = new StreamReader(path)) { version = Convert.ToInt32(Decrypt(sr.ReadLine(), true)); sr.Dispose(); } return version; } int checkUpdates() { int lines = 0; WebClient wc = new WebClient(); using (StreamReader sr = new StreamReader(wc.OpenRead(versionAddress))) { while (sr.ReadLine() != null) { lines++; } } return lines; } public void updateVersion(int update) { using (FileStream stream = new FileStream(path, FileMode.Create)) { using (TextWriter writer = new StreamWriter(stream)) { update++; string Encrypted = Encrypt(update.ToString(), true); writer.WriteLine(Encrypted); writer.Dispose(); } } } public void downloadUpdate(int downloadPatch) { //this.Dispatcher.BeginInvoke((Action)(() => { progressBar1.Visibility = Visibility.Visible; })); string patchUrl = GetPatch(downloadPatch); string sUrlToDnldFile = patchAddress + patchUrl; string sFileSavePath = localPath + patchUrl; try { Uri url = new Uri(sUrlToDnldFile); WebClient client = new WebClient(); string sFileName = System.IO.Path.GetFileName(url.LocalPath); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); response.Close(); long iSize = response.ContentLength; long iRunningByteTotal = 0; Stream strRemote = client.OpenRead(url); using (FileStream strLocal = new FileStream(sFileSavePath, FileMode.Create, FileAccess.Write, FileShare.None)) { int iByteSize = 0; byte[] byteBuffer = new byte[1024]; while ((iByteSize = strRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0) { strLocal.Write(byteBuffer, 0, iByteSize); iRunningByteTotal += iByteSize; double dIndex = (double)(iRunningByteTotal); double dTotal = (double)iSize; double dProgressPercentage = (dIndex / dTotal); int iProgressPercentage = (int)(dProgressPercentage * 100); bw.ReportProgress(iProgressPercentage); } } strRemote.Close(); updateVersion(version()); UnRar(patchUrl); RemoveRar(patchUrl); } catch (Exception e) { MessageBox.Show(e.ToString()); } } string GetPatch(int version) { WebClient wc = new WebClient(); using (StreamReader sr = new StreamReader(wc.OpenRead(versionAddress))) { for (int i = 0; i < version; i++) { sr.ReadLine(); } return sr.ReadLine().Substring(2); } } void UnRar(string patch) { using (var archive = ArchiveFactory.Open(patch)) { foreach (var entry in archive.Entries) { if (!entry.IsDirectory) { toolStripStatusLabel1.Text = "Applying: " + entry.FilePath; //processLabel.Dispatcher.Invoke(new Action(() => //{ // progressBar1.Visibility = Visibility.Visible; // processLabel.Content = "Applying: " + entry.FilePath; //})); entry.WriteToDirectory(System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName), ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); } } } } void RemoveRar(string patch) { if (File.Exists(patch)) { File.Delete(patch); } } private void button1_Click(object sender, EventArgs e) { try { if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text)) { MessageBox.Show("Please check that your email and password are correct.", "401", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } TcpClient client = new TcpClient(); client.Connect(IPAddress.Parse("46.20.46.198"), 81); MD5 md5Hash = MD5.Create(); string username = textBox1.Text; string password = GetMd5Hash(md5Hash, textBox2.Text); string data = Encrypt(username + "/" + password, true); Stream stream = client.GetStream(); byte[] bytesToSend = new byte[2048]; bytesToSend = Encoding.Default.GetBytes(data); stream.Write(bytesToSend, 0, bytesToSend.Length); byte[] bytesToReveice = new byte[2048]; int received = stream.Read(bytesToReveice, 0, bytesToReveice.Length); Array.Resize(ref bytesToReveice, received); string user = Decrypt(Encoding.Default.GetString(bytesToReveice), true); if (user == "0") { MessageBox.Show("Please check that your email and password are correct.", "401", MessageBoxButtons.OK, MessageBoxIcon.Information); } else if (user == "-1") { MessageBox.Show("Your account has been banned.", "403", MessageBoxButtons.OK, MessageBoxIcon.Information); } else if (user == "2") { MessageBox.Show("You are not allowed to login yet.", "402", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { string param = "-osk_server 46.20.46.198 -osk_token " + user + " -osk_store http://store.outspark.com/game/fiesta"; if (File.Exists(Application.StartupPath + @"\Corsage.bin")) { ProcessStartInfo info = new ProcessStartInfo(Application.StartupPath + @"\Corsage.bin", param); info.UseShellExecute = false; Process process = new Process(); process.StartInfo = info; process.Start(); Application.Exit(); } else { MessageBox.Show("The system cannot find the file specified"); Application.Exit(); } } } catch { MessageBox.Show("The remote server returned an error: (404) Not Found.", "404", MessageBoxButtons.OK, MessageBoxIcon.Information); } } static string GetMd5Hash(MD5 md5Hash, string input) { // Convert the input string to a byte array and compute the hash. byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input)); // Create a new Stringbuilder to collect the bytes // and create a string. StringBuilder sBuilder = new StringBuilder(); // Loop through each byte of the hashed data // and format each one as a hexadecimal string. for (int i = 0; i < data.Length; i++) { sBuilder.Append(data[i].ToString("x2")); } // Return the hexadecimal string. return sBuilder.ToString(); } public static string Encrypt(string toEncrypt, bool useHashing) { byte[] keyArray; byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt); string key = "mpiuqgi6zO8uoVogsBp8h5v9g7EuQ3bSBlM9agqA2Nnm4elloOfENIUFGHdW"; if (useHashing) { MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider(); keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key)); hashmd5.Clear(); } else keyArray = UTF8Encoding.UTF8.GetBytes(key); TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); tdes.Key = keyArray; tdes.Mode = CipherMode.ECB; tdes.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = tdes.CreateEncryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); tdes.Clear(); return Convert.ToBase64String(resultArray, 0, resultArray.Length); } public static string Decrypt(string cipherString, bool useHashing) { byte[] keyArray; byte[] toEncryptArray = Convert.FromBase64String(cipherString); string key = "mpiuqgi6zO8uoVogsBp8h5v9g7EuQ3bSBlM9agqA2Nnm4elloOfENIUFGHdW"; if (useHashing) { MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider(); keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key)); hashmd5.Clear(); } else { keyArray = UTF8Encoding.UTF8.GetBytes(key); } TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); tdes.Key = keyArray; tdes.Mode = CipherMode.ECB; tdes.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = tdes.CreateDecryptor(); byte[] resultArray = cTransform.TransformFinalBlock( toEncryptArray, 0, toEncryptArray.Length); tdes.Clear(); return UTF8Encoding.UTF8.GetString(resultArray); } } }