fact: "You CAN'T beat a Bastard."
BastardDomain Forums \ Development \ Proxy code
Proxy code
Fox
The Guzzler

avatar Fox

Registered: 10 Apr 2001
Location: North-Brabant
Posts: 9399
9 Jan 2005

Voor de php-freaks onder ons.
Hier versie 0.1 van mijn proxy classe.

Hij houdt zelf een lijst met proxies bij en pikt hier steeds een random proxy uit, die zeker werkt. (test hem meteen)

Kan handig zijn als je jou scripts anoniem wilt laten surfen. (GPL)

[php]
<?php
define("CHECK_PROXY_HOST", "www.bastarddomain.nl"smilie;
define("CHECK_PROXY_URL", "http://" . CHECK_PROXY_HOST . $_SERVER['PHP_SELF'] . "?check"smilie;
define("CHECK_PROXY_STRING", "proxy=up"smilie;
define("CHECK_PROXY_SITE", "http://www.atomintersoft.com/products/alive-proxy/proxy-list/"smilie;

define("TABLE_PROXY", "proxy"smilie;
define("ONLINE", 1);
define("OFFLINE", 0);
define("TRACE", 0);

if (isset($_GET['check'])) {
die(CHECK_PROXY_STRING);
}

class Proxy {
var $ip;
var $port;
var $database = "<db>";

function Proxy() {
$mysql_user = "<user>";
$mysql_pass = "<pass>";
mysql_connect($mysql_host, $mysql_user, $mysql_pass);
mysql_select_db($this->database);

if (!$this->proxy_table_exists()) {
$this->proxy_create_table();
$this->proxy_update();
}
$this->proxy_pick($this->ip, $this->port);

}

function trace($str) {
if (TRACE) {
echo $str . "<br />\n";
flush();
}
return TRACE;
}

function proxy_create_table() {
$query = "CREATE TABLE " . TABLE_PROXY . " (id int auto_increment primary key, ip varchar(15), port mediumint unsigned, status tinyint unsigned)";
mysql_query($query) or die(mysql_error());
return true;
}

function proxy_table_exists() {
$result = mysql_list_tables($this->database);
while(list($table) = mysql_fetch_row($result))
if ($table == TABLE_PROXY)
return true;
return false;
}

function proxy_search() {
$fd = fopen(CHECK_PROXY_SITE, "r"smilie;
while (!feof($fd))
$content .= fgets($fd, 128);
fclose($fd);

$pattern = "'(\d{1,3}(?:\.\d{1,3}){3})smilie\d{1,5})'";
preg_match_all($pattern, $content, $matches);
$proxy = array();
for ($i = 0; $i < count($matches[0]); $i++) {
$proxy[$i]['ip'] = $matches[1][$i];
$proxy[$i]['port'] = $matches[2][$i];
}
$this->trace($i . " proxies were found"smilie;
return $proxy;
}

function proxy_save($array) {
$n = 0;
for ($i = 0; $i < count($array); $i++) {
if (!$this->proxy_in_list($array[$i]['ip'], $array[$i]['port']) && $this->proxy_check($array[$i]['ip'], $array[$i]['port'])) {
$this->proxy_add($array[$i]['ip'], $array[$i]['port']);
$n++;
}
}
$this->trace($n . " proxies were saved"smilie;
return $n;
}

function proxy_update() {
$p = proxy_search();
$n = proxy_save($p);
$this->trace($n . " proxies were stored"smilie;
return $n;
}

function proxy_in_list($ip, $port) {
$query = "SELECT id FROM " . TABLE_PROXY . " WHERE ip = '$ip' AND port = '$port'";
$result = mysql_query($query) or die(mysql_error());
list($id) = mysql_fetch_row($result);
return isset($id);
}

function proxy_count() {
$query = "SELECT COUNT(id) FROM " . TABLE_PROXY . " WHERE status = '" . ONLINE . "'";
$result = mysql_query($query) or die(mysql_error());
list($cnt) = mysql_fetch_row($result);
$this->trace("The list contains $cnt proxies"smilie;
}

function proxy_add($ip, $port) {
$query = "INSERT INTO " . TABLE_PROXY . " (id, ip, port) VALUES (null, '$ip', '$port')";
mysql_query($query) or die(mysql_error());
$this->trace($ip . ":" . $port . " was added to the list"smilie;
return mysql_insert_id();
}

function proxy_delete($ip, $port) {
$query = "DELETE FROM " . TABLE_PROXY . " WHERE ip = '$ip' AND port = '$port'";
mysql_query($query) or die(mysql_error());
$this->trace($ip . ":" . $port . " was deleted from the list"smilie;
return true;
}

function proxy_online($ip, $port) {
$query = "UPDATE " . TABLE_PROXY . " SET status = '" . ONLINE . "' WHERE ip = '$ip' AND port = '$port'";
mysql_query($query) or die(mysql_error());
$this->trace("The status of " . $ip . ":" . $port . " was changed to online"smilie;
return true;
}

function proxy_offline($ip, $port) {
$query = "UPDATE " . TABLE_PROXY . " SET status = '" . OFFLINE . "' WHERE ip = '$ip' AND port = '$port'";
mysql_query($query) or die(mysql_error());
$this->trace("The status of " . $ip . ":" . $port . " was changed to offline"smilie;
return true;
}


function proxy_check($proxy_ip, $proxy_port, $timeout=30) {
$fp = @fsockopen($proxy_ip, $proxy_port, $errno, $errstr, $timeout);
if (!$fp) {
return false;
} else {
$out = "GET " . CHECK_PROXY_URL . " HTTP/1.1\r\n";
$out .= "Host: " . CHECK_PROXY_HOST . "\r\n";
$out .= "Connection: Close\r\n";
$out .= "\r\n";

fwrite($fp, $out);
while (!feof($fp))
$in .= fgets($fp, 128);
fclose($fp);
if (ereg(CHECK_PROXY_STRING, $in)) {
$this->trace("The status of " . $proxy_ip . ":" . $proxy_port . " is online"smilie;
return true;
} else {
$this->proxy_offline($proxy_ip, $proxy_port);
$this->trace("The status of " . $proxy_ip . ":" . $proxy_port . " is offline"smilie;
return false;
}
}
}

function proxy_pick_any(&$ip, &$port) {
$query = "SELECT ip, port FROM " . TABLE_PROXY . " WHERE status = '" . ONLINE . "' ORDER BY RAND() LIMIT 1";
$result = mysql_query($query) or die(mysql_error());
list($ip, $port) = mysql_fetch_row($result);

mysql_free_result($result);
$this->trace($ip . ":" . $port . " was picked from the list"smilie;
return $ip . ":" . $port;
}

function proxy_pick(&$ip, &$port) {
$this->proxy_pick_any($ip, $port);
if (!$this->proxy_check($ip, $port, 1))
$this->proxy_pick($ip, $port);
$this->trace($ip . ":" . $port . " was is online and ready to use"smilie;
//$this->ip = $ip;
//$this->port = $port;
return $ip . ":" . $port;
}

function proxy_show_all_online () {
$query = "SELECT ip, port FROM " . TABLE_PROXY . " WHERE status = '" . ONLINE . "'";
$result = mysql_query($query) or die(mysql_error());
while(list($ip, $port) = mysql_fetch_row($result)) {
echo $ip . ":" . $port . "<br />\n";
}

mysql_free_result($result);
}

}

$p = new Proxy();
echo $p->ip . ":" . $p->port;
?>
[/php]


This post was edited by Fox at 13 Jan 2005


Refuse to do what you don't want to do.
[user image]
Rainman
BMW Bastard

avatar Rainman

Registered: 11 Apr 2001
Location: Everywhere
Posts: 5391
9 Jan 2005

Vewy nice, dat kan wel eens handig zijn
The only reason that you're conscious right now is because I don't want to carry you. - Jack Bauer
[user image]
Gorgeous George
Opmerk Bastard

Registered: 13 Jul 2001
Location: BehindMy21
Posts: 4748
10 Jan 2005

misschien handig om binnen de code tags smilietags niet te converten
Rainman
BMW Bastard

avatar Rainman

Registered: 11 Apr 2001
Location: Everywhere
Posts: 5391
10 Jan 2005

vaag: "smilie; ); smilie
The only reason that you're conscious right now is because I don't want to carry you. - Jack Bauer
[user image]
Fox
The Guzzler

avatar Fox

Registered: 10 Apr 2001
Location: North-Brabant
Posts: 9399
10 Jan 2005

Zou inderdaad wel handig zijn ja ...
Maar volgens mij heb ik dat al ooit eens willen doen maar was dat niet zo heel makkelijk.

Zal er nog eens over nadenken.

Refuse to do what you don't want to do.
[user image]
Rainman
BMW Bastard

avatar Rainman

Registered: 11 Apr 2001
Location: Everywhere
Posts: 5391
11 Jan 2005

Maar waarom pakt ie " ) ; als wink smiley???

This post was edited by Rainman at 11 Jan 2005


The only reason that you're conscious right now is because I don't want to carry you. - Jack Bauer
[user image]
Caman
3D Bastard

avatar Caman

Registered: 11 Apr 2001
Location: up yours
Posts: 4721
11 Jan 2005

ik vind het maar niks, smilies in de code smilie
Take my hand and lead me to myself...
[user image]
Fox
The Guzzler

avatar Fox

Registered: 10 Apr 2001
Location: North-Brabant
Posts: 9399
11 Jan 2005

    Quote originally posted by Rainman @ 2005-01-11 09:14:33
    Maar waarom pakt ie " ) ; als wink smiley???


Inderdaad. Volgens mij is zelfs enkel het haakje vervangen.
Misschien slaat de AI toe en doet ie waar ie zelf zin in heeft.
Refuse to do what you don't want to do.
[user image]
Rainman
BMW Bastard

avatar Rainman

Registered: 11 Apr 2001
Location: Everywhere
Posts: 5391
11 Jan 2005

of de regexp spoort niet, maar das natuurlijk maar een gok smilie
The only reason that you're conscious right now is because I don't want to carry you. - Jack Bauer
[user image]
Fox
The Guzzler

avatar Fox

Registered: 10 Apr 2001
Location: North-Brabant
Posts: 9399
11 Jan 2005

Een eigen willetje ...

Refuse to do what you don't want to do.
[user image]
Fox
The Guzzler

avatar Fox

Registered: 10 Apr 2001
Location: North-Brabant
Posts: 9399
13 Jan 2005

Hier een link zonder smilies:
Proxy class

Refuse to do what you don't want to do.
[user image]
 1 
© 2000 - 2020 BastardDomain.com