/// <summary> /// Refreshes the client's login status /// </summary> public void RefreshLoginStatus() { Debug.Log("Checking user login.."); Debug.Log($"Client auth server token: {MojangAPI.GetClientToken()}"); SetAuthImage(AuthImageStatus.LOADING); StartCoroutine(MojangAPI.GetLoginStatus(HandleLoginResponse)); }
/// <summary> /// Logs the user in using the filled-in username and password /// </summary> public void LoginUser() { string username = EmailInput.text; string password = PasswordInput.text; // check that username and password are filled out if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) { return; } SetAuthImage(AuthImageStatus.LOADING); StartCoroutine(MojangAPI.Login(username, password, HandleLoginResponse)); }
private MSession getSession(AuthenticationResponse mcToken) { if (mcToken == null) { throw new ArgumentNullException("mcToken was null"); } if (!MojangAPI.CheckGameOwnership(mcToken.AccessToken)) { MessageBox.Show("로그인 실패 : 게임 구매를 하지 않았습니다."); this.Close(); } var profile = MojangAPI.GetProfileUsingToken(mcToken.AccessToken); return(new MSession { AccessToken = mcToken.AccessToken, UUID = profile.UUID, Username = profile.Name }); }
public MLoginResponse RequestSession(string accessToken) { try { if (!MojangAPI.CheckGameOwnership(accessToken)) { return(new MLoginResponse(MLoginResult.NoProfile, null, null, null)); } var profile = MojangAPI.GetProfileUsingToken(accessToken); var session = new MSession { Username = profile.Name, AccessToken = accessToken, UUID = profile.UUID }; return(new MLoginResponse(MLoginResult.Success, session, null, null)); } catch (Exception ex) { return(new MLoginResponse(MLoginResult.UnknownError, null, ex.ToString(), null)); } }
private static void Main() { TestTimer Timer = new TestTimer(); try { var a = MojangAPI.GetStatistics(); Console.WriteLine(a.ToString() + "\n" + Timer.ToString()); var api = MojangAPI.GetServiceStatus(); foreach (var list in api) { Console.WriteLine($"{list.Key} : {list.Value}"); } Console.WriteLine("UUID:" + MojangAPI.NameToUUID("Notch") + "\n" + Timer.ToString()); } catch (Exception ex) { } /* * try * { * var ping = new ServerPing("mc.hypixel.net", 25565); * var server = ping.Ping(); * Console.WriteLine(Timer.ToString()); * Console.WriteLine(server.description.text); * Console.WriteLine("{0} / {1}", server.players.online, server.players.max); * Console.WriteLine(server.version.name); * Console.WriteLine(server.modinfo); * * } * catch(Exception ex) * { * Console.WriteLine("服务器信息获取失败:"+ex.Message+"\n"+ Timer.ToString()); * } */ Console.WriteLine("初始化" + Timer.ToString()); using (_fs = new FileStream("mc.log", FileMode.Create)) { using (_tw = new StreamWriter(_fs)) { //这里图方便没有检验LauncherCoreCreationOption.Create()返回的是不是null var core = LauncherCore.Create(); core.GameExit += core_GameExit; core.GameLog += core_GameLog; Console.WriteLine("创建核心" + Timer.ToString()); var launch = new LaunchOptions { Version = core.GetVersion("1.13.2"), //Authenticator = new YggdrasilRefresh(new Guid(),false), Authenticator = new OfflineAuthenticator("test"), //Authenticator = new YggdrasilValidate(Guid.Parse("****"), Guid.Parse("****"), Guid.Parse("****"), "***"), //Authenticator = new YggdrasilLogin("***@**", "***", true,Guid.Parse("****")), //Authenticator = new YggdrasilAuto("***@**", "***", null, null, null, null), //Server = new ServerInfo {Address = "mc.hypixel.net"}, Mode = null, MaxMemory = 2048, MinMemory = 1024, Size = new WindowSize { Height = 768, Width = 1280 } }; Console.WriteLine("设置参数" + Timer.ToString()); var result = core.Launch(launch, (Action <MinecraftLaunchArguments>)(x => { })); Console.WriteLine("开启游戏" + Timer.ToString()); if (!result.Success) { Console.WriteLine("启动失败:[{0}] {1}", result.ErrorType, result.ErrorMessage); if (result.Exception != null) { Console.WriteLine(result.Exception.Message); Console.WriteLine(result.Exception.Source); Console.WriteLine(result.Exception.StackTrace); } Console.ReadKey(); return; } Console.WriteLine($"AccessToken:{result.Handle.Info.AccessToken} " + "\n" + Timer.ToString()); GC.Collect(0); Are.WaitOne(); Console.WriteLine("游戏已关闭"); result = null; GC.Collect(0); Console.ReadKey(); } } }
/// <summary> /// Gets the next packet from the server /// </summary> /// <returns></returns> public PacketData ReadNextPacket() { int packetId; byte[] payload; lock (_streamReadLock) { int length = VarInt.ReadNext(ReadBytes); List <byte> buffer = new List <byte>(); // check if data is compressed if (_compressionThreshold >= 0) { int dataLength = VarInt.ReadNext(ReadBytes); length -= VarInt.GetBytes(dataLength).Length; // remove size of data length from rest of packet length if (dataLength != 0) { byte[] compressedBuffer = ReadBytes(length); buffer.AddRange(ZlibStream.UncompressBuffer(compressedBuffer)); } else { buffer.AddRange(ReadBytes(length)); } } else { buffer.AddRange(ReadBytes(length)); } packetId = VarInt.ReadNext(buffer); payload = buffer.ToArray(); } // handles some stuff during login phase if (State == ProtocolState.LOGIN) { // handle compression packet if (packetId == (int)ClientboundIDs.LogIn_SetCompression) { _compressionThreshold = VarInt.ReadNext(new List <byte>(payload)); return(ReadNextPacket()); } // handle protocol encryption packet if (packetId == (int)ClientboundIDs.LogIn_EncryptionRequest) { var encRequestPkt = new EncryptionRequestPacket() { Payload = payload }; var aesSecret = CryptoHandler.GenerateSharedSecret(); var authHash = CryptoHandler.SHAHash(Encoding.ASCII.GetBytes(encRequestPkt.ServerID).Concat(aesSecret, encRequestPkt.PublicKey)); Debug.Log($"Sending hash to Mojang servers: {authHash}"); // check session with mojang if (!MojangAPI.JoinServer(authHash)) { throw new UnityException("Invalid session. (Try restarting game or relogging into Minecraft account)"); } // use pub key to encrypt shared secret using (var rsaProvider = CryptoHandler.DecodeRSAPublicKey(encRequestPkt.PublicKey)) { byte[] encSecret = rsaProvider.Encrypt(aesSecret, false); byte[] encToken = rsaProvider.Encrypt(encRequestPkt.VerifyToken, false); // respond to server with private key var responsePkt = new EncryptionResponsePacket() { SharedSecret = encSecret, VerifyToken = encToken }; WritePacket(responsePkt); // enable aes encryption _aesStream = new AesStream(Client.GetStream(), aesSecret); _encrypted = true; // read the next packet return(ReadNextPacket()); } } } return(new PacketData { ID = packetId, Payload = payload }); }