added a Main with code to screen-scrape the ac parameter
[rsa2client.git] / src / mcsClient / Main.java
1 package mcsClient;
2 import java.io.*;
3 import java.security.*;
4 import java.security.cert.*;
5 import java.net.*;
6 import javax.net.*;
7 import javax.net.ssl.*;
8
9 public class Main {
10     private static boolean stop;
11     public static void main(String[] s) throws Exception {
12         if (s.length != 1 || s[0].indexOf('@')==-1) {
13             System.err.println("usage: java -jar mcs.jar <username>@<hostname>");
14             System.exit(-1);
15         }
16
17         final String user = s[0].substring(0, s[0].indexOf('@'));
18         String host = s[0].substring(s[0].indexOf('@')+1);
19         System.out.print("password for "+s[0]+":  ");
20
21         stop = false;
22         new Thread() {
23             public void run() {
24                 while (!stop) {
25                     System.out.print("\010*");
26                     try { Thread.currentThread().sleep(1); }
27                     catch(InterruptedException ie) { ie.printStackTrace(); }
28                 }
29             }
30         }.start();
31         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
32         final String password = in.readLine();
33         stop = true;
34         System.out.println();
35
36         Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
37         TrustManager[] trustAllCerts = new TrustManager[] {
38             new X509TrustManager() {
39                 public X509Certificate[] getAcceptedIssuers() { return null; }
40                 public void checkServerTrusted(X509Certificate[] certs, String authType)
41                     throws CertificateException { return; }
42                 public void checkClientTrusted(X509Certificate[] certs, String authType)
43                     throws CertificateException { return; }
44             }
45         };
46         SSLContext sc = SSLContext.getInstance("SSL");
47         sc.init(null, trustAllCerts, new SecureRandom());
48         HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
49         HostnameVerifier hv = new HostnameVerifier() {
50                 public boolean verify(String urlHostName, SSLSession session) {
51                     return true; } };
52         HttpsURLConnection.setDefaultHostnameVerifier(hv);
53
54         Authenticator.setDefault(new Authenticator() {
55                 protected PasswordAuthentication getPasswordAuthentication() {
56                     return new PasswordAuthentication (user, password.toCharArray());
57                 }
58             });
59
60         BufferedReader br;
61         HttpURLConnection con;
62
63         System.out.println();
64         System.out.println();
65         System.out.println("*** requesting welcome.ssi...");
66         con = (HttpURLConnection)new URL("https://"+host+"/private/welcome.ssi").openConnection();
67         ((HttpsURLConnection)con).setHostnameVerifier(new HostnameVerifier() {
68                 public boolean verify(String hostname, SSLSession session) { return true; } });
69         br = new BufferedReader(new InputStreamReader(con.getInputStream()));
70         for(String ss = br.readLine(); ss!=null; ss=br.readLine())
71             System.err.println(ss);
72
73         String token = null;
74         System.out.println();
75         System.out.println();
76         System.out.println("*** requesting rc_frame.ssi...");
77         con = (HttpURLConnection)new URL("https://"+host+"/private/rc_frame.ssi").openConnection();
78         ((HttpsURLConnection)con).setHostnameVerifier(new HostnameVerifier() {
79                 public boolean verify(String hostname, SSLSession session) { return true; } });
80         br = new BufferedReader(new InputStreamReader(con.getInputStream()));
81         for(String ss = br.readLine(); ss!=null; ss=br.readLine()) {
82             System.err.println(ss);
83             if (ss.startsWith("ac =\"")) {
84                 ss = ss.substring(5).trim();
85                 while(ss.charAt(ss.length()-1)=='\"')
86                     ss = ss.substring(0, ss.length()-1);
87                 token = ss;
88             }
89         }
90         
91         System.out.println();
92         System.out.println();
93         if (token!=null)
94             McsClient.go(host, user, token);
95     }
96 }