Search in sources :

Example 1 with HTTPProxyData

use of com.trilead.ssh2.HTTPProxyData in project intellij-community by JetBrains.

the class SshProxyFactory method createAndRegister.

@Nullable
public static ProxyData createAndRegister(final ConnectionSettings connectionSettings) {
    if (!connectionSettings.isUseProxy())
        return null;
    final int type = connectionSettings.getProxyType();
    ProxyData result = null;
    if (ProxySettings.SOCKS4 == type || ProxySettings.SOCKS5 == type) {
        result = new SocksProxyData(connectionSettings);
        SocksAuthenticatorManager.getInstance().register(connectionSettings);
    } else if (ProxySettings.HTTP == type) {
        result = new HTTPProxyData(connectionSettings.getProxyHostName(), connectionSettings.getProxyPort(), connectionSettings.getProxyLogin(), connectionSettings.getProxyPassword());
    }
    return result;
}
Also used : HTTPProxyData(com.trilead.ssh2.HTTPProxyData) ProxyData(com.trilead.ssh2.ProxyData) HTTPProxyData(com.trilead.ssh2.HTTPProxyData) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with HTTPProxyData

use of com.trilead.ssh2.HTTPProxyData in project intellij-community by JetBrains.

the class BasicWithHTTPProxy method main.

public static void main(String[] args) {
    String hostname = "my-ssh-server";
    String username = "joe";
    String password = "joespass";
    String proxyHost = "192.168.1.1";
    // default port used by squid
    int proxyPort = 3128;
    try {
        /* Create a connection instance */
        Connection conn = new Connection(hostname);
        /* We want to connect through a HTTP proxy */
        conn.setProxyData(new HTTPProxyData(proxyHost, proxyPort));
        // if the proxy requires basic authentication:
        // conn.setProxyData(new HTTPProxyData(proxyHost, proxyPort, "username", "secret"));
        /* Now connect (through the proxy) */
        conn.connect();
        /* Authenticate.
			 * If you get an IOException saying something like
			 * "Authentication method password not supported by the server at this stage."
			 * then please check the FAQ.
			 */
        boolean isAuthenticated = conn.authenticateWithPassword(username, password);
        if (isAuthenticated == false)
            throw new IOException("Authentication failed.");
        /* Create a session */
        Session sess = conn.openSession();
        sess.execCommand("uname -a && date && uptime && who");
        System.out.println("Here is some information about the remote host:");
        /* 
			 * This basic example does not handle stderr, which is sometimes dangerous
			 * (please read the FAQ).
			 */
        InputStream stdout = new StreamGobbler(sess.getStdout());
        BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
        while (true) {
            String line = br.readLine();
            if (line == null)
                break;
            System.out.println(line);
        }
        /* Show exit status, if available (otherwise "null") */
        System.out.println("ExitCode: " + sess.getExitStatus());
        /* Close this session */
        sess.close();
        /* Close the connection */
        conn.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
        System.exit(2);
    }
}
Also used : StreamGobbler(com.trilead.ssh2.StreamGobbler) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Connection(com.trilead.ssh2.Connection) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) HTTPProxyData(com.trilead.ssh2.HTTPProxyData) Session(com.trilead.ssh2.Session)

Example 3 with HTTPProxyData

use of com.trilead.ssh2.HTTPProxyData in project intellij-community by JetBrains.

the class SocketFactory method open.

public static Socket open(final String hostname, final int port, final ProxyData proxyData, final int connectTimeout) throws IOException {
    final Socket sock = new Socket();
    if (proxyData == null) {
        InetAddress addr = TransportManager.createInetAddress(hostname);
        sock.connect(new InetSocketAddress(addr, port), connectTimeout);
        sock.setSoTimeout(0);
        return sock;
    }
    if (proxyData instanceof SelfConnectionProxyData) {
        // already connected
        return ((SelfConnectionProxyData) proxyData).connect();
    }
    if (proxyData instanceof HTTPProxyData) {
        HTTPProxyData pd = (HTTPProxyData) proxyData;
        /* At the moment, we only support HTTP proxies */
        InetAddress addr = TransportManager.createInetAddress(pd.proxyHost);
        sock.connect(new InetSocketAddress(addr, pd.proxyPort), connectTimeout);
        sock.setSoTimeout(0);
        /* OK, now tell the proxy where we actually want to connect to */
        StringBuffer sb = new StringBuffer();
        sb.append("CONNECT ");
        sb.append(hostname);
        sb.append(':');
        sb.append(port);
        sb.append(" HTTP/1.0\r\n");
        if ((pd.proxyUser != null) && (pd.proxyPass != null)) {
            String credentials = pd.proxyUser + ":" + pd.proxyPass;
            char[] encoded = Base64.encode(credentials.getBytes("ISO-8859-1"));
            sb.append("Proxy-Authorization: Basic ");
            sb.append(encoded);
            sb.append("\r\n");
        }
        if (pd.requestHeaderLines != null) {
            for (int i = 0; i < pd.requestHeaderLines.length; i++) {
                if (pd.requestHeaderLines[i] != null) {
                    sb.append(pd.requestHeaderLines[i]);
                    sb.append("\r\n");
                }
            }
        }
        sb.append("\r\n");
        OutputStream out = sock.getOutputStream();
        out.write(sb.toString().getBytes("ISO-8859-1"));
        out.flush();
        /* Now parse the HTTP response */
        byte[] buffer = new byte[1024];
        InputStream in = sock.getInputStream();
        int len = ClientServerHello.readLineRN(in, buffer);
        String httpReponse = new String(buffer, 0, len, "ISO-8859-1");
        if (httpReponse.startsWith("HTTP/") == false)
            throw new IOException("The proxy did not send back a valid HTTP response.");
        if ((httpReponse.length() < 14) || (httpReponse.charAt(8) != ' ') || (httpReponse.charAt(12) != ' '))
            throw new IOException("The proxy did not send back a valid HTTP response.");
        int errorCode = 0;
        try {
            errorCode = Integer.parseInt(httpReponse.substring(9, 12));
        } catch (NumberFormatException ignore) {
            throw new IOException("The proxy did not send back a valid HTTP response.");
        }
        if ((errorCode < 0) || (errorCode > 999))
            throw new IOException("The proxy did not send back a valid HTTP response.");
        if (errorCode != 200) {
            throw new HTTPProxyException(httpReponse.substring(13), errorCode);
        }
        while (true) {
            len = ClientServerHello.readLineRN(in, buffer);
            if (len == 0)
                break;
        }
        return sock;
    }
    throw new IOException("Unsupported ProxyData");
}
Also used : HTTPProxyException(com.trilead.ssh2.HTTPProxyException) InetSocketAddress(java.net.InetSocketAddress) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) SelfConnectionProxyData(com.trilead.ssh2.SelfConnectionProxyData) InetAddress(java.net.InetAddress) HTTPProxyData(com.trilead.ssh2.HTTPProxyData) Socket(java.net.Socket)

Aggregations

HTTPProxyData (com.trilead.ssh2.HTTPProxyData)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Connection (com.trilead.ssh2.Connection)1 HTTPProxyException (com.trilead.ssh2.HTTPProxyException)1 ProxyData (com.trilead.ssh2.ProxyData)1 SelfConnectionProxyData (com.trilead.ssh2.SelfConnectionProxyData)1 Session (com.trilead.ssh2.Session)1 StreamGobbler (com.trilead.ssh2.StreamGobbler)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 Socket (java.net.Socket)1 Nullable (org.jetbrains.annotations.Nullable)1