I want to use a proxy with authentication to my below code to communicate and get data from whois servers.
My code is as follows:
$whoisserver = "whois.verisign-grs.com";
$port = 43;
$timeout = 10;
$fp = #fsockopen($whoisserver, $port, $errno, $errstr, $timeout) or die("Socket Error " . $errno . " - " . $errstr);
fputs($fp, $domain . "\r\n");
$out = "";
while(!feof($fp)){
$out .= fgets($fp);
}
fclose($fp);
In the above I want to use a username and password to authenticate my proxy.
What do I change in the code above to achieve my desired outcome?
Related
I connect to the Minecraft server via tcp, how do I use a proxy server
I know how to use a proxy for HTTP requests, but I don’t really understand how to do it for TCP
$data = "\x00";
$data .= makeVarInt($proto);
$data .= pack('c', strlen($ip)) . $ip;
$data .= pack('n', $port);
$data .= "\x02";
$handshake = pack('c', strlen($data)) . $data;
$nick = generateRandomString(5)."_RAGE_". generateRandomString(5);
//Create TCP socket
$socket = #stream_socket_client("tcp://$ip:$port", $errno, $errstr, 10);
//Check for errors
if ($errno > 0) {
echo "ERROR: " . $errstr . PHP_EOL;
continue;
}
//Send login handshake packet
fwrite($socket, $handshake);
//Make login start packet
$data = "\x00";
$data .= pack('c', strlen($nick)) . $nick;
$data = pack('c', strlen($data)) . $data;
//Send login start packet
fwrite($socket, $data);
Im using PHPSECLIB to send a file and a XML to a SFTP server.
In this case the server im trying to reach is outside my work network.
To connect to the internet outside we have a proxy to do that.
What i need to do is configure the proxy of this connection to the one i need.
EDIT --
I have the following code, how can i pass the username and password of my proxy ?
$proxyHost = '******'
$fsock = fsockopen($proxyHost, $proxyPort);
$address = '*****';
$port = '*****';
$request = "CONNECT $address:$port HTTP/1.0\r\nContent-Length: 0\r\n\r\n";
if(fputs($fsock, $request) != strlen($request)) {
exit("premature termination");
}
$response = fgets($fsock);
$sftp = new SFTP($fsock);
.......
Quoting https://github.com/phpseclib/phpseclib/issues/1339#issuecomment-462224179:
With authorization:
$fsock = fsockopen('127.0.0.1', 80, $errno, $errstr, 1);
if (!$fsock) {
echo $errstr; exit;
}
fputs($fsock, "CONNECT website.com:22 HTTP/1.0\r\n");
fputs($fsock, "Proxy-Authorization: Basic " . base64_encode('user:pass') . "\r\n");
fputs($fsock, "\r\n");
while ($line = fgets($fsock, 1024)) {
if ($line == "\r\n") {
break;
}
//echo $line;
}
$ssh = new Net_SSH2($fsock);
$ssh->login('user', 'pass');
echo $ssh->exec('ls -latr');
If that doesn't work then run the script and tell me what the headers you get back are. Digest authentication is more of a PITA then Basic but it's not impossible.
More info on how authorization works with HTTP proxies:
https://www.rfc-editor.org/rfc/rfc7235#section-4.3
I am using Mailchimp api 1.3 php wrapper. The problem is that a server where my customer hosts a site will cache response, it won't even make api call. The exact the same code works with me and other customers:
$api = new MCAPI($apiKey);
$doubleOptin = false;
$mergeVar = array(
'FNAME' => '',
'LNAME' => ''
);
$api->listSubscribe($listId, $email, $mergeVar, 'html', $doubleOptin);
print_r($api);
method listSubscribe() calls method callServer where I guess is the problem:
$payload = "POST " . $this->apiUrl["path"] . "?" . $this->apiUrl["query"] . "&method=" . $method . " HTTP/1.0\r\n";
$payload .= "Host: " . $host . "\r\n";
$payload .= "User-Agent: MCAPI/" . $this->version ."\r\n";
$payload .= "Content-type: application/x-www-form-urlencoded\r\n";
$payload .= "Content-length: " . strlen($post_vars) . "\r\n";
$payload .= "Connection: close \r\n\r\n";
$payload .= $post_vars;
ob_start();
if ($this->secure){
$sock = fsockopen("ssl://".$host, 443, $errno, $errstr, 30);
} else {
$sock = fsockopen($host, 80, $errno, $errstr, 30);
}
if(!$sock) {
$this->errorMessage = "Could not connect (ERR $errno: $errstr)";
$this->errorCode = "-99";
ob_end_clean();
return false;
}
$response = "";
fwrite($sock, $payload);
stream_set_timeout($sock, $this->timeout);
$info = stream_get_meta_data($sock);
while ((!feof($sock)) && (!$info["timed_out"])) {
$response .= fread($sock, $this->chunkSize);
$info = stream_get_meta_data($sock);
}
var_dump($response); exit;
Does anybody have any idea why fsockopen and fwrite never sends call to Mailchimp? Weird thing is that I can actually read $response, but it is always the same from cache.
If the code works for you and there is some query caching then you could try to add a timestamp to the request.
"&time=".time()
This way you always have a "fresh" request.
I'm trying to connect APNS, but i need to pass through a proxy, here the connection test code:
if (!extension_loaded('openssl')) {
exit("need openssl");
}
$http = array();
$http['http']['proxy'] = 'tcp://proxy.net:8080';
$http['http']['request_fulluri'] = true;
$ssl = array();
$ssl['ssl']['local_cert'] = 'ck.pem';
$ssl['ssl']['passphrase'] = 'passphrase';
$opts = array_merge($http,$ssl);
$context = stream_context_create($opts);
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $context);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
The problem is that I have always a timeout error, like if i make a direct connection to apple gateway, like if options in stream_context_creat were ognored.
OpenSSL and Socket support are enabled, and port 2195 is open.
Any ideas?
Edit 1:
Trying connect to proxy only it works
$fp = stream_socket_client('tcp://proxy-dmz.pgol.net:8080', $err, $errstr, 60, STREAM_CLIENT_CONNECT);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected!' . PHP_EOL;
Edit 2:
And a script like this (found in some forum) seems to work... i'm stuck
$ip = "tcp://proxy.net"; // proxy IP
$port = "8080"; // proxy port
$url = "http://search.yahoo.com/search?p=test";
$request = "GET $url HTTP/1.0\r\nHost:www.yahoo.com:80\r\n\r\n";
$fp = fsockopen($ip,$port); // connect to proxy
fputs($fp, $request);
$data="";
while (!feof($fp)) $data.=fgets($fp,64000);
fclose($fp);
print $data;
I need to pass the session to an asynchronous call via fsockopen in php.
Can you help me pass the session to the new socket?
SOLUTION:
The following code works.
start.php
<?php
session_start();
$_SESSION['var1'] = 'value1';
async_call('/async.php');
echo '<pre>';
print_r($_SESSION);
echo $_COOKIE['PHPSESSID'] . "\r\n";
echo 'verify.php';
function async_call($filepath) {
$host = 'sandbox'; // set to your domain
$sock = fsockopen($host, 80);
fwrite($sock, "GET $filepath HTTP/1.1\r\n");
fwrite($sock, "Host: $host\r\n");
fwrite($sock, "Cookie: PHPSESSID=" . $_COOKIE['PHPSESSID'] . "\r\n");
fwrite($sock, "Connection: close\r\n");
fwrite($sock, "\r\n");
fflush($sock);
fclose($sock);
}
?>
async.php
<?php
session_start();
logger('confirm logger is working');
logger($_SESSION); // this is always blank!
function logger($msg) {
$filename = 'debug.log';
$fd = fopen($filename, "a");
if (is_array($msg))
$msg = json_encode($msg);
$msg = '[' . date('Y/m/d h:i:s', time()) . "]\t" . $msg;
fwrite($fd, $msg . "\n");
fclose($fd);
}
?>
verify.php
<?php
$logfile = 'debug.log';
echo 'start.php';
echo '<pre>' . file_get_contents($logfile);
?>
I could get this working locally, but it turned out the shared host I was staging on disallowed fsockopen without any documentation. All good on my own servers. Thanks for the help.
Thanks for the help, Andreas. Turns out the shared host I was staging on disallowed fsockopen without any documentation. That's why I could get it working locally but not on the staging server.
The following code works.
start.php
<?php
session_start();
$_SESSION['var1'] = 'value1';
async_call('/async.php');
echo '<pre>';
print_r($_SESSION);
echo $_COOKIE['PHPSESSID'] . "\r\n";
echo 'verify.php';
function async_call($filepath) {
$host = 'sandbox'; // set to your domain
$sock = fsockopen($host, 80);
fwrite($sock, "GET $filepath HTTP/1.1\r\n");
fwrite($sock, "Host: $host\r\n");
fwrite($sock, "Cookie: PHPSESSID=" . $_COOKIE['PHPSESSID'] . "\r\n");
fwrite($sock, "Connection: close\r\n");
fwrite($sock, "\r\n");
fflush($sock);
fclose($sock);
}
?>
async.php
<?php
session_start();
logger('confirm logger is working');
logger($_SESSION); // this is always blank!
function logger($msg) {
$filename = 'debug.log';
$fd = fopen($filename, "a");
if (is_array($msg))
$msg = json_encode($msg);
$msg = '[' . date('Y/m/d h:i:s', time()) . "]\t" . $msg;
fwrite($fd, $msg . "\n");
fclose($fd);
}
?>
verify.php
<?php
$logfile = 'debug.log';
echo 'start.php';
echo '<pre>' . file_get_contents($logfile);
?>