Skip to content

Commit

Permalink
Merge pull request #526 from ant-media/getClientIpFromWS
Browse files Browse the repository at this point in the history
Get Client IP from WebSocket Communication
  • Loading branch information
mekya authored Jan 13, 2025
2 parents 3db8829 + 31e6363 commit 4df36be
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package io.antmedia.enterprise.streamapp;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.websocket.HandshakeResponse;
import jakarta.websocket.server.HandshakeRequest;
import jakarta.websocket.server.ServerEndpointConfig;

import org.apache.catalina.connector.RequestFacade;
import org.apache.tomcat.websocket.server.DefaultServerEndpointConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

public class AMSEndpointConfigurator extends DefaultServerEndpointConfigurator {
public static final String USER_AGENT = "user-agent";
public static final String ORIGIN = "origin";
private static final Object X_REAL_IP = "x-real-ip";
public static final String CLINT_IP = "client-ip";

protected static Logger logger = LoggerFactory.getLogger(AMSEndpointConfigurator.class);

Expand All @@ -31,6 +36,34 @@ public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request,
String origin = requestHeaders.get(ORIGIN).toString();
userProperties.put(ORIGIN, origin);
}

String clientIP = "N/A";
if(requestHeaders.containsKey(X_REAL_IP)){
clientIP = requestHeaders.get(X_REAL_IP).toString();
}
else {
Field requestField;
try {
requestField = request.getClass().getDeclaredField("request");
requestField.setAccessible(true);

RequestFacade requestFacade = (RequestFacade) requestField.get(request);

if (requestFacade != null) {
requestField = requestFacade.getClass().getDeclaredField("request");
requestField.setAccessible(true);

HttpServletRequest servletRequest = (HttpServletRequest) requestField.get(requestFacade);
clientIP = servletRequest.getRemoteAddr();
}
}
catch (Exception e) {
logger.warn("Client IP cannot be gathered");
}

}

userProperties.put(CLINT_IP, clientIP);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ public class WebSocketLocalHandler {

WebSocketCommunityHandler handler;
private String userAgent = "N/A";
private String clientIP = "N/A";


protected static Logger logger = LoggerFactory.getLogger(WebSocketLocalHandler.class);

@OnOpen
public void onOpen(Session session, EndpointConfig config) {
if(config.getUserProperties().containsKey(AMSEndpointConfigurator.USER_AGENT)) {
userAgent = (String) config.getUserProperties().get(AMSEndpointConfigurator.USER_AGENT);
clientIP = (String) config.getUserProperties().get(AMSEndpointConfigurator.CLINT_IP);
}

logger.info("Web Socket opened session:{} user-agent:{}", session.getId(), userAgent);
Expand Down Expand Up @@ -104,6 +107,7 @@ private void createHandler(ApplicationContext context, Session session) {
}

handler.setUserAgent(userAgent);
handler.setClientIP(clientIP);
} catch (Exception e) {
logger.error("WebSocket handler cannot be created");
logger.error(ExceptionUtils.getMessage(e));
Expand All @@ -121,4 +125,4 @@ public void sendNotInitializedError(Session session) {
logger.error(ExceptionUtils.getStackTrace(e));
}
}
}
}

0 comments on commit 4df36be

Please sign in to comment.