added a Main with code to screen-scrape the ac parameter
authorAdam Megacz <adam@megacz.com>
Sun, 10 Jan 2010 02:08:12 +0000 (18:08 -0800)
committerAdam Megacz <adam@megacz.com>
Sun, 10 Jan 2010 02:08:12 +0000 (18:08 -0800)
Makefile
src/mcsClient/Main.java [new file with mode: 0644]

index 1a77725..657d6c4 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,9 @@
-mcs.jar:
+mcs.jar: $(shell find class) $(shell find src -name \*.java)
        rm -rf build
        mkdir build
        javac -cp class -d build `find src -name \*.java`
-       jar cvf $@- -C class .
+       echo 'Main-Class: mcsClient/Main' > manifest
+       jar cvmf manifest $@- -C class .
+       rm -f manifest
        jar uvf $@- -C build .
        mv $@- $@
diff --git a/src/mcsClient/Main.java b/src/mcsClient/Main.java
new file mode 100644 (file)
index 0000000..f8c9214
--- /dev/null
@@ -0,0 +1,96 @@
+package mcsClient;
+import java.io.*;
+import java.security.*;
+import java.security.cert.*;
+import java.net.*;
+import javax.net.*;
+import javax.net.ssl.*;
+
+public class Main {
+    private static boolean stop;
+    public static void main(String[] s) throws Exception {
+        if (s.length != 1 || s[0].indexOf('@')==-1) {
+            System.err.println("usage: java -jar mcs.jar <username>@<hostname>");
+            System.exit(-1);
+        }
+
+        final String user = s[0].substring(0, s[0].indexOf('@'));
+        String host = s[0].substring(s[0].indexOf('@')+1);
+        System.out.print("password for "+s[0]+":  ");
+
+        stop = false;
+        new Thread() {
+            public void run() {
+                while (!stop) {
+                    System.out.print("\010*");
+                    try { Thread.currentThread().sleep(1); }
+                    catch(InterruptedException ie) { ie.printStackTrace(); }
+                }
+            }
+        }.start();
+        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
+        final String password = in.readLine();
+        stop = true;
+        System.out.println();
+
+        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
+        TrustManager[] trustAllCerts = new TrustManager[] {
+            new X509TrustManager() {
+                public X509Certificate[] getAcceptedIssuers() { return null; }
+                public void checkServerTrusted(X509Certificate[] certs, String authType)
+                    throws CertificateException { return; }
+                public void checkClientTrusted(X509Certificate[] certs, String authType)
+                    throws CertificateException { return; }
+            }
+        };
+        SSLContext sc = SSLContext.getInstance("SSL");
+        sc.init(null, trustAllCerts, new SecureRandom());
+        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
+        HostnameVerifier hv = new HostnameVerifier() {
+                public boolean verify(String urlHostName, SSLSession session) {
+                    return true; } };
+        HttpsURLConnection.setDefaultHostnameVerifier(hv);
+
+        Authenticator.setDefault(new Authenticator() {
+                protected PasswordAuthentication getPasswordAuthentication() {
+                    return new PasswordAuthentication (user, password.toCharArray());
+                }
+            });
+
+        BufferedReader br;
+        HttpURLConnection con;
+
+        System.out.println();
+        System.out.println();
+        System.out.println("*** requesting welcome.ssi...");
+        con = (HttpURLConnection)new URL("https://"+host+"/private/welcome.ssi").openConnection();
+        ((HttpsURLConnection)con).setHostnameVerifier(new HostnameVerifier() {
+                public boolean verify(String hostname, SSLSession session) { return true; } });
+        br = new BufferedReader(new InputStreamReader(con.getInputStream()));
+        for(String ss = br.readLine(); ss!=null; ss=br.readLine())
+            System.err.println(ss);
+
+        String token = null;
+        System.out.println();
+        System.out.println();
+        System.out.println("*** requesting rc_frame.ssi...");
+        con = (HttpURLConnection)new URL("https://"+host+"/private/rc_frame.ssi").openConnection();
+        ((HttpsURLConnection)con).setHostnameVerifier(new HostnameVerifier() {
+                public boolean verify(String hostname, SSLSession session) { return true; } });
+        br = new BufferedReader(new InputStreamReader(con.getInputStream()));
+        for(String ss = br.readLine(); ss!=null; ss=br.readLine()) {
+            System.err.println(ss);
+            if (ss.startsWith("ac =\"")) {
+                ss = ss.substring(5).trim();
+                while(ss.charAt(ss.length()-1)=='\"')
+                    ss = ss.substring(0, ss.length()-1);
+                token = ss;
+            }
+        }
+        
+        System.out.println();
+        System.out.println();
+        if (token!=null)
+            McsClient.go(host, user, token);
+    }
+}