NetworkInterfaceManager.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package com.primeton.damp.utils;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import java.net.Inet4Address;
  5. import java.net.NetworkInterface;
  6. import java.net.SocketException;
  7. import java.net.UnknownHostException;
  8. import java.util.ArrayList;
  9. import java.util.Collections;
  10. import java.util.Enumeration;
  11. import java.util.List;
  12. import java.util.Objects;
  13. /**
  14. *
  15. * 导入 java.net.InetAddress 太保不允许,因此写入到代码里,防止代码扫描工具报安全问题
  16. * <pre>
  17. *
  18. * Created by zhenqin.
  19. * User: zhenqin
  20. * Date: 2019/7/18
  21. * Time: 11:02
  22. * Vendor: yiidata.com
  23. * To change this template use File | Settings | File Templates.
  24. *
  25. * </pre>
  26. *
  27. * @author zhenqin
  28. */
  29. public enum NetworkInterfaceManager {
  30. INSTANCE;
  31. private java.net.InetAddress m_local;
  32. private java.net.InetAddress m_localHost;
  33. private static Logger logger = LoggerFactory.getLogger(NetworkInterfaceManager.class);
  34. private NetworkInterfaceManager() {
  35. load();
  36. }
  37. public java.net.InetAddress findValidateIp(List<java.net.InetAddress> addresses) {
  38. java.net.InetAddress local = null;
  39. int maxWeight = -1;
  40. for (java.net.InetAddress address : addresses) {
  41. if(address instanceof Inet4Address) {
  42. int weight = 0;
  43. if(address.isSiteLocalAddress()) {
  44. weight += 8;
  45. }
  46. if(address.isLinkLocalAddress()) {
  47. weight += 4;
  48. }
  49. if(address.isLoopbackAddress()) {
  50. weight += 2;
  51. }
  52. if(!Objects.equals(address.getHostName(), address.getHostAddress())) {
  53. weight += 1;
  54. }
  55. // 找到权重最大的网卡地址,绑定
  56. if(weight > maxWeight) {
  57. maxWeight = weight;
  58. local = address;
  59. }
  60. }
  61. }
  62. return local;
  63. }
  64. public String getLocalHostAddress() {
  65. return m_local.getHostAddress();
  66. }
  67. public String getLocalHostName() {
  68. try {
  69. if(m_localHost == null) {
  70. m_localHost = java.net.InetAddress.getLocalHost();
  71. }
  72. return m_localHost.getHostName();
  73. } catch (UnknownHostException e) {
  74. return m_local.getHostName();
  75. }
  76. }
  77. private String getProperty(String jvmArg, String envArg) {
  78. String value = System.getenv(envArg);
  79. if(value == null) {
  80. value = System.getProperty(jvmArg);
  81. }
  82. return value;
  83. }
  84. private final void load() {
  85. String ip = getProperty("host.name", "HOST_NAME");
  86. if(ip != null) {
  87. try {
  88. m_local = java.net.InetAddress.getByName(ip);
  89. return;
  90. } catch (UnknownHostException e) {
  91. logger.error("ip 获取异常。", e);
  92. //ignore
  93. }
  94. }
  95. try {
  96. Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
  97. List<NetworkInterface> nis = (interfaces == null) ? Collections.emptyList() : Collections.list(interfaces);
  98. List<java.net.InetAddress> addresses = new ArrayList<>(nis.size());
  99. java.net.InetAddress local = null;
  100. try {
  101. for (NetworkInterface ni : nis) {
  102. if(ni.isUp() && !ni.isLoopback()) {
  103. addresses.addAll(Collections.list(ni.getInetAddresses()));
  104. }
  105. }
  106. local = findValidateIp(addresses);
  107. } catch (Exception e) {
  108. //ignore
  109. }
  110. if(local != null) {
  111. m_local = local;
  112. return;
  113. }
  114. } catch (SocketException e) {
  115. //ignore except
  116. }
  117. m_local = java.net.InetAddress.getLoopbackAddress();
  118. }
  119. }