|
| 1 | +<?php |
| 2 | + |
| 3 | +class RtspClient |
| 4 | +{ |
| 5 | + protected $addr; |
| 6 | + protected $port; |
| 7 | + protected $user; |
| 8 | + protected $pass; |
| 9 | + |
| 10 | + protected $cseq = 1; |
| 11 | + protected $auth = []; |
| 12 | + |
| 13 | + protected $rawResponse; |
| 14 | + |
| 15 | + protected $socket; |
| 16 | + |
| 17 | + function __construct(string $addr, int $port, string $user = null, string $pass = null) |
| 18 | + { |
| 19 | + $this->addr = $addr; |
| 20 | + $this->port = $port; |
| 21 | + $this->user = $user; |
| 22 | + $this->pass = $pass; |
| 23 | + |
| 24 | + $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); |
| 25 | + } |
| 26 | + |
| 27 | + public function connect(): bool |
| 28 | + { |
| 29 | + return socket_connect($this->socket, $this->addr, $this->port); |
| 30 | + } |
| 31 | + |
| 32 | + public function disconnect(): void |
| 33 | + { |
| 34 | + socket_shutdown($this->socket); |
| 35 | + socket_close($this->socket); |
| 36 | + } |
| 37 | + |
| 38 | + public function getSocketError(): int |
| 39 | + { |
| 40 | + return socket_last_error($this->socket); |
| 41 | + } |
| 42 | + |
| 43 | + public function getRawResponse(): ?string |
| 44 | + { |
| 45 | + return $this->rawResponse; |
| 46 | + } |
| 47 | + |
| 48 | + protected function digest(string $user, string $realm, string $password, string $nonce, string $method, string $uri): string |
| 49 | + { |
| 50 | + $ha1 = hash("md5", $user.":".$realm.":".$password); |
| 51 | + $ha2 = hash("md5", $method.":".$uri); |
| 52 | + $result = hash("md5", $ha1.":".$nonce.":".$ha2); |
| 53 | + |
| 54 | + return $result; |
| 55 | + } |
| 56 | + |
| 57 | + public function request(string $method, string $uri, array $options = []): array |
| 58 | + { |
| 59 | + if (count($this->auth) > 0) { |
| 60 | + $options['Authorization'] = $this->auth['authtype'].' username="'.$this->user.'",realm="'.$this->auth['realm'].'",nonce="'.$this->auth['nonce'].'",uri="'.$this->auth['uri'].'",response="'.$this->digest($this->user, $this->auth['realm'], $this->pass, $this->auth['nonce'], $method, $this->auth['uri']).'"'; |
| 61 | + } |
| 62 | + |
| 63 | + $request = $method." ".$uri." RTSP/1.0\r\n"; |
| 64 | + $request .= "CSeq: ".$this->cseq++."\r\n"; |
| 65 | + foreach ($options as $k => $v) { |
| 66 | + $request .= $k.": ".$v."\r\n"; |
| 67 | + } |
| 68 | + $request .= "\r\n"; |
| 69 | + socket_write($this->socket, $request); |
| 70 | + |
| 71 | + $this->rawResponse = ''; |
| 72 | + while ($r = socket_read($this->socket, 4096)) { |
| 73 | + $this->rawResponse .= $r; |
| 74 | + if (strpos($this->rawResponse, "\r\n\r\n")) { break; } |
| 75 | + } |
| 76 | + |
| 77 | + $headers = $this->interpretResponseHeaders($this->rawResponse); |
| 78 | + |
| 79 | + $body = ''; |
| 80 | + if (array_key_exists('Content-Length', $headers)) { |
| 81 | + $body = socket_read($this->socket, $headers['Content-Length']); |
| 82 | + $this->rawResponse .= $body; |
| 83 | + } |
| 84 | + |
| 85 | + if (isset($headers['WWW-Authenticate'])) { |
| 86 | + // Did we already try to authenticate or do we not have credentials set? |
| 87 | + if (!$this->user || !$this->pass || isset($options['Authorization'])) { |
| 88 | + throw new RuntimeException('RTSP authorization failed'); |
| 89 | + } |
| 90 | + |
| 91 | + $wwwauth = preg_split('/(?<!,) /', $response['WWW-Authenticate']); |
| 92 | + if (count($wwwauth) != 2) { |
| 93 | + throw new RuntimeException("Invalid WWW-Authenticate string: {$response['WWW-Authenticate']}"); |
| 94 | + } |
| 95 | + |
| 96 | + $authtype = $wwwauth[0]; |
| 97 | + if ($authtype != 'Digest') { |
| 98 | + throw new RuntimeException("Unsupported auth method: {$authtype}"); |
| 99 | + } |
| 100 | + |
| 101 | + $authdata = []; |
| 102 | + $x = preg_split('/, */', $wwwauth[1]); |
| 103 | + foreach ($x as $a) { |
| 104 | + $y = preg_split('/=/', $a); |
| 105 | + $y[1] = preg_replace('/"/', '', $y[1]); |
| 106 | + $authdata[$y[0]] = (isset($y[1])) ? $y[1] : ''; |
| 107 | + } |
| 108 | + |
| 109 | + $this->auth = array( |
| 110 | + 'authtype'=>$authtype, |
| 111 | + 'realm'=>$authdata['realm'], |
| 112 | + 'nonce'=>$authdata['nonce'], |
| 113 | + 'uri'=>$uri |
| 114 | + ); |
| 115 | + |
| 116 | + $options['Authorization'] = $this->auth['authtype'].' username="'.$this->user.'",realm="'.$this->auth['realm'].'",nonce="'.$this->auth['nonce'].'",uri="'.$this->auth['uri'].'",response="'.$this->digest($this->user, $this->auth['realm'], $this->pass, $this->auth['nonce'], $method, $this->auth['uri']).'"'; |
| 117 | + |
| 118 | + return $this->request($method, $uri, $options); |
| 119 | + } |
| 120 | + |
| 121 | + return [ |
| 122 | + 'headers' => $headers, |
| 123 | + 'body' => $body |
| 124 | + ]; |
| 125 | + } |
| 126 | + |
| 127 | + protected function interpretResponseHeaders(string $response): array |
| 128 | + { |
| 129 | + $return = []; |
| 130 | + $lines = preg_split('/\r\n/', $response); |
| 131 | + foreach ($lines as $k => $v) { |
| 132 | + if ($k == 0) { |
| 133 | + $r = preg_split('/ /', $v, 3); |
| 134 | + $return['proto'] = (isset($r[0])) ? $r[0] : ''; |
| 135 | + $return['code'] = (isset($r[1])) ? $r[1] : ''; |
| 136 | + $return['msg'] = (isset($r[2])) ? $r[2] : ''; |
| 137 | + } else { |
| 138 | + $r = preg_split('/: /', $v); |
| 139 | + if (isset($r[1])) { |
| 140 | + $return[$r[0]] = (isset($r[1])) ? $r[1] : ''; |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + return $return; |
| 146 | + } |
| 147 | + |
| 148 | +} |
| 149 | + |
0 commit comments