Skip to content

android ssl via proxy #672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gjsbe opened this issue Feb 27, 2018 · 8 comments
Closed

android ssl via proxy #672

gjsbe opened this issue Feb 27, 2018 · 8 comments

Comments

@gjsbe
Copy link

gjsbe commented Feb 27, 2018

Hi,
I would like to know if it's possibile to use api to connect using ssl and behind a proxy.
Also If there's an option to pass proxy username and password.
Thank you very much

@marci4
Copy link
Collaborator

marci4 commented Feb 27, 2018

@gjsbe
Copy link
Author

gjsbe commented Feb 27, 2018

Hi,
thank you very much, but there's another problem: if I have a proxy pac url how can I use it?

@gjsbe
Copy link
Author

gjsbe commented Feb 27, 2018

Sorry... I found a solution but I have a big problem... It doesn't work:

           SSLContext sslContext = SSLContext.getDefault();
           SSLSocketFactory sslFactory = sslContext.getSocketFactory();

            List<Proxy> proxies = ProxySelector.getDefault().select(new URI("wss://server.it:3333"));
            Proxy proxy;

            if(!proxies.isEmpty()) {

                if(!proxies.get(0).type().equals(Proxy.Type.DIRECT)) {
                    proxy = proxies.get(0);
                } else {
                    proxy = null;
                }

            } else {
                proxy = null;
            }

            if(proxy != null) {
                Socket proxySocket = new Socket(proxy);
                proxySocket.connect(new InetSocketAddress(host, port));
                ws.setSocket(sslFactory.createSocket(proxySocket, host, port, true));
            } else {
                ws.setSocket(sslFactory.createSocket());
            }

select method of proxyselector returns DIRECT for wss and ws protocols.
If I use https uri inside proxyselector select method returns HTTP Type Proxy, but when I use it inside proxy Socket it gives me Invalid proxy, because Android works only with SOCKS proxy (for sockets).

@marci4
Copy link
Collaborator

marci4 commented Feb 28, 2018

Hello @gjsbe,

Why is the example not working for you?
What kind of proxy do you wanna use?
I don't get what you are trying to achieve.. sorry

Greetings
marci4

@gjsbe
Copy link
Author

gjsbe commented Mar 1, 2018

Hi, the problem, from latest android sdk version, is that you cannot set http proxy for a Socket.
You need SOCKS Proxy, but for the ws we need http proxy connection.
So I writed my own proxy connection method (native).
Thank you anyway.

@marci4
Copy link
Collaborator

marci4 commented Mar 1, 2018

Hello @gjsbe,

Do you think you can share your solution to help other developers in the future?
Thank you!

Greetings
marci4

@gjsbe
Copy link
Author

gjsbe commented Mar 2, 2018

`

        Socket socket = new Socket();
        socket.connect(new InetSocketAddress(proxy.getHost(), proxy.getPort()));
        socket.setKeepAlive(true);
        socket.setTcpNoDelay(true);
        socket.setReceiveBufferSize(8192);

        String msg = "CONNECT " + dest.getHost() + ":" + dest.getPort() + " HTTP/1.0\r\n";

        msg += "User-Agent: APP/1.0\r\n" +
            "Keep-Alive: 300\r\n" +
            "Proxy-Connection: keep-alive\r\n\r\n";

        byte[] outData = msg.getBytes("ASCII");
        socket.getOutputStream().write(outData);
        socket.getOutputStream().flush();

        int readed;
        ByteArrayOutputStream responseStream = new ByteArrayOutputStream();
        byte[] readBytes = new byte[8192];

        while((readed = socket.getInputStream().read(readBytes)) > 0) {
            responseStream.write(readBytes, 0, readed);

            if(socket.getInputStream().available() == 0) {
                break;
            }
        }

        String response = new String(responseStream.toByteArray(), "ASCII");

`

If response is HTTP 200 then you can use proxy socket inside sslSocketFactory...
There are some things that I don't like, for example the while with available() if... is not a good implementation in some case.
bye

@marci4
Copy link
Collaborator

marci4 commented Mar 4, 2018

It is a starting point for many! :)
Thank you!

Issue solved. Closing it!

Greetings
marci4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants