Remote.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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.Closeable;
  25. import java.io.FileInputStream;
  26. import java.io.FileNotFoundException;
  27. import java.io.FileOutputStream;
  28. import java.io.IOException;
  29. import java.net.InetAddress;
  30. import java.net.MalformedURLException;
  31. import java.net.UnknownHostException;
  32. import java.net.URL;
  33. import java.security.GeneralSecurityException;
  34. import java.security.KeyManagementException;
  35. import java.security.KeyPair;
  36. import java.security.KeyPairGenerator;
  37. import java.security.KeyStore;
  38. import java.security.KeyStoreException;
  39. import java.security.NoSuchAlgorithmException;
  40. import java.security.UnrecoverableKeyException;
  41. import java.security.cert.Certificate;
  42. import java.security.cert.CertificateException;
  43. import java.security.cert.X509Certificate;
  44. import java.util.ArrayList;
  45. import java.util.List;
  46. import javax.jmdns.JmDNS;
  47. import javax.jmdns.ServiceInfo;
  48. import javax.net.ssl.SSLSocket;
  49. import javax.net.ssl.SSLSocketFactory;
  50. import javax.net.ssl.KeyManager;
  51. import javax.net.ssl.KeyManagerFactory;
  52. import com.google.anymote.common.AnymoteFactory;
  53. import com.google.anymote.common.ErrorListener;
  54. import com.google.anymote.device.DeviceAdapter;
  55. import com.google.anymote.device.MessageReceiver;
  56. import com.google.polo.exception.PoloException;
  57. import com.google.polo.pairing.PairingContext;
  58. import com.google.polo.ssl.DummySSLSocketFactory;
  59. import com.google.polo.ssl.SslUtil;
  60. public class Remote implements Closeable
  61. {
  62. private static final String STORE = "bigscreenbot.keystore";
  63. private static final char[] PASSWORD = "b1GsSC33Nb0T".toCharArray();
  64. private static final char[] NULL = new char[]{};
  65. private static final String TYPE = "_anymote._tcp.local.";
  66. private static final String LOCAL_ALIAS = "anymote-remote";
  67. private static final String REMOTE_ALIAS = "anymote-server-%1$X";
  68. private Settings settings;
  69. private JmDNS mdns;
  70. private KeyStore store;
  71. SSLSocketFactory factory;
  72. public Remote(Settings settings) throws UnknownHostException, IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException, KeyManagementException, GeneralSecurityException
  73. {
  74. this.settings = settings;
  75. String interfaze = settings.getProperty("interface");
  76. if (interfaze != null)
  77. mdns = JmDNS.create(InetAddress.getByName(interfaze));
  78. else
  79. mdns = JmDNS.create();
  80. store = KeyStore.getInstance(KeyStore.getDefaultType());
  81. FileInputStream stream = null;
  82. try
  83. {
  84. stream = new FileInputStream(STORE);
  85. store.load(stream, PASSWORD);
  86. }
  87. catch (FileNotFoundException exception)
  88. {
  89. store.load(null, PASSWORD);
  90. }
  91. finally
  92. {
  93. if (stream != null)
  94. stream.close();
  95. }
  96. if (!store.containsAlias(LOCAL_ALIAS))
  97. {
  98. KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
  99. KeyPair pair = generator.generateKeyPair();
  100. X509Certificate certificate = SslUtil.generateX509V3Certificate(pair, "CN=anymote/bigscreenbot/" + System.getProperty("os.name") + "/" + System.getProperty("os.arch") + "/" + System.getProperty("user.name") + "@" + InetAddress.getLocalHost().getHostName());
  101. store.setKeyEntry(LOCAL_ALIAS, pair.getPrivate(), NULL, new Certificate[]{ certificate });
  102. store();
  103. }
  104. KeyManagerFactory factory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  105. factory.init(store, NULL);
  106. this.factory = DummySSLSocketFactory.fromKeyManagers(factory.getKeyManagers());
  107. }
  108. @Override
  109. public void close() throws IOException
  110. {
  111. mdns.close();
  112. }
  113. public List<String> listDevices()
  114. {
  115. List<String> devices = new ArrayList<String>();
  116. for (ServiceInfo info : mdns.list(TYPE))
  117. devices.add(info.getName());
  118. return devices;
  119. }
  120. public void startPairDevice(String device) throws MalformedURLException, UnknownHostException, IOException, PoloException
  121. {
  122. ServiceInfo info = mdns.getServiceInfo(TYPE, device);
  123. InetAddress address;
  124. int port;
  125. if (info != null)
  126. {
  127. address = info.getInetAddresses()[0];
  128. port = info.getPort();
  129. }
  130. else
  131. {
  132. URL url = new URL("http://" + device);
  133. address = InetAddress.getByName(url.getHost());
  134. port = url.getPort();
  135. if (port == -1)
  136. port = 9551;
  137. }
  138. SSLSocket socket = (SSLSocket)factory.createSocket(address, port);
  139. PairingContext context = PairingContext.fromSslSocket(socket, false);
  140. }
  141. public void finishPairDevice(String device, String code)
  142. {
  143. }
  144. public void fling(String url)
  145. {
  146. }
  147. public void fling(String device, String url)
  148. {
  149. }
  150. private void store() throws FileNotFoundException, IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException
  151. {
  152. FileOutputStream stream = null;
  153. try
  154. {
  155. stream = new FileOutputStream(STORE);
  156. store.store(stream, PASSWORD);
  157. }
  158. finally
  159. {
  160. if (stream != null)
  161. stream.close();
  162. }
  163. }
  164. }
  165. // vim: expandtab