PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
Network.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
27namespace pocketmine\network;
28
32use function base64_encode;
33use function get_class;
34use function preg_match;
35use function spl_object_id;
36use function time;
37use const PHP_INT_MAX;
38
39class Network{
41 private array $interfaces = [];
42
44 private array $advancedInterfaces = [];
45
47 private array $rawPacketHandlers = [];
48
53 private array $bannedIps = [];
54
55 private BidirectionalBandwidthStatsTracker $bandwidthTracker;
56 private string $name;
57 private NetworkSessionManager $sessionManager;
58
59 public function __construct(
60 private \Logger $logger
61 ){
62 $this->sessionManager = new NetworkSessionManager();
63 $this->bandwidthTracker = new BidirectionalBandwidthStatsTracker(5);
64 }
65
66 public function getBandwidthTracker() : BidirectionalBandwidthStatsTracker{ return $this->bandwidthTracker; }
67
71 public function getInterfaces() : array{
72 return $this->interfaces;
73 }
74
75 public function getSessionManager() : NetworkSessionManager{
76 return $this->sessionManager;
77 }
78
79 public function getConnectionCount() : int{
80 return $this->sessionManager->getSessionCount();
81 }
82
83 public function getValidConnectionCount() : int{
84 return $this->sessionManager->getValidSessionCount();
85 }
86
87 public function tick() : void{
88 foreach($this->interfaces as $interface){
89 $interface->tick();
90 }
91
92 $this->sessionManager->tick();
93 }
94
98 public function registerInterface(NetworkInterface $interface) : bool{
99 $ev = new NetworkInterfaceRegisterEvent($interface);
100 $ev->call();
101 if(!$ev->isCancelled()){
102 $interface->start();
103 $this->interfaces[$hash = spl_object_id($interface)] = $interface;
104 if($interface instanceof AdvancedNetworkInterface){
105 $this->advancedInterfaces[$hash] = $interface;
106 $interface->setNetwork($this);
107 foreach(Utils::stringifyKeys($this->bannedIps) as $ip => $until){
108 $interface->blockAddress($ip);
109 }
110 foreach($this->rawPacketHandlers as $handler){
111 $interface->addRawPacketFilter($handler->getPattern());
112 }
113 }
114 $interface->setName($this->name);
115 return true;
116 }
117 return false;
118 }
119
123 public function unregisterInterface(NetworkInterface $interface) : void{
124 if(!isset($this->interfaces[$hash = spl_object_id($interface)])){
125 throw new \InvalidArgumentException("Interface " . get_class($interface) . " is not registered on this network");
126 }
127 (new NetworkInterfaceUnregisterEvent($interface))->call();
128 unset($this->interfaces[$hash], $this->advancedInterfaces[$hash]);
129 $interface->shutdown();
130 }
131
135 public function setName(string $name) : void{
136 $this->name = $name;
137 foreach($this->interfaces as $interface){
138 $interface->setName($this->name);
139 }
140 }
141
142 public function getName() : string{
143 return $this->name;
144 }
145
146 public function updateName() : void{
147 foreach($this->interfaces as $interface){
148 $interface->setName($this->name);
149 }
150 }
151
152 public function sendPacket(string $address, int $port, string $payload) : void{
153 foreach($this->advancedInterfaces as $interface){
154 $interface->sendRawPacket($address, $port, $payload);
155 }
156 }
157
161 public function blockAddress(string $address, int $timeout = 300) : void{
162 $this->bannedIps[$address] = $timeout > 0 ? time() + $timeout : PHP_INT_MAX;
163 foreach($this->advancedInterfaces as $interface){
164 $interface->blockAddress($address, $timeout);
165 }
166 }
167
168 public function unblockAddress(string $address) : void{
169 unset($this->bannedIps[$address]);
170 foreach($this->advancedInterfaces as $interface){
171 $interface->unblockAddress($address);
172 }
173 }
174
178 public function registerRawPacketHandler(RawPacketHandler $handler) : void{
179 $this->rawPacketHandlers[spl_object_id($handler)] = $handler;
180
181 $regex = $handler->getPattern();
182 foreach($this->advancedInterfaces as $interface){
183 $interface->addRawPacketFilter($regex);
184 }
185 }
186
190 public function unregisterRawPacketHandler(RawPacketHandler $handler) : void{
191 unset($this->rawPacketHandlers[spl_object_id($handler)]);
192 }
193
194 public function processRawPacket(AdvancedNetworkInterface $interface, string $address, int $port, string $packet) : void{
195 if(isset($this->bannedIps[$address]) && time() < $this->bannedIps[$address]){
196 $this->logger->debug("Dropped raw packet from banned address $address $port");
197 return;
198 }
199 $handled = false;
200 foreach($this->rawPacketHandlers as $handler){
201 if(preg_match($handler->getPattern(), $packet) === 1){
202 try{
203 $handled = $handler->handle($interface, $address, $port, $packet);
204 }catch(PacketHandlingException $e){
205 $handled = true;
206 $this->logger->error("Bad raw packet from /$address:$port: " . $e->getMessage());
207 $this->blockAddress($address, 600);
208 break;
209 }
210 }
211 }
212 if(!$handled){
213 $this->logger->debug("Unhandled raw packet from /$address:$port: " . base64_encode($packet));
214 }
215 }
216}
unregisterInterface(NetworkInterface $interface)
Definition Network.php:123
blockAddress(string $address, int $timeout=300)
Definition Network.php:161
registerRawPacketHandler(RawPacketHandler $handler)
Definition Network.php:178
unregisterRawPacketHandler(RawPacketHandler $handler)
Definition Network.php:190
registerInterface(NetworkInterface $interface)
Definition Network.php:98