Skip to content

Commit f665f94

Browse files
committed
Add some IP-Features
1 parent cbdcbd3 commit f665f94

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/express/ExpressUtils.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import java.io.*;
66
import java.math.BigInteger;
7+
import java.net.Inet4Address;
8+
import java.net.UnknownHostException;
79
import java.security.SecureRandom;
810

911
public class ExpressUtils {
@@ -60,4 +62,11 @@ public static String randomToken(int byteLength, int radix) {
6062
secureRandom.nextBytes(token);
6163
return new BigInteger(1, token).toString(radix); //hex encoding
6264
}
65+
66+
/**
67+
* @return Your ip.
68+
*/
69+
public static String getYourIp() throws UnknownHostException {
70+
return Inet4Address.getLocalHost().getHostAddress();
71+
}
6372
}

src/express/http/Request.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import express.http.cookie.Cookie;
88

99
import java.io.*;
10+
import java.net.InetAddress;
11+
import java.net.InetSocketAddress;
1012
import java.net.URI;
1113
import java.util.HashMap;
1214
import java.util.List;
@@ -20,10 +22,11 @@ public class Request {
2022
private final HttpExchange HTTP_EXCHANGE;
2123
private final URI URI; // Request URI
2224
private final InputStream BODY; // Request body
23-
private final Headers HEADERS; // Request Headers
25+
private final Headers HEADERS; // Request Headers
2426
private final String CONTENT_TYPE; // Request content-type
27+
private final long CONTENT_LENGTH; // Request content-length
2528
private final Authorization AUTH; // Authorization header parsed
26-
private final long CONTENT_LENGTH;
29+
private final InetSocketAddress INET; // Client socket adress
2730

2831
private final HashMap<String, Object> MIDDLEWARE; // Middleware Data
2932
private final HashMap<String, Cookie> COOKIES; // Request cookies
@@ -44,6 +47,7 @@ public Request(HttpExchange exchange) {
4447
this.URI = exchange.getRequestURI();
4548
this.HEADERS = exchange.getRequestHeaders();
4649
this.BODY = exchange.getRequestBody();
50+
this.INET = exchange.getRemoteAddress();
4751

4852
// Parse content length
4953
String contentLength = HEADERS.get("Content-Length") != null ? HEADERS.get("Content-Length").get(0) : null;
@@ -158,6 +162,24 @@ public String getHost() {
158162
return HEADERS.get("Host").get(0);
159163
}
160164

165+
/**
166+
* Returns the InetAdress from the client.
167+
*
168+
* @return The InetAdress.
169+
*/
170+
public InetAddress getAddress() {
171+
return INET.getAddress();
172+
}
173+
174+
/**
175+
* Returns the IP-Adress from the client.
176+
*
177+
* @return The IP-Adress.
178+
*/
179+
public String getIp() {
180+
return INET.getAddress().getHostAddress();
181+
}
182+
161183
/**
162184
* @return The request content-type.
163185
*/

0 commit comments

Comments
 (0)