I have a list of proxies, need to use them in a php script I am writing.
How can I test that the proxy will work before I use it? is that possible? at the moment if the proxy doesn't work the script just dies after 30 seconds. Is there a quicker way to identify whether it will work or not?
perhaps create a socket connection? send something to it that will reveal whether the proxy is open?
thanks
you can use fsockopen for this, where you can specify a timeout.
A simple proxy list checker. You can check a list ip:port if that port is opened on that IP.
<?php
$fisier = file_get_contents('proxy_list.txt'); // Read the file with the proxy list
$linii = explode("\n", $fisier); // Get each proxy
$fisier = fopen("bune.txt", "a"); // Here we will write the good ones
for($i = 0; $i < count($linii) - 1; $i++) test($linii[$i]); // Test each proxy
function test($proxy)
{
global $fisier;
$splited = explode(':',$proxy); // Separate IP and port
if($con = #fsockopen($splited[0], $splited[1], $eroare, $eroare_str, 3))
{
fwrite($fisier, $proxy . "\n"); // Check if we can connect to that IP and port
print $proxy . '<br>'; // Show the proxy
fclose($con); // Close the socket handle
}
}
fclose($fisier); // Close the file
?>
you may also want to use set_time_limit so that you can run your script for longer.
Code taken from: http://www.php.net/manual/en/function.fsockopen.php#95605
You can use this function
function check($proxy=null)
{
$proxy= explode(':', $proxy);
$host = $proxy[0];
$port = $proxy[1];
$waitTimeoutInSeconds = 10;
$bool=false;
if($fp = #fsockopen($host,$port,$errCode,$errStr,$waitTimeoutInSeconds)){
$bool=true;
}
fclose($fp);
return $bool;
}
Related
I am trying to force a php script (currently on XAMPP, soon on a dedicated server) to use a certain proxy for outgoing traffic.
On this site i found the following solution for it :
stream_context_set_default(['http'=>['proxy'=>'ip:port']]);
how do i verify that my script is actually using that proxy?
thanks in advance
You can use fsockopen for this, where you can specify a timeout.
A simple proxy list checker. You can check a list ip:port if that port is opened on that IP.
<?php
$fisier = file_get_contents('proxy_list.txt'); // Read the file with the proxy list
$linii = explode("\n", $fisier); // Get each proxy
$fisier = fopen("bune.txt", "a"); // Here we will write the good ones
for($i = 0; $i < count($linii) - 1; $i++) test($linii[$i]); // Test each proxy
function test($proxy)
{
global $fisier;
$splited = explode(':',$proxy); // Separate IP and port
if($con = #fsockopen($splited[0], $splited[1], $eroare, $eroare_str, 3))
{
fwrite($fisier, $proxy . "\n"); // Check if we can connect to that IP and port
print $proxy . '<br>'; // Show the proxy
fclose($con); // Close the socket handle
}
}
fclose($fisier); // Close the file
?>
you may also want to use set_time_limit so that you can run your script for longer.
Code taken from: http://www.php.net/manual/en/function.fsockopen.php#95605
Just for 1 proxy :
function test()
{
$fisier = fopen("bune.txt", "a"); // Here we will write the good ones
if($con = #fsockopen($IP, $port, $eroare, $eroare_str, 3))
{
fwrite($fisier, $proxy . "\n"); // Check if we can connect to that IP and port
print $proxy . '<br>'; // Show the proxy
fclose($con); // Close the socket handle
}
}
I am trying to connect to my Asterisk Manager interface using Http Connection. This is not working for me. Anyone can help for me?
I need to login to Asteric manager interface using php. PHP file should be in another server.
manager.conf
[general]
enabled = yes
webenabled = yes
port = 5038
bindaddr = 0.0.0.0
displayconnects=no ;only effects 1.6+
[sameera]
secret = 123123
deny=0.0.0.0/0.0.0.0
permit=192.168.100.122/255.255.255.0
read = system,call,log,verbose,command,agent,user,config,command,dtmf,reporting,cdr,dialplan,originate
write = system,call,log,verbose,command,agent,user,config,command,dtmf,reporting,cdr,dialplan,originate
writetimeout = 5000
192.168.100.122 is my server ip address
login.php
<?php
$timeout = 3;
$socket = fsockopen("192.168.100.122",'12321',$errno,$errstr,$timeout);
fputs($socket,"Action: Login\r\n");
fputs($socket,"Username: sameera\r\n");
fputs($socket,"Secret: 123123\r\n\r\n");
$line="";
$response="";
while($line != "\r\n"){
$line = fgetss($socket,128);
$response .= $line;
}
echo $response;
?>
Your port number is wrong, change 12321 to 5039
$socket = fsockopen("192.168.100.122",'5039',$errno,$errstr,$timeout);
<?php
$timeout = 3;
$socket = fsockopen("127.0.0.1",'5038',$errno,$errstr,$timeout);
fputs($socket,"Action: Login\r\n");
fputs($socket,"Username: julio\r\n");
fputs($socket,"Secret: 12345\r\n\r\n");
$line="";
$response="";
while($line != "\r\n"){
$line = fgetss($socket,128);
$response .= $line;
}
echo $response;
?>
Use phpagi/ami library,NOT re-create it again
Do debug of your code and ensure your firewall allow connect.
I'm trying to set up a TCP socket server, which should support many client connections at the same time, together with receiving and sending data.
For this purpose I'm trying to use PHP's socket_select(); due to server always hanged on socket_read(); process, where it should continue, no matter if there were data, or not. I Tried to run following code below, but it always hangs on a new client connection.
// TCP socket created
$clients = array($socket);
while (true) { // Infinite loop for server
$newsock = socket_accept($socket);
if ($newsock !== false) {
// Adds a new client
$clients[] = $newsock;
}
$read = $clients;
$write = NULL;
$except = NULL;
$num_changed_sockets = socket_select($read, $write, $except, 0);
if ($num_changed_sockets === false) {
// Error here
} else if ($num_changed_sockets > 0) {
// Something happened
if (in_array($socket, $read)) {
$key = array_search($socket, $read);
unset($read($key]);
}
foreach ($read as $read_socket) {
$data = socket_read($read_socket, 128); // This should not hang!
if ($data === false) {
// Disconnect client
}
// Reads data, sends answer...
}
}
// Something to send for all clients
}
Is it also possible to use socket_select(); without having a copy of my clients in an array, where the listener is included? Just having a clean array for clients.
Sockets are by default blocking, you should put the passive listening socket ($socket in your case) in the read set passed to socket_select as well, and it will be readable when you can accept new connections.
Just set the listening socket $socket to non-blocking mode. Clients get connected and data is being read, but the client disconnects are not handled in real time (there is a pretty big delay on it). Is there reason for that?
I have a problem implementing an API that works with Java, but fails to work with cURL. We've gone through everything so far and there must be something that is different between the requests that Java makes and what we make.
In PHP we can get header data by looking at $_SERVER['HTTP_*'] variables and we can get request body from file_get_contents('php://input'); But we cannot get the exact data sent from user agent to client.
Is it possible to get the full request, that user agent sends, with PHP? Headers and body included? If so, then how?
The only example I found is here, but this one gets the body the way I mentioned, while it gets headers by parsing through $_SERVER, which seems like a hack since it's never 100% of what was actually sent.
All help and tips are appreciated!
for headers you can try apache_request_headers() and for body I dont know other method than file_get_contents('php://input');
Old question, but for anyone needing to do this in the future... The best (probably only) way would be to take full control of the server by being the server.
Set up a socket server listening on port 80 (if this is all you need the server to do), or any other port if 80 is not available.
That way you can capture the request completely unmodified. Examples of basic socket servers are plentiful, here is a simplified version of the latest one I implemented, which will print the full request:
<?php
//Read the port number from first parameter on the command line if set
$port = (isset($argv[1])) ? intval($argv[1]) : 80;
//Just a helper
function dlog($string) {
echo '[' . date('Y-m-d H:i:s') . '] ' . $string . "\n";
}
//Create socket
while (($sock = #socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
dlog("socket_create() failed: reason: " . socket_strerror(socket_last_error()));
sleep(1);
}
//Reduce blocking if previous connections weren't ended correctly
if (!socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1)) {
dlog("socket_set_option() failed: reason: " . socket_strerror(socket_last_error($sock)));
exit;
}
//Bind to port
$tries = 0;
while (#socket_bind($sock, 0, $port) === false) {
dlog("socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)));
sleep(1);
$tries++;
if ($tries>30) {
dlog("socket_bind() failed 30 times giving up...");
exit;
}
}
//Start listening
while (#socket_listen($sock, 5) === false) {
dlog("socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)));
sleep(1);
}
//Makes it possible to accept several simultaneous connections
socket_set_nonblock($sock);
//Keeps track of active connections
$clients = array();
dlog("server started...");
while(true) {
//Accept new connections
while (($msgsock = #socket_accept($sock)) !== false) {
//Prevent blocking
socket_set_nonblock($msgsock);
//Get IP - just for logging
socket_getpeername($msgsock, $remote_address);
//Add new client to array
$clients[] = array('sock' => $msgsock, 'timeout' => time()+30, 'ip' => $remote_address);
dlog("$remote_address connected, client count: ".count($clients));
}
//Loop existing clients and read input
foreach($clients as $key => $client) {
$rec = '';
$buf = '';
while (true) {
//Read 2 kb into buffer
$buf = socket_read($clients[$key]['sock'], 2048, PHP_BINARY_READ);
//Break if error reading
if ($buf === false) break;
//Append buffer to input
$rec .= $buf;
//If no more data is available socket read returns an empty string - break
if ($buf === '') break;
}
if ($rec=='') {
//If nothing was received from this client for 30 seconds then end the connection
if ($clients[$key]['timeout']<time()) {
dlog('No data from ' . $clients[$key]['ip'] . ' for 30 seconds. Ending connection');
//Close socket
socket_close($client['sock']);
//Clean up clients array
unset($clients[$key]);
}
} else {
//If something was received increase the timeout
$clients[$key]['timeout']=time()+30;
//And.... DO SOMETHING
dlog('Raw data received from ' . $clients[$key]['ip'] . "\n------\n" . $rec . "\n------");
}
}
//Allow the server to do other stuff by sleeping for 50 ms on each iteration
usleep(50000);
}
//We'll never reach here, but some logic should be implemented to correctly end the server
foreach($clients as $key => $client) {
socket_close($client['sock']);
}
#socket_close($sock);
exit;
To start the server on port 8080 just run php filename.php 8080 from a shell.
These aren't "with php" but you might find them useful for your purposes nevertheless
ssldump http://ssldump.sourceforge.net/
mod_dumpio http://httpd.apache.org/docs/2.2/mod/mod_dumpio.html
mod_dumpost https://github.com/danghvu/mod_dumpost
Like many people, I can do a lot of things with PHP. One problem I do face constantly is that other people can do it much cleaner, much more organized and much more structured. This also results in much faster execution times and much less bugs.
I just finished writing a basic PHP Socket Server (the real core), and am asking you if you can tell me what I should do different before I start expanding the core. I'm not asking about improvements such as encrypted data, authentication or multi-threading.
I'm more wondering about questions like "should I maybe do it in a more object oriented way (using PHP5)?", or "is the general structure of the way the script works good, or should some things be done different?". Basically, "is this how the core of a socket server should work?"
In fact, I think that if I just show you the code here many of you will immediately see room for improvements. Please be so kind to tell me. Thanks!
#!/usr/bin/php -q
<?
// config
$timelimit = 180; // amount of seconds the server should run for, 0 = run indefintely
$address = $_SERVER['SERVER_ADDR']; // the server's external IP
$port = 9000; // the port to listen on
$backlog = SOMAXCONN; // the maximum of backlog incoming connections that will be queued for processing
// configure custom PHP settings
error_reporting(1); // report all errors
ini_set('display_errors', 1); // display all errors
set_time_limit($timelimit); // timeout after x seconds
ob_implicit_flush(); // results in a flush operation after every output call
//create master IPv4 based TCP socket
if (!($master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) die("Could not create master socket, error: ".socket_strerror(socket_last_error()));
// set socket options (local addresses can be reused)
if (!socket_set_option($master, SOL_SOCKET, SO_REUSEADDR, 1)) die("Could not set socket options, error: ".socket_strerror(socket_last_error()));
// bind to socket server
if (!socket_bind($master, $address, $port)) die("Could not bind to socket server, error: ".socket_strerror(socket_last_error()));
// start listening
if (!socket_listen($master, $backlog)) die("Could not start listening to socket, error: ".socket_strerror(socket_last_error()));
//display startup information
echo "[".date('Y-m-d H:i:s')."] SERVER CREATED (MAXCONN: ".SOMAXCONN.").\n"; //max connections is a kernel variable and can be adjusted with sysctl
echo "[".date('Y-m-d H:i:s')."] Listening on ".$address.":".$port.".\n";
$time = time(); //set startup timestamp
// init read sockets array
$read_sockets = array($master);
// continuously handle incoming socket messages, or close if time limit has been reached
while ((!$timelimit) or (time() - $time < $timelimit)) {
$changed_sockets = $read_sockets;
socket_select($changed_sockets, $write = null, $except = null, null);
foreach($changed_sockets as $socket) {
if ($socket == $master) {
if (($client = socket_accept($master)) < 0) {
echo "[".date('Y-m-d H:i:s')."] Socket_accept() failed, error: ".socket_strerror(socket_last_error())."\n";
continue;
} else {
array_push($read_sockets, $client);
echo "[".date('Y-m-d H:i:s')."] Client #".count($read_sockets)." connected (connections: ".count($read_sockets)."/".SOMAXCONN.")\n";
}
} else {
$data = #socket_read($socket, 1024, PHP_NORMAL_READ); //read a maximum of 1024 bytes until a new line has been sent
if ($data === false) { //the client disconnected
$index = array_search($socket, $read_sockets);
unset($read_sockets[$index]);
socket_close($socket);
echo "[".date('Y-m-d H:i:s')."] Client #".($index-1)." disconnected (connections: ".count($read_sockets)."/".SOMAXCONN.")\n";
} else {
if ($data = trim($data)) { //remove whitespace and continue only if the message is not empty
switch ($data) {
case "exit": //close connection when exit command is given
$index = array_search($socket, $read_sockets);
unset($read_sockets[$index]);
socket_close($socket);
echo "[".date('Y-m-d H:i:s')."] Client #".($index-1)." disconnected (connections: ".count($read_sockets)."/".SOMAXCONN.")\n";
break;
default: //for experimental purposes, write the given data back
socket_write($socket, "\n you wrote: ".$data);
}
}
}
}
}
}
socket_close($master); //close the socket
echo "[".date('Y-m-d H:i:s')."] SERVER CLOSED.\n";
?>
One small thing, personally i'd create a function for outputting instead of just using echo, that way its easy to turn it off, change the format etc.. eg
function log($message = '')
{
echo '['.date('Y-m-d H:i:s').']'.$message;
}
and then you can use :
log("SERVER CREATED (MAXCONN: ".SOMAXCONN.").\n");
instead of
echo "[".date('Y-m-d H:i:s')."] SERVER CREATED (MAXCONN: ".SOMAXCONN.").\n";
Oh and be sure to use === instead of == otherwise you might get some odd results.
I'd move your switch $data into a function as that will likely expand.
Also since it looks like you're using a text based protocol, might want to explode/strtok to get the first level command and check it against an array of valid commands. Could also have an array that describes what internal function to call and use call_user_func_array to dispatch the call.