static async Task MainAsync()
        { // create an instance of the socket. In this case i've used the .Net 4.5 object defined in the project
            INetworkSocket socket = new RconSocket();
            IList <ContainerListResponse> containers = await client.Containers.ListContainersAsync(
                new ContainersListParameters()
            {
                Limit = 10,
            });

            string containerMetric = "";
            string ipContainer     = string.Empty;

            foreach (ContainerListResponse f in containers)
            {
                if (f.State.ToLowerInvariant() != "running")
                {
                    continue;
                }
                foreach (KeyValuePair <string, EndpointSettings> item in f.NetworkSettings.Networks)
                {
                    ipContainer = item.Value.IPAddress;
                }
                containerMetric += string.Format(" & image: {0} state: {1};", f.Image, f.State);
            }

            // create the RconMessenger instance and inject the socket
            RconMessenger messenger = new RconMessenger(socket);

            // initiate the connection with the remote server
            bool isConnected = await messenger.ConnectAsync(ipContainer, 25575);

            await messenger.AuthenticateAsync("cheesesteakjimmys");

            // if we fall here, we're good to go! from this point on the connection is authenticated and you can send commands
            // to the server
            string response = await messenger.ExecuteCommandAsync("/list");

            string[] data        = response.Substring(10, 5).Trim().Split('/');
            int      _maxPlayers = int.Parse(data[1]);
            int      _players    = int.Parse(data[0]);

            Console.WriteLine(response.Substring(10, 5).Trim());

            telemetry.TrackMetric(new MetricTelemetry("Nr of players", _players));
            telemetry.TrackMetric(new MetricTelemetry("Nr of maximum players", _maxPlayers));
            telemetry.TrackEvent("Container is " + containerMetric);
        }
        /// <summary>
        /// Asynchronously sends an RCON command to the server.
        /// Returns the response the server sent back.
        /// </summary>
        /// <param name="command">Command to send.</param>
        /// <returns>Server response.</returns>
        public async Task <string> RunCommand(string command)
        {
            INetworkSocket socket    = new RconSocket();
            RconMessenger  messenger = new RconMessenger(socket);

            try
            {
                bool isConnected = await messenger.ConnectAsync(Address, Port);

                bool authenticated = await messenger.AuthenticateAsync(Password);

                if (authenticated)
                {
                    return(await messenger.ExecuteCommandAsync(command));
                }
                else
                {
                    return("Authentication failed.");
                }
            }
            catch (SocketException exc)
            {
                return("Connection failed:\n" + exc.Message);
            }
            catch (AggregateException exc)
            {
                if (exc.InnerException.GetType() == typeof(SocketException))
                {
                    return("Connection failed:\n" + exc.InnerException);
                }
                else
                {
                    return("Exception unaccounted for:\n" + exc.Message);
                }
            }
            catch (Exception exc)
            {
                return("Exception unaccounted for:\n" + exc.Message);
            }
            finally
            {
                messenger.CloseConnection();
                socket.CloseConnection();
            }
        }
Пример #3
0
        private void Disconnect(INetworkSocket socket, RconMessenger messenger)
        {
            try
            {
                if (messenger != null)
                {
                    messenger.CloseConnection();
                }

                if (socket != null)
                {
                    socket.CloseConnection();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Пример #4
0
        public async Task <RconResponse> Execute(string command)
        {
            // create an instance of the socket. In this case i've used the .Net 4.5 object defined in the project
            INetworkSocket socket = new RconSocket();

            // create the RconMessenger instance and inject the socket
            RconMessenger messenger = new RconMessenger(socket);

            try
            {
                // initiate the connection with the remote server
                bool isConnected = await messenger.ConnectAsync(Address, Port);

                if (!isConnected)
                {
                    Disconnect(socket, messenger);
                    return(new RconResponse()
                    {
                        Success = false,
                        Message = "",
                        Error = "Failed to connect to the RCON server"
                    });
                }

                //Authicate with the server
                if (_hasPassword)
                {
                    bool authenticated = await messenger.AuthenticateAsync(_password);

                    if (!authenticated)
                    {
                        Disconnect(socket, messenger);
                        return(new RconResponse()
                        {
                            Success = false,
                            Message = "",
                            Error = "Failed to authicated to RCON server"
                        });
                    }
                }

                // if we fall here, we're good to go! from this point on the connection is authenticated and you can send commands
                // to the server

                //Attempt to send the command.
                var response = await messenger.ExecuteCommandAsync(command);

                //It should be something. If its empty we have failed
                if (string.IsNullOrEmpty(response))
                {
                    return(new RconResponse()
                    {
                        Success = false,
                        Message = "",
                        Error = "RCON returned no response."
                    });
                }

                //Check if the response is valid
                bool success = !string.IsNullOrEmpty(response);
                return(new RconResponse()
                {
                    Success = success,
                    Message = response,
                    Error = ""
                });
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(new RconResponse()
                {
                    Success = false,
                    Message = "",
                    Error = "An exception occured while performing RCON: " + e.Message
                });
            }
            finally
            {
                Disconnect(socket, messenger);
            }
        }