Skip to content

Commit

Permalink
add ip
Browse files Browse the repository at this point in the history
  • Loading branch information
amirfaramarzi committed Jul 5, 2021
1 parent ba63a60 commit 963f202
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public function onMessage(TcpConnection $connection , Request $request): void
$dispatch = $this->dispatcher->dispatch($request->method() , $request->path());

if ($dispatch[0] === 1){
Request::setRemoteAddress($connection->getRemoteAddress());
Request::setLocalAddress($connection->getLocalAddress());
$this->request = $request;
if (! empty($dispatch[1]['middleware'])){
Request::setVariables($dispatch[2]);
Expand Down
47 changes: 47 additions & 0 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
class Request extends WorkerRequest
{
protected static array $variables;
protected static string $localAddress;
protected static string $remoteAddress;

public static function setLocalAddress(string $localAddress): void
{
self::$localAddress = $localAddress;
}

public static function setRemoteAddress(string $remoteAddress): void
{
self::$remoteAddress = $remoteAddress;
}

public static function setVariables(array $variables): void
{
Expand Down Expand Up @@ -82,4 +94,39 @@ public function fullUrl(): string

return $protocol . $this->host() . $this->uri();
}

public function remoteIp(): string
{
$ip = explode(self::$remoteAddress , ':');

return $ip[0];
}

public function remotePort(): string
{
$ip = explode(self::$remoteAddress , ':');

return $ip[1];
}

public function localIp(): string
{
$ip = explode(self::$localAddress , ':');

return $ip[0];
}

public function localPort(): string
{
$ip = explode(self::$localAddress , ':');

return $ip[1];
}

public function realIp(): ?string
{
return $this->header('client-ip', $this->header('x-forwarded-for',
$this->header('x-real-ip', $this->header('x-client-ip',
$this->header('via', $this->remoteIp())))));
}
}

0 comments on commit 963f202

Please sign in to comment.