Last active
April 22, 2020 13:18
-
-
Save kirmartuk/6abc12778caa2e418458f604e770af39 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Utils which helps work with ip | |
* generate own ip | |
* convert ip to tiny url | |
* convert tiny url to ip | |
* change ip form | |
*/ | |
public class Utils { | |
// Key for encrypt && decrypt decimal ip to tiny url | |
static String key = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; | |
/** | |
* @return device's ip in short link form | |
*/ | |
public static String getOwnShortLink() { | |
return ipToTinyUrl(getIp()); | |
} | |
/** | |
* @param ip - generated in {@link #getIp()} | |
* @return decimal ip | |
*/ | |
public static long convertIpToLong(String ip) { | |
return Integer.parseInt(ip.split("\\.")[0]) * 16777216L | |
+ Integer.parseInt(ip.split("\\.")[1]) * 65536L | |
+ Integer.parseInt(ip.split("\\.")[2]) * 256L | |
+ Integer.parseInt(ip.split("\\.")[3]); | |
} | |
/** | |
* @param shortLink - generated in {@link #ipToTinyUrl(String)} | |
* @return ip from short link | |
*/ | |
public static String getIpFromShortLink(String shortLink) { | |
return convertLongToIp(tinyUrlToIp(shortLink)); | |
} | |
/** | |
* @param decimalIp - ip in decimal form | |
* @return ip in string form | |
*/ | |
public static String convertLongToIp(long decimalIp) { | |
return decimalIp / 16777216L // 256^3 | |
+ "." | |
+ decimalIp % 16777216L / 65536L // 256^2 | |
+ "." | |
+ decimalIp % 16777216L % 65536L / 256L // 256^1 | |
+ "." | |
+ decimalIp % 16777216L % 65536L % 256L; // 256^0 | |
} | |
/** | |
* @return computer's ip | |
*/ | |
public static String getIp() { | |
String ipAddress = null; | |
Enumeration<NetworkInterface> net = null; | |
try { | |
net = NetworkInterface.getNetworkInterfaces(); | |
} catch (SocketException e) { | |
e.printStackTrace(); | |
} | |
assert net != null; | |
while (net.hasMoreElements()) { | |
NetworkInterface element = net.nextElement(); | |
Enumeration<InetAddress> addresses = element.getInetAddresses(); | |
while (addresses.hasMoreElements()) { | |
InetAddress ip = addresses.nextElement(); | |
if (ip instanceof Inet4Address && ip.isSiteLocalAddress()) { | |
ipAddress = ip.getHostAddress(); | |
} | |
} | |
} | |
return ipAddress; | |
} | |
/** | |
* @param ip - generated in {@link #getIp()} | |
* @return tiny url | |
*/ | |
public static String ipToTinyUrl(String ip) { | |
long decimalIp = convertIpToLong(ip); | |
StringBuilder tinyUrl = new StringBuilder(); | |
while (decimalIp > 0) { | |
tinyUrl.append(key.charAt((int) (decimalIp % 62))); | |
decimalIp /= 62; | |
} | |
return tinyUrl.reverse().toString(); | |
} | |
/** | |
* @param tinyUrl - get tiny Url created in {@link #ipToTinyUrl(String)} | |
* @return decimal ip | |
*/ | |
public static long tinyUrlToIp(String tinyUrl) { | |
long ip = 0; | |
for (int i = 0; i < tinyUrl.length(); i++) { | |
ip = (ip * 62) + key.indexOf(tinyUrl.charAt(i)); | |
} | |
return ip; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment