Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.91 KB | None | 0 0
  1. <?php
  2. /**
  3. *
  4. * @ This file is created by http://DeZender.Net
  5. * @ deZender (PHP7 Decoder for ionCube Encoder)
  6. *
  7. * @ Version : 4.0.9.0
  8. * @ Author : DeZender
  9. * @ Release on : 08.08.2019
  10. * @ Official site : http://DeZender.Net
  11. *
  12. */
  13.  
  14. class phpsocks
  15. {
  16. public $socket;
  17. public $type;
  18. public $host;
  19. public $port;
  20. public $user;
  21. public $pass;
  22. public $chain = [];
  23. public $remote_dns = false;
  24. public $conn_timeout;
  25. public $sock_timeout;
  26. public $stream_context;
  27. public $packet_logs;
  28. protected $replies5 = ['Succeeded', 'General failure', 'Connection not allowed by ruleset', 'Network unreachable', 'Host unreachable', 'Connection refused', 'TTL expired', 'Command not supported', 'Address type not supported'];
  29. protected $proxy_types = ['socks5', 'socks5h'];
  30.  
  31. public function __construct($proxies, $sock_timeout = 10, $conn_timeout = 3)
  32. {
  33. foreach ((array) $proxies as $proxy) {
  34. $proxy = str_replace('://', ':', $proxy);
  35. @list($type, $host, $port, $user, $pass) = explode(':', $proxy, 5);
  36.  
  37. if (!in_array($type, $this->proxy_types)) {
  38. throw new Exception('(' . datehis() . ') Error: invalid proxy type ' . $type);
  39. }
  40. else if (!filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
  41. throw new Exception('(' . datehis() . ') Error: invalid host ' . $host);
  42. }
  43. else if (($port < 1) || (65535 < $port)) {
  44. throw new Exception('(' . datehis() . ') Error: invalid port ' . $port);
  45. }
  46.  
  47. if ($type == 'socks5h') {
  48. $this->remote_dns = true;
  49. }
  50.  
  51. $this->chain[] = ['type' => $type, 'host' => $host, 'port' => $port, 'user' => $user, 'pass' => $pass];
  52. }
  53.  
  54. if (!sizeof($this->chain)) {
  55. throw new Exception('(' . datehis() . ') Error: wtf proxies empty!');
  56. }
  57.  
  58. $this->sock_timeout = (int) $sock_timeout;
  59. $this->conn_timeout = (int) $conn_timeout;
  60. $this->stream_context = stream_context_create();
  61. }
  62.  
  63. public function packet_log($message = NULL)
  64. {
  65. if (empty($message)) {
  66. $tmp = $this->packet_logs;
  67. unset($this->packet_logs);
  68. return $tmp;
  69. }
  70.  
  71. $this->packet_logs[] = $message;
  72. }
  73.  
  74. private function datehis()
  75. {
  76. return date('H:i:s', time());
  77. }
  78.  
  79. public function proxychains($host, $port)
  80. {
  81. $total = sizeof($this->chain);
  82. $this->chain[] = ['host' => $host, 'port' => $port];
  83.  
  84. for ($i = 0; $i < $total; ++$i) {
  85. $current = $this->chain[$i];
  86. $next = $this->chain[$i + 1];
  87. $this->type = $current['type'];
  88. $this->host = $current['host'];
  89. $this->port = $current['port'];
  90. $this->user = $current['user'];
  91. $this->pass = $current['pass'];
  92.  
  93. if (!is_resource($this->socket)) {
  94. if (0 < $i) {
  95. throw new Exception('(' . datehis() . ') Error: connection lost!');
  96. }
  97.  
  98. $this->connect();
  99. }
  100.  
  101. $this->socks5($next['host'], $next['port']);
  102. }
  103.  
  104. array_pop($this->chain);
  105. return true;
  106. }
  107.  
  108. public function connect()
  109. {
  110. $conn = $this->host . ':' . $this->port;
  111. $this->socket = @stream_socket_client($conn, $errno, $errstr, $this->conn_timeout, STREAM_CLIENT_CONNECT, $this->stream_context);
  112.  
  113. if ($this->socket === false) {
  114. throw new Exception('(' . datehis() . ') Error: connect(' . $conn . '): #' . $errno . ' ' . $errstr);
  115. }
  116.  
  117. if (!stream_set_timeout($this->socket, $this->sock_timeout)) {
  118. throw new Exception('(' . datehis() . ') Error: connect(' . $conn . '): stream_set_timeout');
  119. }
  120.  
  121. return true;
  122. }
  123.  
  124. public function negotiate()
  125. {
  126. $packet = pack('C3', 5, 1, 0);
  127.  
  128. if (!$this->send($packet, 3)) {
  129. throw new Exception('(' . datehis() . ') Error: negotiate(): unable to send packet to socks proxy');
  130. }
  131. else {
  132. if (!($read = $this->read(2, 2))) {
  133. throw new Exception('(' . datehis() . ') Error: negotiate(): unable to read reply from socks proxy');
  134. }
  135. }
  136.  
  137. $res = @unpack('Cversion/Cmethod', $read);
  138. if (!(isset($res['version']) && isset($res['method']))) {
  139. throw new Exception('(' . datehis() . ') Error: negotiate(): unable to get response from socks_proxy');
  140. }
  141. else if ($res['version'] !== 5) {
  142. throw new Exception('(' . datehis() . ') Error: negotiate(): socks proxy version mismatch ' . $res['version']);
  143. }
  144. else if (!in_array($res['method'], [0, 2])) {
  145. throw new Exception('(' . datehis() . ') Error: negotiate(): unsupported socks method ' . $res['method']);
  146. }
  147.  
  148. return true;
  149. }
  150.  
  151. public function socks5($host, $port)
  152. {
  153. $this->negotiate();
  154. $host = $this->resolve($host);
  155. $long_addr = ip2long($host);
  156.  
  157. if ($long_addr === false) {
  158. $packet = pack('C5', 5, 1, 0, 3, strlen($host));
  159. $packet .= $host . pack('n', $port);
  160. }
  161. else {
  162. $packet = pack('C4Nn', 5, 1, 0, 1, $long_addr, $port);
  163. }
  164.  
  165. if (!$this->send($packet, strlen($packet))) {
  166. throw new Exception('(' . datehis() . ') Error: socks5(): send packet');
  167. }
  168. else {
  169. if (!($read = $this->read(10, 10))) {
  170. throw new Exception('(' . datehis() . ') Error: socks5(): read reply');
  171. }
  172. }
  173.  
  174. $read = str_pad($read, 10, 0, STR_PAD_RIGHT);
  175. $res = @unpack('Cver/Crep/Crsv/Catyp/Laddr/Sport', $read);
  176. if (!(isset($res['ver']) && isset($res['rep']))) {
  177. throw new Exception('(' . datehis() . ') Error: socks5(): unable to parse response');
  178. }
  179. else if ($res['ver'] !== 5) {
  180. throw new Exception('(' . datehis() . ') Error: socks5(): version mismatch');
  181. }
  182. else if ($res['rep'] !== 0) {
  183. if (isset($this->replies5[$res['rep']])) {
  184. throw new Exception('(' . datehis() . ') Error: socks5(): ' . $this->replies5[$res['rep']]);
  185. }
  186.  
  187. throw new Exception('(' . datehis() . ') Error: socks5(): unknown error ' . $res['rep']);
  188. }
  189.  
  190. return true;
  191. }
  192.  
  193. public function resolve($host)
  194. {
  195. if ($this->remote_dns !== true) {
  196. $resolve = gethostbyname($host);
  197.  
  198. if (!filter_var($resolve, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
  199. throw new Exception('(' . datehis() . ') Error: failed to resolve ' . $host);
  200. }
  201.  
  202. return $resolve;
  203. }
  204.  
  205. return $host;
  206. }
  207.  
  208. public function send($data, $length = 0)
  209. {
  210. if (!is_resource($this->socket)) {
  211. throw new Exception('(' . datehis() . ') Error: send(): Connection lost');
  212. }
  213.  
  214. $this->show_packet('(' . datehis() . ') --->', $data);
  215.  
  216. if ($length === 0) {
  217. $written = fwrite($this->socket, $data);
  218. }
  219. else {
  220. $written = fwrite($this->socket, $data, $length);
  221. }
  222.  
  223. return $written;
  224. }
  225.  
  226. public function read($max_read_size = 65536, $buffer = 4096)
  227. {
  228. if (!is_resource($this->socket)) {
  229. throw new Exception('(' . datehis() . ') Error: read(): Connection lost');
  230. }
  231.  
  232. if ($max_read_size < $buffer) {
  233. $buffer = $max_read_size;
  234. }
  235.  
  236. stream_set_blocking($this->socket, false);
  237. $sleep_counter = 0;
  238. $read = '';
  239.  
  240. while (!feof($this->socket)) {
  241. $str = fread($this->socket, $buffer);
  242.  
  243. if ($str) {
  244. $read .= $str;
  245. }
  246. else {
  247. $sleep_counter++;
  248. ................................................................................
  249. ................................................
  250. .............
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement