Exemplo n.º 1
0
        public static void Disconnect()
        {
            SendMessage("disconnect", "");

            receiving = false;

            NetworkManager.serverInfo.Clear();

            VisualManager.Cleanup();

            conn.Close();
        }
Exemplo n.º 2
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);
            }
        }
Exemplo n.º 3
0
        public static void RecvLoop()
        {
            ASCIIEncoding ascii = new ASCIIEncoding();

            byte[] bytes = new byte[20480];
            try
            {
                Queue netQueue = new Queue();

                while (receiving == true)
                {
                    int    bytesRec = conn.Receive(bytes);
                    string jsonData = Encoding.UTF8.GetString(bytes, 0, bytesRec);
                    netQueue.Clear();

                    if (jsonData.Length > 0)
                    {
                        if (jsonData[0].Equals('[') && jsonData[(jsonData.Length - 1)].Equals(']'))
                        {
                            netQueue.Enqueue(jsonData);
                        }
                        else
                        {
                            string incompleteMessage = jsonData;
                            bool   messageComplete   = false;
                            while (messageComplete == false)
                            {
                                bytesRec = conn.Receive(bytes);
                                jsonData = Encoding.UTF8.GetString(bytes, 0, bytesRec);
                                if (jsonData[(jsonData.Length - 1)].Equals(']'))
                                {
                                    messageComplete    = true;
                                    incompleteMessage += jsonData;

                                    netQueue.Enqueue(incompleteMessage);
                                }
                                else
                                {
                                    incompleteMessage += jsonData;
                                }
                            }
                        }
                    }



                    foreach (string netMessage in netQueue)
                    {
                        List <string> encryptedData = JsonConvert.DeserializeObject <List <string> >(netMessage);

                        jsonData = EncryptionManager.Decrypt(encryptedData[0], KeyGenerator.SecretKey, encryptedData[1]);

                        Dictionary <string, string> message = JsonConvert.DeserializeObject <Dictionary <string, string> >(jsonData);


                        switch (message["messagetype"])
                        {
                        case "serverData":
                        {
                            Net.serverInfo.Handle(message);
                        }
                        break;

                        case "outboundMessage":
                        {
                            Net.outboundMessage.Handle(message);
                        }
                        break;

                        case "channelUpdate":
                        {
                            Net.channelUpdate.Handle(message);
                        }
                        break;

                        case "userlistUpdate":
                        {
                            // Placeholder
                        }
                        break;

                        case "channelHistory":
                        {
                            Net.channelHistory.Handle(message);
                        }
                        break;

                        case "additionalHistory":
                        {
                            Net.additionalHistory.Handle(message);
                        }
                        break;

                        case "commandData":
                        {
                            Net.commandData.Handle(message);
                        }
                        break;

                        case "connectionTerminated":
                        {
                            Net.connectionTerminated.Handle(message);
                        }
                        break;

                        case "errorOccured":
                        {
                            MessageBox.Show("Error - " + message["data"]);
                        }
                        break;

                        default:
                        {
                            MessageBox.Show("Error - Unknown messagetype received");
                        }
                        break;
                        }
                    }
                }
            }
            catch (System.Net.Sockets.SocketException)
            {
                if (receiving == true)
                {
                    //MessageBox.Show("Error - Connection Lost"); // annoying while developing
                    NetworkManager.serverInfo.Clear();
                    Disconnect();
                    VisualManager.SystemMessage("Error - Connection Lost");
                }
            }
        }
Exemplo n.º 4
0
        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);
            }
        }