コード例 #1
1
  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!");
  }
コード例 #2
0
  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();
      }
    }
  }