Exemple #1
0
        private string Receive(Socket s, Secure secure)
        {
            byte[] buf = new byte[s.ReceiveBufferSize];
            s.Receive(buf);
            string        sx  = Bytes.ADTS(buf);
            StringBuilder bid = new StringBuilder();

            foreach (char x in sx.ToCharArray())
            {
                if (x == 0x1a)
                {
                    break;
                }
                else
                {
                    bid.Append(x);
                }
            }
            string sxe  = bid.ToString();
            string bidx = secure.AES256Decrypt(sxe);

//Log.log("received \"" + bidx + "\"");
            return(bidx);
        }
Exemple #2
0
        public void MainServer()
        {
            try
            {
                server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint iPEnd = new IPEndPoint(IPAddress.Any, CONTROL_PORT);
                server.Bind(iPEnd);
                server.Listen(10);
                Log.serverLog("Server Listening");
                while (true)
                {
                    Socket socket = server.Accept();
                    void serve(Socket sock)
                    {
                        sock.SendBufferSize    = 262144;
                        sock.ReceiveBufferSize = 262144;
                        string currentIP = ((IPEndPoint)socket.RemoteEndPoint).Address.ToString();

                        Log.serverLog("New connection: " + currentIP);
                        //HAND SHAKE
                        string msg;

                        msg = Receive(sock);
                        IPEndPoint remoteEndPoint = sock.RemoteEndPoint as IPEndPoint;

                        if (msg.Substring(0, 9).Equals("handshake")) // Share request
                        {
                            if (!Setting.Sharing)
                            {
                                Send("{\"reply\":\"deny\",\"reason\":\"notSharing\"}", sock);
                                Log.serverLog("Denied request: You're currently not sharing", Log.WARN);
                                history.RegisterHistory(NetworkHistory.COMMU_TYPE.INCOMMING_SHARE, remoteEndPoint.Address.ToString(),
                                                        "{\"file\":\"\",\"status\":\"deny\"}");
                                MessageBox.Show("notSharing", "Pass", MessageBoxButton.OK);
                                return;
                            }
                            if (!msg.Substring(9, msg.Length - 9).Equals(VERSION))
                            {
                                Send("{\"reply\":\"deny\",\"reason\":\"VersionMismatch\"}", sock);
                                Log.serverLog("Denied request: FROM" + msg.Substring(9, msg.Length - 9) + " You're: " + VERSION, Log.WARN);
                                history.RegisterHistory(NetworkHistory.COMMU_TYPE.INCOMMING_SHARE, remoteEndPoint.Address.ToString(),
                                                        "{\"file\":\"\",\"status\":\"vers\"}");
                                MessageBox.Show("versMismatch", "Pass", MessageBoxButton.OK);
                                return;
                            }
                            else
                            {
                                Send("{\"reply\":\"approve\"}", sock);
                                Log.serverLog("Approved: " + ((IPEndPoint)sock.RemoteEndPoint).Address.ToString(), Log.WARN);
                            }
                            Log.serverLog("Preparing");
                            //Security
                            string           AES;
                            Secure.RSASystem rsa = new Secure.RSASystem();
                            Send(rsa.PubKey, sock);
                            Log.serverLog("Sent RSA public key");
                            //get AES key
                            AES = rsa.RSADecrypt(Receive(sock));
                            Secure secure = new Secure(AES);
                            Log.serverLog("AES key was replied");
                            //Set IV
                            string ivx = rsa.RSADecrypt(Receive(sock));
                            secure.SetIV(ivx);
                            Log.serverLog("IV value was received");
                            //File Go.
                            JObject json = JObject.Parse(Receive(sock, secure));
                            Log.serverLog("File Info Received");
                            if (!json.Value <string>("reply").Equals("OK"))
                            {
                                Log.serverLog("Not OK!", Log.ERR);
                                history.RegisterHistory(NetworkHistory.COMMU_TYPE.INCOMMING_SHARE, remoteEndPoint.Address.ToString(),
                                                        "{\"file\":\"\",\"status\":\"file_error\"}");
                                MessageBox.Show(Rm.GetString(json.Value <string>("reply")), "Pass", MessageBoxButton.OK);
                                return;
                            }
                            Log.serverLog("File info displayed");
                            long   bytes = long.Parse(json.Value <string>("size"));
                            string unit  = " Byte";
                            double bas;
                            //Byte to suitable unit
                            if (bytes > 1024)
                            {
                                if (bytes > 1024000)
                                {
                                    if (bytes > 1024000000)
                                    {
                                        unit = "GB";
                                        bas  = bytes / 1000000000.0;
                                    }
                                    else
                                    {
                                        unit = "MB";
                                        bas  = bytes / 1000000.0;
                                    }
                                }
                                else
                                {
                                    unit = "KB";
                                    bas  = bytes / 1000.0;
                                }
                            }
                            else
                            {
                                bas = bytes;
                            }
                            bas = Math.Round(bas * 100) / 100;
                            frameControl.Invoke(true);
                            string       file_name = json.Value <string>("name");
                            DialogResult result    = MessageBoxClass.Show(((IPEndPoint)sock.RemoteEndPoint).Address.ToString() +
                                                                          Rm.GetString("appv1") + file_name + Rm.GetString("appv2") +
                                                                          " (" + bas + unit + ')', "Pass", Rm.GetString("allow"), Rm.GetString("deny"));
                            if (result == DialogResult.Yes)
                            {
                                Send("approve", sock, secure);
                            }
                            else
                            {
                                Send("deny", sock, secure);
                                frameControl.Invoke(false);
                                history.RegisterHistory(NetworkHistory.COMMU_TYPE.INCOMMING_SHARE, remoteEndPoint.Address.ToString(),
                                                        "{\"file\":\"" + file_name + "\",\"status\":\"user_deny\"}");
                                return;
                            }
                            string hashValue = Receive(sock, secure);
                            Log.serverLog("Received hash");
                            string targetPath = Setting.DefaultSave + '\\' + json.Value <string>("name");
                            Log.serverLog("Write to " + targetPath);
                            //file download start;
                            Log.serverLog("Downloading started");
                            MD5        md5     = MD5.Create();
                            int        packets = 0;
                            FileStream stream  = File.OpenWrite(targetPath);
                            Log.serverLog("Writing stream connected");
                            while (true)
                            {
                                //Log.serverLog("Waiting for data piece");
                                string  dataSegment  = Receive(sock);
                                JObject data         = JObject.Parse(dataSegment);
                                string  segment      = (string)data.GetValue("segment");
                                string  hash         = (string)data.GetValue("hash");
                                bool    isEnd        = (bool)data.GetValue("isEnd");
                                byte[]  finalSegment = secure.AES256Decrypt(Convert.FromBase64String(segment));

                                //Log.serverLog("Got data piece: " + hash);

                                string hashed = Bytes.getRawString(md5.ComputeHash(finalSegment));
                                if (hash.Equals(hashed))
                                {
                                    Send("ok", sock, secure);
                                    //Log.serverLog("Wrote " + finalSegment.Length + " bytes");
                                    stream.Write(finalSegment, 0, finalSegment.Length);
                                    packets++;
                                    if (isEnd)
                                    {
                                        break;
                                    }
                                }
                                else
                                {
                                    Send("retry", sock, secure);
                                    Log.serverLog("HASH MISMATCH! requested resend(Hash=" + hashed + ')', Log.ERR);
                                }
                            }
                            stream.Close();
                            //Hash check
                            byte[] hashValueCheck;
                            string stringHashValue;
                            Log.serverLog("Hash check in progress");
                            using (FileStream hashStream = File.OpenRead(targetPath))
                            {
                                hashValueCheck  = md5.ComputeHash(hashStream);
                                stringHashValue = Bytes.getRawString(hashValueCheck);
                            }
                            if (!hashValue.Equals(stringHashValue))
                            {
                                Log.serverLog("FILE HASH MISMATCH! \"" + stringHashValue + "\" vs. n\"" + hashValue + '\"');
                                history.RegisterHistory(NetworkHistory.COMMU_TYPE.INCOMMING_SHARE, remoteEndPoint.Address.ToString(),
                                                        "{\"file\":\"" + file_name + "\",\"status\":\"hash_mismatch\"}");
                                Send("hash", sock, secure);
                            }
                            else
                            {
                                Log.serverLog("FILE HASH MATCH! " + stringHashValue);
                                history.RegisterHistory(NetworkHistory.COMMU_TYPE.INCOMMING_SHARE, remoteEndPoint.Address.ToString(),
                                                        "{\"file\":\"" + file_name + "\",\"status\":\"sucess\"}");
                                Send("goodbye", sock, secure);
                            }
                            Log.serverLog("Done: received " + packets + " file pieces");
                        }
                        else
                        {
                            Log.serverLog("Invalid Message: \"" + msg + "\"");
                            history.RegisterHistory(NetworkHistory.COMMU_TYPE.INCOMMING_SHARE, remoteEndPoint.Address.ToString(),
                                                    "{\"file\":\"\",\"status\":\"invalid_msg\"}");
                        }
                        sock.Close();
                    }
                    Task.Run(() => serve(socket));
                }
            }
            catch (Exception e)
            {
                Log.serverLog("Server Stopped: " + e.Message);
                return;
            }
        }