Beispiel #1
0
        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()));
            }
        }
Beispiel #2
0
        public static bool isHave(AddFile file)
        {
            string curentDir = Environment.CurrentDirectory;

            if (!File.Exists(curentDir + "\\" + DefaultFile + "\\" + file.getNewName()))
            {
                return(false);
            }

            ulong size = (ulong)(new FileInfo(DefaultFile + "\\" + file.getNewName())).Length;

            if (size != file.getSize())
            {
                return(false);
            }

            return(true);
        }
Beispiel #3
0
        public static void createFile(AddFile file)
        {
            if (!File.Exists(DefaultFile + "\\" + file.getNewName()))
            {
                if (!Directory.Exists(DefaultFile))
                {
                    Directory.CreateDirectory(DefaultFile);
                }

                var stream = new BinaryWriter(File.Create(DefaultFile + "\\" + file.getNewName()));
                for (ulong i = 0; i < file.getSize(); i++)
                {
                    stream.Write((byte)1);
                }
                stream.Close();
                stream.Dispose();
            }
            else
            {
                throw new Exception("File not found!");
            }
        }
Beispiel #4
0
 private void errorDonwload(object file)
 {
     if (file.GetType() == typeof(AddFile))
     {
         var File = (AddFile)file;
         if (File.getCreator() != null && File.getCreator().message != null)
         {
             foreach (var i in File.getCreator().message.Controls)
             {
                 if (i.GetType() == typeof(Panel))
                 {
                     AddFile addFile = this.getFile(View.getInfa((Panel)i));
                     if (addFile == null)
                     {
                         continue;
                     }
                     if (addFile.getCreator().Equals(File.getCreator()) && addFile.getName() == File.getName() && addFile.getNewName() == File.getNewName())
                     {
                         View.updateFileMessage(addFile.getPanel(), File.getError());
                         return;
                     }
                 }
             }
         }
         if (File.getCreator() != null)
         {
             foreach (var i in this.panel1.Controls)
             {
                 if (i.GetType() == typeof(Panel))
                 {
                     AddFile addFile = this.getFile(View.getInfa((Panel)i));
                     if (addFile == null)
                     {
                         continue;
                     }
                     if (addFile.getCreator().Equals(File.getCreator()) && addFile.getName() == File.getName() && addFile.getNewName() == File.getNewName())
                     {
                         View.updateFileMessage(addFile.getPanel(), File.getError());
                         return;
                     }
                 }
             }
         }
     }
 }
Beispiel #5
0
        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;
            }
            }
        }
Beispiel #6
0
        public static Panel createFileMessage(AddFile message, int PanelWidth, downloadClick click, bool isMine)
        {
            var MesView = new MessageView(PanelWidth * 9 / 10);

            var image = new Button();

            image.Text   = "";
            image.Width  = MesView.ImageWidth;
            image.Height = MesView.ImageHeight;
            image.Top    = MesView.ImageTop;
            image.Region = MesView.ImageRegion;
            image.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            image.BackgroundImage       = View.getAvatar(message.getCreator().avatarNumber);


            if (!isMine)
            {
                image.Left = MesView.ImageLeft;
            }

            var fileName = new Label();

            fileName.Text        = message.getName() + "\nsize: " + Convert.ToString(message.getSize()) + "\n" + message.getNewName();
            fileName.Font        = new Font(fileName.Font.Name, 8.0f, FontStyle.Bold | FontStyle.Underline);
            fileName.ForeColor   = Color.White;
            fileName.AutoSize    = true;
            fileName.MaximumSize = new Size(PanelWidth * 7 / 10, 0);
            fileName.MinimumSize = new Size(MesView.TextWidth, MesView.TextHeight);
            fileName.Height      = fileName.PreferredHeight;
            fileName.Width       = fileName.PreferredWidth;
            fileName.Top         = MesView.TextTop;
            fileName.Click      += new EventHandler(click);
            fileName.Cursor      = Cursors.Hand;

            if (!isMine)
            {
                fileName.Left = MesView.TextLeft + 2 * MesView.ImageLeft + MesView.ImageWidth;
            }
            else
            {
                fileName.Left = MesView.TextLeft;
                image.Left    = MesView.ImageLeft + 2 * MesView.TextLeft + fileName.Width;
            }


            var name = new Label();

            name.Text   = message.getCreator().Name + "#" + message.getCreator().HostName;
            name.Width  = MesView.NameWidth;
            name.Left   = fileName.Left;
            name.Top    = fileName.Height + MesView.TextTop + MesView.NameTop;
            name.Height = MesView.NameHeight;

            var date = new Label();

            date.Text   = message.getTime().ToString();
            date.Width  = MesView.DateWidth;
            date.Left   = name.Left + name.Width + MesView.DateLeft;
            date.Top    = name.Top;
            date.Height = name.Height;


            var fullMessage = new Panel();

            fullMessage.Width       = image.Width + fileName.Width + 2 * MesView.ImageLeft + 2 * MesView.TextLeft;
            fullMessage.Text        = "";
            fullMessage.Height      = Math.Max(MesView.ImageHeight + 2 * MesView.ImageTop, fileName.Height + MesView.TextTop + MesView.NameTop + name.Height);
            fullMessage.BorderStyle = BorderStyle.None;
            fullMessage.BackColor   = Color.LightBlue;

            fullMessage.Controls.Add(image);
            fullMessage.Controls.Add(fileName);
            fullMessage.Controls.Add(name);
            fullMessage.Controls.Add(date);

            if (!isMine)
            {
                fullMessage.Left = MesView.MessageLeft;
            }
            else
            {
                fullMessage.Left = PanelWidth - MesView.MessageLeft - fullMessage.Width;
            }

            System.Drawing.Drawing2D.GraphicsPath Mess_Path = new System.Drawing.Drawing2D.GraphicsPath();
            int r = 30;

            Mess_Path.AddPie(0, 0, 2 * r, 2 * r, 180, 90);
            Mess_Path.AddPie(0, fullMessage.Height - 2 * r, 2 * r, 2 * r, 90, 90);
            Mess_Path.AddPie(fullMessage.Width - 2 * r, 0, 2 * r, 2 * r, 270, 90);
            Mess_Path.AddPie(fullMessage.Width - 2 * r, fullMessage.Height - 2 * r, 2 * r, 2 * r, 0, 90);

            Mess_Path.AddRectangle(new Rectangle(r, 0, fullMessage.Width - 2 * r, fullMessage.Height));
            Mess_Path.AddRectangle(new Rectangle(0, r, r, fullMessage.Height - 2 * r));
            Mess_Path.AddRectangle(new Rectangle(fullMessage.Width - r, r, r, fullMessage.Height - 2 * r));

            fullMessage.Region = new Region(Mess_Path);
            fullMessage.Top    = MesView.MessageTop;

            return(fullMessage);
        }
Beispiel #7
0
 public static void open(AddFile file)
 {
     if (Storage.isHave(file))
     {
         string curentDir = Environment.CurrentDirectory;
         System.Diagnostics.Process.Start("\"" + curentDir + "\\" + DefaultFile + "\\" + file.getNewName() + "\"");
     }
 }