Search code examples
phpsftpphpseclibhttp-tunneling

PHPSeclib Proxy send username and password as arguments


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);

    .......

Solution

  • 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