Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.net.SocketException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -36,6 +37,8 @@ public final class NetUtils {

private static final Logger LOGGER = LoggerFactory.getLogger(NetUtils.class);

private static final String COLON = ":";

// one interface can bind to multiple address
// we only save one ip for each interface name.
// eg:
Expand Down Expand Up @@ -151,25 +154,30 @@ public static IpPort parseIpPortFromURI(String uriAddress) {
}

/**
* 对于配置为0.0.0.0的地址,let it go
* schema, e.g. http
* adddress, e.g 0.0.0.0:8080
* return 实际监听的地址
* if address is 0.0.0.0, replace it with {@link #hostAddress}
*
* @param schema e.g. http
* @param address e.g 0.0.0.0:8080
* @return the address actually to be bound to
*/
public static String getRealListenAddress(String schema, String address) {
if (address == null) {
return null;
}
address = checkAddress(address);
if (address == null) {
return null;
}
try {
URI originalURI = new URI(schema + "://" + address);
IpPort ipPort = NetUtils.parseIpPort(originalURI.getAuthority());
if (ipPort == null) {
LOGGER.error("address {} is not valid.", address);
LOGGER.error("schema {} or address {} is not valid.", schema, address);
return null;
}
return originalURI.toString();
} catch (URISyntaxException e) {
LOGGER.error("address {} is not valid.", address);
LOGGER.error("schema {} or address {} is not valid.", schema, address);
return null;
}
}
Expand Down Expand Up @@ -201,4 +209,33 @@ public static boolean canTcpListen(InetAddress address, int port) {
return false;
}
}

/**
* For security reason, 0.0.0.0 is not allowed to be bound to,
* and should be replaced with {@link #hostAddress}
*
* @param address address in configuration
* @return if host is 0.0.0.0, replace it with {@link #hostAddress};
* otherwise, return as it is.
*/
private static String checkAddress(String address) {
if (!address.contains(COLON)) {
LOGGER.error("unexpected address format");
return null;
}
String ip = address.substring(0, address.indexOf(COLON));
InetAddress inetAddress = null;
try {
inetAddress = InetAddress.getByName(ip);
} catch (UnknownHostException e) {
LOGGER.error("host {} is unknown.", ip);
return null;
}
if (inetAddress.isAnyLocalAddress()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any security reason for this change?
We need to tell add the release-note to tell user they cannot use "0.0.0.0" any more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I misunderstood the requirement. I've communicated with liubao and jimin, and ensure that our current mechanism is right. This pull request will be closed later.

LOGGER.warn("address {} is not allowed to be bound to, choose {} as alternate, may not be correct.",
ip, NetUtils.getHostAddress());
address = NetUtils.getHostAddress() + address.substring(address.indexOf(COLON));
}
return address;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ public void testGetRealListenAddress() {
Assert.assertEquals("http://1.1.1.1:8080", NetUtils.getRealListenAddress("http", "1.1.1.1:8080"));
}

@Test
public void testGetRealListenAddressOnConfiguredAddressIsAll0() {
String hostAddressFieldName = "hostAddress";
String preservedHost = Deencapsulation.getField(NetUtils.class, hostAddressFieldName);
Deencapsulation.setField(NetUtils.class, hostAddressFieldName, "12.12.12.12");
Assert.assertEquals("http://12.12.12.12:8080", NetUtils.getRealListenAddress("http", "0.0.0.0:8080"));

Deencapsulation.setField(NetUtils.class, hostAddressFieldName, preservedHost);
}

@Test
public void testNetworkInterface() {
Map<String, InetAddress> org = Deencapsulation.getField(NetUtils.class, "allInterfaceAddresses");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@
import io.servicecomb.common.rest.codec.RestObjectMapper;
import io.servicecomb.demo.compute.Person;
import io.servicecomb.demo.server.User;
import io.servicecomb.foundation.common.net.NetUtils;

@Ignore
public class JaxrsIntegrationTestBase {

private final String baseUrl = "http://127.0.0.1:8080/";
private final String baseUrl = "http://" + NetUtils.getHostAddress() + ":8080/";

private final RestTemplate restTemplate = new RestTemplate();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ cse:
registry:
address: http://127.0.0.1:30100
rest:
address: 0.0.0.0:8080
address: 127.0.0.1:8080
Copy link
Contributor

@liubao68 liubao68 Dec 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么需要修改这里的地址?不修改会影响用例正常运行吗?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

因为现在监听IP的时候不允许监听0.0.0.0了。我们的测试里面全都是provider配的监听0.0.0.0,consumer连接用的127.0.0.1,现在这样会连不上。
其他的测试我已经改成consumer端用NetUtils获取本机IP了。但是有的测试consumer是从配置文件中获取provider的IP做连接的,不好改,所以我就改成两方都用127.0.0.1了。

handler:
chain:
Provider:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@
import io.servicecomb.common.rest.codec.RestObjectMapper;
import io.servicecomb.demo.compute.Person;
import io.servicecomb.demo.server.User;
import io.servicecomb.foundation.common.net.NetUtils;

@Ignore
public class SpringMvcIntegrationTestBase {

private final String baseUrl = "http://127.0.0.1:8080/";
private final String baseUrl = "http://" + NetUtils.getHostAddress() + ":8080/";

private final RestTemplate restTemplate = new RestTemplate();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ cse:
registry:
address: http://127.0.0.1:30100
rest:
address: 0.0.0.0:8080?sslEnabled=false
address: 127.0.0.1:8080?sslEnabled=false
highway:
address: 0.0.0.0:7070
address: 127.0.0.1:7070
handler:
chain:
Provider:
Expand Down