-
Notifications
You must be signed in to change notification settings - Fork 26.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
当机器同时存在内网和外网IP时,无法发布内网IP服务 #3802
Comments
+1 to you proposal, would you like to send a pull request to fix this? |
I can try it, not a lot of spare time. |
@nevin9939 I send a pull request, could you pls review it and try it to see whether meet your requirement? Thanks. |
@lexburner private static InetAddress getLocalAddress0() {
InetAddress candidateAddress = null;
int interfacesIndex = 0;
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
if (null != interfaces) {
while (interfaces.hasMoreElements()) {
try {
NetworkInterface network = interfaces.nextElement();
// Remove loopback interface, subinterface, not running interface
if (network.isLoopback() || network.isVirtual() || !network.isUp()) {
continue;
}
Enumeration<InetAddress> addresses = network.getInetAddresses();
while (addresses.hasMoreElements()) {
try {
Optional<InetAddress> addressOp = toValidAddress(addresses.nextElement());
if (addressOp.isPresent()) {
InetAddress tempAddress = addressOp.get();
if (candidateAddress == null || interfacesIndex > network.getIndex()) {
// Priority is given to the network card with a small index value,
// which can exclude the IP of the self-built virtual machine.
candidateAddress = tempAddress;
interfacesIndex = network.getIndex();
}
}
} catch (Throwable e) {
logger.warn(e);
}
}
} catch (Throwable e) {
logger.warn(e);
}
}
}
if(candidateAddress == null){
InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();
if (jdkSuppliedAddress == null) {
throw new UnknownHostException("The JDK InetAddress.getLocalHost() method unexpectedly returned null.");
}
Optional<InetAddress> addressOp = toValidAddress(jdkSuppliedAddress);
if (addressOp.isPresent()) {
candidateAddress = addressOp.get();
}
}
} catch (Throwable e) {
logger.warn(e);
}
return candidateAddress;
} |
Looks good to me, I will take it as consideration. |
this sounds like a solid solution to filter out the network interfaces first: if (network.isLoopback() || network.isVirtual() || !network.isUp()) {
continue;
} And, it looks we should either rollback the logic 'isSiteLocalAddress()' or add a flag for it. |
#3953 enhanced this issue. |
Environment
Steps to reproduce this issue
查看源代码发现在 request #3520 中调整为优先获取外网IP,造成了当机器同时存在内网IP和外网IP时,期望只提供内网IP服务时就无法做到,个人认为最好有个配置开关,有用户决定是提供内网服务还是外网服务
The text was updated successfully, but these errors were encountered: