-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcproc.php
88 lines (88 loc) · 1.91 KB
/
tcproc.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
<?php
if(empty($argv[2]))
{
die("Syntax: tcproc <port> <executable> [executable arguments ...]\n");
}
if(defined("PHP_WINDOWS_VERSION_MAJOR"))
{
echo "Oh God, you're on Windows... There's a good chance this won't work.\r\n";
}
$cmd = $argv[2];
if(!empty($argv[3]))
{
for($i = 3; $i < count($argv); $i++)
{
$cmd .= " ".$argv[$i];
}
}
$server = stream_socket_server("tcp://0.0.0.0:".$argv[1], $errno, $errstr) or die($errstr."\n");
stream_set_blocking($server, false);
$clients = [];
$null = null;
do
{
$start = microtime(true);
while(($client = @stream_socket_accept($server, 0)) !== false)
{
stream_set_blocking($client, false);
$proc = proc_open($cmd, [
["pipe", "r"],
["pipe", "w"],
["pipe", "w"]
], $pipes);
if(is_resource($proc))
{
for($i = 0; $i <= 2; $i++)
{
stream_set_blocking($pipes[$i], false);
}
array_push($clients, [
"sock" => $client,
"proc" => $proc,
"pipes" => $pipes
]);
}
else
{
fwrite($client, "tcproc: failed to start process\n");
fclose($client);
}
}
foreach($clients as $i => $client)
{
if(proc_get_status($client["proc"])["running"] !== true)
{
fclose($client["sock"]);
unset($clients[$i]);
}
else if(@feof($client["sock"]) !== false)
{
proc_close($client["proc"]);
unset($clients[$i]);
}
else
{
$read = [$client["sock"], $client["pipes"][1], $client["pipes"][2]];
if(stream_select($read, $null, $null, 0) > 0)
{
if(in_array($client["sock"], $read))
{
fwrite($client["pipes"][0], fread($client["sock"], 1024));
}
else if(in_array($client["pipes"][1], $read))
{
fwrite($client["sock"], fread($client["pipes"][1], 1024));
}
else if(in_array($client["pipes"][2], $read))
{
fwrite($client["sock"], fread($client["pipes"][2], 1024));
}
}
}
}
if(($remaining = (0.050 - (microtime(true) - $start))) > 0)
{
time_nanosleep(0, $remaining * 1000000000);
}
}
while(true);