All pastes #620996 Raw Edit

Something

public text v1 · immutable
·published 2007-07-15 21:47 UTC
rendered paste body
class proxycontrol {

	var $good_proxy = null;

	var $proxy_file = null;

	var $DIR = null;

	var $tproxy_file = null;

	var $WASPROXY = null;

	var $timeout = null;

	var $TO = null;

	var $FROM = null;

	var $pc = null;



	function proxycontrol ($file, $tfile = '') {

		$this->LoadFromFile ($file);

		$this->proxy_file = $file;

		$this->tproxy_file = ($tfile ? $tfile : $file);

		$this->good_proxy = '';

		$this->pc = -1;

		$this->p_invalid = 5;

		$this->RPA = 0;

		if (count ($this->proxy)) {

			$this->WASPROXY = 1;

			return null;

		}



		$this->WASPROXY = 0;

	}



	function stopch () {

		if (is_file ($this->DIR . 'stop.txt')) {

			$tmp = file ($this->DIR . 'stop.txt');

			$stop = intval ($tmp[0]);

			if ($stop) {

				echo '<font color=#F45A20><b>[STOP]</b></font>';

				flush ();

				exit ();

			}

		}

	}



	function setcheckurl ($url) {

		$this->churl = $url;

	}



	function setdir ($DIR) {

		$this->DIR = $DIR;

	}



	function setip ($ip) {

		$this->IP = $ip;

	}



	function isproxy () {

		return count ($this->proxy);

	}



	function getmicrotime () {

		list ($usec, $sec) = explode (' ', microtime ());

		return (double)$usec + (double)$sec;

	}



	function curlfetch ($url, $params = '', $rurl = '') {

		$temp = parse_url ($url);

		if ($temp['port'] != '80') {

			(true ? $temp['port'] : ':' . $temp['port']);

		}



		$url = trim ($temp['scheme'] . '://' . $temp['host'] . '' . $temp['path'] . ($temp['query'] ? '?' . $temp['query'] : ''));

		$temp = array ();

		$user_agent = 'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';

		$ch = curl_init ();

		curl_setopt ($ch, CURLOPT_URL, $url);

		curl_setopt ($ch, CURLOPT_REFERER, ($rurl ? $rurl : $url));

		curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);

		curl_setopt ($ch, CURLOPT_HEADER, 1);

		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

		curl_setopt ($ch, CURLOPT_TIMEOUT, ($this->timeout ? $this->timeout : 7));

		if (0 < strlen ($params)) {

			curl_setopt ($ch, CURLOPT_POST, 1);

			curl_setopt ($ch, CURLOPT_POSTFIELDS, $params);

			curl_setopt ($ch, CURLOPT_PROXY, $this->proxy[$this->pc]['proxy']);

			$pre_curl = $this->getmicrotime ();

			curl_setopt ($ch, CURLOPT_PROXY, $this->proxy[$this->pc]['proxy']);

		}



		$result = curl_exec ($ch);

		curl_close ($ch);

		$this->proxy[$this->pc]['atime'] = $this->getmicrotime () - $pre_curl;

