public ServerInfoJson getServerInfo(HttpUrl serverUrl, String serverID, String id)
      throws IOException {
    // set timeout to 30 seconds
    OkHttpClient client =
        defaultClient
            .newBuilder()
            .connectTimeout(30, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS)
            .build();

    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("version", 1);
    jsonObject.addProperty("command", "get_server_info");
    jsonObject.addProperty("stop_when_error", "false");
    jsonObject.addProperty("stop_when_success", "false");
    jsonObject.addProperty("id", id);
    jsonObject.addProperty("serverID", serverID);
    RequestBody requestBody =
        RequestBody.create(MediaType.parse("text/plain"), gson.toJson(jsonObject));
    Request request = new Request.Builder().url(serverUrl).post(requestBody).build();
    Response response = client.newCall(request).execute();
    InputStream in = response.body().byteStream();
    JsonReader reader = null;
    ServerInfoJson serverInfoJson = null;
    try {
      reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
      serverInfoJson = gson.fromJson(reader, ServerInfoJson.class);
    } finally {
      if (reader != null) {
        reader.close();
      }
    }

    if (serverInfoJson != null) {
      if (serverInfoJson.server != null) {
        // server info found!
        return serverInfoJson;
      } else if (serverInfoJson.sites != null && !serverInfoJson.sites.isEmpty()) {
        String site = serverInfoJson.sites.get(0);
        return getServerInfo(
            new HttpUrl.Builder().scheme("http").host(site).addPathSegment("Serv.php").build(),
            serverID,
            id);
      }
    }

    throw new IOException("No server info found!");
  }
  public ServerInfoJson requestTunnel(ServerInfoJson infoJson, String serverID, String id)
      throws IOException {
    if (infoJson == null || infoJson.env == null || Util.isEmpty(infoJson.env.control_host)) {
      return null;
    }

    // set timeout to 30 seconds
    OkHttpClient client =
        defaultClient
            .newBuilder()
            .connectTimeout(30, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS)
            .build();

    final String server = infoJson.env.control_host;

    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("command", "request_tunnel");
    jsonObject.addProperty("version", 1);
    jsonObject.addProperty("serverID", serverID);
    jsonObject.addProperty("id", id);

    RequestBody requestBody =
        RequestBody.create(MediaType.parse("text/plain"), gson.toJson(jsonObject));
    Request request =
        new Request.Builder()
            .url(HttpUrl.parse("http://" + server + "/Serv.php"))
            .post(requestBody)
            .build();
    Response response = client.newCall(request).execute();
    JsonReader reader = null;
    try {
      InputStream in = response.body().byteStream();
      reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
      return gson.fromJson(reader, ServerInfoJson.class);
    } finally {
      if (reader != null) {
        reader.close();
      }
    }
  }
示例#3
0
  static Response publishInstallAndWaitForResponse(
      final Context context, final String applicationId, final boolean isAutoPublish) {
    try {
      if (context == null || applicationId == null) {
        throw new IllegalArgumentException("Both context and applicationId must be non-null");
      }
      AttributionIdentifiers identifiers =
          AttributionIdentifiers.getAttributionIdentifiers(context);
      SharedPreferences preferences =
          context.getSharedPreferences(ATTRIBUTION_PREFERENCES, Context.MODE_PRIVATE);
      String pingKey = applicationId + "ping";
      String jsonKey = applicationId + "json";
      long lastPing = preferences.getLong(pingKey, 0);
      String lastResponseJSON = preferences.getString(jsonKey, null);

      // prevent auto publish from occurring if we have an explicit call.
      if (!isAutoPublish) {
        setShouldAutoPublishInstall(false);
      }

      GraphObject publishParams = GraphObject.Factory.create();
      publishParams.setProperty(ANALYTICS_EVENT, MOBILE_INSTALL_EVENT);

      Utility.setAppEventAttributionParameters(
          publishParams,
          identifiers,
          Utility.getHashedDeviceAndAppID(context, applicationId),
          getLimitEventAndDataUsage(context));
      publishParams.setProperty(AUTO_PUBLISH, isAutoPublish);
      publishParams.setProperty("application_package_name", context.getPackageName());

      String publishUrl = String.format(PUBLISH_ACTIVITY_PATH, applicationId);
      Request publishRequest = Request.newPostRequest(null, publishUrl, publishParams, null);

      if (lastPing != 0) {
        GraphObject graphObject = null;
        try {
          if (lastResponseJSON != null) {
            graphObject = GraphObject.Factory.create(new JSONObject(lastResponseJSON));
          }
        } catch (JSONException je) {
          // return the default graph object if there is any problem reading the data.
        }
        if (graphObject == null) {
          return Response.createResponsesFromString(
                  "true", null, new RequestBatch(publishRequest), true)
              .get(0);
        } else {
          return new Response(null, null, null, graphObject, true);
        }
      } else if (identifiers == null
          || (identifiers.getAndroidAdvertiserId() == null
              && identifiers.getAttributionId() == null)) {
        throw new FacebookException("No attribution id available to send to server.");
      } else {
        if (!Utility.queryAppSettings(applicationId, false).supportsAttribution()) {
          throw new FacebookException("Install attribution has been disabled on the server.");
        }

        Response publishResponse = publishRequest.executeAndWait();

        // denote success since no error threw from the post.
        SharedPreferences.Editor editor = preferences.edit();
        lastPing = System.currentTimeMillis();
        editor.putLong(pingKey, lastPing);

        // if we got an object response back, cache the string of the JSON.
        if (publishResponse.getGraphObject() != null
            && publishResponse.getGraphObject().getInnerJSONObject() != null) {
          editor.putString(
              jsonKey, publishResponse.getGraphObject().getInnerJSONObject().toString());
        }
        editor.apply();

        return publishResponse;
      }
    } catch (Exception e) {
      // if there was an error, fall through to the failure case.
      Utility.logd("Facebook-publish", e);
      return new Response(null, null, new FacebookRequestError(null, e));
    }
  }