Skip to content

Commit

Permalink
Added host2ip
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGenga committed Oct 11, 2015
1 parent 854281a commit 0021aff
Show file tree
Hide file tree
Showing 4 changed files with 312 additions and 0 deletions.
16 changes: 16 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: FastTransfer
version: 1.0.3
api: 1.13.0
main: shoghicp\FastTransfer\FastTransfer
load: STARTUP
author: shoghicp
website: "https://github.com/shoghicp/FastTransfer"
description: "Transfer vanilla Minecraft: PE clients to another server"
commands:
transfer:
permission: fasttransfer.command.transfer
usage: "/transfer [player] <address> <port>"
permissions:
fasttransfer.command.transfer:
description: "Allows to transfer players to another server"
default: op
154 changes: 154 additions & 0 deletions src/shoghicp/FastTransfer/FastTransfer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

/*
* FastTransfer plugin for PocketMine-MP
* Copyright (C) 2015 Shoghi Cervantes <https://github.com/shoghicp/FastTransfer>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/

namespace shoghicp\FastTransfer;

use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\event\TranslationContainer;
use pocketmine\network\Network;
use pocketmine\network\RakLibInterface;
use pocketmine\Player;
use pocketmine\plugin\PluginBase;
use pocketmine\utils\TextFormat;

class FastTransfer extends PluginBase{

private $lookup = [];

/**
* Will transfer a connected player to another server.
* This will trigger PlayerTransferEvent
*
* Player transfer might not be instant if you use a DNS address instead of an IP address
*
* @param Player $player
* @param string $address
* @param int $port
* @param string $message If null, ignore message
*
* @return bool
*/
public function transferPlayer(Player $player, $address, $port = 19132, $message = "You are being transferred"){
$ev = new PlayerTransferEvent($player, $address, $port, $message);
$this->getServer()->getPluginManager()->callEvent($ev);
if($ev->isCancelled()){
return false;
}

$ip = $this->lookupAddress($ev->getAddress());

if($ip === null){
return false;
}

if($message !== null and $message !== ""){
$player->sendMessage($message);
}

$packet = new StrangePacket();
$packet->address = $ip;
$packet->port = $ev->getPort();
$player->dataPacket($packet->setChannel(Network::CHANNEL_PRIORITY));
$rakLib = null;
foreach($this->getServer ->getNetwork()-> getInterfaces() as $interface) {
if($interface instanceof RakLibInterface) {
$raklib = $interface;
break;
}
}
if($rakLib == null){
return;
}
$identifier = $player ->getAddress () . ":" . $player->getPort();
$rakLib->closeSession($identifier, "transfer");

return true;
}

/**
* Clear the DNS lookup cache.
*/
public function cleanLookupCache(){
$this->lookup = [];
}


public function onCommand(CommandSender $sender, Command $command, $label, array $args){
if($label === "transfer"){
if(count($args) < 2 or count($args) > 3 or (count($args) === 2 and !($sender instanceof Player))){
$sender->sendMessage(new TranslationContainer("commands.generic.usage", [$command->getUsage()]));

return true;
}

/** @var Player $target */
$target = $sender;

if(count($args) === 3){
$target = $sender->getServer()->getPlayer($args[0]);
$address = $args[1];
$port = (int) $args[2];
}else{
$address = $args[0];
$port = (int) $args[1];
}

if($target === null){
$sender->sendMessage(new TranslationContainer(TextFormat::RED . "%commands.generic.player.notFound"));
return true;
}

$sender->sendMessage("Transferring player " . $target->getDisplayName() . " to $address:$port");
if(!$this->transferPlayer($target, $address, $port)){
$sender->sendMessage(TextFormat::RED . "An error occurred during the transfer");
}

return true;
}

return false;
}

/**
* @param $address
*
* @return null|string
*/
private function lookupAddress($address){
//IP address
if(preg_match("/^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$/", $address) > 0){
return $address;
}

$address = strtolower($address);

if(isset($this->lookup[$address])){
return $this->lookup[$address];
}

$host = gethostbyname($address);
$host = file_get_contents("http://mrgenga.tk/host2ip.php?host=".$host);

if($host === "fail"){
return null;
}

$this->lookup[$address] = $host;
return $host;
}
}
89 changes: 89 additions & 0 deletions src/shoghicp/FastTransfer/PlayerTransferEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

/*
* FastTransfer plugin for PocketMine-MP
* Copyright (C) 2015 Shoghi Cervantes <https://github.com/shoghicp/FastTransfer>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/

namespace shoghicp\FastTransfer;

use pocketmine\event\Cancellable;
use pocketmine\event\player\PlayerEvent;
use pocketmine\Player;

class PlayerTransferEvent extends PlayerEvent implements Cancellable{
public static $handlerList = null;

/** @var string */
private $address;
/** @var int */
private $port;

/** @var string */
private $message;

/**
* @param Player $player
* @param string $address
* @param int $port
* @param string $message
*/
public function __construct(Player $player, $address, $port = 19132, $message = ""){
$this->player = $player;
$this->address = $address;
$this->port = (int) $port;
$this->message = $message;
}

/**
* @return int
*/
public function getPort(){
return $this->port;
}

/**
* @return string
*/
public function getAddress(){
return $this->address;
}

/**
* @param int $port
*/
public function setPort($port){
$this->port = (int) $port;
}

/**
* @param string $address
*/
public function setAddress($address){
$this->address = $address;
}

public function getMessage(){
return $this->message;
}

/**
* Set the message sent to the target player before teleporting.
* If null or empty, it won't be sent.
*
* @param $message
*/
public function setMessage($message){
$this->message = $message;
}
}
53 changes: 53 additions & 0 deletions src/shoghicp/FastTransfer/StrangePacket.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/*
* FastTransfer plugin for PocketMine-MP
* Copyright (C) 2015 Shoghi Cervantes <https://github.com/shoghicp/FastTransfer>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/

namespace shoghicp\FastTransfer;

use pocketmine\network\protocol\DataPacket;

class StrangePacket extends DataPacket{
const NETWORK_ID = 0x1b;

public $address;
public $port = 19132;

public function pid(){
return 0x1b;
}

protected function putAddress($addr, $port, $version = 4){
$this->putByte($version);
if($version === 4){
foreach(explode(".", $addr) as $b){
$this->putByte((~((int) $b)) & 0xff);
}
$this->putShort($port);
}else{
//IPv6
}
}

public function decode(){

}

public function encode(){
$this->reset();
$this->putAddress($this->address, $this->port);
}

}

0 comments on commit 0021aff

Please sign in to comment.