-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathServer.php
710 lines (685 loc) · 18.8 KB
/
Server.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
<?php
namespace Phpcraft;
use Asyncore\Condition;
use Exception;
use hellsh\UUID;
use Phpcraft\
{Command\ServerCommandSender, Enum\ChatPosition, Event\ServerTickEvent, Exception\IOException, Exception\NoConnectionException, Packet\ClientboundChatMessagePacket, Packet\KeepAliveRequestPacket, Packet\ServerboundPacketId, Permission\Group};
use http\Client;
use SplObjectStorage;
/**
* A basic Minecraft server.
* This deals with connections, configurations, handshakes, status requests, keep alive packets, and teleportation confirmations.
* This does not include implementing an entire server; that is what packet_function and IntegratedServer are for.
*/
class Server extends BareServer implements ServerCommandSender
{
/**
* The streams the server listens for new connections on.
*
* @var array<resource> $streams
*/
public $streams;
/**
* A private key generated using openssl_pkey_new(["private_key_bits" => 1024, "private_key_type" => OPENSSL_KEYTYPE_RSA]) to use for online mode, or null to use offline mode.
*
* @var resource $private_key
*/
public $private_key;
/**
* A ClientConnection array of all clients that are connected to the server.
*
* @var SplObjectStorage $clients
* @see Server::getPlayers()
*/
public $clients;
/**
* True if you'd like every client's config to be persisted across connections from the same client.
* This is true by default on the IntegratedServer.
*
* @var bool $persist_configs
*/
public $persist_configs = false;
/**
* The amount of bytes at which a packet will be compressed. This applies bi-directionally, but the server decides on the value. Use -1 to disable compression.
*
* @var int $compression_threshold
*/
public $compression_threshold = 256;
/**
* @var array<Group> $groups
*/
public $groups = [];
/**
* The function called when a client has entered state 3 (playing) with the ClientConnection as argument.
*
* @var callable $join_function
*/
public $join_function = null;
/**
* The function called when the server receives a packet from a client in state 3 (playing) unless it's a keep alive response with the ClientConnection and ServerboundPacketId as arguments.
*
* @var callable $packet_function
*/
public $packet_function = null;
/**
* The function called when a client's disconnected from the server with the ClientConnection as argument.
*
* @var callable $disconnect_function
*/
public $disconnect_function = null;
/**
* The function called when to get the server's response to a list ping request with the ClientConnection as argument or null if called internally to get list ping information, e.g. in a plugin.
* See the documentation of ServerConnection::getStatus for an example of all the data a server may respond with (excluding "ping"), or if you return null, the list ping won't be completed.
* Additionally, if you set "no_ping", the client will show "(no connection)" where usually the ping in ms would be.
*
* @var callable $list_ping_function
*/
public $list_ping_function = null;
/**
* If true, clients using incompatible versions will not be prevented from logging in.
*
* @var bool $allow_incompatible_versions
* @since 0.5 Inverted from $deny_incompatible_versions
*/
public $allow_incompatible_versions = false;
/**
* @var Condition $open_condition
* @since 0.2.1
*/
public $open_condition;
protected $tick_loop;
/**
* @param array<resource> $streams An array of streams created by stream_socket_server to listen for clients on.
* @param resource|null $private_key A private key generated using openssl_pkey_new(["private_key_bits" => 1024, "private_key_type" => OPENSSL_KEYTYPE_RSA]) to use for online mode, or null to use offline mode.
*/
function __construct(array $streams = [], $private_key = null)
{
foreach($streams as $stream)
{
stream_set_blocking($stream, false);
}
$this->streams = $streams;
$this->private_key = $private_key;
$this->clients = new SplObjectStorage();
$this->eidCounter = new Counter();
$this->list_ping_function = function(ClientConnection $con = null)
{
$data = [
"description" => [
"text" => "A \\Phpcraft\\Server"
]
];
if($con !== null)
{
$players = [];
foreach($this->clients as $client)
{
if($client->state == Connection::STATE_PLAY)
{
array_push($players, [
"name" => $client->username,
"id" => $client->uuid->toString(true)
]);
}
}
$data["players"] = [
"online" => count($players),
"max" => count($players) + 1,
"sample" => $players
];
$versions = Versions::minecraftReleases(false);
$data["version"] = [
"name" => "Phpcraft ".$versions[count($versions) - 1]." - ".$versions[0],
"protocol" => (Versions::protocolSupported($con->protocol_version) ? $con->protocol_version : Versions::protocol(false)[0])
];
}
return $data;
};
$this->groups = [
"default" => new Group($this, [])
];
$this->open_condition = new Condition(function()
{
return $this->isOpen();
});
$this->open_condition->add(function()
{
$this->handle(true);
}, 0.001);
$this->tick_loop = $this->open_condition->add(function(bool $lagging)
{
PluginManager::fire(new ServerTickEvent($this, $lagging));
}, 0.05);
}
/**
* Returns true if the server has at least one socket to listen for new connections on or at least one client.
*
* @return bool
*/
function isOpen(): bool
{
return count($this->streams) > 0 || count($this->clients) > 0;
}
private function handle(bool $full): void
{
if($full)
{
foreach($this->streams as $stream)
{
while(($in = @stream_socket_accept($stream, 0)) !== false)
{
$con = new ClientConnection($in, $this);
$con->disconnect_after = microtime(true) + 3;
$this->clients->attach($con);
}
}
}
foreach($this->clients as $con)
{
assert($con instanceof ClientConnection);
if($full && $con->isOpen())
{
try
{
if($con->state == Connection::STATE_HANDSHAKE)
{
switch($con->handleInitialPacket())
{
case 1:
if($this->allow_incompatible_versions && !Versions::protocolSupported($con->protocol_version))
{
$con->disconnect("You're using an incompatible version.");
}
else
{
$con->disconnect_after = microtime(true) + 3;
}
break;
case 2: // Legacy List Ping
$json = ($this->list_ping_function)($con);
if($json)
{
if(!isset($json["players"]))
{
$json["players"] = [];
}
if(!isset($json["players"]["online"]))
{
$json["players"]["online"] = 0;
}
if(!isset($json["players"]["max"]))
{
$json["players"]["max"] = 0;
}
$data = "§1\x00127\x00".@$json["version"]["name"]."\x00".ChatComponent::fromArray($json["description"])
->toString(ChatComponent::FORMAT_SILCROW)."\x00".$json["players"]["online"]."\x00".$json["players"]["max"];
$con->writeByte(0xFF);
$con->writeShort(mb_strlen($data));
$con->writeRaw(mb_convert_encoding($data, "utf-16be"));
$con->send(true);
}
$con->close();
}
}
else
{
while(($packet_id = $con->readPacket(0)) !== false)
{
if($con->state == Connection::STATE_PLAY) // Playing
{
$packetId = ServerboundPacketId::getById($packet_id, $con->protocol_version);
if($packetId === null)
{
$con->disconnect("Invalid packet ID: ".dechex($packet_id));
break;
}
if($packetId->name == "keep_alive_response")
{
$con->next_heartbeat = microtime(true) + 15;
$con->disconnect_after = 0;
}
else if($packetId->name == "teleport_confirm")
{
if(gmp_cmp($con->readVarInt(), $con->tpidCounter->current()) == 0)
{
$con->tp_confirm_deadline = 0;
}
}
else if($this->packet_function)
{
($this->packet_function)($con, $packetId);
}
}
else if($con->state == Connection::STATE_LOGIN) // Login
{
if($packet_id == 0x00) // Login Start
{
$con->username = $con->readString();
if(Account::validateUsername($con->username))
{
if($this->private_key)
{
$con->disconnect_after = microtime(true) + 10;
$con->sendEncryptionRequest($this->private_key);
}
else
{
$con->disconnect_after = 0;
$con->finishLogin(UUID::name("OfflinePlayer:".$con->username), $this->eidCounter, $this->compression_threshold);
if($this->join_function)
{
($this->join_function)($con);
}
$con->next_heartbeat = microtime(true) + 15;
}
}
else
{
$con->disconnect_after = 1;
break;
}
}
else if($packet_id == 0x01 && isset($con->username)) // Encryption Response
{
$con->disconnect_after = 0;
$con->handleEncryptionResponse($this->private_key, function($res) use (&$con)
{
if(!is_array($res))
{
return;
}
Phpcraft::$user_cache->set($res["id"], $con->username);
$con->finishLogin(new UUID($res["id"]), $this->eidCounter, $this->compression_threshold);
foreach($this->clients as $client)
{
if($client !== $con && $client->state == Connection::STATE_PLAY && $client->username == $con->username)
{
$client->disconnect(["text" => "You've logged in from a different location."]);
$this->handle(false); // Properly dispose of $client before continuing with a new connection using the same identity to avoid issues.
}
}
if($this->join_function)
{
try
{
($this->join_function)($con);
}
catch(Exception $e)
{
$con->disconnect(get_class($e).": ".$e->getMessage());
}
}
$con->next_heartbeat = microtime(true) + 15;
});
}
else
{
$con->disconnect_after = 1;
break;
}
}
else // Can only be 1; Status
{
if($packet_id == 0x00)
{
$json = ($this->list_ping_function)($con);
if($json)
{
if($no_ping = isset($json["no_ping"]))
{
unset($json["no_ping"]);
}
$con->writeVarInt(0x00);
$con->writeString(json_encode($json));
$con->send();
}
else
{
$no_ping = true;
}
if($no_ping)
{
$con->disconnect_after = 1;
break;
}
}
else if($packet_id == 0x01)
{
$con->writeVarInt(0x01);
$con->writeLong($con->readLong());
$con->send();
$con->disconnect_after = 1;
break;
}
}
}
}
if($con->disconnect_after != 0 && $con->disconnect_after <= microtime(true))
{
$con->disconnect("Keep alive timeout");
continue;
}
if($con->tp_confirm_deadline != 0 && $con->tp_confirm_deadline <= microtime(true))
{
$con->disconnect("Teleportation confirmation timeout");
continue;
}
if($con->next_heartbeat != 0 && $con->next_heartbeat <= microtime(true))
{
(new KeepAliveRequestPacket(time()))->send($con);
$con->next_heartbeat = 0;
$con->disconnect_after = microtime(true) + 30;
}
}
catch(NoConnectionException $ignored)
{
}
catch(Exception $e)
{
if($con->username)
{
echo "Disconnected ".$con->username.": ".get_class($e).": ".$e->getMessage()."\n".$e->getTraceAsString()."\n";
}
$con->disconnect(get_class($e).": ".$e->getMessage());
}
}
if(!$con->isOpen())
{
$this->clients->detach($con);
if($this->disconnect_function)
{
($this->disconnect_function)($con);
}
}
}
}
/**
* @param array<string,array<string,string|array<string>>> $groups
* @return Server
*/
function setGroups(array $groups): Server
{
$this->groups = [];
foreach($groups as $name => $data)
{
$this->groups[$name] = new Group($this, $data);
}
if(!array_key_exists("default", $this->groups))
{
$this->groups["default"] = new Group($this, []);
}
return $this;
}
/**
* Returns the group with the given name or null if not found.
*
* @param string $name
* @return Group|null
*/
function getGroup(string $name): ?Group
{
return @$this->groups[$name];
}
/**
* Returns true if the server has at least one socket to listen for new connections.
*
* @return bool
*/
function isListening(): bool
{
return count($this->streams) > 0;
}
/**
* Returns the ports the server is listening on.
*
* @return array<int>
*/
function getPorts(): array
{
$ports = [];
foreach($this->streams as $stream)
{
$name = stream_socket_get_name($stream, false);
array_push($ports, intval(substr($name, strpos($name, ":", -6) + 1)));
}
return $ports;
}
/**
* Returns the "description" key from $this->list_ping_function's return array, cast to ChatComponent.
*
* @return ChatComponent
* @see Server::$list_ping_function
*/
function getMotd(): ChatComponent
{
return ChatComponent::cast(($this->list_ping_function)()["description"]);
}
/**
* Returns true if the server is in online mode.
*
* @return bool
*/
function isOnlineMode(): bool
{
return $this->private_key !== null;
}
/**
* Sends a message to all players and prints it to the console.
*
* @param array|string|null|ChatComponent $msg
* @param int $position
* @return Server $this
*/
function broadcast($msg, int $position = ChatPosition::SYSTEM): Server
{
$msg = ChatComponent::cast($msg);
echo $msg->toString(ChatComponent::FORMAT_ANSI)."\n";
$msg = new ClientboundChatMessagePacket($msg);
foreach($this->getPlayers() as $c)
{
try
{
$msg->send($c);
}
catch(Exception $ignored)
{
}
}
return $this;
}
/**
* Returns all clients in state 3 (playing).
*
* @return ClientConnection[]
*/
function getPlayers(): array
{
$clients = [];
foreach($this->clients as $client)
{
if($client->state == Connection::STATE_PLAY)
{
array_push($clients, $client);
}
}
return $clients;
}
/**
* Sends a message to the server console and all players with the given permission, e.g. "everything" for administrators.
*
* @param array|string|null|ChatComponent $msg
* @param string $permission
* @return Server
*/
function adminBroadcast($msg, string $permission = "everything"): Server
{
$msg = ChatComponent::cast($msg);
echo $msg->toString(ChatComponent::FORMAT_ANSI)."\n";
return $this->permissionBroadcast($permission, $msg);
}
/**
* Sends a message to all clients in playing state with the given permission.
*
* @param string $permission
* @param array|string|null|ChatComponent $msg
* @param int $position
* @return Server $this
*/
function permissionBroadcast(string $permission, $msg, int $position = ChatPosition::SYSTEM): Server
{
$msg = new ClientboundChatMessagePacket(ChatComponent::cast($msg));
foreach($this->clients as $c)
{
assert($c instanceof ClientConnection);
try
{
if($c->state == Connection::STATE_PLAY && $c->hasPermission($permission))
{
$msg->send($c);
}
}
catch(IOException $e)
{
}
}
return $this;
}
/**
* Sends a message to the server console and "[Server: $message]" to all players with the given permission.
*
* @param array|string|null|ChatComponent $message
* @param string $permission
* @return void
*/
function sendAdminBroadcast($message, string $permission = "everything"): void
{
$message = ChatComponent::cast($message);
echo $message->toString(ChatComponent::FORMAT_ANSI)."\n";
$this->permissionBroadcast($permission, ChatComponent::text("[Server: ")
->gray()
->add($message)
->add("]"));
}
/**
* Gets the ClientConfiguration of a player who might be offline.
*
* @param string|UUID $name_or_uuid
* @return ClientConfiguration|null
*/
function getOfflinePlayer(string $name_or_uuid): ?ClientConfiguration
{
$player = $this->getPlayer($name_or_uuid);
if($player !== null)
{
return $player->config;
}
if($name_or_uuid instanceof UUID)
{
return new ClientConfiguration($this, null, "config/player_data/".$name_or_uuid->toString(false).".json");
}
$name_or_uuid = strtolower($name_or_uuid);
foreach(Phpcraft::$user_cache as $uuid => $_name)
{
if(strtolower($_name) == $name_or_uuid)
{
return new ClientConfiguration($this, null, "config/player_data/{$uuid}.json");
}
}
return null;
}
/**
* Returns a client in state 3 (playing) with the given name or UUID, or null if not found.
*
* @param string|UUID $name_or_uuid
* @return ClientConnection|null
*/
function getPlayer($name_or_uuid): ?ClientConnection
{
foreach($this->clients as $client)
{
assert($client instanceof ClientConnection);
if($client->state == Connection::STATE_PLAY && ($client->username == $name_or_uuid || $client->uuid == $name_or_uuid))
{
return $client;
}
}
return null;
}
function getName(): string
{
return "Server";
}
/**
* Prints a message to the console.
* Available in accordance with the CommandSender interface.
* If you want to print to console specifically, just use PHP's `echo`.
*
* @param array|string|null|ChatComponent $message
* @return void
*/
function sendMessage($message): void
{
echo ChatComponent::cast($message)
->toString(ChatComponent::FORMAT_ANSI)."\n";
}
/**
* Closes all server listen sockets and client connections.
*
* @param array|string|null|ChatComponent $reason The reason for closing the server, sent to clients before disconnecting them.
* @return void
*/
function close($reason = null): void
{
$this->softClose();
foreach($this->clients as $client)
{
assert($client instanceof ClientConnection);
$client->disconnect($reason);
}
}
/**
* Closes all server listen sockets but keeps connected clients.
*
* @return void
*/
function softClose(): void
{
foreach($this->streams as $stream)
{
stream_socket_shutdown($stream, STREAM_SHUT_RDWR);
fclose($stream);
}
$this->streams = [];
}
function hasPermission(string $permission): bool
{
return true;
}
/**
* Available in accordance with the CommandSender interface.
*
* @return bool true
*/
function hasServer(): bool
{
return true;
}
/**
* Available in accordance with the CommandSender interface.
*
* @return Server $this
*/
function getServer(): ?Server
{
return $this;
}
function hasPosition(): bool
{
return false;
}
function getPosition(): ?Point3D
{
return null;
}
}