Пример #1
0
        public void Receive(ref MinecraftClient client, GameStream stream)
        {
            VarInt secretLength = stream.ReadVarInt();

            byte[] sharedSecret = stream.ReadByteArray((int)secretLength.Value);
            VarInt tokenLength  = stream.ReadVarInt();

            byte[] verifyToken = stream.ReadByteArray((int)tokenLength.Value);

            var decryptedToken = Protocol.CryptoProvider.Decrypt(verifyToken, false);

            for (int i = 0; i < decryptedToken.Length; i++)
            {
                if (decryptedToken[i] != client.VerifyToken[i])
                {
                    client.Disconnect("Unable to authenticate");
                    return;
                }
            }

            client.SharedToken = Protocol.CryptoProvider.Decrypt(sharedSecret, false);
            AsnKeyBuilder.AsnMessage encodedKey = AsnKeyBuilder.PublicKeyToX509(Protocol.Key);
            byte[] shaData = Encoding.UTF8.GetBytes(Protocol.ServerID)
                             .Concat(client.SharedToken)
                             .Concat(encodedKey.GetBytes()).ToArray();
            string hash = Cryptography.JavaHexDigest(shaData);

            WebClient    webClient = new();
            StreamReader webReader = new(webClient.OpenRead(
                                             new Uri(string.Format(SessionChecker, client.Player.Username, hash))
                                             ));

            string response = webReader.ReadToEnd();

            webReader.Close();
            JToken json = JToken.Parse(response);

            if (string.IsNullOrEmpty(response))
            {
                client.Disconnect("Failed to verify username");
                return;
            }

            client.Stream = new(new AesStream(client.Stream.BaseStream, client.SharedToken));
            string?uuid = json["id"] !.Value <string>();

            if (uuid != client.Player.UniqueId.toString().Remove('-'))
            {
                client.Disconnect("Failed to verify UniqueID");
                return;
            }

            client.EncryptionPassed = true;
            new LoginSuccess(true).Send(ref client, client.Stream);
        }
Пример #2
0
        public static void Main(string[] args)
        {
            var endPoint = ParseEndPoint(args[0]);
            var ping     = ServerPing.DoPing(endPoint);

            Console.WriteLine("{0}/{1} {2} ({3}): {4} [{5}ms latency]",
                              ping.Players.OnlinePlayers,
                              ping.Players.MaxPlayers,
                              ping.Version.Name,
                              ping.Version.ProtocolVersion,
                              ping.Description,
                              (int)ping.Latency.TotalMilliseconds);
            Console.WriteLine("Player list sample:");
            if (ping.Players.Players != null)
            {
                foreach (var player in ping.Players.Players)
                {
                    Console.WriteLine("{0} ({1})", player.Name, player.Id);
                }
            }
            if (!string.IsNullOrEmpty(ping.Icon))
            {
                Console.WriteLine("Server icon: {0}", ping.Icon);
            }

            //var lastLogin = LastLogin.GetLastLogin();
            //var session = Session.DoLogin(lastLogin.Username, lastLogin.Password);
            var session = new Session("TestBot");

            // Connect to server
            var client = new MinecraftClient(session);

            client.Connect(endPoint);

            client.ChatMessage += (sender, e) => Console.WriteLine(e.Message);
            string command;

            do
            {
                command = Console.ReadLine();
                if (command == null)
                {
                    continue; // MonoDevelop debugger does this sometimes
                }
                if (command.StartsWith("say "))
                {
                    client.SendChat(command.Substring(4));
                }
                if (command == "detect")
                {
                    var coordinates = new Coordinates3D((int)client.Position.X, (int)client.Position.Y - 1, (int)client.Position.Z);
                    Console.WriteLine("Looking up " + coordinates);
                    var id = client.World.GetBlockId(coordinates);
                    Console.WriteLine("Block ID: " + id);
                }
            } while (command != "quit");

            client.Disconnect("Quitting");
        }
Пример #3
0
        static void Main(string[] args)
        {
            var MinecraftServer = new MinecraftClient(args[0], int.Parse(args[1]), "USERNAME HERE", "PASSWORD HERE", true);

            MinecraftServer.ServerState = 2;

            MinecraftServer.Message += (sender, message, name) => {
                Console.WriteLine("<" + name + "> " + message);
            };

            //MinecraftServer.DebugMessage += (sender, message) => {
            //    Console.WriteLine("[DEBUG][" + sender.ToString() + "] " + message);
            //};

            MinecraftServer.LoginFailure += (sender, message) => {
                Console.WriteLine("Login Error: " + message);
            };

            MinecraftServer.ErrorMessage += (sender, message) => {
                Console.WriteLine("[ERROR][" + sender.ToString() + "] " + message);
            };

            MinecraftServer.InfoMessage += (sender, message) => {
                Console.WriteLine("[INFO][" + sender.ToString() + "] " + message);
            };

            MinecraftServer.PlayerRespawned += () => {
                Console.WriteLine("[Info] You respawned!");
            };

            if (MinecraftServer.VerifyNames)
            {
                MinecraftServer.Login();
            }

            MinecraftServer.Connect();

            string command;

            do
            {
                command = Console.ReadLine();

                if (command.StartsWith("say "))
                {
                    MinecraftServer.SendChat(command.Substring(4));
                }

                if (command.StartsWith("respawn"))
                {
                    MinecraftServer.Respawn();
                }
            } while (command != "quit");

            MinecraftServer.Disconnect();

            Console.ReadKey();
        }
