1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.miloss.fgsms.common;
23
24 import java.net.InetAddress;
25 import java.net.NetworkInterface;
26 import java.util.ArrayList;
27 import java.util.Enumeration;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
30 import org.apache.log4j.Level;
31
32
33
34
35
36
37
38 public class IpAddressUtility {
39
40 public static final String logname = "fgsms.Utility";
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public static String modifyURL(String url, boolean isClient) {
56 if (Utility.stringIsNullOrEmpty(url))
57 throw new IllegalArgumentException("URL cannot be null or empty");
58 String str = url;
59
60 ArrayList<String> ips = new ArrayList<String>();
61 String host = "localhost";
62 ips.add("127.0.0.1");
63
64
65 try {
66 InetAddress addr = InetAddress.getLocalHost();
67 host = addr.getHostName().toLowerCase();
68 InetAddress localhost = InetAddress.getLocalHost();
69 ips.add(localhost.getHostAddress());
70 InetAddress[] appmyips = InetAddress.getAllByName(localhost.getCanonicalHostName());
71 if (appmyips != null && appmyips.length > 1) {
72 for (int i = 0; i < appmyips.length; i++) {
73 ips.add(appmyips[i].getHostAddress());
74 }
75 }
76 Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
77 while (en.hasMoreElements()) {
78 NetworkInterface intf = en.nextElement();
79 Enumeration<InetAddress> eip = intf.getInetAddresses();
80 while (eip.hasMoreElements()) {
81 InetAddress ip = eip.nextElement();
82 ips.add(ip.getHostAddress());
83 }
84 }
85 } catch (Exception ex) {
86 Logger.getLogger(logname).log(Level.WARN, "Error caught attempting to obtain local ip addresses " + ex.getLocalizedMessage());
87 }
88
89
90
91
92
93
94
95 if (isClient) {
96
97 boolean containsLocalhostOrLocalIP = false;
98 if (url.contains("://localhost:")) {
99 containsLocalhostOrLocalIP = true;
100 }
101 if (url.contains("://localhost/")) {
102 containsLocalhostOrLocalIP = true;
103 }
104 for (int i = 0; i < ips.size(); i++) {
105 if (url.contains("://" + ips.get(i) + "/")) {
106 containsLocalhostOrLocalIP = true;
107 break;
108 }
109 if (url.contains("://" + ips.get(i) + ":")) {
110 containsLocalhostOrLocalIP = true;
111 break;
112 }
113 }
114 if (containsLocalhostOrLocalIP) {
115
116 String prefix = str.substring(0, 5).toLowerCase();
117 for (int i = 0; i < ips.size(); i++) {
118
119 if (prefix.equals("https")) {
120 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+\\/", "://" + host + ":443/");
121 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+:", "://" + host + ":");
122 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+?$", "://" + host + ":443/");
123 } else if (prefix.equals("http:")) {
124 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+\\/", "://" + host + ":80/");
125 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+:", "://" + host + ":");
126 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+?$", "://" + host + ":80/");
127
128 } else {
129 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+\\/", "://" + host + "/");
130 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+:", "://" + host + ":");
131 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+?$", "://" + host + "/");
132 }
133
134 }
135 if (str.startsWith("jms:")) {
136 str = str.replaceFirst("\\:[a-zA-Z0-9\\-\\.]+:", host);
137 }
138 Logger.getLogger(logname).log(Level.DEBUG, "URL Modifier - original " + url + " modified " + str + " client=true");
139 return str;
140 }
141
142
143 String ipurl = ContainsIPAddress(str);
144 if (!Utility.stringIsNullOrEmpty(ipurl)) {
145
146
147
148
149 try {
150 try {
151
152 InetAddress addr = InetAddress.getByName(ipurl);
153
154
155 host = addr.getHostName().toLowerCase();
156 if (Utility.stringIsNullOrEmpty(host)) {
157 host = ipurl;
158 }
159 } catch (Exception e) {
160 host = ipurl;
161 }
162
163 str = str.replaceFirst(ipurl, host);
164
165
166
167
168
169 if (str.startsWith("jms:")) {
170 str = str.replaceFirst("\\:[a-zA-Z0-9\\-\\.]+:", "\\:" + host);
171 }
172 } catch (Exception ex) {
173 Logger.getLogger(logname).log(Level.WARN, "Client side url modifier, error caught trying to rdns the remote entry of " + url + ex.getLocalizedMessage());
174 }
175 Logger.getLogger(logname).log(Level.DEBUG, "URL Modifier - original " + url + " modified " + str + " client=true");
176 return str;
177 } else {
178
179
180 String hostname = "";
181
182 String hostnameregex = "://([^/:]+)";
183 Pattern p = Pattern.compile(hostnameregex);
184 Matcher m = p.matcher(str);
185 boolean find = m.find();
186 if (!find)
187 {
188 Logger.getLogger(logname).log(Level.DEBUG, "unexpected error modifing the url " + url + " maybe it's not an actual url?");
189 return url;
190 }
191 hostname = str.substring(m.start(), m.end());
192 hostname = hostname.replace("://", "");
193 boolean found = false;
194
195 try {
196 InetAddress localaddr = InetAddress.getByName(hostname);
197 String hostnameip = localaddr.getHostAddress();
198 for (int i = 0; i < ips.size(); i++) {
199 if (ips.get(i).equalsIgnoreCase(hostnameip)) {
200
201 found = true;
202
203
204
205 }
206 }
207
208 } catch (Exception e) {
209 }
210 if (found) {
211 String prefix = str.substring(0, 5).toLowerCase();
212 if (prefix.equals("https")) {
213 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+\\/", "://" + host + ":443/");
214 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+:", "://" + host + ":");
215 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+?$", "://" + host + ":443/");
216 } else if (prefix.equals("http:")) {
217 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+\\/", "://" + host + ":80/");
218 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+:", "://" + host + ":");
219 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+?$", "://" + host + ":80/");
220
221 } else if (str.startsWith("jms:")) {
222 str = str.replaceFirst("\\:[a-zA-Z0-9\\-\\.]+:", host);
223 } else {
224 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+\\/", "://" + host + "/");
225 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+:", "://" + host + ":");
226 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+?$", "://" + host + "/");
227 }
228
229 }
230 else
231 {
232 {
233 String prefix = str.substring(0, 5).toLowerCase();
234 if (prefix.equals("https")) {
235 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+\\/", "://" + hostname + ":443/");
236 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+:", "://" + hostname + ":");
237 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+?$", "://" + hostname + ":443/");
238 } else if (prefix.equals("http:")) {
239 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+\\/", "://" + hostname + ":80/");
240 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+:", "://" + hostname + ":");
241 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+?$", "://" + hostname + ":80/");
242
243 } else if (str.startsWith("jms:")) {
244 str = str.replaceFirst("\\:[a-zA-Z0-9\\-\\.]+:", hostname);
245 } else {
246 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+\\/", "://" + hostname + "/");
247 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+:", "://" + hostname + ":");
248 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+?$", "://" + hostname + "/");
249 }
250
251 }
252 }
253 Logger.getLogger(logname).log(Level.DEBUG, "URL Modifier - original " + url + " modified " + str + " client=true");
254 return str;
255
256
257 }
258
259 } else {
260
261
262
263
264
265
266
267
268
269
270
271
272
273 {
274 String prefix = str.substring(0, 5).toLowerCase();
275 for (int i = 0; i < ips.size(); i++) {
276 if (prefix.equals("https")) {
277 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+\\/", "://" + host + ":443/");
278 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+:", "://" + host + ":");
279 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+?$", "://" + host + ":443/");
280 } else if (prefix.equals("http:")) {
281 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+\\/", "://" + host + ":80/");
282 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+:", "://" + host + ":");
283 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+?$", "://" + host + ":80/");
284
285 } else {
286 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+\\/", "://" + host + "/");
287 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+:", "://" + host + ":");
288 str = str.replaceFirst("\\://[a-zA-Z0-9\\-\\.]+?$", "://" + host + "/");
289 }
290
291
292
293 }
294
295 if (str.contains("?")) {
296 str = str.substring(0, str.indexOf("?"));
297 }
298 }
299
300 if (str.startsWith("jms:")) {
301 str = str.replaceFirst("\\:[a-zA-Z0-9\\-\\.]+:", ":" + host + ":");
302 }
303 }
304 Logger.getLogger(logname).log(Level.DEBUG, "URL Modifier - original " + url + " modified " + str);
305
306 return str;
307 }
308
309
310
311
312
313
314 private static String ContainsIPAddress(String str) {
315 if (Utility.stringIsNullOrEmpty(str)) {
316 return "";
317 }
318 String reg = "\\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b";
319 String worker = str;
320 try {
321 Pattern p = Pattern.compile(reg);
322 Matcher m = p.matcher(worker);
323 if (m.find()) {
324 return str.substring(m.start(), m.end());
325 } else {
326 return "";
327 }
328
329 } catch (Exception ex) {
330 }
331 return "";
332 }
333 }