package com.ycl.utils.common; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.text.StrTokenizer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletRequest; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.regex.Pattern; /** * @author Lyq * @version 1.0 * @date 2022/9/7 */ public class NetworkUtil { public static final String REQ_TOKEY_KEY = "Authorization"; /** * Logger for this class */ private static final Logger LOGGER = LoggerFactory.getLogger(NetworkUtil.class); /** * 方法名: getIpAddress * 方法描述: 获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址; * 参数 @param request * 参数 @return * 参数 @throws IOException 参数说明 * 返回类型 String 返回类型 */ public final static String getIpAddress(HttpServletRequest request) { // 获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址 if(null==request){ return ""; } String ip = request.getHeader("x-original-forwarded-for"); // if (LOGGER.isInfoEnabled()) { // LOGGER.info("getIpAddress(HttpServletRequest) - x-original-forwarded-for - String ip=" + ip); // } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("X-Forwarded-For"); // if (LOGGER.isInfoEnabled()) { // LOGGER.info("getIpAddress(HttpServletRequest) - X-Forwarded-For - String ip=" + ip); // } } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); // if (LOGGER.isInfoEnabled()) { // LOGGER.info( // "getIpAddress(HttpServletRequest) - Proxy-Client-IP - String ip=" + ip); // } } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); // if (LOGGER.isInfoEnabled()) { // LOGGER.info( // "getIpAddress(HttpServletRequest) - WL-Proxy-Client-IP - String ip=" + ip); // } } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_CLIENT_IP"); // if (LOGGER.isInfoEnabled()) { // LOGGER.info( // "getIpAddress(HttpServletRequest) - HTTP_CLIENT_IP - String ip=" + ip); // } } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_X_FORWARDED_FOR"); // if (LOGGER.isInfoEnabled()) { // LOGGER // .info("getIpAddress(HttpServletRequest) - HTTP_X_FORWARDED_FOR - String ip=" // + ip); // } } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); // if (LOGGER.isInfoEnabled()) { // LOGGER.info("getIpAddress(HttpServletRequest) - getRemoteAddr - String ip=" + ip); // } } //防范ip欺骗,如果有多个逗号分隔的ip,必须读最后一个 if (ip.length() > 15) { String[] ips = ip.split(","); ip = ips[ips.length -1]; } return ip; } public static final String _255 = "(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"; public static final Pattern pattern = Pattern.compile("^(?:" + _255 + "\\.){3}" + _255 + "$"); public static String longToIpV4(long longIp) { int octet3 = (int) ((longIp >> 24) % 256); int octet2 = (int) ((longIp >> 16) % 256); int octet1 = (int) ((longIp >> 8) % 256); int octet0 = (int) ((longIp) % 256); return octet3 + "." + octet2 + "." + octet1 + "." + octet0; } public static long ipV4ToLong(String ip) { String[] octets = ip.split("\\."); return (Long.parseLong(octets[0]) << 24) + (Integer.parseInt(octets[1]) << 16) + (Integer.parseInt(octets[2]) << 8) + Integer.parseInt(octets[3]); } public static boolean isIPv4Private(String ip) { long longIp = ipV4ToLong(ip); return (longIp >= ipV4ToLong("10.0.0.0") && longIp <= ipV4ToLong("10.255.255.255")) || (longIp >= ipV4ToLong("172.16.0.0") && longIp <= ipV4ToLong("172.31.255.255")) || longIp >= ipV4ToLong("192.168.0.0") && longIp <= ipV4ToLong("192.168.255.255"); } public static boolean isIPv4Valid(String ip) { return pattern.matcher(ip).matches(); } public static String getIpFromRequest(HttpServletRequest request) { String ip; boolean found = false; if ((ip = request.getHeader("x-forwarded-for")) != null) { StrTokenizer tokenizer = new StrTokenizer(ip, ","); while (tokenizer.hasNext()) { ip = tokenizer.nextToken().trim(); if (isIPv4Valid(ip) && !isIPv4Private(ip)) { found = true; break; } } } if (!found) { ip = request.getRemoteAddr(); } return ip; } /** * 方法名: getAccessToken * 方法描述: 获取token * 参数 @param request * 参数 @return 参数说明 * 返回类型 String 返回类型 */ public static String getAccessToken(HttpServletRequest request) { String tokenNumber = request.getHeader(REQ_TOKEY_KEY); if (StringUtils.isBlank(tokenNumber)) { tokenNumber = request.getParameter("Authorization"); LOGGER.debug("tokenNumber=" + tokenNumber); } return tokenNumber; } /** /** * 从请求头中获取指定key的值 * * @param request * @param key * @return */ public static String getHeaderByKey(HttpServletRequest request, String key) { if (StringUtils.isBlank(key)) { return null; } return request.getHeader(key); } /** * 从请求头中获取是否需要显示英文 * * @param request * @param * @return true 显示英文、false 显示中文 */ public static boolean isEnglish(HttpServletRequest request) { if ("en".equalsIgnoreCase(getHeaderByKey(request,"language"))) { return true; }else{ return false; } } public static String getHostNameForLiunx() { try { return (InetAddress.getLocalHost()).getHostName(); } catch (UnknownHostException uhe) { String host = uhe.getMessage(); // host = "hostname: hostname" if (host != null) { int colon = host.indexOf(':'); if (colon > 0) { return host.substring(0, colon); } } return "UnknownHost"; } } public static String getHostName() { if (System.getenv("COMPUTERNAME") != null) { return System.getenv("COMPUTERNAME"); } else { return getHostNameForLiunx(); } } public static boolean internalIp(String ip) { byte[] addr = textToNumericFormatV4(ip); if (null != addr) { return internalIp(addr) || "127.0.0.1".equals(ip); } return false; } private static boolean internalIp(byte[] addr) { final byte b0 = addr[0]; final byte b1 = addr[1]; // 10.x.x.x/8 final byte SECTION_1 = 0x0A; // 172.16.x.x/12 final byte SECTION_2 = (byte) 0xAC; final byte SECTION_3 = (byte) 0x10; final byte SECTION_4 = (byte) 0x1F; // 192.168.x.x/16 final byte SECTION_5 = (byte) 0xC0; final byte SECTION_6 = (byte) 0xA8; switch (b0) { case SECTION_1: return true; case SECTION_2: if (b1 >= SECTION_3 && b1 <= SECTION_4) { return true; } case SECTION_5: switch (b1) { case SECTION_6: return true; } default: return false; } } /** * 将IPv4地址转换成字节 * * @param text IPv4地址 * @return byte 字节 */ public static byte[] textToNumericFormatV4(String text) { if (text.length() == 0) { return null; } byte[] bytes = new byte[4]; String[] elements = text.split("\\.", -1); try { long l; int i; switch (elements.length) { case 1: l = Long.parseLong(elements[0]); if ((l < 0L) || (l > 4294967295L)) { return null; } bytes[0] = (byte) (int) (l >> 24 & 0xFF); bytes[1] = (byte) (int) ((l & 0xFFFFFF) >> 16 & 0xFF); bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF); bytes[3] = (byte) (int) (l & 0xFF); break; case 2: l = Integer.parseInt(elements[0]); if ((l < 0L) || (l > 255L)) { return null; } bytes[0] = (byte) (int) (l & 0xFF); l = Integer.parseInt(elements[1]); if ((l < 0L) || (l > 16777215L)) { return null; } bytes[1] = (byte) (int) (l >> 16 & 0xFF); bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF); bytes[3] = (byte) (int) (l & 0xFF); break; case 3: for (i = 0; i < 2; ++i) { l = Integer.parseInt(elements[i]); if ((l < 0L) || (l > 255L)) { return null; } bytes[i] = (byte) (int) (l & 0xFF); } l = Integer.parseInt(elements[2]); if ((l < 0L) || (l > 65535L)) { return null; } bytes[2] = (byte) (int) (l >> 8 & 0xFF); bytes[3] = (byte) (int) (l & 0xFF); break; case 4: for (i = 0; i < 4; ++i) { l = Integer.parseInt(elements[i]); if ((l < 0L) || (l > 255L)) { return null; } bytes[i] = (byte) (int) (l & 0xFF); } break; default: return null; } } catch (NumberFormatException e) { return null; } return bytes; } }