Пример #4
0
        public static ServerStatus IsOnline(IPEndPoint endPoint)
        {
            ServerStatus onlineMode = ServerStatus.Undefined;

            try {
                var reset  = new AutoResetEvent(false);
                var client = new MinecraftClient(new Session("slidxmc"));
                client.LoggedIn += (s, e) =>
                {
// If we managed to login with an invalid session, something's wrong.
                    Task.Factory.StartNew(() =>
                    {
// We cannot disconnect from the network thread, which is what this event handler fires on, so we
// do so in a new task instead.
                        client.Disconnect("Offline-mode server!");
                        onlineMode = ServerStatus.OfflineMode;
                        reset.Set();
                    }
                                          );
                };
                client.Disconnected += (s, e) =>
                {
                    Console.WriteLine(System.DateTime.Now.ToString() + ": Disconnected from server: " + e.Reason);
                    if (e.Reason.Contains("Failed to verify username!"))
                    {
                        onlineMode = ServerStatus.OnlineMode;
                    }
                    // This message is from BungeeCord.
                    if (e.Reason.Contains("Not authenticated with Minecraft.net"))
                    {
                        onlineMode = ServerStatus.OnlineMode;
                    }
                    // Old * reasons.
                    if (e.Reason.Contains("Outdated server!"))
                    {
                        onlineMode = ServerStatus.OnlineMode;
                    }
                    if (e.Reason.Contains("Outdated client!"))
                    {
                        onlineMode = ServerStatus.OnlineMode;
                    }
                    reset.Set();
                };
                client.Connect(endPoint);
                reset.WaitOne();
                return(onlineMode);
            }
            catch (SocketException e)
            {
                Console.WriteLine("We're busted, Dave! " + e.Message);
                return(ServerStatus.Offline);
            }
        }
Пример #5
0
        static void Main(string[] args)
        {
            // Set up log providers
            LogProvider.RegisterProvider(new FileLogWriter("packetLog.txt", LogImportance.Low));
            LogProvider.RegisterProvider(new ConsoleLogWriter(LogImportance.High));

            // Set up endpoint, ping server
            var endPoint = ParseEndPoint(args[0]);
            var ping     = ServerPing.DoPing(endPoint);

            Console.WriteLine("{0}/{1} {2} ({3}): {4}", ping.CurrentPlayers, ping.MaxPlayers, ping.ServerVersion,
                              ping.ProtocolVersion, ping.MotD);

            //var lastLogin = LastLogin.GetLastLogin();
            //var session = Session.DoLogin(lastLogin.Username, lastLogin.Password);
            var session = new Session("TestBot");

            // Connect to server
            var client = new MinecraftClient(session);

            client.Connect(endPoint);

            client.PlayerDied   += (s, e) => Console.WriteLine("Player died! Type 'respawn' to respawn.");
            client.Disconnected += (s, e) => Console.WriteLine("Disconnected: " + e.Reason);

            string input = "";

            while (input != "quit")
            {
                input = Console.ReadLine();
                if (input == null)
                {
                    continue;
                }

                if (input.StartsWith("move "))
                {
                    var parts   = input.Split(' ');
                    var amountX = int.Parse(parts[1]);
                    var amountZ = int.Parse(parts[2]);

                    client.Move(amountX, amountZ);
                }
                else if (input.StartsWith("look "))
                {
                    var parts  = input.Split(' ');
                    var amount = float.Parse(parts[2]);
                    if (parts[1] == "yaw")
                    {
                        client.Yaw = amount;
                    }
                    else
                    {
                        client.Pitch = amount;
                    }
                }
                else if (input.StartsWith("lookat "))
                {
                    var parts = input.Split(' ');
                    var pos   = new Vector3
                    {
                        X = double.Parse(parts[1]),
                        Y = double.Parse(parts[2]),
                        Z = double.Parse(parts[3])
                    };

                    client.LookAt(pos);
                }
                else if (input.StartsWith("say "))
                {
                    client.SendChat(input.Substring(4));
                }
                else if (input == "respawn")
                {
                    client.Respawn();
                }
                else if (input == "save")
                {
                    client.World.Save("testWorld", true);
                }
                else if (input == "under")
                {
                    Console.WriteLine(client.World.GetBlock(client.Position + Vector3.Down).GetType().Name);
                }
            }

            client.Disconnect("Quitting");
        }