Skip to content

Commit 0744bf9

Browse files
committed
optimize code for better maintainability
Signed-off-by: zane-neo <[email protected]>
1 parent 3149936 commit 0744bf9

File tree

1 file changed

+38
-21
lines changed

1 file changed

+38
-21
lines changed

common/src/main/java/org/opensearch/ml/common/httpclient/MLHttpClientFactory.java

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -78,35 +78,52 @@ private static boolean hasPrivateIpAddress(InetAddress[] ipAddress) {
7878
if (bytes.length != 4) {
7979
return true;
8080
} else {
81-
int firstOctets = bytes[0] & 0xff;
82-
int firstInOctal = parseWithOctal(String.valueOf(firstOctets));
83-
int firstInHex = Integer.parseInt(String.valueOf(firstOctets), 16);
84-
if (firstInOctal == 127 || firstInHex == 127) {
85-
return bytes[1] == 0 && bytes[2] == 0 && bytes[3] == 1;
86-
} else if (firstInOctal == 10 || firstInHex == 10) {
87-
return true;
88-
} else if (firstInOctal == 172 || firstInHex == 172) {
89-
int secondOctets = bytes[1] & 0xff;
90-
int secondInOctal = parseWithOctal(String.valueOf(secondOctets));
91-
int secondInHex = Integer.parseInt(String.valueOf(secondOctets), 16);
92-
return (secondInOctal >= 16 && secondInOctal <= 32) || (secondInHex >= 16 && secondInHex <= 32);
93-
} else if (firstInOctal == 192 || firstInHex == 192) {
94-
int secondOctets = bytes[1] & 0xff;
95-
int secondInOctal = parseWithOctal(String.valueOf(secondOctets));
96-
int secondInHex = Integer.parseInt(String.valueOf(secondOctets), 16);
97-
return secondInOctal == 168 || secondInHex == 168;
98-
}
81+
// case 127.0.0.1
82+
boolean isLocalHost = eqCheckValue(bytes[0], 127)
83+
&& eqCheckValue(bytes[1], 0)
84+
&& eqCheckValue(bytes[2], 0)
85+
&& eqCheckValue(bytes[3], 0);
86+
// case 10.x.x.x
87+
boolean isLocalHost10 = eqCheckValue(bytes[0], 10);
88+
// case 172.16.x.x - 172.31.x.x
89+
boolean isLocalHost172 = eqCheckValue(bytes[0], 172) && rangeCheckValue(bytes[1]);
90+
// case 192.168.x.x
91+
boolean isLocalHost192 = eqCheckValue(bytes[0], 192) && eqCheckValue(bytes[1], 168);
92+
// case 169.254.x.x
93+
boolean isLocalHost169 = eqCheckValue(bytes[0], 169) && eqCheckValue(bytes[1], 254);
94+
95+
return isLocalHost || isLocalHost10 || isLocalHost172 || isLocalHost192 || isLocalHost169;
9996
}
10097
}
10198
}
10299
return Arrays.stream(ipAddress).anyMatch(x -> x.isSiteLocalAddress() || x.isLoopbackAddress() || x.isAnyLocalAddress());
103100
}
104101

105-
private static int parseWithOctal(String input) {
102+
private static boolean eqCheckValue(byte input, int targetValue) {
103+
int original = input & 0xff;
104+
return original == targetValue || parseWithRadix(original, 8) == targetValue || parseWithRadix(original, 16) == targetValue;
105+
}
106+
107+
private static boolean rangeCheckValue(byte input) {
108+
int original = input & 0xff;
109+
if (original >= 16 && original <= 31) {
110+
return true;
111+
} else {
112+
int octalValue = parseWithRadix(original, 8);
113+
if (octalValue >= 16 && octalValue <= 31) {
114+
return true;
115+
} else {
116+
int hexValue = parseWithRadix(original, 16);
117+
return hexValue >= 16 && hexValue <= 31;
118+
}
119+
}
120+
}
121+
122+
private static int parseWithRadix(int input, int radix) {
106123
try {
107-
return Integer.parseInt(input, 8);
124+
return Integer.parseInt(String.valueOf(input), radix);
108125
} catch (NumberFormatException e) {
109-
return Integer.parseInt(input);
126+
return input;
110127
}
111128
}
112129
}

0 commit comments

Comments
 (0)