javasocketssslhttpsproxy-server

Java HTTPS Proxy/Redirect Server


I am trying to have a server (written in java) redirect to a HTTPS url (the url will never change) when accessed. If I compile the code with

java -Djavax.net.ssl.keyStore=mySrvKeystore -Djavax.net.ssl.keyStorePassword=password ProxyServer

and enter in the address, port, and localport as

https://google.com 443 5000

And try accessing

localhost:5000

on my machine, then I get the error

java.net.UnknownHostException: https://google.com

After debugging, I am pretty sure it breaks in this code block when I try to create the SSLSocket (secureServer).

    SSLSocket secureServer;
    try { 
        SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
        secureServer = (SSLSocket) factory.createSocket(host, port);
        from_server = secureServer.getInputStream();
        to_server = secureServer.getOutputStream();
    }

Solution

  • The argument you pass as the host to factory.createSocket(host,port) must not have the protocol prepended to it. It should simply be google.com.

    The reason is that Java is going to take that host parameter and pass it as input to a DNS lookup. If you were to type host https://google.com on the command line, you'd get a similar failure.