Search in sources :

Example 1 with HttpException

use of jodd.http.HttpException in project jodd by oblac.

the class SocketHttpConnectionProvider method createHttpConnection.

/**
	 * Creates new connection from current {@link jodd.http.HttpRequest request}.
	 *
	 * @see #createSocket(String, int, int)
	 */
public HttpConnection createHttpConnection(HttpRequest httpRequest) throws IOException {
    SocketHttpConnection httpConnection;
    final boolean https = httpRequest.protocol().equalsIgnoreCase("https");
    if (https) {
        SSLSocket sslSocket = createSSLSocket(httpRequest.host(), httpRequest.port(), httpRequest.connectionTimeout(), httpRequest.trustAllCertificates(), httpRequest.verifyHttpsHost());
        httpConnection = new SocketHttpSecureConnection(sslSocket);
    } else {
        Socket socket = createSocket(httpRequest.host(), httpRequest.port(), httpRequest.connectionTimeout());
        httpConnection = new SocketHttpConnection(socket);
    }
    // prepare connection config
    httpConnection.setTimeout(httpRequest.timeout());
    try {
        // additional socket initialization
        httpConnection.init();
    } catch (Throwable throwable) {
        // @wjw_add
        httpConnection.close();
        throw new HttpException(throwable);
    }
    return httpConnection;
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) HttpException(jodd.http.HttpException) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket)

Example 2 with HttpException

use of jodd.http.HttpException in project jodd by oblac.

the class Socks4ProxySocketFactory method createSocks4ProxySocket.

/**
	 * Connects to the SOCKS4 proxy and returns proxified socket.
	 */
private Socket createSocks4ProxySocket(String host, int port) {
    Socket socket = null;
    String proxyHost = proxy.getProxyAddress();
    int proxyPort = proxy.getProxyPort();
    String user = proxy.getProxyUsername();
    try {
        socket = new Socket(proxyHost, proxyPort);
        InputStream in = socket.getInputStream();
        OutputStream out = socket.getOutputStream();
        socket.setTcpNoDelay(true);
        byte[] buf = new byte[1024];
        // 1) CONNECT
        int index = 0;
        buf[index++] = 4;
        buf[index++] = 1;
        buf[index++] = (byte) (port >>> 8);
        buf[index++] = (byte) (port & 0xff);
        InetAddress addr = InetAddress.getByName(host);
        byte[] byteAddress = addr.getAddress();
        for (byte byteAddres : byteAddress) {
            buf[index++] = byteAddres;
        }
        if (user != null) {
            System.arraycopy(user.getBytes(), 0, buf, index, user.length());
            index += user.length();
        }
        buf[index++] = 0;
        out.write(buf, 0, index);
        // 2) RESPONSE
        int len = 6;
        int s = 0;
        while (s < len) {
            int i = in.read(buf, s, len - s);
            if (i <= 0) {
                throw new HttpException(ProxyInfo.ProxyType.SOCKS4, "stream is closed");
            }
            s += i;
        }
        if (buf[0] != 0) {
            throw new HttpException(ProxyInfo.ProxyType.SOCKS4, "proxy returned VN " + buf[0]);
        }
        if (buf[1] != 90) {
            try {
                socket.close();
            } catch (Exception ignore) {
            }
            throw new HttpException(ProxyInfo.ProxyType.SOCKS4, "proxy returned CD " + buf[1]);
        }
        byte[] temp = new byte[2];
        in.read(temp, 0, 2);
        return socket;
    } catch (RuntimeException rtex) {
        closeSocket(socket);
        throw rtex;
    } catch (Exception ex) {
        closeSocket(socket);
        throw new HttpException(ProxyInfo.ProxyType.SOCKS4, ex.toString(), ex);
    }
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) HttpException(jodd.http.HttpException) InetAddress(java.net.InetAddress) Socket(java.net.Socket) IOException(java.io.IOException) HttpException(jodd.http.HttpException)

Example 3 with HttpException

use of jodd.http.HttpException in project jodd by oblac.

the class HTTPProxySocketFactory method createHttpProxySocket.

private Socket createHttpProxySocket(String host, int port) {
    Socket socket = null;
    String proxyAddress = proxy.getProxyAddress();
    int proxyPort = proxy.getProxyPort();
    try {
        socket = new Socket(proxyAddress, proxyPort);
        String hostport = "CONNECT " + host + ":" + port;
        String proxyLine;
        String username = proxy.getProxyUsername();
        if (username == null) {
            proxyLine = "";
        } else {
            String password = proxy.getProxyPassword();
            proxyLine = "\r\nProxy-Authorization: Basic " + Base64.encodeToString((username + ":" + password));
        }
        socket.getOutputStream().write((hostport + " HTTP/1.1\r\nHost: " + hostport + proxyLine + "\r\n\r\n").getBytes("UTF-8"));
        InputStream in = socket.getInputStream();
        StringBuilder recv = new StringBuilder(100);
        int nlchars = 0;
        while (true) {
            int i = in.read();
            if (i == -1) {
                throw new HttpException(ProxyInfo.ProxyType.HTTP, "Invalid response");
            }
            char c = (char) i;
            recv.append(c);
            if (recv.length() > 1024) {
                throw new HttpException(ProxyInfo.ProxyType.HTTP, "Received header longer then 1024 chars");
            }
            if ((nlchars == 0 || nlchars == 2) && c == '\r') {
                nlchars++;
            } else if ((nlchars == 1 || nlchars == 3) && c == '\n') {
                nlchars++;
            } else {
                nlchars = 0;
            }
            if (nlchars == 4) {
                break;
            }
        }
        String recvStr = recv.toString();
        BufferedReader br = new BufferedReader(new StringReader(recvStr));
        String response = br.readLine();
        if (response == null) {
            throw new HttpException(ProxyInfo.ProxyType.HTTP, "Empty proxy response");
        }
        Matcher m = RESPONSE_PATTERN.matcher(response);
        if (!m.matches()) {
            throw new HttpException(ProxyInfo.ProxyType.HTTP, "Unexpected proxy response");
        }
        int code = Integer.parseInt(m.group(1));
        if (code != HttpURLConnection.HTTP_OK) {
            throw new HttpException(ProxyInfo.ProxyType.HTTP, "Invalid return status code: " + code);
        }
        return socket;
    } catch (RuntimeException rtex) {
        closeSocket(socket);
        throw rtex;
    } catch (Exception ex) {
        closeSocket(socket);
        throw new HttpException(ProxyInfo.ProxyType.HTTP, ex.toString(), ex);
    }
}
Also used : Matcher(java.util.regex.Matcher) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) HttpException(jodd.http.HttpException) Socket(java.net.Socket) IOException(java.io.IOException) HttpException(jodd.http.HttpException)

