Java https proxy (using https.proxyPort and https.proxyHost) -
i making java application relies on setting http.proxyport
, http.proxyhost
. there 2 processes: 1 regular program, other proxy. have simple socket listener running on http.proxyport
(which control). it's simple as
while (true) { try { socket connection = server.accept(); handler handler = new handler(connection); handler.start(); } catch (exception ex) { ex.printstacktrace(); } }
so whenever "process 1" makes http request -
url yahoo = new url("http://www.google.ca/"); urlconnection yc = yahoo.openconnection(); system.out.println(yc.getclass().getname()); bufferedreader in = new bufferedreader(new inputstreamreader(yc.getinputstream()));
it goes through proxy. if client using https protocol? instead use https://google.ca
? there's property https.proxyport
, https.proxyhost
, i've literally been trying months (on , off, it's not important) without luck. i've read bunch of threads (i list @ end know have done something).
my closest attempt far: server
try { system.setproperty("javax.net.ssl.keystore", "test.jks"); system.setproperty("javax.net.ssl.keystorepassword", "2520xe"); sslserversocketfactory sslserversocketfactory = (sslserversocketfactory) sslserversocketfactory.getdefault(); sslserversocket sslserversocket = (sslserversocket) sslserversocketfactory.createserversocket(9999); system.out.println("ready"); sslsocket sslsocket = (sslsocket) sslserversocket.accept(); inputstream inputstream = sslsocket.getinputstream(); inputstreamreader inputstreamreader = new inputstreamreader(inputstream); bufferedreader bufferedreader = new bufferedreader(inputstreamreader); outputstream toclient = sslsocket.getoutputstream(); toclient.write(("http/1.0 200 connection established\n" + "content-length: " + "shut down!".getbytes().length + "\r\n").getbytes("utf-8")); toclient.write("shut down!".getbytes("utf-8")); toclient.close(); } catch (exception exception) { exception.printstacktrace(); }
client
try { system.setproperty("https.proxyhost", "127.0.0.1"); system.setproperty("https.proxyport", "9999"); url yahoo = new url("https://www.google.ca/"); urlconnection yc = yahoo.openconnection(); system.out.println(yc.getclass().getname()); bufferedreader in = new bufferedreader( new inputstreamreader( yc.getinputstream())); string inputline; while ((inputline = in.readline()) != null) system.out.println(inputline); in.close(); } catch (exception ex) { ex.printstacktrace(); }
and error javax.net.ssl.sslexception: unrecognized ssl message, plaintext connection?
googled came mail stuff instead.
basically, need create java proxy server, that's set client https.proxyport
, https.proxyhost
flags, , can send data client app, may not modified in way (it's using url connection = new url("https://...")
)
a few of sites tried...
- creating java proxy server accepts https
- http://stilius.net/java/java_ssl.php
- there else getting java accept certificates, can't find of links. have code, encountered more errors thing i'm doing right now, can include if helps (i didn't because long question)
as auntyellow commented: don't need ssl-fiddling yourself. https-proxying forwarding binary data between 2 parties.
to cite draft-luotonen-web-proxy-tunneling-01.txt:
client -> server server -> client -------------------------------------- ----------------------------------- connect home.netscape.com:443 http/1.0 user-agent: mozilla/4.0 <<< empty line >>> http/1.0 200 connection established proxy-agent: netscape-proxy/1.1 <<< empty line >>> <<< data tunneling both directions begins >>>
so need ensure trust client enough connect proxies firewall-position given host , port. because of common practice limit allowed port 443, reject connection localhost , "untrusted" parties.
this "simple" server usable https.proxy
in java if not jet convinced:
import java.io.*; import java.net.serversocket; import java.net.socket; import java.util.regex.matcher; import java.util.regex.pattern; /** * created http://stackoverflow.com/q/16351413/1266906. */ public class server extends thread { public static void main(string[] args) { (new server()).run(); } public server() { super("server thread"); } @override public void run() { try (serversocket serversocket = new serversocket(9999)) { socket socket; try { while ((socket = serversocket.accept()) != null) { (new handler(socket)).start(); } } catch (ioexception e) { e.printstacktrace(); // todo: implement catch } } catch (ioexception e) { e.printstacktrace(); // todo: implement catch return; } } public static class handler extends thread { public static final pattern connect_pattern = pattern.compile("connect (.+):(.+) http/(1\\.[01])", pattern.case_insensitive); private final socket clientsocket; private boolean previouswasr = false; public handler(socket clientsocket) { this.clientsocket = clientsocket; } @override public void run() { try { string request = readline(clientsocket); system.out.println(request); matcher matcher = connect_pattern.matcher(request); if (matcher.matches()) { string header; { header = readline(clientsocket); } while (!"".equals(header)); outputstreamwriter outputstreamwriter = new outputstreamwriter(clientsocket.getoutputstream(), "iso-8859-1"); final socket forwardsocket; try { forwardsocket = new socket(matcher.group(1), integer.parseint(matcher.group(2))); system.out.println(forwardsocket); } catch (ioexception | numberformatexception e) { e.printstacktrace(); // todo: implement catch outputstreamwriter.write("http/" + matcher.group(3) + " 502 bad gateway\r\n"); outputstreamwriter.write("proxy-agent: simple/0.1\r\n"); outputstreamwriter.write("\r\n"); outputstreamwriter.flush(); return; } try { outputstreamwriter.write("http/" + matcher.group(3) + " 200 connection established\r\n"); outputstreamwriter.write("proxy-agent: simple/0.1\r\n"); outputstreamwriter.write("\r\n"); outputstreamwriter.flush(); thread remotetoclient = new thread() { @override public void run() { forwarddata(forwardsocket, clientsocket); } }; remotetoclient.start(); try { if (previouswasr) { int read = clientsocket.getinputstream().read(); if (read != -1) { if (read != '\n') { forwardsocket.getoutputstream().write(read); } forwarddata(clientsocket, forwardsocket); } else { if (!forwardsocket.isoutputshutdown()) { forwardsocket.shutdownoutput(); } if (!clientsocket.isinputshutdown()) { clientsocket.shutdowninput(); } } } else { forwarddata(clientsocket, forwardsocket); } } { try { remotetoclient.join(); } catch (interruptedexception e) { e.printstacktrace(); // todo: implement catch } } } { forwardsocket.close(); } } } catch (ioexception e) { e.printstacktrace(); // todo: implement catch } { try { clientsocket.close(); } catch (ioexception e) { e.printstacktrace(); // todo: implement catch } } } private static void forwarddata(socket inputsocket, socket outputsocket) { try { inputstream inputstream = inputsocket.getinputstream(); try { outputstream outputstream = outputsocket.getoutputstream(); try { byte[] buffer = new byte[4096]; int read; { read = inputstream.read(buffer); if (read > 0) { outputstream.write(buffer, 0, read); if (inputstream.available() < 1) { outputstream.flush(); } } } while (read >= 0); } { if (!outputsocket.isoutputshutdown()) { outputsocket.shutdownoutput(); } } } { if (!inputsocket.isinputshutdown()) { inputsocket.shutdowninput(); } } } catch (ioexception e) { e.printstacktrace(); // todo: implement catch } } private string readline(socket socket) throws ioexception { bytearrayoutputstream bytearrayoutputstream = new bytearrayoutputstream(); int next; readerloop: while ((next = socket.getinputstream().read()) != -1) { if (previouswasr && next == '\n') { previouswasr = false; continue; } previouswasr = false; switch (next) { case '\r': previouswasr = true; break readerloop; case '\n': break readerloop; default: bytearrayoutputstream.write(next); break; } } return bytearrayoutputstream.tostring("iso-8859-1"); } } }
Comments
Post a Comment