Beispiel #1
0
        public static bool Connect(string ip, int port, bool anon)
        {
            if (anon)
            {
                userID = EncryptionManager.SHA256HAsh(KeyGenerator.GetUniqueKey(32));
                VisualManager.SystemMessage("Connecting in anonymous mode...");
                VisualManager.SystemMessage("eID is " + userID);
                //userID = "testid";
            }
            else
            {
                string macAddr =
                    (
                        from nic in NetworkInterface.GetAllNetworkInterfaces()
                        where nic.OperationalStatus == OperationalStatus.Up
                        select nic.GetPhysicalAddress().ToString()
                    ).FirstOrDefault();

                userID = EncryptionManager.SHA256HAsh(macAddr);

                VisualManager.SystemMessage("Connecting...");
            }

            IPAddress  ipAddress = IPAddress.Parse(ip);
            IPEndPoint remoteEP  = new IPEndPoint(ipAddress, port);

            NetworkManager.conn = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                conn.Connect(remoteEP);
                receiving = true;
                VisualManager.SystemMessage("Connected to server");
                return(true);
            }
            catch (System.Net.Sockets.SocketException)
            {
                VisualManager.SystemMessage("Failed to connect to server");
                return(false);
            }
        }
        public static bool Handshake(string IPAddr, int port, bool anon, string username, string password, ConnectionScreen connSc = null)
        {
            if (NetworkManager.receiving == true)
            {
                NetworkManager.Disconnect();
                return(true);
            }
            if (NetworkManager.Connect(IPAddr, port, (bool)anon))
            {
                try
                {
                    NetworkManager.SendMessage("serverInfoRequest", "", enc: false);

                    Dictionary <string, string> message = NetworkManager.ReceiveMessage(); // Receive serverInfo

                    KeyGenerator.SecretKey = KeyGenerator.GetUniqueKey(16);

                    NetworkManager.SendMessage("clientSecret", EncryptionManager.RSAEncrypt(KeyGenerator.SecretKey, message["data"].ToString()), enc: false);

                    message = NetworkManager.ReceiveMessage(true); // Receive gotSecret

                    string version = ConfigManager.GetSetting("version");

                    List <string> connRequest = new List <string> {
                        username, password, version
                    };

                    string jsonConnReq = JsonConvert.SerializeObject(connRequest);

                    NetworkManager.SendMessage("connectionRequest", jsonConnReq);

                    message = NetworkManager.ReceiveMessage(true);

                    if (message["messagetype"] == "CRAccepted")
                    {
                        NetworkManager.ReceiveMessages();
                        VisualManager.ClearUsers();
                        VisualManager.ClearChan();
                        VisualManager.SystemMessage("Handshake complete");
                        if (connSc != null)
                        {
                            connSc.Close();
                        }
                        handshakeInfo["ip"]       = IPAddr;
                        handshakeInfo["port"]     = port;
                        handshakeInfo["username"] = username;
                        handshakeInfo["password"] = password;
                        handshakeInfo["anon"]     = anon;

                        return(true);
                    }
                    else if (message["messagetype"] == "CRDenied")
                    {
                        VisualManager.SystemMessage("Connection denied - " + message["data"]);
                        if (connSc != null)
                        {
                            connSc.Close();
                        }
                        return(false);
                    }
                    return(false);
                }
                catch (System.Net.Sockets.SocketException)
                {
                    VisualManager.SystemMessage("Connection was lost during handshake");
                    if (connSc != null)
                    {
                        connSc.Close();
                    }
                    return(false);
                }
            }
            else
            {
                VisualManager.SystemMessage("Connection failed");
                return(false);
            }
        }