Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- *
- * @ This file is created by http://DeZender.Net
- * @ deZender (PHP7 Decoder for ionCube Encoder)
- *
- * @ Version : 4.0.9.0
- * @ Author : DeZender
- * @ Release on : 08.08.2019
- * @ Official site : http://DeZender.Net
- *
- */
- class phpsocks
- {
- public $socket;
- public $type;
- public $host;
- public $port;
- public $user;
- public $pass;
- public $chain = [];
- public $remote_dns = false;
- public $conn_timeout;
- public $sock_timeout;
- public $stream_context;
- public $packet_logs;
- 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'];
- protected $proxy_types = ['socks5', 'socks5h'];
- public function __construct($proxies, $sock_timeout = 10, $conn_timeout = 3)
- {
- foreach ((array) $proxies as $proxy) {
- $proxy = str_replace('://', ':', $proxy);
- @list($type, $host, $port, $user, $pass) = explode(':', $proxy, 5);
- if (!in_array($type, $this->proxy_types)) {
- throw new Exception('(' . datehis() . ') Error: invalid proxy type ' . $type);
- }
- else if (!filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
- throw new Exception('(' . datehis() . ') Error: invalid host ' . $host);
- }
- else if (($port < 1) || (65535 < $port)) {
- throw new Exception('(' . datehis() . ') Error: invalid port ' . $port);
- }
- if ($type == 'socks5h') {
- $this->remote_dns = true;
- }
- $this->chain[] = ['type' => $type, 'host' => $host, 'port' => $port, 'user' => $user, 'pass' => $pass];
- }
- if (!sizeof($this->chain)) {
- throw new Exception('(' . datehis() . ') Error: wtf proxies empty!');
- }
- $this->sock_timeout = (int) $sock_timeout;
- $this->conn_timeout = (int) $conn_timeout;
- $this->stream_context = stream_context_create();
- }
- public function packet_log($message = NULL)
- {
- if (empty($message)) {
- $tmp = $this->packet_logs;
- unset($this->packet_logs);
- return $tmp;
- }
- $this->packet_logs[] = $message;
- }
- private function datehis()
- {
- return date('H:i:s', time());
- }
- public function proxychains($host, $port)
- {
- $total = sizeof($this->chain);
- $this->chain[] = ['host' => $host, 'port' => $port];
- for ($i = 0; $i < $total; ++$i) {
- $current = $this->chain[$i];
- $next = $this->chain[$i + 1];
- $this->type = $current['type'];
- $this->host = $current['host'];
- $this->port = $current['port'];
- $this->user = $current['user'];
- $this->pass = $current['pass'];
- if (!is_resource($this->socket)) {
- if (0 < $i) {
- throw new Exception('(' . datehis() . ') Error: connection lost!');
- }
- $this->connect();
- }
- $this->socks5($next['host'], $next['port']);
- }
- array_pop($this->chain);
- return true;
- }
- public function connect()
- {
- $conn = $this->host . ':' . $this->port;
- $this->socket = @stream_socket_client($conn, $errno, $errstr, $this->conn_timeout, STREAM_CLIENT_CONNECT, $this->stream_context);
- if ($this->socket === false) {
- throw new Exception('(' . datehis() . ') Error: connect(' . $conn . '): #' . $errno . ' ' . $errstr);
- }
- if (!stream_set_timeout($this->socket, $this->sock_timeout)) {
- throw new Exception('(' . datehis() . ') Error: connect(' . $conn . '): stream_set_timeout');
- }
- return true;
- }
- public function negotiate()
- {
- $packet = pack('C3', 5, 1, 0);
- if (!$this->send($packet, 3)) {
- throw new Exception('(' . datehis() . ') Error: negotiate(): unable to send packet to socks proxy');
- }
- else {
- if (!($read = $this->read(2, 2))) {
- throw new Exception('(' . datehis() . ') Error: negotiate(): unable to read reply from socks proxy');
- }
- }
- $res = @unpack('Cversion/Cmethod', $read);
- if (!(isset($res['version']) && isset($res['method']))) {
- throw new Exception('(' . datehis() . ') Error: negotiate(): unable to get response from socks_proxy');
- }
- else if ($res['version'] !== 5) {
- throw new Exception('(' . datehis() . ') Error: negotiate(): socks proxy version mismatch ' . $res['version']);
- }
- else if (!in_array($res['method'], [0, 2])) {
- throw new Exception('(' . datehis() . ') Error: negotiate(): unsupported socks method ' . $res['method']);
- }
- return true;
- }
- public function socks5($host, $port)
- {
- $this->negotiate();
- $host = $this->resolve($host);
- $long_addr = ip2long($host);
- if ($long_addr === false) {
- $packet = pack('C5', 5, 1, 0, 3, strlen($host));
- $packet .= $host . pack('n', $port);
- }
- else {
- $packet = pack('C4Nn', 5, 1, 0, 1, $long_addr, $port);
- }
- if (!$this->send($packet, strlen($packet))) {
- throw new Exception('(' . datehis() . ') Error: socks5(): send packet');
- }
- else {
- if (!($read = $this->read(10, 10))) {
- throw new Exception('(' . datehis() . ') Error: socks5(): read reply');
- }
- }
- $read = str_pad($read, 10, 0, STR_PAD_RIGHT);
- $res = @unpack('Cver/Crep/Crsv/Catyp/Laddr/Sport', $read);
- if (!(isset($res['ver']) && isset($res['rep']))) {
- throw new Exception('(' . datehis() . ') Error: socks5(): unable to parse response');
- }
- else if ($res['ver'] !== 5) {
- throw new Exception('(' . datehis() . ') Error: socks5(): version mismatch');
- }
- else if ($res['rep'] !== 0) {
- if (isset($this->replies5[$res['rep']])) {
- throw new Exception('(' . datehis() . ') Error: socks5(): ' . $this->replies5[$res['rep']]);
- }
- throw new Exception('(' . datehis() . ') Error: socks5(): unknown error ' . $res['rep']);
- }
- return true;
- }
- public function resolve($host)
- {
- if ($this->remote_dns !== true) {
- $resolve = gethostbyname($host);
- if (!filter_var($resolve, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
- throw new Exception('(' . datehis() . ') Error: failed to resolve ' . $host);
- }
- return $resolve;
- }
- return $host;
- }
- public function send($data, $length = 0)
- {
- if (!is_resource($this->socket)) {
- throw new Exception('(' . datehis() . ') Error: send(): Connection lost');
- }
- $this->show_packet('(' . datehis() . ') --->', $data);
- if ($length === 0) {
- $written = fwrite($this->socket, $data);
- }
- else {
- $written = fwrite($this->socket, $data, $length);
- }
- return $written;
- }
- public function read($max_read_size = 65536, $buffer = 4096)
- {
- if (!is_resource($this->socket)) {
- throw new Exception('(' . datehis() . ') Error: read(): Connection lost');
- }
- if ($max_read_size < $buffer) {
- $buffer = $max_read_size;
- }
- stream_set_blocking($this->socket, false);
- $sleep_counter = 0;
- $read = '';
- while (!feof($this->socket)) {
- $str = fread($this->socket, $buffer);
- if ($str) {
- $read .= $str;
- }
- else {
- $sleep_counter++;
- ................................................................................
- ................................................
- .............
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement