|
|
@ -1,14 +1,11 @@ |
|
|
|
package com.tduck.cloud.common.util; |
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil; |
|
|
|
import cn.hutool.core.map.MapUtil; |
|
|
|
import cn.hutool.core.util.ArrayUtil; |
|
|
|
import cn.hutool.core.util.ObjectUtil; |
|
|
|
import cn.hutool.core.util.StrUtil; |
|
|
|
import cn.hutool.http.HttpUtil; |
|
|
|
import lombok.experimental.UtilityClass; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import sun.net.util.IPAddressUtil; |
|
|
|
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
@ -55,7 +52,7 @@ public class AddressUtils { |
|
|
|
|
|
|
|
|
|
|
|
public boolean internalIp(String ip) { |
|
|
|
byte[] addr = IPAddressUtil.textToNumericFormatV4(ip); |
|
|
|
byte[] addr = textToNumericFormatV4(ip); |
|
|
|
return internalIp(addr) || "127.0.0.1".equals(ip); |
|
|
|
} |
|
|
|
|
|
|
@ -93,7 +90,77 @@ public class AddressUtils { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
getRealAddressByIP("218.23.216.254"); |
|
|
|
/** |
|
|
|
* 将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; |
|
|
|
} |
|
|
|
} |
|
|
|