Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor http client, fixes threads shut down not properly #392

Merged
merged 2 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@
import io.dongtai.iast.agent.monitor.impl.PerformanceMonitor;
import io.dongtai.iast.agent.report.AgentRegisterReport;
import io.dongtai.iast.agent.util.FileUtils;
import io.dongtai.iast.agent.util.http.HttpClientUtils;
import io.dongtai.iast.agent.util.HttpClientUtils;
import io.dongtai.iast.common.utils.base64.Base64Encoder;
import io.dongtai.log.DongTaiLog;
import org.json.JSONObject;

import java.io.*;
import java.io.File;
import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.lang.management.ManagementFactory;
import java.lang.reflect.InvocationTargetException;
import java.net.*;
import java.util.jar.JarFile;

/**
Expand Down Expand Up @@ -196,79 +195,20 @@ private static String getGrpcPackagePath() {
return TMP_DIR + "dongtai-grpc.jar";
}


/**
* 从远程URI下载jar包到指定的本地文件
*
* @param fileUrl 远程URI
* @param fileName 本地文件路径
* @return 下载结果,成功为true,失败为false
*/
private boolean downloadJarPackageToCacheFromUrl(String fileUrl, String fileName) {
boolean status = false;
try {
URL url = new URL(fileUrl);
Proxy proxy = HttpClientUtils.loadProxy();
HttpURLConnection connection = proxy == null ? (HttpURLConnection) url.openConnection()
: (HttpURLConnection) url.openConnection(proxy);

connection.setRequestMethod("GET");
connection.setRequestProperty("User-Agent", "DongTai-IAST-Agent");
connection.setRequestProperty("Authorization", "Token " + properties.getServerToken());
connection.setUseCaches(false);
connection.setDoOutput(true);

if ("application/json".equals(connection.getContentType())) {
BufferedReader streamReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
StringBuilder responseStrBuilder = new StringBuilder();
String inputStr;
while ((inputStr = streamReader.readLine()) != null) {
responseStrBuilder.append(inputStr);
}

JSONObject jsonObject = new JSONObject(responseStrBuilder.toString());
DongTaiLog.error("DongTai Core Package: {} download failed. response: {}", fileUrl, jsonObject);
return false;
} else {
BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
final File classPath = new File(new File(fileName).getParent());

if (!classPath.mkdirs() && !classPath.exists()) {
DongTaiLog.info("Check or create local file cache path, path is " + classPath);
}
FileOutputStream fileOutputStream = new FileOutputStream(fileName);
byte[] dataBuffer = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
fileOutputStream.write(dataBuffer, 0, bytesRead);
}
dataBuffer = null;
in.close();
fileOutputStream.close();
DongTaiLog.info("The remote file " + fileUrl + " was successfully written to the local cache.");
status = true;
}
} catch (Exception ignore) {
DongTaiLog.error("The remote file " + fileUrl + " download failure, please check the dongtai-token.");
}
return status;
}

/**
* 更新IAST引擎需要的jar包,用于启动时加载和热更新检测引擎 - iast-core.jar - iast-inject.jar
*
* @return 更新状态,成功为true,失败为false
*/
public boolean downloadPackageFromServer() {
String baseUrl = properties.getBaseUrl();
// 自定义jar下载地址
String spyJarUrl = "".equals(properties.getCustomSpyJarUrl()) ? baseUrl + INJECT_PACKAGE_REMOTE_URI : properties.getCustomSpyJarUrl();
String coreJarUrl = "".equals(properties.getCustomCoreJarUrl()) ? baseUrl + ENGINE_PACKAGE_REMOTE_URI : properties.getCustomCoreJarUrl();
String apiJarUrl = "".equals(properties.getCustomApiJarUrl()) ? baseUrl + API_PACKAGE_REMOTE_URI : properties.getCustomApiJarUrl();
return downloadJarPackageToCacheFromUrl(spyJarUrl, getInjectPackageCachePath()) &&
downloadJarPackageToCacheFromUrl(coreJarUrl, getEnginePackageCachePath()) &&
downloadJarPackageToCacheFromUrl(apiJarUrl, getApiPackagePath()) &&
downloadJarPackageToCacheFromUrl(baseUrl + "/api/v1/engine/download?engineName=dongtai-grpc", getGrpcPackagePath());
String spyJarUrl = "".equals(properties.getCustomSpyJarUrl()) ? INJECT_PACKAGE_REMOTE_URI : properties.getCustomSpyJarUrl();
String coreJarUrl = "".equals(properties.getCustomCoreJarUrl()) ? ENGINE_PACKAGE_REMOTE_URI : properties.getCustomCoreJarUrl();
String apiJarUrl = "".equals(properties.getCustomApiJarUrl()) ? API_PACKAGE_REMOTE_URI : properties.getCustomApiJarUrl();
return HttpClientUtils.downloadRemoteJar(spyJarUrl, getInjectPackageCachePath()) &&
HttpClientUtils.downloadRemoteJar(coreJarUrl, getEnginePackageCachePath()) &&
HttpClientUtils.downloadRemoteJar(apiJarUrl, getApiPackagePath()) &&
HttpClientUtils.downloadRemoteJar("/api/v1/engine/download?engineName=dongtai-grpc", getGrpcPackagePath());
}

