|
19 | 19 | import java.io.File;
|
20 | 20 | import java.io.InputStream;
|
21 | 21 | import java.io.UnsupportedEncodingException;
|
22 |
| -import java.net.InetSocketAddress; |
23 |
| -import java.net.URLEncoder; |
| 22 | +import java.net.*; |
24 | 23 | import java.nio.charset.Charset;
|
25 | 24 | import java.security.MessageDigest;
|
26 | 25 | import java.security.NoSuchAlgorithmException;
|
@@ -547,14 +546,14 @@ public void copyResponse(ChannelHandlerContext ctx, Request request, Response re
|
547 | 546 | }
|
548 | 547 |
|
549 | 548 | static String getRemoteIPAddress(MessageEvent e) {
|
550 |
| - String fullAddress = ((InetSocketAddress) e.getRemoteAddress()).getAddress().getHostAddress(); |
551 |
| - if (fullAddress.matches("/[0-9]+[.][0-9]+[.][0-9]+[.][0-9]+[:][0-9]+")) { |
552 |
| - fullAddress = fullAddress.substring(1); |
553 |
| - fullAddress = fullAddress.substring(0, fullAddress.indexOf(":")); |
554 |
| - } else if (fullAddress.matches(".*[%].*")) { |
555 |
| - fullAddress = fullAddress.substring(0, fullAddress.indexOf("%")); |
556 |
| - } |
557 |
| - return fullAddress; |
| 549 | + final InetAddress inetAddress = ((InetSocketAddress) e.getRemoteAddress()).getAddress(); |
| 550 | + final byte[] address = inetAddress.getAddress(); |
| 551 | + try { //create a new inetaddress only from numeric ( without host and interface information) |
| 552 | + return InetAddress.getByAddress(address).getHostAddress(); |
| 553 | + } catch (UnknownHostException unknownHostException) { //should never happen, else address is wrong |
| 554 | + Logger.error(unknownHostException,"Error: resolving numeric address: %s", inetAddress.getHostAddress()); |
| 555 | + } |
| 556 | + return null; |
558 | 557 | }
|
559 | 558 |
|
560 | 559 | public Request parseRequest(ChannelHandlerContext ctx, HttpRequest nettyRequest, MessageEvent messageEvent) throws Exception {
|
|
0 commit comments