private void finishDownload(AddFile file) { bool isGoot = true; for (ulong i = 0; i < file.getCountBlock(); i++) { if (file.flagBlock[i] == 0) { isGoot = false; this.Send(false, HeadFileDop, file.getName(), file.getNewName(), Convert.ToString(i), Convert.ToString(file.getBlockSize())); } } if (isGoot) { if (file.isSave) { Storage.setIsSave(false); } this.downloadFile.Remove(file); this.form.UpdateDownloadInfo(file); } else { this.Send(false, HeadFileDop, file.getName(), file.getNewName(), Convert.ToString(file.getCountBlock() - 1), Convert.ToString(file.getBlockSize())); } }
private void OnMessage(byte[] Message, bool isRsa = false) { byte[][] mess = Program.Split(Message, Encoding.UTF8.GetBytes(Separator), true); if (mess.Length == 0) { return; } //this.form.SendToFormMessage(((this.perhaps != null) ? "aes_per" : "null") + ((!isRsa) ? "aes_per" : "null")); //for(int i = 0; i<mess.Length; i++) // this.form.SendToFormMessage("sr4.M: " + i + ": " + mess[i].Length + ": " + Encoding.UTF8.GetString(mess[i])); string head = Encoding.UTF8.GetString(mess[0]); switch (head) { case HeadFileDop: { if ((mess.Length == 5) && (this.user != null)) { string name = Encoding.UTF8.GetString(mess[1]), newName = Encoding.UTF8.GetString(mess[2]), blockNumber = Encoding.UTF8.GetString(mess[3]), blockSize = Encoding.UTF8.GetString(mess[4]); ulong number = Convert.ToUInt64(blockNumber); int size = Convert.ToInt32(blockSize); try { byte[] block = Storage.getBlockFile(name, size, number); int hash = Program.HechBytes(block); string header = HeadFile + Separator + name + Separator + newName + Separator + Convert.ToString(number) + Separator + Convert.ToString(hash) + Separator; byte[] buffer = Encoding.UTF8.GetBytes(header); this.Send(Program.ConcatByte(buffer, block)); this.form.SendToFormMessage("Dop file block: " + number); } catch (Exception e) { this.form.SendToFormMessage("Dop file block eror: " + e.Message + "\n" + e.StackTrace); } } break; } case HeadFile: { if ((mess.Length == 4) && (this.user != null)) { string name = Encoding.UTF8.GetString(mess[1]), newName = Encoding.UTF8.GetString(mess[2]), Error = Encoding.UTF8.GetString(mess[3]); var file = new AddFile(this.user, name, 0, 0, newName); file.setError(Error); this.form.downloadError(file); } if ((mess.Length == 5) && (this.user != null)) { string name = Encoding.UTF8.GetString(mess[1]), newName = Encoding.UTF8.GetString(mess[2]), blockSize = Encoding.UTF8.GetString(mess[3]), countBlock = Encoding.UTF8.GetString(mess[4]); var file = Storage.getAddFile(name, newName, Convert.ToInt32(blockSize), Convert.ToUInt64(countBlock)); if (file != null) { this.startUpload(file); } else { this.Send(false, HeadFile, name, newName, "file not found"); } } else if ((mess.Length == 6) && (this.user != null)) { this.Send(Client.Good); string name = Encoding.UTF8.GetString(mess[1]), newName = Encoding.UTF8.GetString(mess[2]); ulong blockNumber = Convert.ToUInt64(Encoding.UTF8.GetString(mess[3])); AddFile file = null; foreach (var i in this.downloadFile) { if (i.getNewName() == newName) { file = i; break; } } if (file != null) { if (mess[5].Length > file.getBlockSize()) { System.Array.Resize(ref mess[5], file.getBlockSize()); } if (blockNumber == file.getCountBlock() - 1 && file.getSize() % (ulong)file.getBlockSize() != 0) { System.Array.Resize(ref mess[5], (int)(file.getSize() % (ulong)file.getBlockSize())); } int hash2 = Program.HechBytes(mess[5]); if (Convert.ToInt32(Encoding.UTF8.GetString(mess[4])) == hash2) { try { Storage.setBlockFile(file.getNewName(), file.getBlockSize(), blockNumber, mess[5]); file.flagBlock[blockNumber] = 1; if (blockNumber > 100 && blockNumber % (file.getCountBlock() / 100) == 0) { if (file.isSave) { Storage.saveStatistic(blockNumber / (file.getCountBlock() / 100)); } this.form.UpdateDownloadInfo(file); this.form.SendToFormMessage("download: " + blockNumber / (file.getCountBlock() / 100) + "%"); } } catch (Exception e) { this.form.SendToFormMessage("download error: " + e.StackTrace); } } if (blockNumber == file.getCountBlock() - 1) { this.finishDownload(file); } } } break; } case HeadAddFile: { if ((mess.Length == 5) && (this.user != null)) { string name = Encoding.UTF8.GetString(mess[1]), size = Encoding.UTF8.GetString(mess[2]), time = Encoding.UTF8.GetString(mess[4]); string newName = Storage.getNewName(name); bool isPrivate = Convert.ToBoolean(Encoding.UTF8.GetString(mess[3])); var file = new AddFile(this.user, name, Convert.ToUInt64(size), Storage.getBlockSize(), Convert.ToDateTime(time), newName); file.isPrivate = isPrivate; this.form.SendToFormMessage(file); } break; } case HeadMessage: { if ((mess.Length != 4) && (this.user.client != null)) { break; } string text = Encoding.UTF8.GetString(mess[2]), time = Encoding.UTF8.GetString(mess[3]); bool isPrivate = Convert.ToBoolean(Encoding.UTF8.GetString(mess[1])); MESSAGE newMessage = new MESSAGE(this.user, Server.me, text, Convert.ToDateTime(time), isPrivate); this.form.SendToFormMessage(newMessage); break; } case HeadUser: { if (mess.Length == 3) { string HostName = Dns.GetHostEntry(((IPEndPoint)this.tcp.Client.RemoteEndPoint).Address).HostName; string name = Encoding.UTF8.GetString(mess[1]); string avatar = Encoding.UTF8.GetString(mess[2]); if (this.user == null) { this.user = new User(name, HostName, System.Convert.ToInt32(avatar), this); } else { this.user.update(System.Convert.ToInt32(avatar)); } this.form.SendToFormMessage("sr4.User"); Storage.SaveContact(this.user); this.form.OnConnect(this.user); } else { this.form.SendToFormMessage("sr4.NOT_USER"); } break; } case IfHaveServer: { this.Send(YesItIsServer); if (this.user == null) { this.Send(GetUserPliace); } break; } case YesItIsServer: { if (this.user == null) { this.Send(GetUserPliace); } break; } case GetUserPliace: { while (true) { if (Server.me != null) { this.Send(Server.me); break; } else { Thread.Sleep(200); } } break; } case EndSeans: { this.OnError(new Exception("Cancel")); break; } case GetSecretKey: { if (mess.Length == 2) { mess[1] = Program.SubBytes(mess[1], 0, 148); this.form.SendToFormMessage("Create Secret Data:" + mess[1].Length); this.form.SendToFormMessage("SecretKey: " + Program.HechBytes(mess[1])); rsa = new CrypterRSA(mess[1]); var aes = new CrypterAES(); byte[] message = new byte[0] { }; message = Program.ConcatByte(message, Encoding.UTF8.GetBytes(HeadSecret + Separator + CrypterAES.getMode() + Separator + CrypterAES.getPadding() + Separator)); message = Program.ConcatByte(message, aes.getKey()); message = Program.ConcatByte(message, Encoding.UTF8.GetBytes(Separator)); message = Program.ConcatByte(message, aes.getIV()); this.perhaps = aes; this.Send(rsa.Coder(message)); this.form.SendToFormMessage("hech: " + aes.GetHashCode()); } break; } case HeadSecret: { if (mess.Length == 5) { this.form.SendToFormMessage("Create Secret Line"); this.user.crypt = new CrypterAES(Convert.ToInt32(Encoding.UTF8.GetString(mess[1])), Convert.ToInt32(Encoding.UTF8.GetString(mess[2])), mess[3], mess[4]); this.rsa = null; this.Send(true, HeadSecret, Client.Good); this.form.SendToFormMessage("hech: " + this.user.crypt.GetHashCode()); } else if (mess.Length == 2) { this.form.SendToFormMessage("1: " + Encoding.UTF8.GetString(mess[1]) + " 2: " + (string)Client.Good); if (Encoding.UTF8.GetString(mess[1]) == Client.Good || true) { this.user.crypt = this.perhaps; this.perhaps = null; this.form.SendToFormMessage("secret good"); } } break; } default: { if (this.perhaps != null && !isRsa) { try { this.OnMessage(this.perhaps.DecoderBytes(Message), true); } catch (Exception e) { this.form.SendToFormMessage("eror pizdaaaaaaaaaaa: " + e.Message + "\n" + e.StackTrace); } } if (this.rsa != null && !isRsa && this.rsa.isPrivate) { try { this.OnMessage(this.rsa.Decoder(Message), true); } catch { return; } } break; } } }
static public void saveStatistic(AddFile file) { try { using (StreamWriter sw = new StreamWriter(Storage.DefaultData + "\\" + DefaultLastDownloadInfo, false, System.Text.Encoding.Default)) { sw.WriteLine(DateTime.Now.Ticks + " " + file.getCountBlock() + " " + file.getBlockSize()); } } catch { } }