Last active
January 25, 2020 20:36
-
-
Save skorotkiewicz/08176c323c236302629e7ab9eb47d8ba to your computer and use it in GitHub Desktop.
PHP Telnet Server with commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
set_time_limit(0); | |
$socketMain = socket_create(AF_INET, SOCK_STREAM, 0) or die(); | |
socket_set_option($socketMain, SOL_SOCKET, SO_REUSEADDR, 1) or die(); | |
socket_bind($socketMain, '127.0.0.1', '1234') or die(); | |
socket_listen($socketMain, 5) or die(); | |
$clients = array(); | |
while(true){ | |
$read = array(); | |
$read[0] = $socketMain; | |
foreach($clients as $x => $client){ | |
if($client->closed == true){ | |
unset($clients[$x]); | |
}else{ | |
$read[] = $client->socket; | |
} | |
} | |
socket_select($read, $null = null, $null = null, 5); | |
if(in_array($socketMain, $read)){ | |
$sckNewClient = socket_accept($socketMain); | |
$clients[] = new clientHandler($sckNewClient); | |
} | |
foreach($clients as $key => $client){ | |
if(in_array($client->socket, $read)){ | |
$client->handle($key); | |
} | |
} | |
} | |
socket_close($socketMain); | |
class clientHandler{ | |
public $closed = false; | |
public $socket = false; | |
public $cmd = false; | |
public $command = false; | |
public $parameter = false; | |
public function clientHandler($socket){ | |
$this->socket = $socket; | |
$this->send(" | |
┌───────────────────────────────────────────────────────────────────────┐ | |
│ Welcome on our Telnet service │ | |
└───────────────────────────────────────────────────────────────────────┘ | |
To show available commands type 'help'. | |
"); | |
} | |
public function handle($key){ | |
$cmd = $this->read(); | |
echo "Client: #$key: $cmd\n"; | |
if($cmd){ | |
@list($command, $parameter) = @explode(' ', $cmd, 2); | |
} | |
if(isset($command) && $command){ | |
switch($command){ | |
case 'echo': | |
$this->send($parameter);; | |
break; | |
case 'time': | |
$this->send(date('Y-m-d H:i:s')); | |
break; | |
case 'dice': | |
if ( is_numeric($parameter) && $parameter > 1 ) | |
$this->send('Roll with ' . $parameter . ' dices: ' . rand( 1*$parameter, 6*$parameter ) ); | |
else | |
$this->send('Roll with one dice: ' . rand(1,6) ); | |
break; | |
case 'hnews': | |
if ( is_numeric($parameter) && $parameter >= 1 ) | |
$this->hnews($parameter); | |
else | |
$this->hnews(3); | |
break; | |
case 'ping': | |
$this->send('Pong'); | |
break; | |
case 'help': | |
$this->helpMessage(); | |
break; | |
case 'clear': | |
$this->send(chr(27).chr(91).'H'.chr(27).chr(91).'J'); | |
break; | |
case 'quit': | |
$this->disconnect(); | |
break; | |
default: | |
$this->send('unknown command'); | |
} | |
} | |
} | |
public function helpMessage(){ | |
$this->send(" | |
┌─────────────┬─────────────────────────────────────────────────────────┐ | |
│ Command │ Description │ | |
├─────────────┼─────────────────────────────────────────────────────────┤ | |
│ quit │ Close the connection │ | |
├─────────────┼─────────────────────────────────────────────────────────┤ | |
│ time │ Date time │ | |
├─────────────┼─────────────────────────────────────────────────────────┤ | |
│ echo │ Resend your message │ | |
├─────────────┼─────────────────────────────────────────────────────────┤ | |
│ ping │ Pong │ | |
├─────────────┼─────────────────────────────────────────────────────────┤ | |
│ dice 1 │ Roll 1 dice, or multiple │ | |
├─────────────┼─────────────────────────────────────────────────────────┤ | |
│ hnews 10 │ Get latest 10 news from Hacker News (max. 500) │ | |
├─────────────┼─────────────────────────────────────────────────────────┤ | |
│ help │ Help commands list │ | |
└─────────────┴─────────────────────────────────────────────────────────┘ | |
"); | |
} | |
public function hnews($limit){ | |
$latest = file_get_contents("https://hacker-news.firebaseio.com/v0/topstories.json"); | |
$storys = json_decode($latest); | |
$i = 0; | |
foreach ($storys as $story) { | |
$news = file_get_contents("https://hacker-news.firebaseio.com/v0/item/$story.json"); | |
$story = json_decode($news, true); | |
@socket_write($this->socket, $story['title'] . "\n" . $story['url'] . "\n\n" ); | |
if (++$i == $limit) break; | |
} | |
$this->send(''); | |
} | |
public function send($sMessage){ | |
@socket_write($this->socket, $sMessage . "\nCommand: " ); | |
} | |
public function read(){ | |
return trim(@socket_read($this->socket, 1024)); | |
} | |
public function disconnect(){ | |
$this->send('bye'); | |
$this->closed = true; | |
socket_shutdown($this->socket); | |
socket_close($this->socket); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment