Example #1
0
 public void run() {
   try {
     InputStream in;
     OutputStream out;
     try {
       in = sk.getInputStream();
       out = sk.getOutputStream();
     } catch (IOException e) {
       throw (new RuntimeException(e));
     }
     while (true) {
       try {
         int len = Utils.int32d(read(in, 4), 0);
         if (!auth && (len > 256)) return;
         Message msg = new MessageBuf(read(in, len));
         String cmd = msg.string();
         Object[] args = msg.list();
         Object[] reply;
         if (auth) {
           Command cc = commands.get(cmd);
           if (cc != null) reply = cc.run(this, args);
           else reply = new Object[] {"nocmd"};
         } else {
           if (cmd.equals("nonce")) {
             reply = new Object[] {nonce};
           } else if (cmd.equals("auth")) {
             if (Arrays.equals((byte[]) args[0], ckey)) {
               reply = new Object[] {"ok"};
               auth = true;
             } else {
               reply = new Object[] {"no"};
             }
           } else {
             return;
           }
         }
         MessageBuf rb = new MessageBuf();
         rb.addlist(reply);
         byte[] rbuf = new byte[4 + rb.size()];
         Utils.uint32e(rb.size(), rbuf, 0);
         rb.fin(rbuf, 4);
         out.write(rbuf);
       } catch (IOException e) {
         return;
       }
     }
   } catch (InterruptedException e) {
   } finally {
     try {
       sk.close();
     } catch (IOException e) {
       throw (new RuntimeException(e));
     }
   }
 }
 public String tryauth(AuthClient cl) throws IOException {
   if (WebBrowser.self == null) {
     throw (new AuthException("Could not find any web browser to launch"));
   }
   Message rpl = cl.cmd("web", method());
   String stat = rpl.string();
   URL url;
   if (stat.equals("ok")) {
     url = new URL(rpl.string());
   } else if (stat.equals("no")) {
     throw (new AuthException(rpl.string()));
   } else {
     throw (new RuntimeException("Unexpected reply `" + stat + "' from auth server"));
   }
   try {
     WebBrowser.self.show(url);
   } catch (WebBrowser.BrowserException e) {
     throw (new AuthException("Could not launch web browser"));
   }
   rpl = cl.cmd("wait");
   stat = rpl.string();
   if (stat.equals("ok")) {
     return (rpl.string());
   } else if (stat.equals("no")) {
     throw (new AuthException(rpl.string()));
   } else {
     throw (new RuntimeException("Unexpected reply `" + stat + "' from auth server"));
   }
 }