<commit_before><?php

namespace DeltaCli;

use DeltaCli\Exception\TunnelConnectionFailure as TunnelConnectionFailureException;

class SshTunnel
{
    /**
     * @var Host
     */
    private $host;

    /**
     * @var int
     */
    private $remotePort;

    /**
     * @var Host
     */
    private $tunnelConnectionsForHost;

    /**
     * @var string
     */
    private $tunnelUsername;

    /**
     * @var integer
     */
    private $tunnelPort;

    /**
     * @var int
     */
    private $sshProcess;

    /**
     * @var bool
     */
    private $batchMode = true;

    public function __construct(Host $host, $remotePort = 22)
    {
        $this->host       = $host;
        $this->remotePort = $remotePort;
    }

    public function setRemotePort($remotePort)
    {
        $this->remotePort = $remotePort;

        return $this;
    }

    public function setBatchMode($batchMode)
    {
        $this->batchMode = $batchMode;

        return $this;
    }

    public function tunnelConnectionsForHost(Host $host, $tunnelUsername)
    {
        $this->tunnelConnectionsForHost = $host;
        $this->tunnelUsername           = ($tunnelUsername ?: $host->getUsername());

        return $this;
    }

    public function getPort()
    {
        if ($this->host->getTunnelHost()) {
            return $this->host->getTunnelHost()->getSshTunnel()->getPort();
        } else {
            return $this->tunnelPort ?: $this->host->getSshPort();
        }
    }

    public function getUsername()
    {
        if ($this->host->getTunnelHost()) {
            return $this->host->getTunnelHost()->getSshTunnel()->getUsername();
        } else {
            return $this->tunnelUsername ?: $this->host->getUsername();
        }
    }

    public function getHostname()
    {
        if ($this->host->getTunnelHost()) {
            return $this->host->getTunnelHost()->getSshTunnel()->getHostname();
        } else {
            return $this->tunnelPort ? 'localhost' : $this->host->getHostname();
        }
    }

    public function getCommand()
    {
        if ($this->host->getTunnelHost()) {
            return $this->host->getTunnelHost()->getSshTunnel()->getCommand();
        } else {
            return sprintf(
                'ssh -p %d -i %s %s',
                $this->getPort(),
                escapeshellarg($this->host->getSshPrivateKey()),
                $this->getSshOptions($this->host)
            );
        }
    }

    public function getSshOptions(Host $host)
    {
        $options = [
            'StrictHostKeyChecking'    => 'no',
            'PreferredAuthentications' => 'publickey',
            'ControlMaster'            => 'auto',
            'ControlPath'              => '~/.ssh/%r@%h:%p'
        ];

        if ($this->batchMode) {
            $options['BatchMode'] = 'yes';
        }

        if ('localhost' === $this->getHostname()) {
            $options['UserKnownHostsFile'] = '/dev/null';
            $options['LogLevel']           = 'error';
        }

        if ($host) {
            foreach ($host->getAdditionalSshOptions() as $option => $value) {
                $options[$option] = $value;
            }
        }

        return $this->assembleSshOptionsString($options);
    }

    public function setUp()
    {
        if ($this->host->getTunnelHost()) {
            $tunnel = $this->host->getTunnelHost()->getSshTunnel();
            $tunnel->tunnelConnectionsForHost($this->host, $this->tunnelUsername);
            $tunnel->setUp();
        }

        if ($this->tunnelConnectionsForHost) {
            $this->tunnelPort = $this->findAvailableLocalPort();<commit_msg>Do not prefer public key auth when batch mode is off.<commit_after><?php

namespace DeltaCli;

use DeltaCli\Exception\TunnelConnectionFailure as TunnelConnectionFailureException;

class SshTunnel
{
    /**
     * @var Host
     */
    private $host;

    /**
     * @var int
     */
    private $remotePort;

    /**
     * @var Host
     */
    private $tunnelConnectionsForHost;

    /**
     * @var string
     */
    private $tunnelUsername;

    /**
     * @var integer
     */
    private $tunnelPort;

    /**
     * @var int
     */
    private $sshProcess;

    /**
     * @var bool
     */
    private $batchMode = true;

    public function __construct(Host $host, $remotePort = 22)
    {
        $this->host       = $host;
        $this->remotePort = $remotePort;
    }

    public function setRemotePort($remotePort)
    {
        $this->remotePort = $remotePort;

        return $this;
    }

    public function setBatchMode($batchMode)
    {
        $this->batchMode = $batchMode;

        return $this;
    }

    public function tunnelConnectionsForHost(Host $host, $tunnelUsername)
    {
        $this->tunnelConnectionsForHost = $host;
        $this->tunnelUsername           = ($tunnelUsername ?: $host->getUsername());

        return $this;
    }

    public function getPort()
    {
        if ($this->host->getTunnelHost()) {
            return $this->host->getTunnelHost()->getSshTunnel()->getPort();
        } else {
            return $this->tunnelPort ?: $this->host->getSshPort();
        }
    }

    public function getUsername()
    {
        if ($this->host->getTunnelHost()) {
            return $this->host->getTunnelHost()->getSshTunnel()->getUsername();
        } else {
            return $this->tunnelUsername ?: $this->host->getUsername();
        }
    }

    public function getHostname()
    {
        if ($this->host->getTunnelHost()) {
            return $this->host->getTunnelHost()->getSshTunnel()->getHostname();
        } else {
            return $this->tunnelPort ? 'localhost' : $this->host->getHostname();
        }
    }

    public function getCommand()
    {
        if ($this->host->getTunnelHost()) {
            return $this->host->getTunnelHost()->getSshTunnel()->getCommand();
        } else {
            return sprintf(
                'ssh -p %d -i %s %s',
                $this->getPort(),
                escapeshellarg($this->host->getSshPrivateKey()),
                $this->getSshOptions($this->host)
            );
        }
    }

    public function getSshOptions(Host $host)
    {
        $options = [
            'StrictHostKeyChecking'    => 'no',
            'ControlMaster'            => 'auto',
            'ControlPath'              => '~/.ssh/%r@%h:%p'
        ];

        if ($this->batchMode) {
            $options['BatchMode']                = 'yes';
            $options['PreferredAuthentications'] = 'publickey';
        }

        if ('localhost' === $this->getHostname()) {
            $options['UserKnownHostsFile'] = '/dev/null';
            $options['LogLevel']           = 'error';
        }

        if ($host) {
            foreach ($host->getAdditionalSshOptions() as $option => $value) {
                $options[$option] = $value;
            }
        }

        return $this->assembleSshOptionsString($options);
    }

    public function setUp()
    {
        if ($this->host->getTunnelHost()) {
            $tunnel = $this->host->getTunnelHost()->getSshTunnel();
            $tunnel->tunnelConnectionsForHost($this->host, $this->tunnelUsername);
            $tunnel->setUp();
        }

        if ($this->tunnelConnectionsForHost) {
            $this->tunnelPort = $this->findAvailableLocalPort();
{'fraction_non_alphanumeric': 0.15008178844056708, 'fraction_numerical': 0.0005452562704471102, 'mean_word_length': 1.823008849557522, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 134, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '793172', 'n_tokens_mistral': 2201, 'n_tokens_neox': 2127, 'n_words': 361}