Example 4 with HttpException

use of jodd.http.HttpException in project jodd by oblac.

the class Socks5ProxySocketFactory method createSocks5ProxySocket.

private Socket createSocks5ProxySocket(String host, int port) {
    Socket socket = null;
    String proxyAddress = proxy.getProxyAddress();
    int proxyPort = proxy.getProxyPort();
    String user = proxy.getProxyUsername();
    String passwd = proxy.getProxyPassword();
    try {
        socket = new Socket(proxyAddress, proxyPort);
        InputStream in = socket.getInputStream();
        OutputStream out = socket.getOutputStream();
        socket.setTcpNoDelay(true);
        byte[] buf = new byte[1024];
        int index = 0;
        // 1) VERSION IDENT/METHOD SELECTION
        buf[index++] = 5;
        buf[index++] = 2;
        // NO AUTHENTICATION REQUIRED
        buf[index++] = 0;
        // USERNAME/PASSWORD
        buf[index++] = 2;
        out.write(buf, 0, index);
        // 2) RESPONSE
        // in.read(buf, 0, 2);
        fill(in, buf, 2);
        boolean check = false;
        switch((buf[1]) & 0xff) {
            case // NO AUTHENTICATION REQUIRED
            0:
                check = true;
                break;
            case // USERNAME/PASSWORD
            2:
                if (user == null || passwd == null) {
                    break;
                }
                // 3) USER/PASS REQUEST
                index = 0;
                buf[index++] = 1;
                buf[index++] = (byte) (user.length());
                System.arraycopy(user.getBytes(), 0, buf, index, user.length());
                index += user.length();
                buf[index++] = (byte) (passwd.length());
                System.arraycopy(passwd.getBytes(), 0, buf, index, passwd.length());
                index += passwd.length();
                out.write(buf, 0, index);
                // 4) RESPONSE, VERIFIED
                // in.read(buf, 0, 2);
                fill(in, buf, 2);
                if (buf[1] == 0) {
                    check = true;
                }
                break;
            default:
        }
        if (!check) {
            try {
                socket.close();
            } catch (Exception ignore) {
            }
            throw new HttpException(ProxyInfo.ProxyType.SOCKS5, "check failed");
        }
        // 5) CONNECT
        index = 0;
        buf[index++] = 5;
        // CONNECT
        buf[index++] = 1;
        buf[index++] = 0;
        byte[] hostb = host.getBytes();
        int len = hostb.length;
        // DOMAINNAME
        buf[index++] = 3;
        buf[index++] = (byte) (len);
        System.arraycopy(hostb, 0, buf, index, len);
        index += len;
        buf[index++] = (byte) (port >>> 8);
        buf[index++] = (byte) (port & 0xff);
        out.write(buf, 0, index);
        // 6) RESPONSE
        // in.read(buf, 0, 4);
        fill(in, buf, 4);
        if (buf[1] != 0) {
            try {
                socket.close();
            } catch (Exception ignore) {
            }
            throw new HttpException(ProxyInfo.ProxyType.SOCKS5, "proxy returned " + buf[1]);
        }
        switch(buf[3] & 0xff) {
            case 1:
                // in.read(buf, 0, 6);
                fill(in, buf, 6);
                break;
            case 3:
                // in.read(buf, 0, 1);
                fill(in, buf, 1);
                // in.read(buf, 0, buf[0]+2);
                fill(in, buf, (buf[0] & 0xff) + 2);
                break;
            case 4:
                // in.read(buf, 0, 18);
                fill(in, buf, 18);
                break;
            default:
        }
        return socket;
    } catch (RuntimeException rttex) {
        closeSocket(socket);
        throw rttex;
    } catch (Exception ex) {
        closeSocket(socket);
        throw new HttpException(ProxyInfo.ProxyType.SOCKS5, ex.toString(), ex);
    }
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) HttpException(jodd.http.HttpException) Socket(java.net.Socket) IOException(java.io.IOException) HttpException(jodd.http.HttpException)

Aggregations

Socket (java.net.Socket)4 HttpException (jodd.http.HttpException)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 OutputStream (java.io.OutputStream)2 BufferedReader (java.io.BufferedReader)1 StringReader (java.io.StringReader)1 InetAddress (java.net.InetAddress)1 Matcher (java.util.regex.Matcher)1 SSLSocket (javax.net.ssl.SSLSocket)1