Repository navigation
Expand file tree
/
Copy pathspawn-bash.php
More file actions
51 lines (46 loc) · 1.67 KB
/
Copy pathspawn-bash.php
File metadata and controls
51 lines (46 loc) · 1.67 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
<?php
/**
* Default-mode (in-process supervisor) sugar-ssh-server example.
*
* Spawns a fresh login bash inside a sugar-pty for each connecting
* SSH user. Ctrl+C, vim, less, tmux all work properly because the
* inner shell has its own controlling terminal (set by sugar-pty's
* pty-shim).
*
* Wire via sshd's ForceCommand exactly like the pre-PTY-upgrade
* deployment — the difference is purely internal to the PHP process:
*
* /etc/ssh/sshd_config.d/wish.conf:
*
* Match User wishuser
* ForceCommand /usr/bin/php /path/to/this/file.php
* PermitTTY yes
*
* Compared to bare ForceCommand-to-bash, this version gets you:
* - middleware composition (Logger, Auth, RateLimit, custom)
* - ability to swap the inner cmd per-user via the Spawn factory
* - clean process isolation (supervisor crash doesn't kill the shell;
* shell exit cleanly tears down the supervisor)
*/
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use SugarCraft\SshServer\Middleware\Logger;
use SugarCraft\SshServer\Middleware\Spawn;
use SugarCraft\SshServer\Server;
use SugarCraft\SshServer\Session;
exit(Server::new()
// InProcessTransport is the default in PR1+ — explicit
// ->withTransport() is unnecessary but documents the choice.
->use(new Logger())
->use(new Spawn(fn (Session $s) => [
'cmd' => ['/bin/bash', '-l'],
'env' => [
'TERM' => $s->term,
'USER' => $s->user,
'LOGNAME' => $s->user,
'HOME' => "/home/{$s->user}",
'PATH' => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
'LANG' => $s->lang,
],
]))
->serve());