ssl - How can a java proxy handle "https" requests to multiple hosts? -
i trying make proxy server in java , able make working proxy handles http requests properly. after searching lot able extend program https requests following answer similar question: https://stackoverflow.com/a/9389125/5309299
here's code after tcp connection established between client , proxy:
string request = ""; byte[] requestbytearr; //read complete request while(true){ string requestline = bufferedreaderfromclient.readline() + "\r\n"; if (requestline.trim().length()==0 && !request.equals("")){ request+=requestline; requestbytearr = request.getbytes(); system.out.println(request); break; } else { request+=requestline; } } string hostname = gethostfromrequest(request); int remoteport = getremoteportfromrequest(request); if (request.startswith("connect")){ //establish connection between host , proxy final socket hostsocket = new socket(hostname, remoteport); //tell client connection successful string statusline = "http/1.1 200 connection established \n" + "proxy-agent: proxyserver/1.0\n" + "\r\n"; outtoclient.write(statusline.getbytes()); outtoclient.flush(); //new thread handle incoming responses host new thread(){ public void run(){ try{ inputstream infromhost = hostsocket.getinputstream(); while(true){ byte[] bufread = new byte[128]; int bytes_received; while ((bytes_received = infromhost.read(bufread)) > 0){ outtoclient.write(bufread, 0, bytes_received); outtoclient.flush(); } } } catch (ioexception e){ e.printstacktrace(); } } }.start(); //main thread handles incoming requests client outputstream outtohost = hostsocket.getoutputstream(); while (true){ byte[] bufread = new byte[128]; int bytes_received; while ((bytes_received = infromclient.read(bufread)) > 0){ outtohost.write(bufread, 0, bytes_received); outtohost.flush(); } } }
obviously, works 1 host, i.e. when client (e.g. chrome browser) sends connect request 1 host (e.g. "www.google.com:443"). want client able connect multiple hosts. problem since requests come after connect request encrypted, proxy server not able determine request meant host, cannot forward requests.
Comments
Post a Comment