示例#1
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();
        }
示例#2
0
        static void Main(string[] args) {
			string serverIp = "127.0.0.1";
			int serverPort = 25565;
			string userName = "******";
			string userPassword = null;

			OptionSet p = new OptionSet ()
				.Add ("ip=", v => serverIp = v)
				.Add ("port=", v => int.Parse (v))
				.Add ("user="******"pass", v => userPassword = ReadPassword());

			p.Parse (args);

			var MinecraftServer = new MinecraftClient(serverIp, serverPort, userName, userPassword, !String.IsNullOrWhiteSpace(userPassword));
            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();
        }
        public void HandleEncryptionRequest(MinecraftClient client, IPacket packet) {
            var ER = (CBEncryptionRequest)packet;
            var SharedKey = new byte[16];

            var Random = RandomNumberGenerator.Create(); // -- Generate a random shared key.
            Random.GetBytes(SharedKey);

            if (ER.ServerID == "" && client.VerifyNames) {
                // -- Verify with Minecraft.net.
                // -- At this point, the server requires a hash containing the server id,
                // -- shared key, and original public key. So we make this, and then pass to Minecraft.net

                List<byte> HashList = new List<byte>();
                HashList.AddRange(Encoding.ASCII.GetBytes(ER.ServerID));
                HashList.AddRange(SharedKey);
                HashList.AddRange(ER.PublicKey);

                var HashData = HashList.ToArray();
                var Hash = JavaHexDigest(HashData);

                var Verify = new Minecraft_Net_Interaction();

                if (!Verify.VerifyName(client.ClientName, client.AccessToken, client.SelectedProfile, Hash)) {
                    client.RaiseLoginFailure(this, "Failed to verify name with Minecraft session server.");
                    client.Disconnect();
                    return;
                }
            } else
                client.RaiseInfo(this, "Name verification disabled, skipping authentication.");

            // -- AsnKeyParser is a part of the cryptography.dll, which is simply a compiled version
            // -- of SMProxy's Cryptography.cs, with the server side parts stripped out.
            // -- You pass it the key data and ask it to parse, and it will 
            // -- Extract the server's public key, then parse that into RSA for us.

            var KeyParser = new AsnKeyParser(ER.PublicKey);
            var Dekey = KeyParser.ParseRSAPublicKey();

            // -- Now we create an encrypter, and encrypt the token sent to us by the server
            // -- as well as our newly made shared key (Which can then only be decrypted with the server's private key)
            // -- and we send it to the server.

            var cryptoService = new RSACryptoServiceProvider(); // -- RSA Encryption class
            cryptoService.ImportParameters(Dekey); // -- Import the Server's public key to use as the RSA encryption key.

            byte[] EncryptedSecret = cryptoService.Encrypt(SharedKey, false); // -- Encrypt the Secret key and verification token.
            byte[] EncryptedVerify = cryptoService.Encrypt(ER.VerifyToken, false);

            client.nh.wSock.InitEncryption(SharedKey); // -- Give the shared secret key to the socket

            var Response = new SBEncryptionResponse(); // -- Respond to the server

            Response.SharedLength = (short)EncryptedSecret.Length;
            Response.SharedSecret = EncryptedSecret;
            Response.VerifyLength = (short)EncryptedVerify.Length;
            Response.VerifyToken = EncryptedVerify;

            Response.Write(client.nh.wSock);

            client.nh.wSock.EncEnabled = true;
            client.nh.RaiseSocketInfo(this, "Encryption Enabled.");
        }
        public void HandleLoginDisconnect(MinecraftClient client, IPacket packet) {
            var Disconnect = (CBLoginDisconnect)packet;

            client.RaiseLoginFailure(this, Disconnect.JSONData);
            client.Disconnect();
        }
        public void HandleDisconnect(MinecraftClient client, IPacket packet) {
            var Disconnect = (CBDisconnect)packet;

            client.RaiseInfo(this, "You were kicked! Reason: " + Disconnect.Reason);
            client.RaiseKicked(Disconnect.Reason);
            client.Disconnect();
        }