/**
Expand All @@ -277,14 +217,13 @@ public boolean downloadPackageFromServer() {
* @return 更新状态,成功为true,失败为false
*/
public boolean downloadPackageFromServerJdk6() {
String baseUrl = properties.getBaseUrl();
// 自定义jar下载地址
String spyJarUrl = "".equals(properties.getCustomSpyJarUrl()) ? baseUrl + INJECT_PACKAGE_REMOTE_URI_JDK6 : properties.getCustomSpyJarUrl();
String coreJarUrl = "".equals(properties.getCustomCoreJarUrl()) ? baseUrl + ENGINE_PACKAGE_REMOTE_URI_JDK6 : properties.getCustomCoreJarUrl();
String apiJarUrl = "".equals(properties.getCustomApiJarUrl()) ? baseUrl + API_PACKAGE_REMOTE_URI_JDK6 : properties.getCustomApiJarUrl();
return downloadJarPackageToCacheFromUrl(spyJarUrl, getInjectPackageCachePath()) &&
downloadJarPackageToCacheFromUrl(coreJarUrl, getEnginePackageCachePath()) &&
downloadJarPackageToCacheFromUrl(apiJarUrl, getApiPackagePath());
String spyJarUrl = "".equals(properties.getCustomSpyJarUrl()) ? INJECT_PACKAGE_REMOTE_URI_JDK6 : properties.getCustomSpyJarUrl();
String coreJarUrl = "".equals(properties.getCustomCoreJarUrl()) ? ENGINE_PACKAGE_REMOTE_URI_JDK6 : properties.getCustomCoreJarUrl();
String apiJarUrl = "".equals(properties.getCustomApiJarUrl()) ? API_PACKAGE_REMOTE_URI_JDK6 : properties.getCustomApiJarUrl();
return HttpClientUtils.downloadRemoteJar(spyJarUrl, getInjectPackageCachePath()) &&
HttpClientUtils.downloadRemoteJar(coreJarUrl, getEnginePackageCachePath()) &&
HttpClientUtils.downloadRemoteJar(apiJarUrl, getApiPackagePath());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import io.dongtai.iast.agent.monitor.IMonitor;
import io.dongtai.iast.agent.monitor.MonitorDaemonThread;
import io.dongtai.iast.agent.util.HttpClientUtils;
import io.dongtai.iast.agent.util.ThreadUtils;
import io.dongtai.iast.agent.util.http.HttpClientUtils;
import io.dongtai.iast.common.constants.*;
import io.dongtai.iast.common.entity.performance.metrics.ThreadInfoMetrics;
import io.dongtai.log.DongTaiLog;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
import io.dongtai.iast.agent.monitor.MonitorDaemonThread;
import io.dongtai.iast.agent.report.AgentRegisterReport;
import io.dongtai.iast.agent.report.HeartBeatReport;
import io.dongtai.iast.agent.util.HttpClientUtils;
import io.dongtai.iast.agent.util.ThreadUtils;
import io.dongtai.iast.agent.util.http.HttpClientUtils;
import io.dongtai.iast.common.constants.AgentConstant;
import io.dongtai.iast.common.constants.ApiPath;
import io.dongtai.iast.common.utils.version.JavaVersionUtils;
import io.dongtai.log.DongTaiLog;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
* @author [email protected]
*/
Expand Down Expand Up @@ -104,8 +107,10 @@ private void forceSwitchPerformanceBreaker(boolean turnOn) {

private String checkForStatus() {
try {
String respRaw = String.valueOf(HttpClientUtils.sendGet(ApiPath.EXCEPT_ACTION, "agentId", String.valueOf(AgentRegisterReport.getAgentFlag())));
if (respRaw != null && !respRaw.isEmpty()) {
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("agentId", String.valueOf(AgentRegisterReport.getAgentFlag()));
String respRaw = HttpClientUtils.sendGet(ApiPath.EXCEPT_ACTION, parameters).toString();
if (!respRaw.isEmpty()) {
JSONObject resp = new JSONObject(respRaw);
JSONObject data = (JSONObject) resp.get("data");
return data.get("exceptRunningStatus").toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import io.dongtai.iast.agent.monitor.IMonitor;
import io.dongtai.iast.agent.monitor.MonitorDaemonThread;
import io.dongtai.iast.agent.report.HeartBeatReport;
import io.dongtai.iast.agent.util.HttpClientUtils;
import io.dongtai.iast.agent.util.ThreadUtils;
import io.dongtai.iast.agent.util.http.HttpClientUtils;
import io.dongtai.iast.common.constants.AgentConstant;
import io.dongtai.iast.common.constants.ApiPath;
import io.dongtai.log.DongTaiLog;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import io.dongtai.iast.agent.manager.EngineManager;
import io.dongtai.iast.agent.middlewarerecognition.IServer;
import io.dongtai.iast.agent.middlewarerecognition.ServerDetect;
import io.dongtai.iast.agent.util.http.HttpClientUtils;
import io.dongtai.iast.agent.util.HttpClientUtils;
import io.dongtai.iast.common.constants.AgentConstant;
import io.dongtai.iast.common.constants.ApiPath;
import io.dongtai.iast.common.utils.base64.Base64Encoder;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package io.dongtai.iast.agent.util;

import io.dongtai.iast.agent.IastProperties;
import io.dongtai.iast.common.enums.HttpMethods;
import io.dongtai.iast.common.utils.AbstractHttpClientUtils;

import java.util.HashMap;
import java.util.Map;

/**
* @author [email protected]
*/
public class HttpClientUtils extends AbstractHttpClientUtils {
private final static IastProperties PROPERTIES = IastProperties.getInstance();
private static String proxyHost = "";
private static int proxyPort = -1;

static {
if (PROPERTIES.isProxyEnable()) {
proxyHost = PROPERTIES.getProxyHost();
proxyPort = PROPERTIES.getProxyPort();
}
}

public static StringBuilder sendGet(String uri, Map<String, String> parameters) {
if (parameters != null && !parameters.isEmpty()) {
StringBuilder uriBuilder = new StringBuilder(uri);
uriBuilder.append("?");
for (Map.Entry<String, String> entry : parameters.entrySet()) {
uriBuilder.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
}
uri = uriBuilder.toString();
}

Map<String, String> headers = new HashMap<String, String>();
setToken(headers);

return sendRequest(HttpMethods.GET, PROPERTIES.getBaseUrl() + uri, null, headers, 0,
proxyHost, proxyPort, null);
}

public static StringBuilder sendPost(String uri, String value) {
Map<String, String> headers = new HashMap<String, String>();
setToken(headers);
headers.put(HEADER_CONTENT_TYPE, MEDIA_TYPE_APPLICATION_JSON);
headers.put(HEADER_CONTENT_ENCODING, REQUEST_ENCODING_TYPE);

return sendRequest(HttpMethods.POST, PROPERTIES.getBaseUrl() + uri, value, headers, 0,
proxyHost, proxyPort, null);
}

public static boolean downloadRemoteJar(String fileURI, String fileName) {
Map<String, String> headers = new HashMap<String, String>();
setToken(headers);

return downloadFile(PROPERTIES.getBaseUrl() + fileURI, fileName, headers, proxyHost, proxyPort);
}

private static void setToken(Map<String, String> headers) {
headers.put(REQUEST_HEADER_TOKEN_KEY, "Token " + PROPERTIES.getServerToken());
}
}

This file was deleted.

Loading