|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Bidirectional proxy from a unix domain socket to the request-replayer, used |
| 5 | + * by .phpt tests that exercise the tracer's unix socket agent transport. |
| 6 | + * |
| 7 | + * Launched by RequestReplayer::launchUnixProxy(), not meant to be run directly. |
| 8 | + * |
| 9 | + * Usage: php unix_socket_proxy.php <socket-path> [<host:port>] [<log-file>] |
| 10 | + * |
| 11 | + * Parent protocol: |
| 12 | + * - prints "1\n" on stdout once the socket accepts connections |
| 13 | + * - exits once stdin hits EOF, i.e. once the test process launching it is gone |
| 14 | + */ |
| 15 | + |
| 16 | +const CHUNK_SIZE = 65536; |
| 17 | +/* Stop reading from a socket while this much data is still queued for its peer. */ |
| 18 | +const MAX_BUFFERED = 4 * CHUNK_SIZE; |
| 19 | + |
| 20 | +if (!isset($argv[1])) { |
| 21 | + fwrite(STDERR, "usage: unix_socket_proxy.php <socket-path> [<host:port>] [<log-file>]\n"); |
| 22 | + exit(1); |
| 23 | +} |
| 24 | + |
| 25 | +$socketPath = $argv[1]; |
| 26 | +$upstreamAddr = isset($argv[2]) ? $argv[2] : 'request-replayer:80'; |
| 27 | +$logFile = isset($argv[3]) ? $argv[3] : '/tmp/unix-proxy-' . basename($socketPath); |
| 28 | + |
| 29 | +ignore_user_abort(true); |
| 30 | + |
| 31 | +@unlink($socketPath); |
| 32 | +$server = @stream_socket_server('unix://' . $socketPath, $errno, $errstr); |
| 33 | +if (!$server) { |
| 34 | + fwrite(STDERR, "unix_socket_proxy: cannot listen on $socketPath: $errstr ($errno)\n"); |
| 35 | + exit(1); |
| 36 | +} |
| 37 | +stream_set_blocking($server, false); |
| 38 | + |
| 39 | +echo "1\n"; /* ready marker, awaited by launchUnixProxy() */ |
| 40 | +flush(); |
| 41 | + |
| 42 | +/* id => ['client' => resource, 'upstream' => resource, 'out' => [side => string], ...] */ |
| 43 | +$conns = []; |
| 44 | +/* (int) resource => [id, side] */ |
| 45 | +$owner = []; |
| 46 | +$nextId = 0; |
| 47 | + |
| 48 | +function other_side($side) |
| 49 | +{ |
| 50 | + return $side === 'client' ? 'upstream' : 'client'; |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Tests grep the proxied bytes out of this file, so keep the format as is: |
| 55 | + * one chunk per write, each starting on a fresh line. |
| 56 | + */ |
| 57 | +function proxy_log($data) |
| 58 | +{ |
| 59 | + global $logFile; |
| 60 | + file_put_contents($logFile, $data . "\n", FILE_APPEND); |
| 61 | +} |
| 62 | + |
| 63 | +function close_pair($id) |
| 64 | +{ |
| 65 | + global $conns, $owner; |
| 66 | + foreach (['client', 'upstream'] as $side) { |
| 67 | + $stream = $conns[$id][$side]; |
| 68 | + unset($owner[(int) $stream]); |
| 69 | + @fclose($stream); |
| 70 | + } |
| 71 | + unset($conns[$id]); |
| 72 | +} |
| 73 | + |
| 74 | +function accept_pending() |
| 75 | +{ |
| 76 | + global $server, $upstreamAddr, $conns, $owner, $nextId; |
| 77 | + |
| 78 | + while (($client = @stream_socket_accept($server, 0)) !== false) { |
| 79 | + $upstream = @stream_socket_client('tcp://' . $upstreamAddr, $errno, $errstr, 5); |
| 80 | + if (!$upstream) { |
| 81 | + proxy_log("upstream connect to $upstreamAddr failed: $errstr ($errno)"); |
| 82 | + fclose($client); |
| 83 | + continue; |
| 84 | + } |
| 85 | + stream_set_blocking($client, false); |
| 86 | + stream_set_blocking($upstream, false); |
| 87 | + |
| 88 | + $id = $nextId++; |
| 89 | + $conns[$id] = [ |
| 90 | + 'client' => $client, |
| 91 | + 'upstream' => $upstream, |
| 92 | + 'out' => ['client' => '', 'upstream' => ''], |
| 93 | + 'eof' => ['client' => false, 'upstream' => false], |
| 94 | + 'shutdown' => ['client' => false, 'upstream' => false], |
| 95 | + ]; |
| 96 | + $owner[(int) $client] = [$id, 'client']; |
| 97 | + $owner[(int) $upstream] = [$id, 'upstream']; |
| 98 | + proxy_log('connected'); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +while (true) { |
| 103 | + $read = [STDIN, $server]; |
| 104 | + $write = []; |
| 105 | + foreach ($conns as $id => $conn) { |
| 106 | + foreach (['client', 'upstream'] as $side) { |
| 107 | + if (!$conn['eof'][$side] && strlen($conn['out'][other_side($side)]) < MAX_BUFFERED) { |
| 108 | + $read[] = $conn[$side]; |
| 109 | + } |
| 110 | + if ($conn['out'][$side] !== '') { |
| 111 | + $write[] = $conn[$side]; |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + $except = null; |
| 117 | + if (@stream_select($read, $write, $except, null) === false) { |
| 118 | + break; /* interrupted by a signal */ |
| 119 | + } |
| 120 | + |
| 121 | + foreach ($read as $stream) { |
| 122 | + if ($stream === STDIN) { |
| 123 | + /* The test process that launched us is gone. */ |
| 124 | + @unlink($socketPath); |
| 125 | + exit(0); |
| 126 | + } |
| 127 | + |
| 128 | + if ($stream === $server) { |
| 129 | + accept_pending(); |
| 130 | + continue; |
| 131 | + } |
| 132 | + |
| 133 | + if (!isset($owner[(int) $stream])) { |
| 134 | + continue; /* closed while handling an earlier stream in this batch */ |
| 135 | + } |
| 136 | + list($id, $side) = $owner[(int) $stream]; |
| 137 | + |
| 138 | + $data = @fread($stream, CHUNK_SIZE); |
| 139 | + if ($data === false || $data === '') { |
| 140 | + $conns[$id]['eof'][$side] = true; |
| 141 | + proxy_log('end'); |
| 142 | + } else { |
| 143 | + proxy_log($data); |
| 144 | + $conns[$id]['out'][other_side($side)] .= $data; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + foreach ($conns as $id => $conn) { |
| 149 | + foreach (['client', 'upstream'] as $side) { |
| 150 | + if ($conns[$id]['out'][$side] === '') { |
| 151 | + continue; |
| 152 | + } |
| 153 | + /* Sockets are non-blocking: a short write is normal, keep the rest queued. */ |
| 154 | + $written = @fwrite($conns[$id][$side], $conns[$id]['out'][$side]); |
| 155 | + if ($written === false) { |
| 156 | + $conns[$id]['out'][$side] = ''; |
| 157 | + $conns[$id]['eof'][$side] = true; |
| 158 | + continue; |
| 159 | + } |
| 160 | + $conns[$id]['out'][$side] = substr($conns[$id]['out'][$side], $written); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + foreach ($conns as $id => $conn) { |
| 165 | + /* |
| 166 | + * Forward each half-close once everything read before it has been |
| 167 | + * handed over. Responses delimited by connection close depend on this. |
| 168 | + */ |
| 169 | + foreach (['client', 'upstream'] as $side) { |
| 170 | + $peer = other_side($side); |
| 171 | + if ($conns[$id]['eof'][$side] |
| 172 | + && $conns[$id]['out'][$peer] === '' |
| 173 | + && !$conns[$id]['shutdown'][$peer] |
| 174 | + ) { |
| 175 | + @stream_socket_shutdown($conns[$id][$peer], STREAM_SHUT_WR); |
| 176 | + $conns[$id]['shutdown'][$peer] = true; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + if ($conns[$id]['eof']['client'] && $conns[$id]['eof']['upstream'] |
| 181 | + && $conns[$id]['out']['client'] === '' && $conns[$id]['out']['upstream'] === '' |
| 182 | + ) { |
| 183 | + close_pair($id); |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments