how to use ssh as sock5 with curl in php? - php

$connection = ssh2_connect($ip_ssh, 22);
if (ssh2_auth_password($connection,$username, $password)) {
if ($tunnel = ssh2_tunnel($connection, '127.0.0.1', 443)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_URL, "http://ipinfo.io");
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true);
curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1:443");
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
$result = curl_exec($ch);
echo $result;
} else {
echo "Tunnel creation failed.\n";
}
} else {
echo "failed;
}
I tried but it did not work. Thank you for your interest .......................................................................................

You can't use the SSH tunnel as a SOCKS proxy with cURL since the tunnel doesn't represent a SOCKS connection.
The resource returned by ssh2_tunnel is a socket stream (like the one returned by fsockopen).
Even with a SOCKS proxy on the remote SSH server (e.g. Tor), I don't think there is a way to get cURL on the local server to go through the SOCKS proxy on the remote server over the tunnel with any combination of cURL proxy options.
Here is an example of using the tunnel:
<?php
$ip_ssh = 'ssh_host';
$username = 'user';
$password = 'password';
$remote_host = 'ipinfo.io';
$connection = ssh2_connect($ip_ssh, 22);
$addr = gethostbyname($remote_host);
if (ssh2_auth_password($connection,$username, $password)) {
if ($tunnel = ssh2_tunnel($connection, $addr, 80)) {
$request = "GET / HTTP/1.0\r\n" .
"Host: $remote_host\r\n" .
"Connection: close\r\n\r\n";
fwrite($tunnel, $request);
$result = '';
while (!feof($tunnel)) {
$result .= fgets($tunnel);
}
echo $result;
} else {
echo "Tunnel creation failed.\n";
}
} else {
echo "failed";
}

Related

PHP Socket Connection to an API

I'm trying to connect to an API using PHP Sockets.
I've tried it in many ways, but I can't get it working. I know there is other answers, but no one works. I really can't achieve it. I'm trying it at least two days.
Here is the error:
php_network_getaddresses: getaddrinfo failed: Name or service not known
Code (Sockets - It does not work):
var $url = 'api.site.com.br/';
public function getUsers(){
$path = "users/?accesstoken=c24f506fe265488435858925096bc4ad7ba0";
$packet= "GET {$path} HTTP/1.1\r\n";
$packet .= "Host: {$url}\r\n";
$packet .= "Connection: close\r\n";
if (!($socket = fsockopen($this->url,80,$err_no,$err_str))){
die( "\n Connection Failed. $err_no - $err_str \n");
}
fwrite($socket, $packet);
return stream_get_contents($socket);
}
Code (Curl - It Works!):
public function getUsers2(){
$path = "users/?accesstoken=c24f506fe265488435858925096bc4ad7ba0";
$method = 'GET';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url . $path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
$output = curl_exec($ch);
$curl_error = curl_error($ch);
curl_close($ch);
return $output;
}
I can't give to you the correct URL, but the URL is working with Curl, so it should work with sockets.
I solved it by doing the following:
public function getUsers(){
$path = "users/?accesstoken=c24f506fe265488435858925096bc4ad7ba0";
$fp = fsockopen($this->url, 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET /{$path} HTTP/1.1\r\n";
$out .= "Host: {$this->url}\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
}
Connecting to a host via socket is not connecting to a URL.
Wrong: api.site.com.br/
Wrong: http://api.site.com.br/
Wrong: https//api.site.com.br/api/
Right: api.site.com.br
You connect to a host. This can be a domainname or an IP. Neither of those has a slash in it.

SMS api not working on server but fine on localhost

This code is working fine on localhost, but not on server although it is generating password on server. cURL is enabled on my server and on localhost it is sending message fine.
<?php
if(isset($_POST["submit"])){
$adm=$_POST["admno"];
$phn=$_POST["phn1"];
include("model.php");
$db = new database;
$r=$db->register($adm);
while($row=mysql_fetch_array($r)){
if($row["phn_no1"]==$phn || $row["phn_no2"]==$phn || $row["phn_no3"]==$phn)
{
$formatted = "".substr($phn,6,10)." ";
$password = $formatted + $adm;
echo $password;
$db->setpassword($adm,$password);
$pre = 'Dear%20Parents,Your%20Password%20is%20';
$suf = '%20ThankYou.CV';
$sms = $pre.$password.$suf;
$ch = curl_init("http://www.perfectbulksms.in/Sendsmsapi.aspx?USERID=ID&PASSWORD=PASS$&SENDERID=SID&TO=$phn&MESSAGE=$sms");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
$result = curl_exec($ch);
curl_close($ch);
header("Location:password.php?msg=new");
} else {
header("Location:register.php?msg=invalid");
}
}
}
?>

Php curl don't work after i uploaded it to my server

It seems that curl works on my localhost but when I upload it to my server it stops working.
This is when i have it on localhost :
And this is on the host provider server :
I am not a professional developer but i think there can be a problem with the host provider one.com
<?php
require 'kint/Kint.class.php';
$cURL = curl_init('162.243.44.4/server_list.json');
curl_setopt($cURL, CURLOPT_PORT, 3000);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
$jsondata = curl_exec($cURL);
Kint::dump($jsondata);
$json = json_decode($jsondata, true);
function ping($host,$port=4000,$timeout=6)
{
$fsock = fsockopen($host, $port, $errno, $errstr, $timeout);
if ( ! $fsock )
{
return 'Offline';
}
else
{
return 'Online';
}
}
Kint::dump($json['servers']);
foreach ($json['servers'] as $js){
echo $js['name'] . ' is ' . ping($js['ip']) . ' <br>';
}
?>
My host provider didn't have cURL activated at their current php version

Change tor exit node programmatically (get new IP)

Is there a way, and which one to change tor exit node in PHP (get new IP)?
Right now, I get new IP every 10 minutes or so..
<?php
// Initialize cURL
$ch = curl_init();
// Set the website you would like to scrape
curl_setopt($ch, CURLOPT_URL, "http://icanhazip.com/");
curl_setopt($ch, CURLOPT_USERAGENT, '');
curl_setopt($ch, CURLOPT_REFERER, '');
curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8118');
// Set cURL to return the results into a PHP variable
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// This executes the cURL request and places the results into a variable.
$curlResults= curl_exec($ch);
// Close curl
curl_close($ch);
// Echo the results to the screen>
echo $curlResults;
?>
I have no idea why people want to close this question, but whatever..
Here is the solution which worked for me. It will change exit node on request.
<?php
$fp = fsockopen('127.0.0.1', 9051, $errno, $errstr, 30);
$auth_code = 'YOUR_PASSWORD';
if ($fp) {
echo "Connected to TOR port<br />";
}
else {
echo "Cant connect to TOR port<br />";
}
fputs($fp, "AUTHENTICATE \"".$auth_code."\"\r\n");
$response = fread($fp, 1024);
list($code, $text) = explode(' ', $response, 2);
if ($code = '250') {
echo "Authenticated 250 OK<br />";
}
else {
echo "Authentication failed<br />";
}
fputs($fp, "SIGNAL NEWNYM\r\n");
$response = fread($fp, 1024);
list($code, $text) = explode(' ', $response, 2);
if ($code = '250') {
echo "New Identity OK<br />";
}
else {
echo "SIGNAL NEWNYM failed<br />";
die();
}
fclose($fp);
?>
Tor already cycles your circuit by default every ten minutes...
https://www.torproject.org/docs/tor-manual.html#MaxCircuitDirtiness
This is not exactly the same as 'get a new IP'. Your IP will probably change, but might not (relay selection is at random). Please see...
https://stem.torproject.org/faq.html#how-do-i-request-a-new-identity-from-tor

How to change tor ip each curl_init()

In order to resolve my project, I installed tor and privoxy on my virtual station (Debian).
I found how to use curl and Tor proxy, but I can't change Ip adress at each curl_init().
here is my code:
#!/usr/bin/env php
<?php
function get_url($url)
{
// ensure PHP cURL library is installed
if(function_exists('curl_init'))
{
$timestart=microtime(true);
$ip = '127.0.0.1';
$port = '9050';
$auth = 'rebootip';
$command = 'signal NEWNYM';
$fp = fsockopen($ip,$port,$error_number,$err_string,10);
if(!$fp)
{
echo "ERROR: $error_number : $err_string";
return false;
}
else
{
fwrite($fp,"AUTHENTICATE \"".$auth."\"\n");
$received = fread($fp,512);
fwrite($fp,$command."\n");
$received = fread($fp,512);
}
fclose($fp);
$ch = curl_init();
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1:9050");
curl_setopt($ch, CURLOPT_PROXYTYPE, 7);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
$response = curl_exec($ch);
$error = curl_error($ch);
print_r($response."\n");
print_r($error."\n");
}
else // PHP cURL library not installed
{
echo 'Please install PHP cURL library';
}
}
echo get_url('http://ipinfo.io/');
is this-I need to change the configuration of "tor" and "privoxy" in order to change ip address ?
thanks in advance :)
To get a different exit node IP address, setup multiple Tor clients listening on port 9050, 9051, ... etc. Then on curl_init, change the proxy port to another available Tor client.
Once you have exhausted your current list of Tor clients, you can restart them to get another exit node. You can even send simple telnet commands directly to your Tor client to change the exit node.

Categories