예제 #1
0
        public static void SecureConnect(TcpClient tcpclient, params string[] value)
        {
            var stream = tcpclient.GetStream();

            if (Program.ServerProperties["SSLENABLED"] == "false")
            {
                SocketUtilities.SendInvalid(stream, "SSL isn't enabled on this server");
                tcpclient.Close();
                return;
            }
            if (value.Length != 2)
            {
                SocketUtilities.SendInvalid(stream, "Incorrect number of parameters");
                tcpclient.Close();
                return;
            }
            string username = value[0];
            string client   = value[1];

            if (Program.Clients.ContainsKey(username))
            {
                SocketUtilities.SendCommand(stream, new CommandParameterPair("INVALIDUN {0}", username));
                tcpclient.Close();
                return;
            }
            string certFile;

            if (!Program.ServerDependencies.TryGetValue("SSLCERT", out certFile))
            {
                SocketUtilities.SendInvalid(stream, "SSL isn't properly supported on this server");
                tcpclient.Close();
                return;
            }
            string password;

            if (!Program.ServerDependencies.TryGetValue("SSLPASS", out password))
            {
                SocketUtilities.SendInvalid(stream, "SSL isn't properyly supported on this serverserver");
                tcpclient.Close();
                return;
            }
            var service = new UserClientService(new SecureClient(stream, certFile, username, client, new ConcurrentDictionary <string, string>(), new ConcurrentDictionary <string, string>(), DateTime.UtcNow, password), true);

            service.SendCommand(new CommandParameterPair("CONNECTED"));
            Program.Clients.TryAdd(username, service);
            var thread = new Thread(ClientManagement.ManageClient);

            thread.Start(service);
            Program.ClientThreads.TryAdd(username, thread);
        }
예제 #2
0
        public static void RequestInfo(TcpClient tcpClient, params string[] value)
        {
            var stream = tcpClient.GetStream();

            if (value.Length != 0)
            {
                SocketUtilities.SendInvalid(stream, "INFOREQ should not send parameters");
                tcpClient.Close();
                return;
            }
            SocketUtilities.SendCommand(stream, new CommandParameterPair("INFORESP",
                                                                         JsonConvert.SerializeObject(Program.ServerProperties.ToDictionary(kvp => kvp.Key, kvp => kvp.Value))));
            Thread          thread  = new Thread(AcceptClientManagement.ManageAnonymous);
            AnonymousThread athread = new AnonymousThread(tcpClient.Client.RemoteEndPoint.ToString(), tcpClient);

            thread.Start(athread);
            Program.AnonymousThreads.TryAdd(tcpClient.Client.RemoteEndPoint.ToString(), thread);
        }
예제 #3
0
        public static void Connect(TcpClient tcpClient, params string[] parameters)
        {
            if (!tcpClient.Connected)
            {
                tcpClient.Close();
                return;
            }
            NetworkStream stream = tcpClient.GetStream();

            if (parameters.Length != 2)
            {
                SocketUtilities.SendInvalid(stream, "Parameters not formatted correctly");
                stream.Close();
                tcpClient.Close();
                return;
            }
            string username = parameters[0];
            string client   = parameters[1];

            if (Program.Clients.ContainsKey(username) | username.Trim().ToLower() == "system" | username.Length > 15)
            {
                SocketUtilities.SendCommand(stream, new CommandParameterPair("INVALIDUN"));
                stream.Close();
                tcpClient.Close();
                return;
            }

            var service = new UserClientService(new UserClient(username, client, tcpClient, new ConcurrentDictionary <string, string>(), new ConcurrentDictionary <string, string>(), DateTime.UtcNow));

            SocketUtilities.SendCommand(stream, new CommandParameterPair("CONNECTED"));

            ConsoleUtilities.PrintWarning("Got here!");
            foreach (IMessagingClient sclient in Program.Clients.Values)
            {
                sclient.Alert(String.Format("{0} has connected", username), 3);
            }
            ConsoleUtilities.PrintInformation("Connected {0}", username);
            Program.Clients.TryAdd(username, service);
            Thread thread = new Thread(ClientManagement.ManageClient);

            thread.Start(service);
            Program.ClientThreads.TryAdd(username, thread);
        }
        public static void AcceptNewClients()
        {
            Console.WriteLine("Thread started");
            while (true)
            {
                try
                {
                    TcpClient tcpClient = Program.ServerSocket.AcceptTcpClient();
                    if (Program.ServerState == 0)
                    {
                        ConsoleUtilities.PrintCritical("Thread has been ended {0}", DateTime.UtcNow);
                        return;
                    }
                    if (Program.ServerState == 2)
                    {
                        Thread.Sleep(Timeout.Infinite);
                    }
                    NetworkStream clientStream = tcpClient.GetStream();
                    ConsoleUtilities.PrintCommand("Connection has been accepted from {0}", tcpClient.Client.RemoteEndPoint);

                    if (Program.AnonymousThreads.ContainsKey(tcpClient.Client.RemoteEndPoint.ToString()))
                    {
                        SocketUtilities.SendInvalid(clientStream, "Your network is already connected. Try again later.");
                        tcpClient.Close();
                        continue;
                    }

                    int messageLength = SocketUtilities.RecieveMessageLength(clientStream);
                    if (messageLength == -1)
                    {
                        ConsoleUtilities.PrintCommand("Connection has been closed for {0}", tcpClient.Client.RemoteEndPoint);
                        tcpClient.Close();
                        continue;
                    }
                    string stringmessage = SocketUtilities.RecieveMessage(clientStream, messageLength);
                    if (stringmessage == null)
                    {
                        tcpClient.Close();
                        continue;
                    }

                    CommandParameterPair message = MessageUtilites.DecodeMessage(stringmessage);
                    if (message == null)
                    {
                        SocketUtilities.SendInvalid(clientStream, "The message was formatted incorrectly.");
                        tcpClient.Close();
                        continue;
                    }

                    if (!Program.InitializeCommands.ContainsKey(message.Command))
                    {
                        SocketUtilities.SendInvalid(clientStream, "The command was not found");
                        tcpClient.Close();
                        continue;
                    }

                    InitializeCommand execute;
                    if (Program.InitializeCommands.TryGetValue(message.Command, out execute))
                    {
                        execute(tcpClient, message.Parameters);
                        continue;
                    }
                    SocketUtilities.SendError(clientStream, "Unable to find command (Concurrency Issues)");
                    if (Program.ServerState == 0)
                    {
                        Console.WriteLine("Thread has been ended");
                        return;
                    }
                    if (Program.ServerState == 2)
                    {
                        Thread.Sleep(Timeout.Infinite);
                    }
                }
                catch (ThreadAbortException e)
                {
                    Console.WriteLine("Thread has been stopped {0}", e.Data.Values);
                    return;
                }
                catch (InvalidOperationException e)
                {
                    ConsoleUtilities.PrintCritical("Accept thread has been terminated {0}", e.Message);
                    return;
                }
                catch (ThreadInterruptedException)
                {
                    Console.WriteLine("Resuming accepting threads");
                }
                catch (IOException e)
                {
                    ConsoleUtilities.PrintWarning("Impropert disconnect. {1}", e.Message);
                }
                catch (SocketException e)
                {
                    ConsoleUtilities.PrintWarning("Improper disconnect. Data: {0}", e.Data);
                }
            }
        }