<commit_before>/*
 * ################################################################
 *
 * ProActive: The Java(TM) library for Parallel, Distributed,
 *            Concurrent computing with Security and Mobility
 *
 * Copyright (C) 1997-2005 INRIA/University of Nice-Sophia Antipolis
 * Contact: <EMAIL>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 *  Initial developer(s):               The ProActive Team
 *                        http://www.inria.fr/oasis/ProActive/contacts.html
 *  Contributor(s):
 *
 * ################################################################
 */
package org.objectweb.proactive.core.ssh;

import java.io.IOException;
import java.net.InetAddress;
import java.util.Random;

import org.apache.log4j.Logger;
import org.objectweb.proactive.core.ssh.SshParameters;
import org.objectweb.proactive.core.util.log.Loggers;
import org.objectweb.proactive.core.util.log.ProActiveLogger;

import com.jcraft.jsch.*;


/**
 * @author mlacage
 *
 * This class implements a very simple wrapper on top of the jsch
 * library. Most notably, it implements thread-safe and exception-safe
 * creation and destruction of SSH tunnels.
 * Thread-safety is achieved by using a single global lock whenever
 * we need to access the non-thread-safe jsch library for tunnel
 * creation or destruction operations. This global lock is stored in
 * JSchSingleton and is accessed with the acquireLock and releaseLock
 * methods
 */
public class SshTunnel {
    static Logger logger = ProActiveLogger.getLogger(Loggers.SSH);
    private static Random _random = new Random();
    private int _localPort;
    private Session _session;
    private String _username;
    private String _distantHost;
    private String _sshPort;
    private int _distantPort;

