Remote.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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/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. final javax.net.ssl.X509ExtendedKeyManager manager = (javax.net.ssl.X509ExtendedKeyManager)factory.getKeyManagers()[0];
  106. this.factory = DummySSLSocketFactory.fromKeyManagers(new KeyManager[]{
  107. new javax.net.ssl.X509ExtendedKeyManager()
  108. {
  109. @Override
  110. public java.security.PrivateKey getPrivateKey(String alias)
  111. {
  112. java.security.PrivateKey key = manager.getPrivateKey(alias);
  113. System.out.println("getPrivateKey(" + alias + ") -> " + key);
  114. return key;
  115. }
  116. @Override
  117. public X509Certificate[] getCertificateChain(String alias)
  118. {
  119. X509Certificate[] chain = manager.getCertificateChain(alias);
  120. System.out.println("getCertificateChain(" + alias + ") -> " + java.util.Arrays.toString(chain));
  121. return chain;
  122. }
  123. @Override
  124. public String chooseServerAlias(String keyType, java.security.Principal[] issuers, java.net.Socket socket)
  125. {
  126. String alias = manager.chooseServerAlias(keyType, issuers, socket);
  127. System.out.println("chooseServerAlias(" + keyType + ", " + java.util.Arrays.toString(issuers) + ", " + socket + ") -> " + alias);
  128. return alias;
  129. }
  130. @Override
  131. public String[] getServerAliases(String keyType, java.security.Principal[] issuers)
  132. {
  133. String[] aliases = manager.getServerAliases(keyType, issuers);
  134. System.out.println("getServerAliases(" + keyType + ", " + java.util.Arrays.toString(issuers) + ") -> " + java.util.Arrays.toString(aliases));
  135. return aliases;
  136. }
  137. @Override
  138. public String chooseClientAlias(String[] keyType, java.security.Principal[] issuers, java.net.Socket socket)
  139. {
  140. String alias = manager.chooseClientAlias(keyType, issuers, socket);
  141. System.out.println("chooseClientAlias(" + java.util.Arrays.toString(keyType) + ", " + java.util.Arrays.toString(issuers) + ", " + socket + ") -> " + alias);
  142. return alias;
  143. }
  144. @Override
  145. public String[] getClientAliases(String keyType, java.security.Principal[] issuers)
  146. {
  147. String[] aliases = manager.getClientAliases(keyType, issuers);
  148. System.out.println("getClientAliases(" + keyType + ", " + java.util.Arrays.toString(issuers) + ") -> " + java.util.Arrays.toString(aliases));
  149. return aliases;
  150. }
  151. }
  152. });
  153. }
  154. public List<String> listDevices()
  155. {
  156. List<String> devices = new ArrayList<String>();
  157. for (ServiceInfo info : mdns.list(TYPE))
  158. devices.add(info.getName());
  159. return devices;
  160. }
  161. public void startPairDevice(String device) throws MalformedURLException, UnknownHostException, IOException, PoloException
  162. {
  163. ServiceInfo info = mdns.getServiceInfo(TYPE, device);
  164. InetAddress address;
  165. int port;
  166. if (info != null)
  167. {
  168. address = info.getInetAddresses()[0];
  169. port = info.getPort();
  170. }
  171. else
  172. {
  173. URL url = new URL("http://" + device);
  174. address = InetAddress.getByName(url.getHost());
  175. port = url.getPort();
  176. if (port == -1)
  177. port = 9551;
  178. }
  179. SSLSocket socket = (SSLSocket)factory.createSocket(address, port);
  180. PairingContext context = PairingContext.fromSslSocket(socket, false);
  181. }
  182. public void finishPairDevice(String device, String code)
  183. {
  184. }
  185. public void fling(String url)
  186. {
  187. }
  188. public void fling(String device, String url)
  189. {
  190. }
  191. @Override
  192. protected void finalize() throws IOException
  193. {
  194. mdns.close();
  195. }
  196. private void store() throws FileNotFoundException, IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException
  197. {
  198. FileOutputStream stream = null;
  199. try
  200. {
  201. stream = new FileOutputStream(STORE);
  202. store.store(stream, PASSWORD);
  203. }
  204. finally
  205. {
  206. if (stream != null)
  207. stream.close();
  208. }
  209. }
  210. }
  211. // vim: expandtab