Ejemplo n.º 1
0
        void BtnLoginClick(object sender, EventArgs e)
        {
            string pass = txtPassword.Text;
            string path = DiskHelper.GetRegistryValue("Path");

            if (IdHandler.IsValidUser(pass))
            {
                IdHandler.GetUuid(pass);
                LoggedIn();
                loggedIn = true;
                _idx     = new Index(path);
                _p2P     = new Network(25565, _idx, path);
                _p2P.Start();
                _idx.Load();
                IndexEventHandlers();

                if (!_idx.Load())
                {
                    _idx.BuildIndex();
                }
                _idx.Start();
                _idx.MakeIntegrityCheck();
            }
            else
            {
                Controls.Add(lblNope);
            }
        }
Ejemplo n.º 2
0
        public MyForm()
        {
            EventHandlers();
            GuiLayout();

            if (!string.IsNullOrEmpty(IdHandler.GetUuid()))
            {
                LoggedIn();
            }
            else
            {
                if (NetworkPorts.IsPortAvailable(25565) == false)
                {
                    Sorry();
                }
                else if (String.IsNullOrEmpty(DiskHelper.GetRegistryValue("Path")) == true ||
                         Directory.Exists(DiskHelper.GetRegistryValue("Path")) == false)
                {
                    FirstStartUp();
                }
                else if (File.Exists(DiskHelper.GetRegistryValue("Path") + @".hidden\userdata") == false)
                {
                    Create();
                }
                else
                {
                    Login();
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Function to receive peers from one or your peers peer list
        /// </summary>
        /// <param name="message">It is the message which is sent to the receiver</param>
        private void ReceivedPeerFetch(PeerFetcherMessage message)
        {
            if (message.type.Equals(TypeCode.REQUEST))
            {
                var outgoing = new List <Peer>();
                var incoming = message.peers;
                // Adding sender to list
                if (!InPeerList(message.fromUuid, _peers))
                {
                    _peers.TryAdd(message.fromUuid, new Peer(message.fromUuid, message.fromIp));
                }

                //Checks whether a incomming peer exists in the peerlist.
                foreach (var incomingPeer in incoming)
                {
                    if (InPeerList(incomingPeer.GetUuid(), _peers))
                    {
                        break;
                    }
                    _peers.TryAdd(incomingPeer.GetUuid(), incomingPeer);
                    DiskHelper.ConsoleWrite("Peer added: " + incomingPeer.GetUuid());
                }

                foreach (var outGoingPeer in _peers)
                {
                    if (InPeerList(outGoingPeer.Value.GetUuid(), incoming))
                    {
                        break;
                    }
                    if (outGoingPeer.Value.GetUuid() == message.fromUuid)
                    {
                        break;
                    }
                    outgoing.Add(outGoingPeer.Value);
                }

                message.CreateReply();
                message.peers = outgoing;
                message.Send();
            }
            else
            {
                // Rechieved response
                foreach (Peer incomingPeer in message.peers)
                {
                    if (InPeerList(incomingPeer.GetUuid(), _peers))
                    {
                        break;
                    }
                    if ((IdHandler.GetUuid().Equals(incomingPeer.GetUuid())))
                    {
                        break;
                    }
                    _peers.TryAdd(incomingPeer.GetUuid(), incomingPeer);
                    Console.WriteLine("Peer added: " + incomingPeer.GetUuid());
                }
            }
        }
Ejemplo n.º 4
0
 public BaseMessage(Peer to)
 {
     this._toUuid    = to.GetUuid();
     this.toIp       = to.GetIp();
     this.fromUuid   = IdHandler.GetUuid();
     this.fromIp     = NetworkHelper.GetLocalIpAddress();
     this.type       = TypeCode.REQUEST;
     this.statusCode = StatusCode.OK;
 }
Ejemplo n.º 5
0
        public void GetUUidIsCorrect()
        {
            string expected = IdHandler.CreateUser(Password);
            string result   = IdHandler.GetUuid(Password);

            IdHandler.RemoveUser();

            Assert.AreEqual(expected, result);
        }
Ejemplo n.º 6
0
        static void Main()
        {
            bool   running  = true;
            bool   firstRun = true;
            MyForm torPdos  = new MyForm();

            //If the ''Path'' variable is not set, the GUI is run to set this up.
            if (string.IsNullOrEmpty(DiskHelper.GetRegistryValue("Path")))
            {
                Application.Run(torPdos);
            }
            //If the ''Path'' variable is set, but the userdata file does not exist,
            //the GUI is run to create this.
            else if (File.Exists(DiskHelper.GetRegistryValue("Path") + @".hidden\userdata") == false)
            {
                Application.Run(torPdos);
            }

            //Gets the local IP and the path to the users TorPDos-folder.
            string ownIp = NetworkHelper.GetLocalIpAddress();
            string path  = (DiskHelper.GetRegistryValue("Path"));

            //Starts the communication with the user, and ensures that
            //the user logs in.
            DiskHelper.ConsoleWrite("Welcome to TorPdos!");
            Console.WriteLine(@"Please login by typing: login [PASSWORD] or gui");
            while (running)
            {
                string console = Console.ReadLine();
                if (console != null)
                {
                    string[] param = console.Split(' ');
                    //Close program
                    if (console.Equals("quit") || console.Equals("q"))
                    {
                        Console.WriteLine(@"Quitting...");
                        _idx.Save();
                        _idx.Stop();
                        _p2P.SavePeer();
                        _p2P.Stop();
                        running = false;
                        Console.WriteLine(" \n Press any button to quit!");
                    }
                    else
                    {
                        //Handles the login of the user through the console.
                        while (IdHandler.GetUuid() == null)
                        {
                            if (console.StartsWith("login") && param.Length == 2)
                            {
                                if (IdHandler.GetUuid(param[1]) == "Invalid Password")
                                {
                                    Console.WriteLine();
                                    Console.WriteLine(@"Invalid password, try again");
                                    Console.WriteLine(@"Please login by typing: login [PASSWORD] or gui");
                                    console = Console.ReadLine();
                                    if (console != null)
                                    {
                                        param = console.Split(' ');
                                    }
                                }

                                //Gives the opportunity to open the GUI for login.
                            }
                            else if (console.Equals("gui"))
                            {
                                Application.Run(torPdos);
                            }
                            else
                            {
                                Console.WriteLine();
                                Console.WriteLine(@"Error! Try again");
                                Console.WriteLine(@"Please login by typing: login [PASSWORD] or gui");
                                console = Console.ReadLine();
                                param   = console.Split(' ');
                            }
                        }

                        //Handles the creation or loading of all the necessary
                        //files and directories.
                        if (firstRun)
                        {
                            // Load Index
                            if (!Directory.Exists(path))
                            {
                                Directory.CreateDirectory(path);
                            }

                            _idx         = new Index(path);
                            torPdos._idx = _idx;

                            // Prepare P2PNetwork
                            try{
                                _p2P = new Network(25565, _idx, path);
                                _p2P.Start();
                                torPdos._p2P = _p2P;
                            }
                            catch (SocketException) {
                                Application.Run(torPdos);
                            }

                            _idx.Load();
                            _idx.FileAdded   += Idx_FileAdded;
                            _idx.FileChanged += Idx_FileChanged;
                            _idx.FileDeleted += Idx_FileDeleted;
                            _idx.FileMissing += Idx_FileMissing;

                            if (!_idx.Load())
                            {
                                _idx.BuildIndex();
                            }

                            _idx.Start();

                            Console.WriteLine(@"Integrity check initialized...");
                            _idx.MakeIntegrityCheck();
                            Console.WriteLine(@"Integrity check finished!");

                            Console.WriteLine(@"Local: " + ownIp);
                            Console.WriteLine(@"Free space on C: " + DiskHelper.GetTotalAvailableSpace("C:\\"));
                            Console.WriteLine(@"UUID: " + IdHandler.GetUuid());
                            firstRun = false;

                            //Restart loop to take input
                            continue;
                        }

                        // Handle input
                        if (console.StartsWith("add") && param.Length == 3)
                        {
                            _p2P.AddPeer(param[1].Trim(), param[2].Trim());
                        }
                        else if (console.Equals("reindex"))
                        {
                            _idx.ReIndex();
                        }
                        else if (console.Equals("gui"))
                        {
                            MyForm torPdos2 = new MyForm();
                            Application.Run(torPdos2);
                        }
                        else if (console.Equals("status"))
                        {
                            _idx.Status();
                        }
                        else if (console.Equals("idxsave"))
                        {
                            _idx.Save();
                        }
                        else if (console.Equals("peersave"))
                        {
                            _p2P.SavePeer();
                        }
                        else if (console.Equals("ping"))
                        {
                            _p2P.Ping();
                        }
                        else if (console.Equals("integrity"))
                        {
                            _idx.MakeIntegrityCheck();
                        }
                        else if (console.Equals("list"))
                        {
                            List <Peer> peers = _p2P.GetPeerList();

                            Console.WriteLine();
                            Console.WriteLine(@"### Your Peerlist contains ###");
                            if (peers.Count > 0)
                            {
                                foreach (Peer peer in peers)
                                {
                                    RankingHandler rankingHandler = new RankingHandler();
                                    rankingHandler.GetRank(peer);
                                    Console.WriteLine(@"(R:" + peer.Rating + @") " + peer.GetUuid() + @" - " +
                                                      peer.GetIp() + @" - " +
                                                      (peer.IsOnline() ? "Online" : "Offline"));
                                    Console.WriteLine(@"disk: " + Convert.ToInt32((peer.diskSpace / 1e+9)) +
                                                      @"GB | avgPing: " + peer.GetAverageLatency() + "\n");
                                }
                            }
                            else
                            {
                                Console.WriteLine(@"The list is empty...");
                            }

                            Console.WriteLine();
                        }
                        else if (console.Trim().Equals(""))
                        {
                        }
                        else
                        {
                            Console.WriteLine(@"Unknown command");
                        }
                    }
                }
            }

            Console.ReadKey();
        }