    /**
     * Authenticate with the ssh server on the distantHost if needed (authenticated
     * connections are cached). The ssh server is expected to be listening on port
     * proactive.ssh.port. If this property is not set, the server is assumed to be
     * listening on distantHost:22. The Authentication is performed with the username
     * proactive.ssh.username if this property is set. It is performed with the value of
     * user.name otherwise (this is supposed to be the username of the user running this
     * JVM). The SSH Authentication is performed with the standard public/private key
     * authentication. The private key used by this client is either any of the private
     * keys located in proactive.ssh.key_directory if it is set or any of the private
     * keys found in user.home/.ssh/.
     * The public key of the server to which we connect is verified with the file
     * proactive.ssh.known_hosts if this property is set of user.home/.ssh/known_hosts.
     *
     * Once authentication is performed, a tunnel is established from
     * localhost:randomPort to distantHost:distantPort. The randomPort can be otained from
     * this Tunnel with the getPort method after this constructor has completed
     * successfully.
     *
     * @param distantHost the name of the machine to which a tunnel must be established.
     * @param distantPort the port number on the distant machine to which a tunnel must
     *        be established
     * @throws IOException an exception is thrown if either the authentication or the
     *         tunnel establishment fails.
     */
    public SshTunnel(String distantHost, int distantPort)
        throws IOException {
        JSchSingleton.acquireLock();
        try {
            String username = SshParameters.getSshUsername(distantHost);
            String sshPort = SshParameters.getSshPort();
            Session session;
            try {
                session = JSchSingleton.getSession(username, distantHost,
                        sshPort);
            } catch (IOException e) {
                throw new IOException("SSH tunnel failed: 127.0.0.1 -->" +
                    distantHost + ":" + distantPort + "for " + username);
            }
            int lport = 0;
            int i = 0;
            while (i < 5) {
                lport = _random.nextInt(65000) + 1024;
                try {
                    session.setPortForwardingL("127.0.0.1", lport, distantHost,
                        distantPort);
                    _session = session;
                    _localPort = lport;
                    _username = username;
                    _distantHost = distantHost;
                    _distantPort = distantPort;
                    _sshPort = sshPort;
                    break;
                } catch (JSchException e) {
                    i++;
                }
            }
            if (i == 5) {
                throw new IOException(
                    "SSH tunnel failed (could not allocate port number): 127.0.0.1 -->" +
                    distantHost + ":" + distantPort);<commit_msg>TCP port range is [1024, 65565] not [1024, 65000+1024] !<commit_after>/*
 * ################################################################
 *
 * ProActive: The Java(TM) library for Parallel, Distributed,
 *            Concurrent computing with Security and Mobility
 *
 * Copyright (C) 1997-2005 INRIA/University of Nice-Sophia Antipolis
 * Contact: <EMAIL>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 *  Initial developer(s):               The ProActive Team
 *                        http://www.inria.fr/oasis/ProActive/contacts.html
 *  Contributor(s):
 *
 * ################################################################
 */
package org.objectweb.proactive.core.ssh;

import java.io.IOException;
import java.net.InetAddress;
import java.util.Random;

import org.apache.log4j.Logger;
import org.objectweb.proactive.core.ssh.SshParameters;
import org.objectweb.proactive.core.util.log.Loggers;
import org.objectweb.proactive.core.util.log.ProActiveLogger;

import com.jcraft.jsch.*;


/**
 * @author mlacage
 *
 * This class implements a very simple wrapper on top of the jsch
 * library. Most notably, it implements thread-safe and exception-safe
 * creation and destruction of SSH tunnels.
 * Thread-safety is achieved by using a single global lock whenever
 * we need to access the non-thread-safe jsch library for tunnel
 * creation or destruction operations. This global lock is stored in
 * JSchSingleton and is accessed with the acquireLock and releaseLock
 * methods
 */
public class SshTunnel {
    static Logger logger = ProActiveLogger.getLogger(Loggers.SSH);
    private static Random _random = new Random();
    private int _localPort;
    private Session _session;
    private String _username;
    private String _distantHost;
    private String _sshPort;
    private int _distantPort;

    /**
     * Authenticate with the ssh server on the distantHost if needed (authenticated
     * connections are cached). The ssh server is expected to be listening on port
     * proactive.ssh.port. If this property is not set, the server is assumed to be
     * listening on distantHost:22. The Authentication is performed with the username
     * proactive.ssh.username if this property is set. It is performed with the value of
     * user.name otherwise (this is supposed to be the username of the user running this
     * JVM). The SSH Authentication is performed with the standard public/private key
     * authentication. The private key used by this client is either any of the private
     * keys located in proactive.ssh.key_directory if it is set or any of the private
     * keys found in user.home/.ssh/.
     * The public key of the server to which we connect is verified with the file
     * proactive.ssh.known_hosts if this property is set of user.home/.ssh/known_hosts.
     *
     * Once authentication is performed, a tunnel is established from
     * localhost:randomPort to distantHost:distantPort. The randomPort can be otained from
     * this Tunnel with the getPort method after this constructor has completed
     * successfully.
     *
     * @param distantHost the name of the machine to which a tunnel must be established.
     * @param distantPort the port number on the distant machine to which a tunnel must
     *        be established
     * @throws IOException an exception is thrown if either the authentication or the
     *         tunnel establishment fails.
     */
    public SshTunnel(String distantHost, int distantPort)
        throws IOException {
        JSchSingleton.acquireLock();
        try {
            String username = SshParameters.getSshUsername(distantHost);
            String sshPort = SshParameters.getSshPort();
            Session session;
            try {
                session = JSchSingleton.getSession(username, distantHost,
                        sshPort);
            } catch (IOException e) {
                throw new IOException("SSH tunnel failed: 127.0.0.1 -->" +
                    distantHost + ":" + distantPort + "for " + username);
            }
            int lport = 0;
            int i = 0;
            while (i < 5) {
                lport = _random.nextInt(64540) + 1024;
                try {
                    session.setPortForwardingL("127.0.0.1", lport, distantHost,
                        distantPort);
                    _session = session;
                    _localPort = lport;
                    _username = username;
                    _distantHost = distantHost;
                    _distantPort = distantPort;
                    _sshPort = sshPort;
                    break;
                } catch (JSchException e) {
                    i++;
                }
            }
            if (i == 5) {
                throw new IOException(
                    "SSH tunnel failed (could not allocate port number): 127.0.0.1 -->" +
                    distantHost + ":" + distantPort);
{'fraction_non_alphanumeric': 0.08938346944269945, 'fraction_numerical': 0.012384456609530647, 'mean_word_length': 2.437384330660086, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 6, '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': '2885824', 'n_tokens_mistral': 2931, 'n_tokens_neox': 2719, 'n_words': 1128}