Browse Source

Code to store the server certificate.

Douglas William Thrift 8 years ago
parent
commit
959b95ae18

+ 9 - 9
src/net/douglasthrift/bigscreenbot/BigScreenBot.java

@@ -364,21 +364,17 @@ public class BigScreenBot extends Bot
                         {
                             try
                             {
-                                if (remote.startPairDevice(arguments[0], new Remote.Pairing()
+                                if (remote.startPairDevice(arguments[0], new Runnable()
                                 {
                                     @Override
-                                    public void prompt()
+                                    public void run()
                                     {
                                         sendMessage(channel, sender, String.format("enter the code from the device (\"%1$s\") to finish pairing", arguments[0]));
                                     }
-
-                                    @Override
-                                    public void interrupted(InterruptedException exception)
-                                    {
-                                        sendMessage(channel, sender, String.format("pairing with device (\"%1$s\") interrupted", arguments[0]));
-                                    }
                                 }))
-                                    sendMessage(channel, sender, String.format("paired with device (\"%1$s\")", arguments[0]));
+                                    sendMessage(channel, sender, String.format("pairing with device (\"%1$s\") succeeded", arguments[0]));
+                                else
+                                    sendMessage(channel, sender, String.format("pairing with device (\"%1$s\") failed", arguments[0]));
                             }
                             catch (MalformedURLException exception)
                             {
@@ -396,6 +392,10 @@ public class BigScreenBot extends Bot
                             {
                                 error(channel, sender, exception);
                             }
+                            catch (GeneralSecurityException exception)
+                            {
+                                error(channel, sender, exception);
+                            }
                         }
                     }.start();
 

+ 9 - 10
src/net/douglasthrift/bigscreenbot/Fixer.java

@@ -30,20 +30,19 @@ import java.util.logging.LogRecord;
 
 import javax.jmdns.impl.ListenerStatus.ServiceListenerStatus;
 
-class Fixer
+class Fixer implements Filter
 {
     private static Logger logger = Logger.getLogger(ServiceListenerStatus.class.getName());
 
-    static
+    Fixer()
     {
-        logger.setFilter(new Filter()
-        {
-            @Override
-            public boolean isLoggable(LogRecord record)
-            {
-                return false;
-            }
-        });
+        logger.setFilter(this);
+    }
+
+    @Override
+    public boolean isLoggable(LogRecord record)
+    {
+        return false;
     }
 }
 

+ 21 - 12
src/net/douglasthrift/bigscreenbot/Remote.java

@@ -85,12 +85,6 @@ import com.google.polo.wire.WireFormat;
 
 public class Remote implements Closeable
 {
-    public static interface Pairing
-    {
-        public void prompt();
-        public void interrupted(InterruptedException exception);
-    }
-
     private static final String STORE = "bigscreenbot.keystore";
     private static final char[] PASSWORD = "b1GsSC33Nb0T".toCharArray();
     private static final char[] NULL = new char[]{};
@@ -207,7 +201,7 @@ public class Remote implements Closeable
         return devices;
     }
 
-    public boolean startPairDevice(String device, final Pairing pairing) throws MalformedURLException, UnknownHostException, IOException, PoloException
+    public boolean startPairDevice(String device, final Runnable runnable) throws MalformedURLException, UnknownHostException, IOException, PoloException, KeyStoreException, NoSuchAlgorithmException, CertificateException
     {
         SSLSocket socket = (SSLSocket)factory.createSocket();
         final InetSocketAddress address = getDevice(device);
@@ -221,7 +215,7 @@ public class Remote implements Closeable
         session.addInputEncoding(option);
         session.addOutputEncoding(option);
 
-        return session.doPair(new PairingListener()
+        if (session.doPair(new PairingListener()
         {
             @Override
             public void onLogMessage(LogLevel level, String message)
@@ -233,7 +227,7 @@ public class Remote implements Closeable
             @Override
             public void onPerformInputDeviceRole(PairingSession session)
             {
-                pairing.prompt();
+                runnable.run();
 
                 Secret secret = new Secret(), oldSecret = secrets.put(address, secret);
 
@@ -247,7 +241,6 @@ public class Remote implements Closeable
                 catch (InterruptedException exception)
                 {
                     session.teardown();
-                    pairing.interrupted(exception);
                 }
             }
 
@@ -270,9 +263,25 @@ public class Remote implements Closeable
                 if (verbose)
                     System.out.println(session + " ended.");
             }
-        });
+        }))
+        {
+            synchronized (store)
+            {
+                Certificate certificate = context.getServerCertificate();
+                String alias = String.format(REMOTE_ALIAS, certificate.hashCode());
+
+                if (store.containsAlias(alias))
+                    store.deleteEntry(alias);
+
+                store.setCertificateEntry(alias, certificate);
+
+                store();
+            }
 
-        // TODO: store server certificate
+            return true;
+        }
+        else
+            return false;
     }
 
     public boolean finishPairDevice(String device, String code) throws MalformedURLException