Remote.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Remote
  2. //
  3. // Douglas
  4. //
  5. // Remote.java
  6. /* Copyright 2011 Douglas Thrift
  7. *
  8. * This file is part of Big Screen Bot.
  9. *
  10. * Big Screen Bot is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * Big Screen Bot is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with Big Screen Bot. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. package net.douglasthrift.bigscreenbot;
  24. import java.io.FileInputStream;
  25. import java.io.FileNotFoundException;
  26. import java.io.FileOutputStream;
  27. import java.io.IOException;
  28. import java.net.InetAddress;
  29. import java.net.MalformedURLException;
  30. import java.net.UnknownHostException;
  31. import java.net.URL;
  32. import java.security.GeneralSecurityException;
  33. import java.security.KeyManagementException;
  34. import java.security.KeyPair;
  35. import java.security.KeyPairGenerator;
  36. import java.security.KeyStore;
  37. import java.security.KeyStoreException;
  38. import java.security.NoSuchAlgorithmException;
  39. import java.security.UnrecoverableKeyException;
  40. import java.security.cert.Certificate;
  41. import java.security.cert.CertificateException;
  42. import java.security.cert.X509Certificate;
  43. import java.util.ArrayList;
  44. import java.util.List;
  45. import javax.jmdns.JmDNS;
  46. import javax.jmdns.ServiceInfo;
  47. import javax.net.ssl.SSLSocket;
  48. import javax.net.ssl.SSLSocketFactory;
  49. import javax.net.ssl.KeyManager;
  50. import javax.net.ssl.KeyManagerFactory;
  51. import com.google.anymote.common.AnymoteFactory;
  52. import com.google.anymote.common.ErrorListener;
  53. import com.google.anymote.device.DeviceAdapter;
  54. import com.google.anymote.device.MessageReceiver;
  55. import com.google.polo.exception.PoloException;
  56. import com.google.polo.pairing.PairingContext;
  57. import com.google.polo.ssl.DummySSLSocketFactory;
  58. import com.google.polo.ssl.SslUtil;
  59. public class Remote
  60. {
  61. private static final String STORE = "bigscreenbot.keystore";
  62. private static final char[] PASSWORD = "b1GsSC33Nb0T".toCharArray();
  63. private static final char[] NULL = new char[]{};
  64. private static final String TYPE = "_anymote._tcp.local.";
  65. private static final String LOCAL_ALIAS = "anymote-remote";
  66. private static final String REMOTE_ALIAS = "anymote-server-%1$X";
  67. private Settings settings;
  68. private JmDNS mdns;
  69. private KeyStore store;
  70. SSLSocketFactory factory;
  71. public Remote(Settings settings) throws UnknownHostException, IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException, KeyManagementException, GeneralSecurityException
  72. {
  73. this.settings = settings;
  74. String interfaze = settings.getProperty("interface");
  75. if (interfaze != null)
  76. mdns = JmDNS.create(InetAddress.getByName(interfaze));
  77. else
  78. mdns = JmDNS.create();
  79. store = KeyStore.getInstance(KeyStore.getDefaultType());
  80. FileInputStream stream = null;
  81. try
  82. {
  83. stream = new FileInputStream(STORE);
  84. store.load(stream, PASSWORD);
  85. }
  86. catch (FileNotFoundException exception)
  87. {
  88. store.load(null, PASSWORD);
  89. }
  90. finally
  91. {
  92. if (stream != null)
  93. stream.close();
  94. }
  95. if (!store.containsAlias(LOCAL_ALIAS))
  96. {
  97. KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
  98. KeyPair pair = generator.generateKeyPair();
  99. X509Certificate certificate = SslUtil.generateX509V3Certificate(pair, "CN=anymote-remote/bigscreenbot/" + System.getProperty("os.name") + "/" + System.getProperty("os.arch") + "/" + System.getProperty("user.name") + "@" + InetAddress.getLocalHost().getHostName());
  100. store.setKeyEntry(LOCAL_ALIAS, pair.getPrivate(), NULL, new Certificate[]{ certificate });
  101. store();
  102. }
  103. KeyManagerFactory factory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  104. factory.init(store, NULL);
  105. this.factory = DummySSLSocketFactory.fromKeyManagers(factory.getKeyManagers());
  106. }
  107. public List<String> listDevices()
  108. {
  109. List<String> devices = new ArrayList<String>();
  110. for (ServiceInfo info : mdns.list(TYPE))
  111. devices.add(info.getName());
  112. return devices;
  113. }
  114. public void startPairDevice(String device) throws MalformedURLException, UnknownHostException, IOException, PoloException
  115. {
  116. ServiceInfo info = mdns.getServiceInfo(TYPE, device);
  117. InetAddress address;
  118. int port;
  119. if (info != null)
  120. {
  121. address = info.getInetAddresses()[0];
  122. port = info.getPort();
  123. }
  124. else
  125. {
  126. URL url = new URL("http://" + device);
  127. address = InetAddress.getByName(url.getHost());
  128. port = url.getPort();
  129. if (port == -1)
  130. port = 9551;
  131. }
  132. SSLSocket socket = (SSLSocket)factory.createSocket(address, port);
  133. PairingContext context = PairingContext.fromSslSocket(socket, false);
  134. }
  135. public void finishPairDevice(String device, String code)
  136. {
  137. }
  138. public void fling(String url)
  139. {
  140. }
  141. public void fling(String device, String url)
  142. {
  143. }
  144. @Override
  145. protected void finalize() throws IOException
  146. {
  147. mdns.close();
  148. }
  149. private void store() throws FileNotFoundException, IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException
  150. {
  151. FileOutputStream stream = null;
  152. try
  153. {
  154. stream = new FileOutputStream(STORE);
  155. store.store(stream, PASSWORD);
  156. }
  157. finally
  158. {
  159. if (stream != null)
  160. stream.close();
  161. }
  162. }
  163. }
  164. // vim: expandtab