Beispiel #1
0
        private void Send(string msg, Socket s, Secure secure)
        {
            if (msg.Length >= 8192)
            {
                return;
            }
            string enc = secure.AES256Encrypt(msg) + "";

//Log.log("Send \""+ Bytes.getRawString(Bytes.AETB(enc)) + "\"");
            s.Send(Bytes.AETB(enc));
        }
Beispiel #2
0
        public int WannaSendTo(string ip, string path)
        {
            try
            {
                determined.Invoke(true);
                Log.clientLog("Connecting to " + ip);
                Socket     socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint iPEnd  = new IPEndPoint(IPAddress.Parse(ip), CONTROL_PORT);
                socket.Connect(iPEnd);
                socket.SendBufferSize    = 262144;
                socket.ReceiveBufferSize = 262144;
                Send("handshake" + VERSION, socket);
                string     reply          = Receive(socket);
                var        j1             = JObject.Parse(reply);
                IPEndPoint remoteEndPoint = socket.RemoteEndPoint as IPEndPoint;
                if (j1.Value <string>("reply").Equals("deny"))
                {
                    Log.clientLog("Denied: " + j1.Value <string>("reason"), Log.WARN);
                    reason = j1.Value <string>("reason").ToString();
                    determined.Invoke(false);
                    history.RegisterHistory(NetworkHistory.COMMU_TYPE.OUTGOING_SHARE, remoteEndPoint.Address.ToString(),
                                            "{\"file\":\"" + path + "\",\"status\":\"" + reason + "\"}");
                    return(DENIED);
                }
                else if (!j1.Value <string>("reply").Equals("approve"))
                {
                    Log.clientLog("Invalid message: " + reply, Log.ERR);
                    reason = "invalid: " + reply;
                    determined.Invoke(false);
                    history.RegisterHistory(NetworkHistory.COMMU_TYPE.OUTGOING_SHARE, remoteEndPoint.Address.ToString(),
                                            "{\"file\":\"" + path + "\",\"status\":\"invalid_msg\"}");
                    return(ERROR);
                }
                Log.clientLog("RSA public key was received");
                string           pubKey = Receive(socket);
                Secure.RSASystem rsa    = new Secure.RSASystem(pubKey);
                Secure           secure = new Secure
                {
                    Key = "32"
                };
                Send(rsa.Encrypt(secure.Key), socket);
                Log.clientLog("Replied AES key");
                //get IV
                Send(rsa.Encrypt(Setting.Actual_IV), socket);
                secure.SetIV(Setting.Actual_IV);
                Log.clientLog("IV was sent");
                //Get Allow
                JObject pack = new JObject();
                if (!File.Exists(path))
                {
                    Log.clientLog("File not exists(or it is directory): " + path, Log.ERR);
                    pack.Add("reply", "FileErr");
                    Send(pack.ToString(), socket, secure);
                    reason = "FileNotFound";
                    determined.Invoke(false);
                    history.RegisterHistory(NetworkHistory.COMMU_TYPE.OUTGOING_SHARE, remoteEndPoint.Address.ToString(),
                                            "{\"file\":\"" + path + "\",\"status\":\"file_error\"}");
                    return(ERROR);
                }
                FileInfo file = new FileInfo(path);
                pack.Add("reply", "OK");
                pack.Add("name", file.Name);
                pack.Add("size", file.Length);
                Log.clientLog("File data: " + file.Name + " " + file.Length + " Bytes");
                Send(pack.ToString(), socket, secure);
                string received = Receive(socket, secure);
                if (received.Equals("deny"))
                {
                    Log.clientLog("User denied", Log.WARN);
                    reason = "Peer denied";
                    determined.Invoke(false);
                    history.RegisterHistory(NetworkHistory.COMMU_TYPE.OUTGOING_SHARE, remoteEndPoint.Address.ToString(),
                                            "{\"file\":\"" + path + "\",\"status\":\"user_deny\"}");
                    return(DENIED);
                }
                else if (!received.Equals("approve"))
                {
                    Log.clientLog("User invalid reply", Log.ERR);
                    reason = "invalid: " + received;
                    determined.Invoke(false);
                    history.RegisterHistory(NetworkHistory.COMMU_TYPE.OUTGOING_SHARE, remoteEndPoint.Address.ToString(),
                                            "{\"file\":\"" + path + "\",\"status\":\"invalid_msg\"}");
                    return(ERROR);
                }
                Log.clientLog("Received Allow Sign!");
                //Get hash and send: MD5;
                Log.clientLog("Computing hash");
                byte[] hashValue;
                string stringHashValue;
                MD5    hash = MD5.Create();
                using (FileStream stream = File.OpenRead(file.FullName))
                {
                    hashValue       = hash.ComputeHash(stream);
                    stringHashValue = Bytes.getRawString(hashValue);
                }
                Log.clientLog("Sending hash: " + stringHashValue);
                Send(stringHashValue, socket, secure);
                //Send start;
                Log.clientLog("Local file stream connected");
                FileStream fileStream = file.OpenRead();
                //const int sendingBufferSize = 320;  //320B
                const int sendingBufferSize = 131072; //128KB
                                                      //const int sendingBufferSize = 1048576; //1MB
                                                      //const int sendingBufferSize = 524288; //0.5MB
                Log.clientLog("Sending pieces");
                initializer.Invoke((int)((file.Length + (sendingBufferSize - (file.Length % sendingBufferSize))) / sendingBufferSize));
                Log.log((file.Length + (sendingBufferSize - (file.Length % sendingBufferSize))) / sendingBufferSize + " Pieces");
                int packets = 0;
                while (true)
                {
                    byte[] sendingBuffer          = new byte[sendingBufferSize];
                    byte[] encryptedSendingBuffer = new byte[sendingBufferSize + 16];
                    int    read = fileStream.Read(sendingBuffer, 0, sendingBufferSize);

//Log.clientLog("Read " + read + " bytes");
//Log.clientLog("Encryption started");

                    byte[] fitByteBuffer = new byte[read];
                    Array.Copy(sendingBuffer, fitByteBuffer, read);
                    encryptedSendingBuffer = secure.AES256Encrypt(fitByteBuffer);
                    string thisData = Convert.ToBase64String(encryptedSendingBuffer);
//Log.clientLog("Encryption done");
                    JObject packet     = new JObject();
                    string  packetHash = Bytes.getRawString(hash.ComputeHash(fitByteBuffer));
                    packet.Add("segment", thisData);
                    packet.Add("hash", packetHash);
                    packet.Add("isEnd", read < sendingBufferSize);
                    thisData = null;
                    encryptedSendingBuffer = null;
                    string replied;
                    int    attemp = 0;
                    do
                    {
                        attemp++;
//Log.clientLog("Sending data piece: " + (String)packet.GetValue("hash"));
                        Send(packet.ToString(), socket);
//Log.clientLog("Waiting for response");
                        replied = Receive(socket, secure);
//Log.clientLog("File segment reply: \"" + replied + "\", Attemp: "+attemp);
                    } while (!replied.Equals("ok")); // Retry if hash is not correct
                    adder.Invoke();
                    packets++;
                    if (read < sendingBufferSize)
                    {
                        break;
                    }
                }
                Log.clientLog("Waiting for hash test result");
                string hashReply = Receive(socket, secure);
                if (hashReply.Equals("hash"))
                {
                    Log.clientLog("Hash Error: originHash=\"" + stringHashValue + '\"');
                    history.RegisterHistory(NetworkHistory.COMMU_TYPE.OUTGOING_SHARE, remoteEndPoint.Address.ToString(),
                                            "{\"file\":\"" + path + "\",\"status\":\"hash_mismatch\"}");
                }
                else if (!hashReply.Equals("goodbye"))
                {
                    Log.clientLog("Invalid hash reply: " + hashReply);
                    history.RegisterHistory(NetworkHistory.COMMU_TYPE.OUTGOING_SHARE, remoteEndPoint.Address.ToString(),
                                            "{\"file\":\"" + path + "\",\"status\":\"invalid_msg\"}");
                    return(ERROR);
                }
                socket.Close();
                fileStream.Close();
                Log.clientLog("Done: sent " + packets + " file pieces");
                history.RegisterHistory(NetworkHistory.COMMU_TYPE.OUTGOING_SHARE, remoteEndPoint.Address.ToString(),
                                        "{\"file\":\"" + path + "\",\"status\":\"sucess\"}");
                return(SUCESS);
            } catch (Exception e)
            {
                Log.clientLog(e.Message + " client error", Log.ERR);
                return(ERROR);
            }
        }