		return $result;

	}



	function loadfromurl ($url) {

		$string = strip_tags ($this->CurlFetch ($url));

		$num = '([0-9]|[0-9]{2}|1\\d\\d|2[0-4]\\d|25[0-5])';

		preg_match_all ('/' . $num . '\\.' . $num . '\\.' . $num . '\\.' . $num . ':([0-9]*)/', $string, $ma);

		$now = mktime ();

		while (is_file ($this->DIR . 'tproxy.lock')) {

			usleep (20);

			if (180 < mktime () - $now) {

				return 1;

			}

		}



		$fp = fopen ('tproxy.lock', 'w+');

		fclose ($fp);

		@chmod ('tproxy.lock', 438);

		$fp = fopen ($this->tproxy_file, 'a+');

		for ($i = 0; $i < count ($ma[0]); ++$i) {

			fputs ($fp, trim ($ma[0][$i]) . '

');

		}



		fclose ($fp);

		@unlink ('tproxy.lock');

		@chmod ($this->tproxy_file, 438);

		if (count ($ma[0])) {

			return count ($ma[0]);

		}



		return 0;

	}



	function writetotfile () {

		while (is_file ($this->DIR . 'tproxy.lock')) {

			usleep (20);

			if (300 < mktime () - $now) {

				return 1;

			}

		}



		$fp = fopen ('tproxy.lock', 'w+');

		fclose ($fp);

		@chmod ('tproxy.lock', 438);

		$fp = fopen ($this->tproxy_file, 'a+');

		$write = 0;

		for ($i = 0; $i < count ($this->proxy); ++$i) {

			if ($this->proxy[$i]['atime'] < 20) {

				if ($this->proxy[$i]['atime']) {

					fputs ($fp, $this->proxy[$i]['proxy'] . '

');

					++$write;

					continue;

				}



				continue;

			}

		}



		fclose ($fp);

		echo 'Write <b>' . $write . '</b><BR>';

		@unlink ($this->DIR . 'tproxy.lock');

	}



	function writetofile () {

		while (is_file ($this->DIR . 'proxy.lock')) {

			usleep (20);

			if (300 < mktime () - $now) {

				return 1;

			}

		}



		$fp = fopen ('proxy.lock', 'w+');

		fclose ($fp);

		@chmod ('proxy.lock', 438);

		$fp = fopen ($this->proxy_file, 'a+');

		for ($i = 0; $i < count ($this->proxy); ++$i) {

			if ($this->proxy[$i]['atime'] < 20) {

				if ($this->proxy[$i]['atime']) {

					fputs ($fp, $this->proxy[$i]['proxy'] . '

');

					continue;

				}



				continue;

			}

		}



		fclose ($fp);

	}



	function loadfromfile ($file) {

		$this->proxy = array ();

		if (is_file ($file)) {

			$tmp = file ($file);

			for ($i = 0; $i < count ($tmp); ++$i) {

				if (trim ($tmp[$i])) {

					$this->proxy[$i]['proxy'] = trim ($tmp[$i]);

					$this->proxy[$i]['invalid'] = 0;

					$this->proxy[$i]['atime'] = 1;

					continue;

				}

			}



			if (count ($this->proxy)) {

				shuffle ($this->proxy);

			}

		}



		$this->pc = 0;

	}



	function reload () {

		if ($this->update_from_url) {

			if (is_file ($this->tproxy_file)) {

				@unlink ($this->tproxy_file);

			}



			$tmp = file ($this->proxy_url_file);

			if (!(count ($tmp))) {

				echo '<font color=red><b>No proxy sites:</b></font> fill files/proxy_sites.txt<br>';

				flush ();

				exit ();

			}



			for ($i = 0; $i < count ($tmp); ++$i) {

				$url = trim ($tmp[$i]);

				echo 'Dowbload from <b>' . $url . '</b>';

				if ($cnt = $this->LoadFromUrl ($url)) {

					echo '<font color=green><b> OK </b></font>[' . $cnt . ']<br>';

					continue;

				}

				else {

					echo '<font color=red><b> NO</b></font><br>';

					continue;

				}

			}

		}



		$this->LoadFromFile ($this->proxy_file);

	}



	function checkproxy () {

		$secr = md5 (mktime ());

		$params = 'param=' . $secr;

		$randn = rand (0, 5);

		$this->churl = str_replace ('[N]', $randn, $this->churl);

		$answ = $this->CurlFetch ($this->churl, $params);

		echo '<textarea cols=45 rows=5>' . $answ . '</textarea><br>';

		if (strpos (' ' . $answ, $this->IP)) {

			++$this->proxy[$this->pc]['invalid'];

			$this->proxy[$this->pc]['atime'] = 21;

			return 0;

		}



		if ($this->XFF) {

			if (strpos (' ' . $answ, 'X_FORWARDED_FOR')) {

				++$this->proxy[$this->pc]['invalid'];

				$this->proxy[$this->pc]['atime'] = 21;

				return 0;

			}

		}



		if (strpos (' ' . $answ, $secr)) {

			$this->proxy[$this->pc]['invalid'] = 0;

			return 1;

		}



		++$this->proxy[$this->pc]['invalid'];

		$this->proxy[$this->pc]['atime'] = 21;

		return 0;

	}



	function set_temp_var ($name, $value) {

		$q = 'select count(value) as count from siv_temp_vars where name=\'' . $name . '\'';

		$tmp = mysql_fetch_assoc (mysql_query ($q));

		if ($tmp['count']) {

			$q = 'update siv_temp_vars set value=\'' . $value . '\' where name=\'' . $name . '\'';

		}

		else {

			$q = 'insert into siv_temp_vars values(\'' . $name . '\',\'' . $value . '\')';

		}



		if (!(mysql_query ($q))) {

			exit ('[Error set temp var]: ' . mysql_error ());

		}

	}



	function inc_temp_var ($name) {

		$q = 'select value from siv_temp_vars where name=\'' . $name . '\'';

		$tmp = mysql_fetch_assoc (mysql_query ($q));

		if (isset ($tmp['value'])) {

			$q = 'update siv_temp_vars set value=\'' . intval ($tmp['value'] + 1) . ('\' where name=\'' . $name . '\'');

		}

		else {

			$q = 'insert into siv_temp_vars values(\'' . $name . '\',\'1\')';

		}



		if (!($r = mysql_query ($q))) {

			exit ('[Inc temp var]: ' . mysql_error ());

		}



		return $tmp['value'];

	}



	function get_temp_var ($name) {

		$q = 'select value from siv_temp_vars where name=\'' . $name . '\'';

		if (!($r = mysql_query ($q))) {

			exit ('[Get temp var]: ' . mysql_error ());

		}



		$tmp = mysql_fetch_assoc ($r);

		return $tmp['value'];

	}



	function proxyerrorthreaddec () {

		$this->inc_temp_var ('threads');

		$this->inc_temp_var ('proxy_err');

		echo '<b>threads dec ok</b><br>';

		flush ();

	}



	function checkall ($mode) {

		$all_n = count ($this->proxy);

		for ($i = 0; $i < $all_n; ++$i) {

			$this->stopch ();

			$this->pc = $i;

			echo 'Process complete: <b><font color=blue>' . ($i + 1) . ('/' . $all_n . '</font></b><br>');

			if ($this->CheckProxy ()) {

				if ($mode == 1) {

					echo '<font color=#5CCA13><b>' . $this->proxy[$this->pc]['proxy'] . '</b></font> Resp: [' . $this->proxy[$this->pc]['atime'] . ']<br>';

				}



				flush ();

				continue;

			}

			else {

				if ($mode == 1) {

					echo '<font color=#D62107><b>' . $this->proxy[$this->pc]['proxy'] . '</b></font><br>';

				}



				flush ();

				continue;

			}

		}

	}



	function getproxy () {

		if (!((!($this->pc + 1 == count ($this->proxy)) AND !(count ($this->proxy) < $this->pc)))) {

			$this->pc = -1;

			for ($i = 0; $i < count ($this->proxy); ++$i) {

				if ($this->p_invalid <= $this->proxy[$i]['invalid']) {

					unset ($this->proxy[$i]);

					continue;

				}

			}



			if (!(count ($this->proxy))) {

				if ($this->WASPROXY) {

					echo '#Error [No more alive proxy]<br>';

					flush ();

					$this->ProxyErrorThreadDec ();

					exit ();

				}

			}



			usort ($this->proxy, 'compare');

		}



		++$this->pc;

		echo '[pc: ' . $this->pc . '] #' . $this->proxy[$this->pc]['proxy'] . '<br>';

		flush ();

		++$this->check_number;

		if (500 < $this->check_number) {

			if ($this->RPA) {

				echo '#Error [No more alive proxy]<br>';

				flush ();

				$this->ProxyErrorTheadDec ();

				exit ();

			}



			echo '#Alert [RELOAD FROM FILE]<br>';

			flush ();

			$this->LoadFromFile ($this->proxy_file);

			$this->RPA = 1;

			$this->check_number = 0;

		}



		if ($this->CheckProxy ()) {

			$this->check_number = 0;

			$this->good_proxy = $this->proxy[$this->pc]['proxy'];

			return 1;

		}



		echo '#Notice [no good proxy ' . $this->check_number . ']<br>';

		flush ();

		$this->GetProxy ();

	}

}



function compare ($left, $right) {

	if ($left['atime'] < $right['atime']) {

		return -1;

	}



	if ($left['atime'] == $right['atime']) {

		return 0;

	}



	if ($right['atime'] < $left['atime']) {

		return 1;

	}

}



?>