-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtelnet_bbs_server.php
More file actions
265 lines (234 loc) · 8.73 KB
/
telnet_bbs_server.php
File metadata and controls
265 lines (234 loc) · 8.73 KB
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
<?php
/**
* PHP Telnet BBS Server
*
* This code is part of a Telnet Bulletin Board System (BBS) server implementation.
* It allows multiple clients to connect over Telnet, register, and log in to the BBS.
* Registered users can execute commands such as help and exit.
*
* This specific implementation is based on the GPL-licensed code written by Federico Saccà in 2024.
*
* @author Federico Saccà
* @license GPL (GNU General Public License)
* @link https://www.federicosacca.it
* @link https://www.gnu.org/licenses/gpl-3.0.html
*/
class Server
{
private $host;
private $port;
private $serverSocket;
private $clients = [];
private $pdo;
public function __construct($host, $port)
{
$this->host = $host;
$this->port = $port;
$this->initializeDatabase();
}
private function initializeDatabase()
{
$dbFile = 'database.sqlite';
$this->pdo = new PDO('sqlite:' . $dbFile);
$this->createUsersTable();
}
private function createUsersTable()
{
$sql = "CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
nickname VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL
)";
$this->pdo->exec($sql);
}
public function start()
{
$this->createServerSocket();
echo "Telnet BBS started at telnet://{$this->host}:{$this->port}\n";
while (true) {
$this->handleIncomingConnections();
$this->handleClientInteractions();
}
}
private function createServerSocket()
{
$this->serverSocket = stream_socket_server("tcp://{$this->host}:{$this->port}", $errno, $errstr);
if (!$this->serverSocket) {
die("Error: $errstr ($errno)\n");
}
}
private function handleIncomingConnections()
{
$read = $this->clients;
$read[] = $this->serverSocket;
$read = array_filter($read, 'is_resource');
if (false === ($numChangedStreams = stream_select($read, $write, $except, 0))) {
die("Error in stream_select()\n");
}
if ($numChangedStreams > 0) {
if (in_array($this->serverSocket, $read)) {
if (false !== ($newClient = @stream_socket_accept($this->serverSocket))) {
$client = new Client($newClient, $this->pdo);
$this->clients[] = $client;
echo "New client connected\n";
$this->sendBannerMessage($client);
unset($read[array_search($this->serverSocket, $read)]);
} else {
echo "Error accepting new client\n";
}
}
}
}
private function handleClientInteractions()
{
foreach ($this->clients as $key => $client) {
$socket = $client->getSocket();
if (is_resource($socket) && !feof($socket)) {
$input = rtrim(fgets($socket, 1024));
if ($client->isAuthenticated()) {
$this->handleAuthenticatedClientInput($client, $input);
} else {
$this->handleUnauthenticatedClientInput($client, $input);
}
// Client disconnected with "exit" or similar, so clean up the clients
if (!is_resource($client->getSocket())) {
unset($this->clients[$key]);
}
} else {
// Client disconnected, clean up
if (is_resource($socket)) {
fclose($socket);
}
unset($this->clients[$key]);
echo "Client disconnected\n";
}
}
}
private function handleAuthenticatedClientInput($client, $input)
{
switch ($input) {
case 'exit':
fclose($client->getSocket());
echo "Client disconnected\n";
break;
case 'help':
$client->sendMessage("Available commands: exit, help\n");
break;
default:
$client->sendMessage("Unknown command. Type 'help' for available commands.\n");
break;
}
}
private function handleUnauthenticatedClientInput($client, $input)
{
switch ($input) {
case 'register':
$client->register();
break;
case 'login':
$client->login();
break;
default:
$client->sendMessage("Invalid input. Please try again.\n");
break;
}
}
private function sendBannerMessage($client)
{
$bannerMessage = "
*********************************************************************
* *
* _____ _ _____ _ _ _____ _____ _____ *
* | _ | |_ ___ |_ _|___| |___ ___| |_ | __ | __ | __| *
* | __| | . | | | | -_| | | -_| _| | __ -| __ -|__ | *
* |__| |_|_| _| |_| |___|_|_|_|___|_| |_____|_____|_____| *
* |_| *
* *
*********************************************************************
* *
* Welcome to Php Telnet BBS Server! *
* *
* To register, type 'register'. *
* To login, type 'login'. *
* *
*********************************************************************
";
$client->sendMessage($bannerMessage);
}
}
class Client
{
private $socket;
private $authenticated = false;
private $pdo;
public function __construct($socket, $pdo)
{
$this->socket = $socket;
$this->pdo = $pdo;
}
public function getSocket()
{
return $this->socket;
}
public function isAuthenticated()
{
return $this->authenticated;
}
public function authenticate()
{
$this->authenticated = true;
}
public function sendMessage($message)
{
fwrite($this->socket, $message);
}
public function register()
{
$this->sendMessage("Enter nickname: ");
$nickname = rtrim(fgets($this->socket, 1024));
$this->sendMessage("Enter password: ");
$password = rtrim(fgets($this->socket, 1024));
if (!empty($nickname) && !empty($password)) {
$stmt = $this->pdo->prepare("SELECT * FROM users WHERE nickname = ?");
$stmt->execute([$nickname]);
$existingUser = $stmt->fetch();
if ($existingUser) {
$this->sendMessage("Nickname already exists. Please choose a different one.\n");
} else {
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
$stmt = $this->pdo->prepare("INSERT INTO users (nickname, password) VALUES (?, ?)");
$stmt->execute([$nickname, $hashedPassword]);
$this->sendMessage("Registration successful. You are now logged in.\n");
$this->sendMessage("Type 'help' for available commands.\n");
$this->authenticate(); // Automatically log in after registration
}
} else {
$this->sendMessage("Invalid input. Please try again.\n");
}
}
public function login()
{
$this->sendMessage("Enter nickname: ");
$nickname = rtrim(fgets($this->socket, 1024));
$this->sendMessage("Enter password: ");
$password = rtrim(fgets($this->socket, 1024));
if (!empty($nickname) && !empty($password)) {
$stmt = $this->pdo->prepare("SELECT * FROM users WHERE nickname = ?");
$stmt->execute([$nickname]);
$user = $stmt->fetch();
if ($user && password_verify($password, $user['password'])) {
$this->authenticate();
$this->sendMessage("Login successful. Welcome, $nickname!\n");
$this->sendMessage("Type 'help' for available commands.\n");
} else {
$this->sendMessage("Invalid nickname or password. Please try again.\n");
fclose($this->socket); // Close client connection
}
} else {
$this->sendMessage("Invalid input. Please try again.\n");
fclose($this->socket); // Close client connection
}
}
}
$server = new Server('0.0.0.0', 2324);
$server->start();