-
Notifications
You must be signed in to change notification settings - Fork 0
/
surrogate.php
116 lines (115 loc) · 3.21 KB
/
surrogate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php /** @noinspection PhpUndefinedFieldInspection */
echo "Phpcraft Surrogate (Minecraft Reverse Proxy)\n\n";
if(empty($argv))
{
die("This is for PHP-CLI. Connect to your server via SSH and use `php surrogate.php`.\n");
}
require __DIR__."/vendor/autoload.php";
use Asyncore\Asyncore;
use Phpcraft\
{ChatComponent, ClientConnection, Command\Command, Event\ProxyConsoleEvent, Event\ProxyJoinEvent, Exception\IOException, PluginManager, ProxyServer};
/**
* @var ProxyServer $server
*/
$server = ProxyServer::cliStart("Phpcraft Surrogate", [
"servers" => [
"default" => [
"address" => "phpcraft integrated server",
"default" => true
],
"localhost" => [
"address" => "localhost:1337",
"host" => "localhost.phpcraft.de:25565"
]
],
"groups" => [
"default" => [
"allow" => [
"use /join",
"use /servers"
]
],
"admin" => [
"allow" => "everything"
]
]
]);
echo "Loading plugins...\n";
PluginManager::$command_prefix = "/surrogate:";
PluginManager::loadPlugins();
echo "Loaded ".count(PluginManager::$loaded_plugins)." plugin(s).\n";
$server->ui->render();
/**
* @param ClientConnection $con
* @param string $server_name
* @return string|null
* @throws IOException
*/
function connectToServer(ClientConnection $con, string $server_name): ?string
{
/**
* @var ProxyServer $server
*/ global $server;
$subserver = @$server->config["servers"][$server_name];
if($subserver === null)
{
return "Unknown server: $server_name";
}
if($subserver["address"] == "phpcraft integrated server")
{
$server->connectToIntegratedServer($con);
return null;
}
else
{
return $server->connectDownstream($con, $subserver["address"]);
}
}
$server->join_function = function(ClientConnection $con) use (&$server)
{
if(PluginManager::fire(new ProxyJoinEvent($server, $con)))
{
$con->close();
return;
}
$con->dimension = null;
$host = $con->getHost();
foreach($server->config["servers"] as $name => $s)
{
if(array_key_exists("host", $s) && $s["host"] == $host && (empty($s["restricted"]) || $con->hasPermission("connect to ".$name)) && connectToServer($con, $name) === null)
{
return;
}
}
$default_servers = [];
foreach($server->config["servers"] as $name => $s)
{
if(!empty($s["default"]) && (empty($s["restricted"]) || $con->hasPermission("connect to ".$name)))
{
array_push($default_servers, $name);
}
}
shuffle($default_servers);
$name = $error = "???";
foreach($default_servers as $name)
{
if(($error = connectToServer($con, $name)) === null)
{
return;
}
}
$con->disconnect(count($default_servers) == 0 ? "[Surrogate] There is no default server. You're either missing a permission, are supposed to connect differently, or Surrogate was misconfigured." : "[Surrogate] Failed to connect to any default server. Most recent error in connecting to $name: $error");
};
Asyncore::on("stdin_line", function(string $msg) use (&$server)
{
if(!Command::handleMessage($server, $msg) && !PluginManager::fire(new ProxyConsoleEvent($server, $msg)))
{
$server->broadcast(ChatComponent::translate("chat.type.announcement", [
"Surrogate",
$msg
]));
}
});
Asyncore::loop();
$server->ui->add("Surrogate is not listening on any ports and has no clients, so it's shutting down.");
$server->ui->render();