-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathstart.php
155 lines (137 loc) · 4.32 KB
/
start.php
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
<?php
/**
* This file is part of workerman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
use \Workerman\Worker;
use Workerman\Protocols\Http\Request;
use Workerman\Protocols\Http\Response;
use Workerman\Connection\TcpConnection;
// Command. For example 'tail -f /var/log/nginx/access.log'.
define('CMD', 'htop');
// Whether to allow client input.
define('ALLOW_CLIENT_INPUT', true);
// Uinix user for command. Recommend nobody www etc.
define('USER', 'www-data');
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker("Websocket://0.0.0.0:7778");
$worker->name = 'phptty';
$worker->user = USER;
$worker->onConnect = function($connection)
{
//To do this, PHP_CAN_DO_PTS must be enabled. See ext/standard/proc_open.c in PHP directory.
/*$descriptorspec = array(
0 => array('pty'),
1 => array('pty'),
2 => array('pty')
);*/
//Pipe can not do PTY. Thus, many features of PTY can not be used.
//e.g. sudo, w3m, luit, all C programs using termios.h, etc.
$descriptorspec = array(
0=>array("pipe", "r"),
1=>array("pipe", "w"),
2=>array("pipe", "w")
);
unset($_SERVER['argv']);
$env = array_merge(
array('COLUMNS'=>130, 'LINES'=> 50), $_SERVER
);
$connection->process = proc_open(CMD, $descriptorspec, $pipes, null, $env);
$connection->pipes = $pipes;
stream_set_blocking($pipes[0], 0);
$connection->process_stdout = new TcpConnection($pipes[1]);
$connection->process_stdout->onMessage = function($process_connection, $data)use($connection)
{
$connection->send($data);
};
$connection->process_stdout->onClose = function($process_connection)use($connection)
{
$connection->close(); //Close WebSocket connection on process exit.
};
$connection->process_stdin = new TcpConnection($pipes[2]);
$connection->process_stdin->onMessage = function($process_connection, $data)use($connection)
{
$connection->send($data);
};
};
$worker->onMessage = function($connection, $data)
{
if(ALLOW_CLIENT_INPUT)
{
fwrite($connection->pipes[0], $data);
}
};
$worker->onClose = function($connection)
{
$connection->process_stdin->close();
$connection->process_stdout->close();
fclose($connection->pipes[0]);
$connection->pipes = null;
proc_terminate($connection->process);
proc_close($connection->process);
$connection->process = null;
};
$worker->onWorkerStop = function($worker)
{
foreach($worker->connections as $connection)
{
$connection->close();
}
};
$web = new Worker('http://0.0.0.0:7779');
$web->name = 'web';
define('WEBROOT', __DIR__ . '/Web');
$web->onMessage = function (TcpConnection $connection, Request $request) {
$path = $request->path();
if ($path === '/') {
$connection->send(exec_php_file(WEBROOT.'/index.html'));
return;
}
$file = realpath(WEBROOT. $path);
if (false === $file) {
$connection->send(new Response(404, array(), '<h3>404 Not Found</h3>'));
return;
}
// Security check! Very important!!!
if (strpos($file, WEBROOT) !== 0) {
$connection->send(new Response(400));
return;
}
if (\pathinfo($file, PATHINFO_EXTENSION) === 'php') {
$connection->send(exec_php_file($file));
return;
}
$if_modified_since = $request->header('if-modified-since');
if (!empty($if_modified_since)) {
// Check 304.
$info = \stat($file);
$modified_time = $info ? \date('D, d M Y H:i:s', $info['mtime']) . ' ' . \date_default_timezone_get() : '';
if ($modified_time === $if_modified_since) {
$connection->send(new Response(304));
return;
}
}
$connection->send((new Response())->withFile($file));
};
function exec_php_file($file) {
\ob_start();
// Try to include php file.
try {
include $file;
} catch (\Exception $e) {
echo $e;
}
return \ob_get_clean();
}
if(!defined('GLOBAL_START'))
{
Worker::runAll();
}