Remote.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. static
  69. {
  70. new Fixer();
  71. }
  72. private Settings settings;
  73. private JmDNS mdns;
  74. private KeyStore store;
  75. private SSLSocketFactory factory;
  76. public Remote(Settings settings) throws UnknownHostException, IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException, KeyManagementException, GeneralSecurityException
  77. {
  78. this.settings = settings;
  79. String interfaze = settings.getProperty("interface");
  80. if (interfaze != null)
  81. mdns = JmDNS.create(InetAddress.getByName(interfaze));
  82. else
  83. mdns = JmDNS.create();
  84. store = KeyStore.getInstance(KeyStore.getDefaultType());
  85. FileInputStream stream = null;
  86. try
  87. {
  88. stream = new FileInputStream(STORE);
  89. store.load(stream, PASSWORD);
  90. }
  91. catch (FileNotFoundException exception)
  92. {
  93. store.load(null, PASSWORD);
  94. }
  95. finally
  96. {
  97. if (stream != null)
  98. stream.close();
  99. }
  100. if (!store.containsAlias(LOCAL_ALIAS))
  101. {
  102. KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
  103. KeyPair pair = generator.generateKeyPair();
  104. X509Certificate certificate = SslUtil.generateX509V3Certificate(pair, "CN=anymote/bigscreenbot/" + System.getProperty("os.name") + "/" + System.getProperty("os.arch") + "/" + System.getProperty("user.name") + "@" + InetAddress.getLocalHost().getHostName());
  105. store.setKeyEntry(LOCAL_ALIAS, pair.getPrivate(), NULL, new Certificate[]{ certificate });
  106. store();
  107. }
  108. KeyManagerFactory factory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  109. factory.init(store, NULL);
  110. this.factory = DummySSLSocketFactory.fromKeyManagers(factory.getKeyManagers());
  111. }
  112. @Override
  113. public void close() throws IOException
  114. {
  115. mdns.close();
  116. }
  117. public List<String> listDevices()
  118. {
  119. List<String> devices = new ArrayList<String>();
  120. for (ServiceInfo info : mdns.list(TYPE))
  121. devices.add(info.getName());
  122. return devices;
  123. }
  124. public void startPairDevice(String device) throws MalformedURLException, UnknownHostException, IOException, PoloException
  125. {
  126. ServiceInfo info = mdns.getServiceInfo(TYPE, device);
  127. InetAddress address;
  128. int port;
  129. if (info != null)
  130. {
  131. address = info.getInetAddresses()[0];
  132. port = info.getPort();
  133. }
  134. else
  135. {
  136. URL url = new URL("http://" + device);
  137. address = InetAddress.getByName(url.getHost());
  138. port = url.getPort();
  139. if (port == -1)
  140. port = 9551;
  141. }
  142. SSLSocket socket = (SSLSocket)factory.createSocket(address, port);
  143. PairingContext context = PairingContext.fromSslSocket(socket, false);
  144. }
  145. public void finishPairDevice(String device, String code)
  146. {
  147. }
  148. public void fling(String url)
  149. {
  150. }
  151. public void fling(String device, String url)
  152. {
  153. }
  154. private void store() throws FileNotFoundException, IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException
  155. {
  156. FileOutputStream stream = null;
  157. try
  158. {
  159. stream = new FileOutputStream(STORE);
  160. store.store(stream, PASSWORD);
  161. }
  162. finally
  163. {
  164. if (stream != null)
  165. stream.close();
  166. }
  167. }
  168. }
  169. // vim: expandtab