public function __construct(EmailAuth $plugin) { $this->plugin = $plugin; $config = $this->plugin->getConfig()->get("dataProviderSettings"); if (!isset($config["host"]) or !isset($config["user"]) or !isset($config["password"]) or !isset($config["database"])) { $this->plugin->getLogger()->critical("Invalid MySQL settings"); $this->plugin->setDataProvider(new DummyDataProvider($this->plugin)); return; } $this->database = new \mysqli($config["host"], $config["user"], $config["password"], $config["database"], isset($config["port"]) ? $config["port"] : 3306); if ($this->database->connect_error) { $this->plugin->getLogger()->critical("Couldn't connect to MySQL: " . $this->database->connect_error); $this->plugin->setDataProvider(new DummyDataProvider($this->plugin)); return; } $resource = $this->plugin->getResource("mysql.sql"); $this->database->query(stream_get_contents($resource)); fclose($resource); $this->plugin->getServer()->getScheduler()->scheduleRepeatingTask(new MySQLPingTask($this->plugin, $this->database), 600); // Each 30 seconds $this->plugin->getLogger()->info("Connected to MySQL server"); }
public function onPacketReceive(CustomPacketReceiveEvent $ev) { $data = json_decode($ev->getPacket()->data); if (!is_array($data) or $data[0] != $this->plugin->getConfig()->get("passcode", false)) { return; } if ($this->plugin->getConfig()->get("servermode", null) == "master") { switch ($data[1]) { case "online": // online // slave->master = [passcode, online] $this->updateList[$ev->getPacket()->address . ":" . $ev->getPacket()->port]["lastcontact"] = $this->makeTimestamp(date("Y-m-d H:i:s")); if (!isset($this->checkFistConnect[$ev->getPacket()->address . ":" . $ev->getPacket()->port])) { $this->checkFistConnect[$ev->getPacket()->address . ":" . $ev->getPacket()->port] = 1; $this->plugin->getLogger()->info(TextFormat::DARK_AQUA . $ev->getPacket()->address . ":" . $ev->getPacket()->port . " " . $this->plugin->get("mastermode-first-connected")); CPAPI::sendPacket(new DataPacket($ev->getPacket()->address, $ev->getPacket()->port, json_encode([$this->plugin->getConfig()->get("passcode"), "hello", "0", "0"]))); } break; case "checkisRegistered": /* checkisRegistered */ /* slave->master = [passcode, checkisRegistered, username, email] */ /* master->slave = [passcode, checkisRegistered, username, email, isRegistered[true||false] */ $username = strtolower($data[2]); $email = strtolower($data[3]); if ($this->plugin->db->getUserData($email) !== false) { $isRegistered = true; } else { if ($this->plugin->db->getEmailToName($username) !== false) { $isRegistered = true; } else { $isRegistered = false; } } $data = [$this->plugin->getConfig()->get("passcode"), "checkisRegistered", $username, $email, $isRegistered]; CPAPI::sendPacket(new DataPacket($ev->getPacket()->address, $ev->getPacket()->port, json_encode($data))); break; case "sendAuthCode": /* sendAuthCode */ /* slave->master = [passcode, sendAuthCode, username, email] */ /* master->slave = [passcode, sendAuthCode, username, email, authcodeSented */ $username = strtolower($data[2]); $email = strtolower($data[3]); $verifyNotWrong = true; $verifyNotRegistered = false; $check = explode("@", $email); if (isset($check[1])) { $lockDomain = $this->plugin->db->getLockDomain(); if ($lockDomain !== null and $check[1] !== $lockDomain) { $verifyNotWrong = false; } } if (!$this->plugin->db->checkUserData($email)) { $verifyNotRegistered = true; } if ($verifyNotWrong != false and $verifyNotRegistered != false) { $nowTime = date("Y-m-d H:i:s"); $serverName = $this->plugin->getConfig()->get("serverName", ""); $authCode = $this->plugin->authCodeGenerator(6); $this->plugin->authcode[$username] = ["authcode" => $authCode, "email" => $email]; $task = new EmailSendTask($email, $username, $nowTime, $serverName, $authCode, $this->plugin->getConfig()->getAll(), $this->plugin->getDataFolder() . "signform.html"); $this->plugin->getServer()->getScheduler()->scheduleAsyncTask($task); $data = [$this->plugin->getConfig()->get("passcode"), "sendAuthCode", $username, $email, true]; CPAPI::sendPacket(new DataPacket($ev->getPacket()->address, $ev->getPacket()->port, json_encode($data))); } else { $data = [$this->plugin->getConfig()->get("passcode"), "sendAuthCode", $username, $email, false]; CPAPI::sendPacket(new DataPacket($ev->getPacket()->address, $ev->getPacket()->port, json_encode($data))); } break; case "checkAuthCode": /* checkAuthCode */ /* slave->master = [passcode, checkAuthCode, username, authCode, passcode_hash] */ /* master->slave = [passcode, checkAuthCode, username, email, isSuccess, orAuthCodeNotExist, passcode_hash] */ $username = strtolower($data[2]); $authCode = $data[3]; $passcode_hash = $data[4]; $isSuccess = false; $orAuthCodeNotExist = false; if (isset($this->plugin->authcode[$username])) { if ($this->plugin->authcode[$username]["authcode"] == $authCode) { $isSuccess = true; } else { $isSuccess = false; } $email = $this->plugin->authcode[$username]["email"]; } else { $email = null; $orAuthCodeNotExist = true; } $data = [$this->plugin->getConfig()->get("passcode"), "checkAuthCode", $username, $email, $isSuccess, $orAuthCodeNotExist, $passcode_hash]; CPAPI::sendPacket(new DataPacket($ev->getPacket()->address, $ev->getPacket()->port, json_encode($data))); break; case "deleteAuthCode": /* deleteAuthCode */ /* slave->master = [passcode, deleteAuthCode, username, email] */ $username = strtolower($data[2]); $email = strtolower($data[3]); if (isset($this->plugin->authcode[$username])) { if ($this->plugin->authcode[$username]["email"] == $email) { unset($this->plugin->authcode[$username]); } } break; case "requestCreateOTP": /* requestCreateOTP */ /* slave->master = [passcode, requestCreateOTP, username, IP] */ /* master->slave = [passcode, requestCreateOTP, username, IP, isGenerated[true|false] */ $username = strtolower($data[2]); $address = $data[3]; $isGenerated = false; $isTimeout = true; if (isset($this->otpDb[$username])) { $progress = $this->makeTimestamp(date("Y-m-d H:i:s")) - $this->makeTimestamp($this->otpDb[$username]["date"]); if ($progress < 3600) { $isTimeout = false; } } if ($isTimeout) { $email = $this->plugin->db->getEmailToName($username); if ($email !== false) { $isGenerated = true; $nowTime = date("Y-m-d H:i:s"); $serverName = $this->plugin->getConfig()->get("serverName", ""); $this->otpDb[$username] = ["passkey" => $this->plugin->authCodeGenerator(6), "requestedIp" => $address, "date" => $nowTime]; $task = new EmailSendTask($email, $username, $nowTime, $serverName, $this->otpDb[$username]["passkey"], $this->plugin->getConfig()->getAll(), $this->plugin->getDataFolder() . "otpform.html"); $this->plugin->getServer()->getScheduler()->scheduleAsyncTask($task); } } $data = [$this->plugin->getConfig()->get("passcode"), "requestCreateOTP", $username, $address, $isGenerated]; CPAPI::sendPacket(new DataPacket($ev->getPacket()->address, $ev->getPacket()->port, json_encode($data))); break; case "requestUseOTP": /* requestUseOTP */ /* slave->master = [passcode, requestUseOTP, username, IP, otp] */ /* master->slave = [passcode, requestUseOTP, username, IP, isSuccess[true|false] */ $username = strtolower($data[2]); $address = $data[3]; $otp = $data[4]; $isSuccess = false; if (isset($this->otpDb[$username])) { $progress = $this->makeTimestamp(date("Y-m-d H:i:s")) - $this->makeTimestamp($this->otpDb[$username]["date"]); if ($progress < 3600) { if ($this->otpDb[$username]["passkey"] === $otp) { $email = $this->plugin->db->getEmailToName($username); if ($email !== false) { $isSuccess = true; $this->onlineUserList[$username] = $ev->getPacket()->address . ":" . $ev->getPacket()->port; $this->plugin->db->updateIPAddress($email, $address); unset($this->otpDb[$username]); } } } } $data = [$this->plugin->getConfig()->get("passcode"), "requestUseOTP", $username, $address, $isSuccess]; CPAPI::sendPacket(new DataPacket($ev->getPacket()->address, $ev->getPacket()->port, json_encode($data))); break; case "defaultInfoRequest": /* defaultInfoRequest */ /* slave->master = [passcode, defaultInfoRequest, username, IP] */ /* master->slave = [passcode, defaultInfoRequest, username, isConnected[true|false], isRegistered[true||false], isAutoLogin[true||false], NBT, isCheckAuthReady, lockDomain] */ $requestedUserName = strtolower($data[2]); $requestedUserIp = $data[3]; // IPCHECK $email = $this->plugin->db->getEmailToIp($requestedUserIp); $userdata = $this->plugin->db->getUserData($email); if ($email === false or $userdata === false) { // did not join $isConnect = false; $isRegistered = false; $isAutoLogin = false; $NBT = null; } else { if (isset($this->onlineUserList[$requestedUserName])) { $isConnect = true; } else { $isConnect = false; } if ($userdata["name"] == $requestedUserName) { $isAutoLogin = true; $isRegistered = true; $this->onlineUserList[$requestedUserName] = $ev->getPacket()->address . ":" . $ev->getPacket()->port; $this->plugin->db->updateIPAddress($email, $requestedUserIp); } else { $isAutoLogin = false; if ($this->plugin->db->getEmailToName($requestedUserName) !== false) { $isRegistered = true; } else { $isRegistered = false; } } $NBT = $this->getPlayerDataFile($requestedUserName); } // EMAIL-CHECK if ($email === false or $userdata === false) { $email = $this->plugin->db->getEmailToName($requestedUserName); $userdata = $this->plugin->db->getUserData($email); if ($email === false or $userdata === false) { // did not join $isConnect = false; $isRegistered = false; $isAutoLogin = false; $NBT = null; } else { if (isset($this->onlineUserList[$requestedUserName])) { $isConnect = true; } else { $isConnect = false; } $isAutoLogin = false; $isRegistered = true; $NBT = $this->getPlayerDataFile($requestedUserName); } } $isCheckAuthReady = $this->plugin->db->checkAuthReady($requestedUserName); $lockDomain = $this->plugin->db->getLockDomain(); $data = [$this->plugin->getConfig()->get("passcode"), "defaultInfoRequest", $requestedUserName, $isConnect, $isRegistered, $isAutoLogin, $NBT, $isCheckAuthReady, $lockDomain]; CPAPI::sendPacket(new DataPacket($ev->getPacket()->address, $ev->getPacket()->port, json_encode($data))); break; case "loginRequest": // loginRequest // slave->master = [passcode, loginRequest, username, password_hash, IP] // master->slave = [passcode, loginRequest, username, isSuccess[true||false]] $username = strtolower($data[2]); $password_hash = $data[3]; $address = $data[4]; $email = $this->plugin->db->getEmailToName($username); $userdata = $this->plugin->db->getUserData($email); if ($email === false or $userdata === false) { $isSuccess = false; } else { if ($userdata["password"] == $password_hash) { $isSuccess = true; $this->onlineUserList[$username] = $ev->getPacket()->address . ":" . $ev->getPacket()->port; $this->plugin->db->updateIPAddress($email, $address); } else { $isSuccess = false; } } $data = [$this->plugin->getConfig()->get("passcode"), "loginRequest", $username, $isSuccess]; CPAPI::sendPacket(new DataPacket($ev->getPacket()->address, $ev->getPacket()->port, json_encode($data))); break; case "logoutRequest": // logoutRequest // slave->master = [passcode, logoutRequest, username, IP, isUserGenerate] // master->slave = [passcode, logoutRequest, username, isSuccess] $username = strtolower($data[2]); $address = $data[3]; $isUserGenerate = $data[4]; $isSuccess = true; if (isset($this->onlineUserList[$username])) { unset($this->onlineUserList[$username]); } if ($isUserGenerate) { $isSuccess = $this->plugin->db->logout($this->plugin->db->getEmailToIp($address)); } $data = [$this->plugin->getConfig()->get("passcode"), "logoutRequest", $username, $isSuccess]; CPAPI::sendPacket(new DataPacket($ev->getPacket()->address, $ev->getPacket()->port, json_encode($data))); break; case "registerRequest": // registerRequest // slave->master = [passcode, registerRequest, username, password_hash, IP, email] // master->slave = [passcode, registerRequest, username, isSuccess[true||false]] $username = strtolower($data[2]); $password_hash = $data[3]; $address = $data[4]; $email = strtolower($data[5]); $isSuccess = $this->plugin->db->addUser($email, $password_hash, $address, $username); $data = [$this->plugin->getConfig()->get("passcode"), "registerRequest", $username, $isSuccess]; if ($this->plugin->db->checkAuthReady($username)) { $this->plugin->db->completeAuthReady($username); } CPAPI::sendPacket(new DataPacket($ev->getPacket()->address, $ev->getPacket()->port, json_encode($data))); break; case "unregisterRequest": // unregisterRequest // slave->master = [passcode, unregisterRequest, username] // master->slave = [passcode, unregisterRequest, username, isSuccess] $username = strtolower($data[2]); $email = $this->plugin->db->getEmailToName($username); $deleteCheck = $this->plugin->db->deleteUser($email); ($email === false or $deleteCheck === false) ? $isSuccess = false : ($isSuccess = true); $data = [$this->plugin->getConfig()->get("passcode"), "unregisterRequest", $username, $isSuccess]; CPAPI::sendPacket(new DataPacket($ev->getPacket()->address, $ev->getPacket()->port, json_encode($data))); break; case "itemSyncro": // itemSyncro // slave->master = [passcode, itemSyncro, username, itemData] // master->slave = [passcode, itemSyncro, username, itemData] $username = strtolower($data[2]); $itemData = $data[3]; $playerdata = $this->getPlayerData($username, $itemData); $this->applyItemData($username, $playerdata); break; case "itemSyncroRequest": // itemSyncroRequest // slave->master = [passcode, itemSyncro, username] // master->slave = [passcode, itemSyncro, username, itemData] $username = strtolower($data[2]); // itemSyncro // slave->master = [passcode, itemSyncro, username, itemData] $data = [$this->plugin->getConfig()->get("passcode"), "itemSyncro", $username, $this->getPlayerDataFile($username)]; CPAPI::sendPacket(new DataPacket($ev->getPacket()->address, $ev->getPacket()->port, json_encode($data))); break; case "economyRequest": // economyRequest // slave->master = [passcode, economySyncro, username] // master->slave = [passcode, economySyncro, username, money] // TODO break; case "economySyncro": // economySyncro // slave->master = [passcode, economySyncro, username, money] // master->slave = [passcode, economySyncro, username, money] $username = strtolower($data[2]); $money = $data[3]; $this->applyEconomyData($username, $money); $data = [$this->plugin->getConfig()->get("passcode"), "economySyncro", $username, $money]; $target_addr = $ev->getPacket()->address . ":" . $ev->getPacket()->port; foreach ($this->updateList as $ipport => $data) { if ($target_addr == $ipport) { continue; } $addr = explode(":", $ipport); CPAPI::sendPacket(new DataPacket($addr[0], $addr[1], json_encode($data))); } break; } } else { if ($this->plugin->getConfig()->get("servermode", null) == "slave") { switch ($data[1]) { case "hello": if (!isset($this->checkFistConnect[$ev->getPacket()->address . ":" . $ev->getPacket()->port])) { $this->checkFistConnect[$ev->getPacket()->address . ":" . $ev->getPacket()->port] = 1; $this->plugin->getLogger()->info(TextFormat::DARK_AQUA . $ev->getPacket()->address . ":" . $ev->getPacket()->port . " " . $this->plugin->get("slavemode-first-connected")); } break; case "checkisRegistered": /* checkisRegistered */ /* slave->master = [passcode, checkisRegistered, username, email] */ /* master->slave = [passcode, checkisRegistered, username, email, isRegistered[true||false] */ $username = strtolower($data[2]); $email = strtolower($data[3]); $isRegistered = $data[4]; $player = $this->plugin->getServer()->getPlayer($username); if (!$player instanceof Player) { return; } if ($isRegistered) { // ALREADY REGISTERED $this->plugin->message($player, $this->plugin->get("that-email-is-already-registered")); return; } else { // NOT REGISTERED $this->plugin->message($player, $this->plugin->get("issuing-an-authorization-code")); /* sendAuthCode */ /* slave->master = [passcode, sendAuthCode, username, email] */ $data = [$this->plugin->getConfig()->get("passcode"), "sendAuthCode", $username, $email]; CPAPI::sendPacket(new DataPacket($ev->getPacket()->address, $ev->getPacket()->port, json_encode($data))); } break; case "requestCreateOTP": /* requestCreateOTP */ /* slave->master = [passcode, requestCreateOTP, username, IP] */ /* master->slave = [passcode, requestCreateOTP, username, IP, isGenerated[true|false] */ $username = $data[2]; $address = $data[3]; $isGenerated = $data[4]; $player = $this->plugin->getServer()->getPlayer($username); if (!$player instanceof Player) { return; } if ($isGenerated) { $this->plugin->message($player, $this->plugin->get("otp-is-generated")); } else { $this->plugin->message($player, $this->plugin->get("otp-is-not-generated")); if (isset($this->plugin->wrongauth[strtolower($player->getAddress())])) { $this->plugin->wrongauth[$player->getAddress()]++; $player->kick($this->plugin->get("banned-brute-force")); if ($this->plugin->wrongauth[$player->getAddress()] >= 7) { $this->plugin->getServer()->blockAddress($player->getAddress(), 400); } } else { $this->plugin->wrongauth[$player->getAddress()] = 1; } } break; case "requestUseOTP": /* requestUseOTP */ /* slave->master = [passcode, requestUseOTP, username, IP, otp] */ /* master->slave = [passcode, requestUseOTP, username, IP, isSuccess[true|false] */ $username = $data[2]; $address = $data[3]; $isSuccess = $data[4]; $player = $this->plugin->getServer()->getPlayer($username); if (!$player instanceof Player) { return; } if ($isSuccess) { $this->plugin->message($player, $this->plugin->get("otp-logined")); $this->authenticatePlayer($player); } else { $this->plugin->message($player, $this->plugin->get("otp-access-denied")); if (isset($this->plugin->wrongauth[strtolower($player->getAddress())])) { $this->plugin->wrongauth[$player->getAddress()]++; $player->kick($this->plugin->get("banned-brute-force")); if ($this->plugin->wrongauth[$player->getAddress()] >= 7) { $this->plugin->getServer()->blockAddress($player->getAddress(), 400); } } else { $this->plugin->wrongauth[$player->getAddress()] = 1; } } break; case "sendAuthCode": /* sendAuthCode */ /* slave->master = [passcode, sendAuthCode, username, email] */ /* master->slave = [passcode, sendAuthCode, username, email, authcodeSented */ $username = strtolower($data[2]); $player = $this->plugin->getServer()->getPlayer($username); $email = strtolower($data[3]); $authcodeSented = $data[4]; if (!$player instanceof Player) { return; } if ($authcodeSented) { $this->plugin->message($player, $this->plugin->get("mail-has-been-sent")); } else { $this->plugin->message($player, $this->plugin->get("rejected-an-authorization-code")); } break; case "checkAuthCode": /* checkAuthCode */ /* slave->master = [passcode, checkAuthCode, username, authCode, passcode_hash] */ /* master->slave = [passcode, checkAuthCode, username, email, isSuccess, orAuthCodeNotExist, passcode_hash] */ $username = strtolower($data[2]); $email = strtolower($data[3]); $isSuccess = $data[4]; $orAuthCodeNotExist = $data[5]; $passcode_hash = $data[6]; $player = $this->plugin->getServer()->getPlayer($username); if (!$player instanceof Player) { return; } if ($orAuthCodeNotExist) { $this->plugin->message($player, $this->plugin->get("authcode-resetted")); $this->deauthenticatePlayer($player); return; } if ($isSuccess) { /* registerRequest */ /* slave->master = [passcode, registerRequest, username, password_hash, IP, email] */ $data = [$this->plugin->getConfig()->get("passcode"), "registerRequest", $player->getName(), $passcode_hash, $player->getAddress(), $email]; CPAPI::sendPacket(new DataPacket($this->plugin->getConfig()->get("masterip"), $this->plugin->getConfig()->get("masterport"), json_encode($data))); $this->plugin->message($player, $this->plugin->get("proceed-to-register-please-wait")); } else { $this->plugin->message($player, $this->plugin->get("wrong-authcode")); if ($player instanceof Player) { if (isset($this->plugin->wrongauth[strtolower($player->getAddress())])) { $this->plugin->wrongauth[$player->getAddress()]++; $player->kick($this->plugin->get("banned-brute-force")); if ($this->plugin->wrongauth[$player->getAddress()] >= 7) { $this->plugin->getServer()->blockAddress($player->getAddress(), 400); } } else { $this->plugin->wrongauth[$player->getAddress()] = 1; } } $this->deauthenticatePlayer($player); } /* deleteAuthCode */ /* slave->master = [passcode, deleteAuthCode, username, email] */ $data = [$this->plugin->getConfig()->get("passcode"), "deleteAuthCode", $player->getName(), $email]; CPAPI::sendPacket(new DataPacket($this->plugin->getConfig()->get("masterip"), $this->plugin->getConfig()->get("masterport"), json_encode($data))); break; case "defaultInfoRequest": /* defaultInfoRequest */ /* slave->master = [passcode, defaultInfoRequest, username, IP] */ /* master->slave = [passcode, defaultInfoRequest, username, isConnected[true|false], isRegistered[true||false], isConnected[true||false], NBT, isCheckAuthReady, lockDomain] */ $username = strtolower($data[2]); $isConnect = $data[3]; $isRegistered = $data[4]; $isAutoLogin = $data[5]; $NBT = $data[6]; $isCheckAuthReady = $data[7]; $lockDomain = strtolower($data[8]); $this->tmpDb[$username] = ["isConnect" => $isConnect, "isRegistered" => $isRegistered, "isAutoLogin" => $isAutoLogin, "isCheckAuthReady" => $isCheckAuthReady, "lockDomain" => $lockDomain]; $player = $this->plugin->getServer()->getPlayer($username); if (!$player instanceof Player) { return; } if ($isConnect) { $this->alreadyLogined($player); return; } $this->applyItemData($username, $this->getPlayerData($username, $NBT)); if ($isAutoLogin) { $this->plugin->message($player, $this->plugin->get("automatic-ip-logined")); $this->authenticatePlayer($player); return; } $this->cueAuthenticatePlayer($player); break; case "loginRequest": // loginRequest // slave->master = [passcode, loginRequest, username, password_hash, IP] // master->slave = [passcode, loginRequest, username, isSuccess[true||false]] $username = strtolower($data[2]); $isSuccess = $data[3]; $player = $this->plugin->getServer()->getPlayer($username); if (!$player instanceof Player) { return; } if ($isSuccess) { $this->plugin->message($player, $this->plugin->get("login-is-success")); $this->authenticatePlayer($player); } else { $this->plugin->message($player, $this->plugin->get("login-is-failed")); $this->deauthenticatePlayer($player); } break; case "logoutRequest": // logoutRequest // slave->master = [passcode, logoutRequest, username, IP, isUserGenerate] // master->slave = [passcode, logoutRequest, username, isSuccess] $username = strtolower($data[2]); $isSuccess = $data[3]; $player = $this->plugin->getServer()->getPlayer($username); if (!$player instanceof Player) { return; } if ($isSuccess) { $this->plugin->message($player, $this->plugin->get("logout-complete")); $player->kick("logout"); } else { $this->plugin->message($player, $this->plugin->get("logout-failed")); } break; case "registerRequest": // registerRequest // slave->master = [passcode, registerRequest, username, password, IP, email] // master->slave = [passcode, registerRequest, username, isSuccess[true||false]] $username = strtolower($data[2]); $isSuccess = $data[3]; $player = $this->plugin->getServer()->getPlayer($username); if (!$player instanceof Player) { return; } if ($isSuccess) { $this->plugin->message($player, $this->plugin->get("register-complete")); $this->authenticatePlayer($player); } else { $this->plugin->message($player, $this->plugin->get("register-failed")); $this->deauthenticatePlayer($player); } break; case "unregisterRequest": // unregisterRequest // slave->master = [passcode, unregisterRequest, username] // master->slave = [passcode, unregisterRequest, username, isSuccess] $username = strtolower($data[2]); $isSuccess = $data[3]; $player = $this->plugin->getServer()->getPlayer($username); if (!$player instanceof Player) { return; } if ($isSuccess) { $player->kick($this->plugin->get("complete-unregister")); } else { $this->plugin->message($player, $this->plugin->get("unregister-is-fail")); } break; case "itemSyncro": // itemSyncro // slave->master = [passcode, itemSyncro, username, itemData] // master->slave = [passcode, itemSyncro, username, itemData] $username = $data[2]; $itemData = $data[3]; $this->applyItemData($username, $this->getPlayerData($username, $itemData)); break; case "economyRequest": // slave // economyRequest // slave->master = [passcode, economySyncro, username] // master->slave = [passcode, economySyncro, username, money] $username = $data[2]; $money = $data[3]; $this->applyEconomyData($username, $money); break; case "economySyncro": // slave // economySyncro // slave->master = [passcode, economySyncro, username, money] // master->slave = [passcode, economySyncro, username, money] $username = $data[2]; $money = $data[3]; $this->applyEconomyData($username, $money); break; } } } }
public function onPacketReceive(CustomPacketReceiveEvent $ev) { $data = json_decode($ev->getPacket()->data); if (!is_array($data) or $data[0] != $this->plugin->getConfig()->get("passcode", false)) { return; } if ($this->plugin->getConfig()->get("servermode", null) == "master") { switch ($data[1]) { case "online": // online // slave->master = [passcode, online] $this->updateList[$ev->getPacket()->address . ":" . $ev->getPacket()->port]["lastcontact"] = $this->makeTimestamp(date("Y-m-d H:i:s")); if (!isset($this->checkFistConnect[$ev->getPacket()->address . ":" . $ev->getPacket()->port])) { $this->checkFistConnect[$ev->getPacket()->address . ":" . $ev->getPacket()->port] = 1; $this->plugin->getLogger()->info(TextFormat::DARK_AQUA . $ev->getPacket()->address . ":" . $ev->getPacket()->port . " " . $this->plugin->get("mastermode-first-connected")); CPAPI::sendPacket(new DataPacket($ev->getPacket()->address, $ev->getPacket()->port, json_encode([$this->plugin->getConfig()->get("passcode"), "hello", "0", "0"]))); } break; case "defaultInfoRequest": /* defaultInfoRequest */ /* slave->master = [passcode, defaultInfoRequest, username, IP] */ /* master->slave = [passcode, defaultInfoRequest, username, isConnected[true|false], isRegistered[true||false], isAutoLogin[true||false], NBT, isCheckAuthReady, lockDomain] */ $requestedUserName = $data[2]; $requestedUserIp = $data[3]; // getUserData $email = $this->plugin->db->getEmailToName($requestedUserName); $userdata = $this->plugin->db->getUserData($email); if ($email === false or $userdata === false) { // did not join $isConnect = false; $isRegistered = false; $isAutoLogin = false; $NBT = null; } else { if (isset($this->onlineUserList[$requestedUserName])) { $isConnect = true; } else { $isConnect = false; } if ($userdata["ip"] == $requestedUserIp and $userdata["name"] == $requestedUserName) { $isAutoLogin = true; } else { $isAutoLogin = false; } $isRegistered = true; $NBT = $this->getPlayerDataFile($requestedUserName); } $isCheckAuthReady = $this->plugin->db->checkAuthReady($requestedUserName); $lockDomain = $this->plugin->db->getLockDomain(); $data = [$this->plugin->getConfig()->get("passcode"), "defaultInfoRequest", $requestedUserName, $isConnect, $isRegistered, $isAutoLogin, $NBT, $isCheckAuthReady, $lockDomain]; CPAPI::sendPacket(new DataPacket($ev->getPacket()->address, $ev->getPacket()->port, json_encode($data))); break; case "loginRequest": // loginRequest // slave->master = [passcode, loginRequest, username, password_hash, IP] // master->slave = [passcode, loginRequest, username, isSuccess[true||false]] $username = $data[2]; $password_hash = $data[3]; $address = $data[4]; $email = $this->plugin->db->getEmailToName($username); $userdata = $this->plugin->db->getUserData($email); if ($email === false or $userdata === false) { $isSuccess = false; } else { if ($userdata["password"] == $password_hash) { $isSuccess = true; $this->onlineUserList[$username] = $ev->getPacket()->address . ":" . $ev->getPacket()->port; $this->plugin->db->updateIPAddress($email, $address); } else { $isSuccess = false; } } $data = [$this->plugin->getConfig()->get("passcode"), "loginRequest", $username, $isSuccess]; CPAPI::sendPacket(new DataPacket($ev->getPacket()->address, $ev->getPacket()->port, json_encode($data))); break; case "logoutRequest": // logoutRequest // slave->master = [passcode, logoutRequest, username, IP, isUserGenerate] // master->slave = [passcode, logoutRequest, username, isSuccess] $username = $data[2]; $address = $data[3]; $isUserGenerate = $data[4]; if ($isUserGenerate) { $this->plugin->db->logout($this->plugin->db->getEmail($player)); } if (isset($this->onlineUserList[$username])) { $isSuccess = true; unset($this->onlineUserList[$username]); } else { $isSuccess = false; } $data = [$this->plugin->getConfig()->get("passcode"), "logoutRequest", $username, $isSuccess]; CPAPI::sendPacket(new DataPacket($ev->getPacket()->address, $ev->getPacket()->port, json_encode($data))); break; case "registerRequest": // registerRequest // slave->master = [passcode, registerRequest, username, password, IP, email] // master->slave = [passcode, registerRequest, username, isSuccess[true||false]] $username = $data[2]; $password = $data[3]; $address = $data[4]; $email = $data[5]; $isSuccess = $this->plugin->db->addUser($email, $password, $address, false, $username); $data = [$this->plugin->getConfig()->get("passcode"), "registerRequest", $username, $isSuccess]; if ($this->plugin->db->checkAuthReady($username)) { $this->plugin->db->completeAuthReady($username); } CPAPI::sendPacket(new DataPacket($ev->getPacket()->address, $ev->getPacket()->port, json_encode($data))); break; case "unregisterRequest": // unregisterRequest // slave->master = [passcode, unregisterRequest, username] // master->slave = [passcode, unregisterRequest, username, isSuccess] $username = $data[2]; if (isset($this->onlineUserList[$username])) { $email = $this->plugin->db->getEmailToName($username); $deleteCheck = $this->plugin->db->deleteUser($email); ($email === false or $deleteCheck === false) ? $isSuccess = false : ($isSuccess = true); } else { $isSuccess = false; } $data = [$this->plugin->getConfig()->get("passcode"), "unregisterRequest", $username, $isSuccess]; CPAPI::sendPacket(new DataPacket($ev->getPacket()->address, $ev->getPacket()->port, json_encode($data))); break; case "itemSyncro": // itemSyncro // slave->master = [passcode, itemSyncro, username, itemData] // master->slave = [passcode, itemSyncro, username, itemData] $username = $data[2]; $itemData = $data[3]; $playerdata = $this->getPlayerData($username, $itemData); $this->applyItemData($username, $playerdata); break; case "itemSyncroRequest": // itemSyncroRequest // slave->master = [passcode, itemSyncro, username] // master->slave = [passcode, itemSyncro, username, itemData] $username = $data[2]; // itemSyncro // slave->master = [passcode, itemSyncro, username, itemData] $data = [$this->plugin->getConfig()->get("passcode"), "itemSyncro", $username, $this->getPlayerDataFile($username)]; CPAPI::sendPacket(new DataPacket($ev->getPacket()->address, $ev->getPacket()->port, json_encode($data))); break; case "economySyncro": // economySyncro // slave->master = [passcode, economySyncro, username, money] // master->slave = [passcode, economySyncro, username, money] $username = $data[2]; $money = $data[3]; $this->applyEconomyData($username, $money); $data = [$this->plugin->getConfig()->get("passcode"), "economySyncro", $username, $money]; $target_addr = $ev->getPacket()->address . ":" . $ev->getPacket()->port; foreach ($this->updateList as $ipport => $data) { if ($target_addr == $ipport) { continue; } CPAPI::sendPacket(new DataPacket($ev->getPacket()->address, $ev->getPacket()->port, json_encode($data))); } break; } } else { if ($this->plugin->getConfig()->get("servermode", null) == "slave") { switch ($data[1]) { case "hello": if (!isset($this->checkFistConnect[$ev->getPacket()->address . ":" . $ev->getPacket()->port])) { $this->checkFistConnect[$ev->getPacket()->address . ":" . $ev->getPacket()->port] = 1; $this->plugin->getLogger()->info(TextFormat::DARK_AQUA . $ev->getPacket()->address . ":" . $ev->getPacket()->port . " " . $this->plugin->get("slavemode-first-connected")); } break; case "defaultInfoRequest": /* defaultInfoRequest */ /* slave->master = [passcode, defaultInfoRequest, username, IP] */ /* master->slave = [passcode, defaultInfoRequest, username, isConnected[true|false], isRegistered[true||false], isConnected[true||false], NBT, isCheckAuthReady, lockDomain] */ $username = $data[2]; $isConnect = $data[3]; $isRegistered = $data[4]; $isAutoLogin = $data[5]; $NBT = $data[6]; $isCheckAuthReady = $data[7]; $lockDomain = $data[8]; $this->tmpDb[$username] = ["isConnect" => $isConnect, "isRegistered" => $isRegistered, "isAutoLogin" => $isAutoLogin, "isCheckAuthReady" => $isCheckAuthReady, "lockDomain" => $lockDomain]; $player = $this->plugin->getServer()->getPlayer($username); if (!$player instanceof Player) { return; } if ($isConnect) { $this->alreadyLogined($player); return; } $this->applyItemData($username, $this->getPlayerData($username, $NBT)); if ($isAutoLogin) { $this->plugin->message($player, $this->plugin->get("automatic-ip-logined")); $this->authenticatePlayer($player); return; } $this->cueAuthenticatePlayer($player); break; case "loginRequest": // loginRequest // slave->master = [passcode, loginRequest, username, password_hash, IP] // master->slave = [passcode, loginRequest, username, isSuccess[true||false]] $username = $data[2]; $isSuccess = $data[3]; $player = $this->plugin->getServer()->getPlayer($username); if (!$player instanceof Player) { return; } if ($isSuccess) { $this->plugin->message($player, $this->plugin->get("login-is-success")); $this->authenticatePlayer($player); } else { $this->plugin->message($player, $this->plugin->get("login-is-failed")); $this->deauthenticatePlayer($player); } break; case "logoutRequest": // logoutRequest // slave->master = [passcode, logoutRequest, username, IP, isUserGenerate] // master->slave = [passcode, logoutRequest, username, isSuccess] $username = $data[2]; $isSuccess = $data[3]; $player = $this->plugin->getServer()->getPlayer($username); if (!$player instanceof Player) { return; } if ($isSuccess) { $this->plugin->message($player, $this->plugin->get("logout-complete")); $player->kick("logout"); } else { $this->plugin->message($player, $this->plugin->get("logout-failed")); } break; case "registerRequest": // registerRequest // slave->master = [passcode, registerRequest, username, password, IP, email] // master->slave = [passcode, registerRequest, username, isSuccess[true||false]] $username = $data[2]; $isSuccess = $data[3]; $player = $this->plugin->getServer()->getPlayer($username); if (!$player instanceof Player) { return; } if ($isSuccess) { $this->plugin->message($player, $this->plugin->get("register-complete")); $this->authenticatePlayer($player); } else { $this->plugin->message($player, $this->plugin->get("register-failed")); $this->deauthenticatePlayer($player); } break; case "itemSyncro": // itemSyncro // slave->master = [passcode, itemSyncro, username, itemData] // master->slave = [passcode, itemSyncro, username, itemData] $username = $data[2]; $itemData = $data[3]; $this->applyItemData($username, $this->getPlayerData($username, $itemData)); break; case "economySyncro": // slave // economySyncro // slave->master = [passcode, economySyncro, username, money] // master->slave = [passcode, economySyncro, username, money] $username = $data[2]; $money = $data[3]; $this->applyEconomyData($username, $money); break; } } } }