diff --git a/src/main/java/com/datastat/constant/Constant.java b/src/main/java/com/datastat/constant/Constant.java index f88ecd0..7c5c6ae 100644 --- a/src/main/java/com/datastat/constant/Constant.java +++ b/src/main/java/com/datastat/constant/Constant.java @@ -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"; + /** * 支持性能数据上传的社区. */ diff --git a/src/main/java/com/datastat/controller/QueryController.java b/src/main/java/com/datastat/controller/QueryController.java index f321e99..66956ba 100644 --- a/src/main/java/com/datastat/controller/QueryController.java +++ b/src/main/java/com/datastat/controller/QueryController.java @@ -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") diff --git a/src/main/java/com/datastat/dao/QueryDao.java b/src/main/java/com/datastat/dao/QueryDao.java index 0181d37..0699099 100644 --- a/src/main/java/com/datastat/dao/QueryDao.java +++ b/src/main/java/com/datastat/dao/QueryDao.java @@ -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 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> 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> user_login = report.stream() .filter(m -> m.getOrDefault("user_login", "").equals(user)).collect(Collectors.toList()); @@ -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 future = esAsyncHttpUtil.executeSearch(esUrl, queryConf.getGiteeAllIndex(), queryJson); String responseBody = future.get().getResponseBody(UTF_8); @@ -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 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 response = Unirest.get(userInfoUrl) .header("cookie", cookie_oauth2_proxy) .asString(); @@ -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\""); @@ -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); diff --git a/src/main/java/com/datastat/model/CustomPropertiesConfig.java b/src/main/java/com/datastat/model/CustomPropertiesConfig.java index fea3610..3eef44a 100644 --- a/src/main/java/com/datastat/model/CustomPropertiesConfig.java +++ b/src/main/java/com/datastat/model/CustomPropertiesConfig.java @@ -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; diff --git a/src/main/java/com/datastat/service/QueryService.java b/src/main/java/com/datastat/service/QueryService.java index 9f763f4..71b1cf3 100644 --- a/src/main/java/com/datastat/service/QueryService.java +++ b/src/main/java/com/datastat/service/QueryService.java @@ -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; @@ -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) { @@ -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) { diff --git a/src/main/java/com/datastat/util/ClientUtil.java b/src/main/java/com/datastat/util/ClientUtil.java index 4813bc2..d5b4590 100644 --- a/src/main/java/com/datastat/util/ClientUtil.java +++ b/src/main/java/com/datastat/util/ClientUtil.java @@ -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 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; + } } + diff --git a/src/main/java/com/datastat/util/CsvFileUtil.java b/src/main/java/com/datastat/util/CsvFileUtil.java index 2ed4e6a..da802f4 100644 --- a/src/main/java/com/datastat/util/CsvFileUtil.java +++ b/src/main/java/com/datastat/util/CsvFileUtil.java @@ -59,7 +59,7 @@ public static List> readFile(String file) { return res; } catch (Exception e) { - logger.error("exception", e); + logger.error("read file exception - {}", e.getMessage()); return null; } }