This repository has been archived by the owner on Jan 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 166
/
socket_console.php
executable file
·184 lines (147 loc) · 4.79 KB
/
socket_console.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
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
#!/usr/bin/php
<?php
/**
* A simple console that lets you communicate and test your Core_Lib_Server enabled application
*
* User: Shane Harter
* Date: 11/12/12
*/
echo PHP_EOL, "PHP Simple Daemon - Socket Console";
echo PHP_EOL, "Enter ip:port to Connect";
echo PHP_EOL;
stream_set_blocking(STDIN, 0);
$socket = false;
$input = '';
$macro_input = '';
$prompt = true;
$flash = '';
while(true) {
// Every few iterations verify that the socket connection is still open
if ($socket && ($input || mt_rand(1,3) == 2) && !is_resource($socket)) {
out("Socket Connection Terminated");
$prompt = true;
$socket = null;
if (!$input)
echo PHP_EOL;
}
if ($input || $prompt)
if (!$socket)
echo "CONNECT > ";
else
echo "SEND > ";
$prompt = false;
if ($macro_input) {
$input = $macro_input;
$macro_input = '';
} else {
$input = fgets(STDIN);
}
$prompt = $input == "\n";
$input = trim($input);
try {
switch(true) {
case empty($input):
continue;
case input() == 'exit':
exit;
case $socket && input() == 'help':
$out = array();
$out[] = '';
$out[] = 'Available Commands:';
$out[] = 'exit';
$out[] = 'help Display this help';
$out[] = '[string] Any provided string will be sent to the server. ';
$out[] = '';
// $out[] = 'Note:';
// $out[] = 'To send any shell commands (eg "exit" or "help") as a command to the server, escape them with a leading slash, eg /help';
// $out[] = '';
out(implode(PHP_EOL, $out));
continue;
case input() == 'help':
$out = array();
$out[] = '';
$out[] = 'Available Commands:';
$out[] = 'exit';
$out[] = 'help Display this help';
$out[] = 'ip:port Connect to the socket server at the provided ip address and port';
$out[] = '';
out(implode(PHP_EOL, $out));
continue;
case $socket && is_string($input):
out("Sending Command...");
fputs($socket, $input, strlen($input));
out("\n");
out("Response:");
$out = '';
while ($_out = fgets($socket, 2048))
$out .= $_out;
out($out);
$out = '';
continue;
case !$socket && strpos($input, ':'):
$conn = explode(':', $input);
out("Connecting to {$conn[0]}:{$conn[1]}");
$errno = $errstr = null;
$socket = fsockopen($conn[0], $conn[1], $errno, $errstr, 60);
if (!$socket) {
out("Connection Error [{$errno}] $errstr");
}
continue;
case $socket:
throw new Exception("Invalid Command");
default:
throw new Exception('Invalid Connection String: Valid ip:port required. Example: 127.0.0.1:8000');
}
}
catch(Exception $e)
{
echo $e->getMessage(), PHP_EOL, PHP_EOL;
}
usleep(20 * 1000);
}
function out($out)
{
global $flash;
if ($flash) {
echo $flash, PHP_EOL;
$flash = '';
}
echo $out, PHP_EOL;
}
function macro($id) {
global $input, $macro_input, $prompt, $address;
if ($macro_input)
$macro_input = '';
switch($id){
case 1:
// Macro 1 reads an optional PID from a pid-reset command.
// Example: SIG> pid 12345 The current pid will be released and this macro will pull 12345 out and set
// it as the new pid.
$arg = @func_get_arg(1);
if (!$arg || strlen(trim($arg)) < 4)
return;
$macro_input = trim(str_replace('pid', '', $arg));
break;
}
if ($macro_input)
$input = '';
}
function input($in = null, Array $commands = array('pid', 'help', 'exit')) {
global $input, $flash;
if (empty($in))
$in = $input;
if (empty($in))
return null;
$matches = array();
foreach ($commands as $command)
if ($in == substr($command, 0, strlen($in)))
$matches[] = $command;
if (count($matches) > 1)
out("Ambiguous Command. Matches: " . implode(', ', $matches));
if (count($matches)) {
if ($in != $matches[0])
$flash = $matches[0];
return $matches[0];
}
return null;
}