Skip to content

Instantly share code, notes, and snippets.

@svilex
Last active August 9, 2021 08:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svilex/b4b00f17724f4e4a528af022fc6f7ddf to your computer and use it in GitHub Desktop.
Save svilex/b4b00f17724f4e4a528af022fc6f7ddf to your computer and use it in GitHub Desktop.
<?php
/**
* _ _
* ___ __ __ (_) | | ___
* / __| \ \ / / | | | | / _ \
* \__ \ \ / / | | | | | __/
* |___/ \_/ |_| |_| \___|
*
* @Author: svile
* @Kik: _svile_
* @Telegram_Group: https://telegram.me/svile
* @E-mail: thesville@gmail.com
* @Github: https://github.com/svilex
*
*
* @name balancer
* @main svile\balancer\Main
* @version 1.0
* @api 3.0.0
* @description This is used on a PocketMine server to transfer players to a free proxy
* @author svile
* @load: POSTWORLD
*
*/
namespace svile\balancer {
use pocketmine\plugin\PluginBase;
use pocketmine\event\Listener;
use pocketmine\event\player\PlayerJoinEvent;
use raklib\protocol\UnconnectedPing;
use raklib\protocol\UnconnectedPong;
class Main extends PluginBase implements Listener
{
/** @var string */
private $ip = 'mcpeproxy.tk';
/** @var int[] */
private $ports = [19140, 19141, 19142, 19143, 19144, 19145, 19146, 19147, 19148, 19149, 19150, 19151, 19152, 19153, 19154, 19155];
/** @var array */
private $lastCheck = [];
/** @var resource */
private $socket;
/** @var int */
private $pingID = 1;
public function onEnable()
{
if (!($this->socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP))) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
$this->getServer()->getLogger()->error("Couldn't create socket: [$errorcode] $errormsg");
$this->getServer()->getPluginManager()->disablePlugin($this);
}
socket_set_option($this->socket, SOL_SOCKET, SO_SNDBUF, 1024 * 1024 * 8);
socket_set_option($this->socket, SOL_SOCKET, SO_RCVBUF, 1024 * 1024 * 8);
socket_set_nonblock($this->socket);
$this->getServer()->getPluginManager()->registerEvents($this, $this);
}
public function onDisable()
{
socket_close($this->socket);
}
public function onPlayerJoinEvent(PlayerJoinEvent $ev)
{
$ports = $this->ports;
foreach ($ports as $port) {
array_push($this->ports, array_shift($this->ports));
if ($this->isFree($port) === true) {
echo 'port: ' . $port . ' is free, transferring' . PHP_EOL;
$ev->getPlayer()->transfer($this->ip, (int)$port);// TODO: use the LoginPacket serverAddress as the ip
return;
}
}
$ev->getPlayer()->kick("§cTrial proxies are full at the moment, try again later or buy a private one. More info at: §fhttp://mcpeproxy.info", false);
}
private function isFree(int $port): bool
{
if (isset($this->lastCheck["$port"]) && (microtime(true) - $this->lastCheck["$port"]) < 10) {
echo 'port: ' . $port . ' was checked in the last 10 sec' . PHP_EOL;
return false;
}
$this->lastCheck["$port"] = microtime(true);
if ($this->ping($port) === false) {
echo 'port: ' . $port . ' is occupied' . PHP_EOL;
return false;
}
return true;
}
private function ping(int $port): bool
{
$pk = new UnconnectedPing();
$pk->pingID = $pingID = $this->pingID++;
$pk->encode();
socket_sendto($this->socket, $pk->buffer, strlen($pk->buffer), 0, '127.0.0.1', $port);
$times = 0;
do {
while (socket_recvfrom($this->socket, $buffer, 65535, 0, $pip, $pport) === false || $pip !== '127.0.0.1' || $pport !== $port || $buffer{0} !== "\x1c") {
if (++$times > 100)// 100ms timeout
return false;
usleep(1000);
}
$pk = new UnconnectedPong();
$pk->buffer = $buffer;
$pk->decode();
} while ($pk->pingID !== $pingID);
$serverInfo = explode(';', $pk->serverName);
if (!isset($serverInfo[4], $serverInfo[5]) || !is_numeric($serverInfo[4]) || !is_numeric($serverInfo[5]) || !is_int($serverInfo[4] + 0) || !is_int($serverInfo[5] + 0) || ($serverInfo[4] + 0) < 0 || ($serverInfo[5] + 0) < 0)
return false;
return (bool)(($serverInfo[4] + 0) < ($serverInfo[5] + 0));
}
}
}
@McpeProxyForEver
Copy link

SVILE LISTEN GET THIS FIXED I CANT JOIN MCPEPROXY ALREADY 2 WEEKS FIX IT I LIVE IN KUWAIT

@VipTeams
Copy link

VipTeams commented Jul 1, 2018

Can i get the trial version. Its saying to contact you for it

@IISpacee
Copy link

Can i get the Trial Version I'm IISpace i plan to use it on Sylphhcf I have 64 Subscribers i don't expect you to give me a trial but I'm trying I'd be pleased if I did get the trial Ty <3 ### CLOSED###

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment