onion.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php /* -*- tab-width: 4; coding: utf-8 -*-
  2. vim: ts=4 noet ai */
  3. /*
  4. Copyright (C) 2016 Desktopd Project
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. function onion_connect ($socks, $hostname, $port, $timeout = 50) {
  17. if (!is_string($hostname)) {
  18. throw new ErrorException('Invalid hostname');
  19. } elseif (!preg_match('/^[a-z2-7]{16,249}\.onion$/', $hostname)) {
  20. throw new ErrorException('Unsupported hostname');
  21. }
  22. $port = (int) $port;
  23. if ($port < 1 || 65535 < $port) {
  24. throw new ErrorException('Invalid port');
  25. }
  26. $stream = stream_socket_client("tcp://$socks", $errno, $errstr, $timeout);
  27. if (!is_resource($stream)) {
  28. throw new RuntimeException("Error connecting the SOCKS server: $errstr ($errno)");
  29. }
  30. fwrite($stream, "\x05\x01\x00");
  31. $response = fread($stream, 2);
  32. if ("\x05\x00" !== $response) {
  33. @fclose($stream);
  34. throw new RuntimeException("Server refused the negotiation");
  35. }
  36. // connection request
  37. $hostnameLength = pack('C', strlen($hostname));
  38. $port = pack('n', $port);
  39. fwrite($stream, "\x05\x01\x00\x03{$hostnameLength}{$hostname}{$port}");
  40. $version = fread($stream, 1);
  41. if ("\x05" !== $version) {
  42. @fclose($stream);
  43. $dumped = is_string($version) ? bin2hex($version) : '?';
  44. throw new RuntimeException("Version error: $dumped");
  45. }
  46. $status = fread($stream, 1);
  47. if ("\x00" !== $status) {
  48. @fclose($stream);
  49. switch ($status) {
  50. case "\x01": throw new RuntimeException("ERROR: General SOCKS server failure");
  51. case "\x02": throw new RuntimeException("ERROR: Connection not allowed by ruleset");
  52. case "\x03": throw new RuntimeException("ERROR: Network unreachable");
  53. case "\x04": throw new RuntimeException("ERROR: Host unreachable");
  54. case "\x05": throw new RuntimeException("ERROR: Connection refused");
  55. case "\x06": throw new RuntimeException("ERROR: TTL expired");
  56. case "\x07": throw new RuntimeException("ERROR: Command not supported");
  57. case "\x08": throw new RuntimeException("ERROR: Address type not supported");
  58. default: throw new RuntimeException("ERROR: Unknown status");
  59. }
  60. throw new RuntimeException("Unknown error");
  61. }
  62. $reserved = fread($stream, 1);
  63. if ("\x00" !== $reserved) {
  64. throw new RuntimeException("Server violated the protocol");
  65. }
  66. $address_type = fread($stream, 1);
  67. if ("\x01" === $address_type) {
  68. // IPv4
  69. $bind_address = fread($stream, 4);
  70. } elseif ("\x04" === $address_type) {
  71. // IPv6
  72. $bind_address = fread($stream, 16);
  73. } else {
  74. @fclose($stream);
  75. throw new RuntimeException("Server returned an unsupported address");
  76. }
  77. $bind_port = fread($stream, 2);
  78. if (is_string($bind_port) && 2 === strlen($bind_port)) {
  79. return $stream;
  80. }
  81. throw new RuntimeException("Server returned an invalid port");
  82. }