Repository navigation
Expand file tree
/
Copy pathhello-server.php
More file actions
57 lines (51 loc) · 1.87 KB
/
Copy pathhello-server.php
File metadata and controls
57 lines (51 loc) · 1.87 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
<?php
/**
* Minimal SugarSshServer server. Wire this up via sshd's ForceCommand
* and any user logging in sees a tiny session banner. Useful as
* the smallest possible end-to-end smoke test of the deployment.
*
* /etc/ssh/sshd_config.d/wish.conf:
*
* Match User wishuser
* ForceCommand /usr/bin/php /path/to/this/file.php
* PermitTTY yes
*/
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use SugarCraft\SshServer\Context;
use SugarCraft\SshServer\Middleware;
use SugarCraft\SshServer\Middleware\Logger;
use SugarCraft\SshServer\Server;
use SugarCraft\SshServer\Session;
use SugarCraft\SshServer\Transport\HostSshdTransport;
final class Banner implements Middleware
{
public function handle(Context $ctx, Session $s, callable $next): void
{
$cols = max(40, $s->cols);
$line = str_repeat('─', $cols);
echo "\n";
echo " Hello, {$s->user}.\n";
echo " You connected from {$s->clientHost}:{$s->clientPort}.\n";
echo " Your terminal is {$s->term} ({$s->cols}×{$s->rows}).\n";
echo " Press any key to disconnect.\n";
echo " {$line}\n";
// Wait for one byte from stdin so the user actually sees the
// banner before sshd tears down the session.
if ($s->isInteractive()) {
fread(STDIN, 1);
}
$next($ctx, $s);
}
}
// HostSshdTransport pinned explicitly because Banner reads STDIN
// directly via fread(STDIN, 1). Under the new InProcessTransport
// default, that STDIN is the supervisor's input pipe (already
// being pumped into a sugar-pty master), so an inline read would
// race with the pump. HostSshd keeps STDIN/STDOUT attached to
// sshd's PTY, the way pre-PTY-upgrade sugar-ssh-server always was.
exit(Server::new()
->withTransport(new HostSshdTransport())
->use(new Logger())
->use(new Banner())
->serve());