Completed
Push — master ( 8561f8...0f383b )
by Vasily
03:40
created

TelnetHoneypotConnection::onFinish()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace PHPDaemon\Examples;
3
4
/**
5
 * @package    NetworkServers
6
 * @subpackage TelnetHoneypot
7
 *
8
 * @author     Vasily Zorin <[email protected]>
9
 */
10
class TelnetHoneypot extends \PHPDaemon\Network\Server {
11
	public $connectionClass = '\PHPDaemon\Examples\TelnetHoneypotConnection';
12
	
13
	/**
14
	 * Setting default config options
15
	 * Overriden from ConnectionPool::getConfigDefaults
16
	 * @return array|false
17
	 */
18
	protected function getConfigDefaults() {
19
		return [
20
			// @todo add description strings
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
21
			'listen' => '0.0.0.0',
22
			'port'   => 23,
23
		];
24
	}
25
}
26
27
class TelnetHoneypotConnection extends \PHPDaemon\Network\Connection {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
28
	/**
29
	 * Called when new data received.
30
	 * @return void
31
	 */
32
	public function onRead() {
33
		while (!is_null($line = $this->readline())) {
34
			$finish =
35
				(strpos($line, $s = "\xff\xf4\xff\xfd\x06") !== FALSE)
36
					|| (strpos($line, $s = "\xff\xec") !== FALSE)
37
					|| (strpos($line, $s = "\x03") !== FALSE)
38
					|| (strpos($line, $s = "\x04") !== FALSE);
39
40
			$e   = explode(' ', rtrim($line, "\r\n"), 2);
41
42
			$cmd = trim($e[0]);
43
44
			if ($cmd === 'ping') {
45
				$this->writeln('pong');
46
			}
47
			elseif (
48
					($cmd === 'exit')
49
					|| ($cmd === 'quit')
50
			) {
51
				$this->writeln('Quit');
52
				$this->finish();
53
			}
54
			else {
55
				$this->writeln('Unknown command "' . $cmd . '"');
56
			}
57
58
			if (
59
				(strlen($line) > 1024)
60
				|| $finish
61
			) {
62
				$this->finish();
63
			}
64
		}
65
	}
66
67
	/**
68
	 * Called when the session finished
69
	 * @return void
70
	 */
71
	public function onFinish() {
72
		$this->writeln('Bye!');
73
	}
74
}
75