| 1 | <?php |
| 2 | /************************************************************* |
| 3 | # 076 License |
| 4 | |
| 5 | Copyright (c) テクニカル諏訪子 |
| 6 | |
| 7 | Permission is hereby granted to any person obtaining a copy of the software |
| 8 | Little Beast (the "Software") to use, modify, merge, copy, publish, distribute, |
| 9 | sublicense, and/or sell copies of the Software, subject to the following conditions: |
| 10 | |
| 11 | 1. **Origin Attribution**: |
| 12 | - You must not misrepresent the origin of the Software; you must not claim |
| 13 | you created the original Software. |
| 14 | - If the Software is used in a product, you must either: |
| 15 | a. Provide clear attribution in the product's documentation, user interface, |
| 16 | or other visible areas, **OR** |
| 17 | b. Pay the original developers a fee they specify in writing. |
| 18 | 2. **Usage Restriction**: |
| 19 | - The Software, or any derivative works, dependencies, or libraries |
| 20 | incorporating it, must not be used for censorship or to suppress freedom of |
| 21 | speech, expression, or creativity. Prohibited uses include, but are not |
| 22 | limited to: |
| 23 | - Censorship of so-called "hate speech", visuals, non-mainstream opinions, |
| 24 | ideas, or objective reality. |
| 25 | - Tools or systems designed to restrict access to information or |
| 26 | artistic works. |
| 27 | 3. **Notice Preservation**: |
| 28 | - This license and the above copyright notice must remain intact in all copies |
| 29 | of the source code. |
| 30 | |
| 31 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 32 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 33 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 34 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 35 | CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE |
| 36 | OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 37 | */ |
| 38 | namespace Std\Lib\Tls; |
| 39 | |
| 40 | /** |
| 41 | * TLSの接続をPHPのストリームにする(fread・fgets・fwrite・feof・fclose・stream_set_timeout・stream_selectが使える) |
| 42 | * |
| 43 | * Std\Lib\Tls::connect()等が作るので、直接使わない。 |
| 44 | * 読み込み中のエラーは例外にせず、終わり(feof)として扱う(Std\Lib\Tls::error()で確認出来る)。 |
| 45 | */ |
| 46 | class Stream { |
| 47 | public const SCHEME = 'lbtls'; |
| 48 | |
| 49 | /** @var resource|null */ |
| 50 | public $context; |
| 51 | private ?Connection $conn = null; |
| 52 | private static array $pending = []; |
| 53 | |
| 54 | /** |
| 55 | * 接続をストリームにする |
| 56 | * |
| 57 | * @param Connection $conn ハンドシェイク済みの接続 |
| 58 | * @return resource |
| 59 | */ |
| 60 | public static function wrap(Connection $conn) { |
| 61 | if (!in_array(self::SCHEME, stream_get_wrappers(), true)) stream_wrapper_register(self::SCHEME, self::class); |
| 62 | $id = spl_object_id($conn); |
| 63 | self::$pending[$id] = $conn; |
| 64 | $s = fopen(self::SCHEME.'://'.$id, 'r+'); |
| 65 | unset(self::$pending[$id]); |
| 66 | // PHPのバッファで書き込みが遅れない様に |
| 67 | stream_set_write_buffer($s, 0); |
| 68 | // シーク出来ないストリームにする(stream_seekが無いので失敗し、PHPが印を付ける)。 |
| 69 | // シーク出来るストリームにfwriteすると、PHPは読み込みのバッファ(まだ読んでいない受信データ)を捨てる |
| 70 | @fseek($s, 0, SEEK_CUR); |
| 71 | return $s; |
| 72 | } |
| 73 | |
| 74 | /** ストリームの接続(Tls::info()等) */ |
| 75 | public static function connection($stream): ?Connection { |
| 76 | if (!is_resource($stream)) return null; |
| 77 | $w = stream_get_meta_data($stream)['wrapper_data'] ?? null; |
| 78 | return $w instanceof self ? $w->conn : null; |
| 79 | } |
| 80 | |
| 81 | public function stream_open(string $path, string $mode, int $options, ?string &$opened): bool { |
| 82 | $id = (int)substr($path, strlen(self::SCHEME) + 3); |
| 83 | $this->conn = self::$pending[$id] ?? null; |
| 84 | return $this->conn !== null; |
| 85 | } |
| 86 | |
| 87 | public function stream_read(int $count): string|false { |
| 88 | return $this->conn->read($count); |
| 89 | } |
| 90 | |
| 91 | public function stream_write(string $data): int { |
| 92 | try { |
| 93 | return $this->conn->write($data); |
| 94 | } catch (TlsException $e) { |
| 95 | return 0; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | public function stream_eof(): bool { |
| 100 | return $this->conn->eof(); |
| 101 | } |
| 102 | |
| 103 | public function stream_close(): void { |
| 104 | $this->conn?->close(); |
| 105 | } |
| 106 | |
| 107 | public function stream_flush(): bool { |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | public function stream_set_option(int $option, int $arg1, ?int $arg2): bool { |
| 112 | if ($option === STREAM_OPTION_READ_TIMEOUT) return stream_set_timeout($this->conn->socket(), $arg1, (int)$arg2); |
| 113 | if ($option === STREAM_OPTION_BLOCKING) return $arg1 === 1; // ブロッキングのみ |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | /** stream_select()用に下のソケットを返す */ |
| 118 | public function stream_cast(int $castAs) { |
| 119 | return $this->conn->socket(); |
| 120 | } |
| 121 | |
| 122 | public function stream_stat(): array { |
| 123 | return []; |
| 124 | } |
| 125 | } |