/* This is a java client for proxy2ssh (a CGI ssh proxy) http://wari.mckay.com/~rm/proxy2ssh/ Robert McKay Configure it: Change the baseurl to the url of your ssh.cgi script. Compile it: javac client.java Use it: ssh -oProxyCommand="java client %h" user@host or (going via a proxy server): ssh -oProxyCommand="java -Dhttp.proxySet=true -Dhttp.proxyHost=squid.acme.com -Dhttp.proxyPort=3128 client %h" user@host Note: This is pretty horrible at the moment.. I might try and clean it up a bit later.. it does work though.. just about. */ import java.io.FileDescriptor; import java.net.HttpURLConnection; import java.net.URLConnection; import java.io.FileOutputStream; import java.io.FileInputStream; import java.io.OutputStream; import java.io.InputStream; import java.net.URL; import java.net.MalformedURLException; public class client extends Thread { public OutputStream post = null; public InputStream get = null; public URLConnection urlget = null; public HttpURLConnection urlpost = null; public OutputStream stdout = null; public InputStream stdin = null; boolean modepost = false; String baseurl = "http://www.acme.com/cgi-bin/ssh.cgi"; public client() { // stdout = new FileOutputStream(FileDescriptor.out); // stdin = new FileInputStream(FileDescriptor.in); stdout = System.out; stdin = System.in; } public client(InputStream getstream) { // stdout = new FileOutputStream(FileDescriptor.out); // stdin = new FileInputStream(FileDescriptor.in); stdout = System.out; stdin = System.in; modepost=false; get=getstream; } public client(OutputStream poststream) { // stdout = new FileOutputStream(FileDescriptor.out); // stdin = new FileInputStream(FileDescriptor.in); stdout = System.out; stdin = System.in; modepost=true; post=poststream; } public void run() { try { if(modepost) { byte wb[] = new byte[1024]; // System.out.println("Writing to connection"); String nl = new String("\n"); post.write(nl.getBytes()); post.flush(); int i=0; while((i=stdin.read(wb))>0) { post.write(wb,0,i); post.flush(); } } else { byte rb[] = new byte[1024]; // System.out.println("Reading from connection"); int i=0; while((i=get.read(rb))>0) { stdout.write(rb,0,i); stdout.flush(); } } } catch(Exception any) { any.printStackTrace(); } } public static void main(String argv[]) { client c = new client(); c.go(argv); } public void go(String argv[]) { try { String hoststr = ""; if (argv.length>0) { hoststr = argv[0]; } else { System.out.println("Please specify hostname"); System.exit(-1); } URL url1 = new URL(baseurl + "?host=" + hoststr); urlget = url1.openConnection(); urlget.setDoOutput(false); urlget.setDoInput(true); // urlget.setRequestMethod("GET"); get = urlget.getInputStream(); byte b[] = new byte[1024]; String key = ""; int start = -1; int end = -1; while(get.read(b)>0) { key += new String(b); start = key.indexOf("Key:"); if (start>=0) { end = key.lastIndexOf("\n"); if (end>=0 && end>start) { break; } } } String keystr = key.substring(start+4, end); // System.out.println("Key is: " + keystr); String url2s = baseurl + "?key=" + keystr; URL url2 = new URL(url2s); urlpost = (HttpURLConnection)url2.openConnection(); urlpost.setUseCaches(false); urlpost.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); urlpost.setFixedLengthStreamingMode(1000000000); urlpost.setAllowUserInteraction(false); urlpost.setDoInput(false); urlpost.setDoOutput(true); post = urlpost.getOutputStream(); client c1 = new client(post); c1.start(); client c2 = new client(get); c2.start(); c1.join(); } catch (Exception e) { e.printStackTrace(); } } }