-
Notifications
You must be signed in to change notification settings - Fork 1
/
protoscope.php
executable file
·173 lines (139 loc) · 5.95 KB
/
protoscope.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
#!/usr/bin/env php
<?php
$protoscope = new Protoscope;
$protoscope->run();
class Protoscope {
const VERSION = '0.7.9';
// Set default config options.
protected $config = array('ip' => '127.0.0.1',
'port' => '4887',
'embed' => TRUE);
public function log($message)
{
$now = date('Y-m-d H:i:s');
echo "[{$now}] {$message}";
}
protected function request($url, $request)
{
$this->log(" request([{$url}], ...)\n");
// Parse URL.
$url = parse_url($url);
switch ($url['scheme']) {
case 'http':
$port = 80;
break;
case 'https':
$port = 443;
break;
default:
$this->log(" URL has no scheme. Assuming port [{$url['port']}].\n");
$port = $url['port'];
if (substr($request, 0, 7) == 'CONNECT') {
$this->log(" CONNECT not yet supported. Responding with 405. \n");
$this->log(" For more info: https://github.com/shiflett/protoscope/issues/2\n");
return "HTTP/1.1 405 Method Not Allowed\r\n\r\n";
}
break;
}
// Open a connection to the server.
$this->log(" fsockopen([{$url['host']}][{$port}])\n");
if (!$stream = fsockopen($url['host'], $port, $error, $message)) {
$this->log("[{$error}][{$message}]\n");
return;
}
// Write the request.
for ($place = 0; $place < strlen($request); $place += $written) {
$written = fwrite($stream, substr($request, $place));
}
$this->log(" [{$place}] bytes written.\n");
// Return the response.
return stream_get_contents($stream);
}
public function run()
{
// Set default timezone if it's not set in php.ini.
if (!ini_get('date.timezone')) {
date_default_timezone_set('America/New_York');
}
set_time_limit(0);
$this->log("Protoscope [{$this->config['ip']}:{$this->config['port']}]\n");
$client = array();
$server = array();
$socket = stream_socket_server("{$this->config['ip']}:{$this->config['port']}", $error, $message);
if (!$socket) {
$this->log("[{$error}] [{$message}]\n");
} else {
// Accept connections indefinitely.
while ($stream = stream_socket_accept($socket, -1)) {
$this->log("--\n");
// Disable blocking, so data can be inspected as it is received.
stream_set_blocking($stream, 0);
$client['headers'] = array();
// Read headers.
$this->log("Headers\n");
while ($line = stream_get_line($stream, 8192, "\n")) {
$line = trim($line);
if (!empty($line)) {
$this->log(" [{$line}]\n");
$client['headers'][] = $line;
}
}
// Read content, if any.
$client['content'] = stream_get_contents($stream);
if (!empty($client['content'])) {
$this->log("Content\n");
$this->log(" [{$client['content']}]\n");
}
$server['headers'] = array();
foreach ($client['headers'] as $key => $header) {
// Skip the request line.
if ($key) {
list ($name, $value) = explode(': ', $header);
// Use this to omit or modify headers to be sent to the server.
switch (strtolower($name)) {
case 'accept-encoding':
case 'keep-alive':
case 'pragma':
break;
case 'connection':
case 'proxy-connection':
$server['headers'][] = "Connection: close";
break;
default:
$server['headers'][] = "{$name}: {$value}";
break;
}
}
}
// Parse request line.
list($server['method'], $server['url'], $server['protocol']) = explode(' ', $client['headers'][0]);
// Build request.
$server['request'] = $client['headers'][0] . "\r\n" . implode("\r\n", $server['headers']);
// Add content.
$server['request'] .= "\r\n\r\n" . $client['content'];
// Send request to server.
$this->log("Request\n");
$this->log(" [{$client['headers'][0]}]\n");
$server['response'] = $this->request($server['url'], $server['request']);
// FIXME: Only embed in text/html responses.
// FIXME: Handle chunked encoding. This will be ignored.
if ($this->config['embed']) {
$server['response'] .= '<div id="protoscope"><pre style="text-align: left; padding: 10px; border-top: #ccc solid 1px; background: #eee">';
foreach ($client['headers'] as $header) {
$server['response'] .= "{$header}\n";
}
$server['response'] .= '</pre></div>';
}
// Send response to client.
$this->log("Response\n");
$this->log(' [' . strlen($server['response']) . " bytes]\n");
fwrite($stream, $server['response']);
$client['request'] = '';
fclose($stream);
$this->log("--\n");
}
fclose($socket);
}
}
}
?>