如何通过xinetd从java服务器获取客户端IP?
我正在通过 xinetd 超级服务器运行一个小型 java 服务器。 我想获取原始客户端 IP,但我不能,因为流位于 xinetd 和 java (stin/stdout) 之间。
有人知道如何在不查看 xinetd 日志文件的情况下获取客户端 IP 吗? (对我来说似乎是一个糟糕的解决方案)
谢谢!
I am running a small java server through xinetd superserver.
I'd like to get the originating client IP but I can't because streams are between xinetd and java (stin/stdout).
Does somebody know how to get the client IP, without looking to xinetd logfile ? (seems a bad solution to me)
Thanks !
鉴于您所描述的情况,搜寻 xinetd 日志文件是您唯一的选择。
如果您的 Java 应用程序通过其标准输入和标准输出与客户端通信,则应用程序无法访问底层套接字。事实上,我认为任何语言都无法做到这一点。
编辑:实际上,您可能可以在 C 和 C++ 中执行此操作,因为它们公开文件描述符 (fds) 并具有用于使用 fds 执行套接字操作的库 API。
但它在纯 Java 中不起作用。即使您可以深入到与 System.in 或 System.out 关联的 Stream 对象内的 fd,我也不认为 Java 类库提供 API用于将 fd 转换为 Socket 对象。要在 fd 的 0 和 1 上执行套接字操作,您需要求助于 JNI 和本机代码。
正如评论者所指出的,如果真正的客户端位于代理后面,那么您从套接字获得的客户端 IP 将是代理的 IP。
Given the situation you have described, trawling the xinetd logfile is your only option.
If your Java application is talking to the client via its standard input and standard output, there is no way for the application to access the underlying socket. Indeed, I don't think you could do this in any language.
EDIT : actually, you probably could do this in C and C++ because they expose the file descriptors (fds) and have library APIs for doing socket operations using fds.
But it won't work in pure Java. Even if you could drill down to the fd inside the Stream objects associated with
System.in
orSystem.out
, I don't think that the Java class libraries provide an API for turning the fd into aSocket
object. To do socket operations on the fd's 0 and 1 you would need to resort to JNI and native code.And as the commenter points out, if the real client is behind a proxy, the client IP that you get from the socket will be the IP of the proxy.