Skip to content

Commit

Permalink
respect X-Forwared-For and X-Real-IP headers from reverse proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Rostislav Raykov committed Dec 8, 2024
1 parent 82d34d5 commit ebfcd6a
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/main/java/org/rostislav/quickdrop/service/FileService.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,25 @@ public ResponseEntity<StreamingResponseBody> downloadFile(Long id, String passwo
}

private void logDownload(FileEntity fileEntity, HttpServletRequest request) {
String downloaderIp = request.getRemoteAddr();
String forwardedFor = request.getHeader("X-Forwarded-For");
String realIp = request.getHeader("X-Real-IP");
String downloaderIp;

if (forwardedFor != null && !forwardedFor.isEmpty()) {
// The X-Forwarded-For header can contain multiple IPs, pick the first one
downloaderIp = forwardedFor.split(",")[0].trim();
} else if (realIp != null && !realIp.isEmpty()) {
downloaderIp = realIp;
} else {
downloaderIp = request.getRemoteAddr();
}

String userAgent = request.getHeader(HttpHeaders.USER_AGENT);
DownloadLog downloadLog = new DownloadLog(fileEntity, downloaderIp, userAgent);
downloadLogRepository.save(downloadLog);
}


public FileEntity getFile(Long id) {
return fileRepository.findById(id).orElse(null);
}
Expand Down

0 comments on commit ebfcd6a

Please sign in to comment.