Beispiel #1
0
  /** Creates a new URL to use as the basis of a connection. */
  public MsgRpcImpl(
      String username, String password, String host, int port, boolean ssl, boolean debugf)
      throws MalformedURLException {
    if (ssl) { // Install the all-trusting trust manager & HostnameVerifier
      try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(
            null,
            new TrustManager[] {
              new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                  return null;
                }

                public void checkClientTrusted(
                    java.security.cert.X509Certificate[] certs, String authType) {}

                public void checkServerTrusted(
                    java.security.cert.X509Certificate[] certs, String authType) {}
              }
            },
            new java.security.SecureRandom());

        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        HttpsURLConnection.setDefaultHostnameVerifier(
            new HostnameVerifier() {
              public boolean verify(String string, SSLSession ssls) {
                return true;
              }
            });
      } catch (Exception e) {
      }
      u = new URL("https", host, port, "/api/1.0/");
    } else {
      u = new URL("http", host, port, "/api/1.0/");
    }

    /* login to msf server */
    Object[] params = new Object[] {username, password};
    Map results = exec("auth.login", params);

    /* save the temp token (lasts for 5 minutes of inactivity) */
    rpcToken = results.get("token").toString();

    /* generate a non-expiring token and use that */
    params = new Object[] {rpcToken};
    results = exec("auth.token_generate", params);
    rpcToken = results.get("token").toString();
  }