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

add track clientip, add github report api. #293

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions src/main/java/com/datastat/constant/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ private Constant() {
*/
public static final String OPENMIND_COMMUNITY = "openmind";

/**
* The name of github platform.
*/
public static final String GITHUB_PLATFORM = "github";

/**
* The name of gutee platform.
*/
public static final String GITEE_PLATFORM = "GITEE";

/**
* 支持性能数据上传的社区.
*/
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/datastat/controller/QueryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,13 @@ public String queryCveDetails(HttpServletRequest request,
@RequestMapping("/newYear/report")
public String queryNewYear(HttpServletRequest request,
@CookieValue(value = "_oauth2_proxy", required = true) String oauth2_proxy) {
return queryService.queryNewYearPer(request, oauth2_proxy);
return queryService.queryNewYearPer(request, oauth2_proxy, "gitee");
}

@RequestMapping("/newYear/report/github")
public String queryNewYearGithub(HttpServletRequest request,
@CookieValue(value = "_oauth2_proxy", required = true) String oauth2_proxy) {
return queryService.queryNewYearPer(request, oauth2_proxy, "github");
}

@RequestMapping("/newYear/monthcount")
Expand Down
29 changes: 19 additions & 10 deletions src/main/java/com/datastat/dao/QueryDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -356,18 +356,18 @@ public String queryCveDetails(CustomPropertiesConfig queryConf, String lastCurso
return esQueryUtils.esFromId(restHighLevelClient, item, lastCursor, Integer.parseInt(pageSize), indexName);
}

public String queryNewYearPer(CustomPropertiesConfig queryConf, String oauth2_proxy, String community) {
public String queryNewYearPer(CustomPropertiesConfig queryConf, String oauth2_proxy, String community,
String platform) {
HashMap<String, Object> resMap = new HashMap<>();
try {
String user = getUserFromCookie(queryConf, oauth2_proxy);
String localFile = env.getProperty("export_path") + community.toLowerCase() + "_" + env.getProperty("year") + ".csv";
String localFile = String.format("%s%s_%s_%s.csv", env.getProperty("export_path"), community.toLowerCase(),
platform, env.getProperty("year"));
String user = getUserFromCookie(queryConf, oauth2_proxy, platform);
List<HashMap<String, Object>> report = CsvFileUtil.readFile(localFile);
resMap.put("code", 200);
resMap.put("msg", "OK");
if (report == null)
if (report == null || user == null)
resMap.put("data", new ArrayList<>());
else if (user == null)
resMap.put("data", report);
else {
List<HashMap<String, Object>> user_login = report.stream()
.filter(m -> m.getOrDefault("user_login", "").equals(user)).collect(Collectors.toList());
Expand Down Expand Up @@ -398,7 +398,7 @@ else if (user == null)

@SneakyThrows
public String queryNewYearMonthCount(CustomPropertiesConfig queryConf, String oauth2_proxy) {
String user = getUserFromCookie(queryConf, oauth2_proxy);
String user = getUserFromCookie(queryConf, oauth2_proxy, "gitee");
String queryJson = String.format(queryConf.getMonthCountQueryStr(), user);
ListenableFuture<Response> future = esAsyncHttpUtil.executeSearch(esUrl, queryConf.getGiteeAllIndex(), queryJson);
String responseBody = future.get().getResponseBody(UTF_8);
Expand All @@ -424,9 +424,17 @@ public String queryNewYearMonthCount(CustomPropertiesConfig queryConf, String oa
}

@SneakyThrows
private String getUserFromCookie(CustomPropertiesConfig queryConf, String oauth2_proxy) {
private String getUserFromCookie(CustomPropertiesConfig queryConf, String oauth2_proxy, String platform) {
String cookie_oauth2_proxy = "_oauth2_proxy=" + oauth2_proxy;
HttpResponse<String> response = Unirest.get(queryConf.getGiteeUserInfoUrl())
String userInfoUrl;
if (Constant.GITEE_PLATFORM.equalsIgnoreCase(platform)) {
userInfoUrl = queryConf.getGiteeUserInfoUrl();
} else if (Constant.GITHUB_PLATFORM.equalsIgnoreCase(platform)) {
userInfoUrl = queryConf.getGithubUserInfoUrl();
} else {
throw new RuntimeException("error platform");
}
HttpResponse<String> response = Unirest.get(userInfoUrl)
.header("cookie", cookie_oauth2_proxy)
.asString();

Expand Down Expand Up @@ -3603,7 +3611,7 @@ public String queryUserOwnerRepos(CustomPropertiesConfig queryConf, String user)
}

@SneakyThrows
public String saveFrontendEvents(String community, String requestBody) {
public String saveFrontendEvents(String community, String requestBody, String clientIp) {
// 检测请求体是否含有header和body
boolean hasHeader = requestBody.contains("\"header\"");
boolean hasBody = requestBody.contains("\"body\"");
Expand Down Expand Up @@ -3651,6 +3659,7 @@ public String saveFrontendEvents(String community, String requestBody) {

eventObj.put("created_at", nowStr);
eventObj.put("community", community);
eventObj.put("clientIp", clientIp);

JsonNode mergedJson = objectMapper.updateValue(eventObj, headerObj);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class CustomPropertiesConfig {
private String sigAction;
private String isvYamlUrl;
private String giteeUserInfoUrl;
private String githubUserInfoUrl;
private String orgName;
private String isvCountToken;
private String checkField;
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/datastat/service/QueryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.datastat.model.vo.*;
import com.datastat.result.ReturnCode;
import com.datastat.util.ArrayListUtil;
import com.datastat.util.ClientUtil;
import com.datastat.util.PageUtils;
import com.datastat.util.RSAUtil;
import com.datastat.util.ResultUtil;
Expand Down Expand Up @@ -322,19 +323,19 @@ public String queryCveDetails(HttpServletRequest request, String community, Stri
return result;
}

public String queryNewYearPer(HttpServletRequest request, String oauth2_proxy) {
public String queryNewYearPer(HttpServletRequest request, String oauth2_proxy, String platform) {
QueryDao queryDao = getQueryDao(request);
String referer = request.getHeader("Referer");
String community = null;
try {
community = referer.split("\\.")[1];
} catch (Exception e) {
logger.error("exception", e);
logger.error("parse community exception - {}", e.getMessage());
return resultJsonStr(404, "error", "Referer error");
}
CustomPropertiesConfig queryConf = getQueryConf(community);

return queryDao.queryNewYearPer(queryConf, oauth2_proxy, community);
return queryDao.queryNewYearPer(queryConf, oauth2_proxy, community, platform);
}

public String queryNewYearMonthCount(HttpServletRequest request, String oauth2_proxy) {
Expand Down Expand Up @@ -1562,8 +1563,9 @@ public String queryUserOwnerRepos(HttpServletRequest request, String user) {

public String saveFrontendEvents(HttpServletRequest request, String community, String requestBody) {
QueryDao queryDao = getQueryDao(request);
String clientIp = ClientUtil.getClientIpAddress(request);
if (!checkCommunity(community)) return ResultUtil.resultJsonStr(404, "error", "not found");
return queryDao.saveFrontendEvents(community, requestBody);
return queryDao.saveFrontendEvents(community, requestBody, clientIp);
}

public String putGlobalNpsIssue(HttpServletRequest request, String token, String community, NpsBody body) {
Expand Down
91 changes: 62 additions & 29 deletions src/main/java/com/datastat/util/ClientUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,82 @@
See the Mulan PSL v2 for more details.
Create: 2024/02
*/

package com.datastat.util;

import java.util.Enumeration;

import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ClientUtil {
private static final Logger logger = LoggerFactory.getLogger(ClientUtil.class);
public static String getClientIpAddress(HttpServletRequest request) {
String[] headerNames = {"x-forwarded-for", "Proxy-Client-IP", "WL-Proxy-Client-IP",
"HTTP_CLIENT_IP", "HTTP_X_FORWARDED_FOR", "X-Real-IP"};
import jakarta.servlet.http.HttpServletRequest;

for (String headerName : headerNames) {
String ip = request.getHeader(headerName);
if (isValidIp(ip)) {
return extractIp(ip);
}
}
import java.net.InetAddress;
import java.net.UnknownHostException;

return request.getRemoteAddr();
}
public final class ClientUtil {

private static boolean isValidIp(String ip) {
return ip != null && ip.length() > 0 && !"unknown".equalsIgnoreCase(ip);

// Private constructor to prevent instantiation of the utility class
private ClientUtil() {
// private constructor to hide the implicit public one
throw new AssertionError("ClientUtil class cannot be instantiated.");
}

private static String extractIp(String ip) {
if (ip.contains(",")) {
return ip.split(",")[0];
/**
* Logger instance for ClientUtil.
*/
private static final Logger LOGGER = LoggerFactory.getLogger(ClientUtil.class);

/**
* Retrieve the client's IP address from the HttpServletRequest.
*
* @param request The HttpServletRequest object
* @return The client's IP address as a string
*/
public static String getClientIpAddress(final HttpServletRequest request) {
String ip = request.getHeader("x-real-ip");
if (checkIp(ip)) {
ip = getForwardedIP(request);
}
if (checkIp(ip)) {
ip = request.getRemoteAddr();
if ("127.0.0.1".equals(ip) || "0:0:0:0:0:0:0:1".equals(ip)) {
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
LOGGER.error("get local host error: " + e.getMessage());
}
ip = inet.getHostAddress();
}
}
return ip;
}

public static void getHeaderValue(HttpServletRequest request) {
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()){
String name = headerNames.nextElement();
String value = request.getHeader(name);
logger.info("request header: name = {}, value = {}", name, value);
}
/**
* Check if the provided string is a valid IP address.
*
* @param ip The IP address to check.
* @return true if the IP address is valid, false otherwise.
*/
private static boolean checkIp(final String ip) {
return null == ip || ip.length() == 0 || "unknown".equalsIgnoreCase(ip);
}

/**
* Retrieve the client's x-forwarded-for IP address from the HttpServletRequest.
*
* @param request The HttpServletRequest object
* @return The client's IP address as a string
*/
private static String getForwardedIP(final HttpServletRequest request) {
String headerName = "x-forwarded-for";
String ip = request.getHeader(headerName);
if (!checkIp(ip)) {
// There will be multiple IP values after multiple reverse proxies, pick the first IP.
if (ip.contains(",")) {
ip = ip.split(",")[0];
}
}
return ip;
}
}

2 changes: 1 addition & 1 deletion src/main/java/com/datastat/util/CsvFileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static List<HashMap<String, Object>> readFile(String file) {
return res;

} catch (Exception e) {
logger.error("exception", e);
logger.error("read file exception - {}", e.getMessage());
return null;
}
}
Expand Down
Loading