IJPL-797 intellij.platform.ide.util.io review internal API: NetUtils has public and internal methods

There are methods that were marked as internal and obsolete despite usages in a few third party plugins:
* `canConnectToRemoteSocket`
* `canConnectToSocket`
* `isLocalhost`

Also, these entities look like implementation details:
* `isSniEnabled`
* `isValidHost`

GitOrigin-RevId: 078dac2b624f30f2ce932604f343b380fe5dfc0f
This commit is contained in:
Vladimir Lagunov
2024-06-18 14:40:59 +02:00
committed by intellij-monorepo-bot
parent bbca802373
commit 0aded4152e
3 changed files with 85 additions and 13 deletions

View File

@@ -489,3 +489,12 @@ e:com.intellij.util.io.PowerStatus
- s:values():com.intellij.util.io.PowerStatus[]
f:com.intellij.util.io.PowerStatus$Companion
- f:getPowerStatus():com.intellij.util.io.PowerStatus
f:com.intellij.util.net.NetUtils
- s:copyStreamContent(com.intellij.openapi.progress.ProgressIndicator,java.io.InputStream,java.io.OutputStream,I):I
- s:copyStreamContent(com.intellij.openapi.progress.ProgressIndicator,java.io.InputStream,java.io.OutputStream,J):J
- s:copyStreamContent(com.intellij.openapi.progress.ProgressIndicator,java.io.InputStream,java.io.OutputStream,J,Z):J
- s:findAvailableSocketPort():I
- s:findAvailableSocketPorts(I):I[]
- s:getLocalHostString():java.lang.String
- s:tryToFindAvailableSocketPort():I
- s:tryToFindAvailableSocketPort(I):I

View File

@@ -490,23 +490,11 @@ e:com.intellij.util.io.PowerStatus
f:com.intellij.util.io.PowerStatus$Companion
- f:getPowerStatus():com.intellij.util.io.PowerStatus
f:com.intellij.util.net.NetUtils
- s:canConnectToRemoteSocket(java.lang.String,I):Z
- s:canConnectToSocket(java.lang.String,I):Z
- s:copyStreamContent(com.intellij.openapi.progress.ProgressIndicator,java.io.InputStream,java.io.OutputStream,I):I
- s:copyStreamContent(com.intellij.openapi.progress.ProgressIndicator,java.io.InputStream,java.io.OutputStream,J):J
- s:copyStreamContent(com.intellij.openapi.progress.ProgressIndicator,java.io.InputStream,java.io.OutputStream,J,Z):J
- s:findAvailableSocketPort():I
- s:findAvailableSocketPorts(I):I[]
- s:getLocalHostString():java.lang.String
- s:isLocalhost(java.lang.String):Z
- s:isSniEnabled():Z
- s:isValidHost(java.lang.String):com.intellij.util.net.NetUtils$ValidHostInfo
- s:tryToFindAvailableSocketPort():I
- s:tryToFindAvailableSocketPort(I):I
e:com.intellij.util.net.NetUtils$ValidHostInfo
- java.lang.Enum
- sf:INVALID:com.intellij.util.net.NetUtils$ValidHostInfo
- sf:VALID:com.intellij.util.net.NetUtils$ValidHostInfo
- sf:VALID_PROXY:com.intellij.util.net.NetUtils$ValidHostInfo
- s:valueOf(java.lang.String):com.intellij.util.net.NetUtils$ValidHostInfo
- s:values():com.intellij.util.net.NetUtils$ValidHostInfo[]

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.util.net;
import com.google.common.net.HostAndPort;
@@ -13,7 +13,10 @@ import com.intellij.openapi.util.io.StreamUtil;
import com.intellij.openapi.util.text.StringUtilRt;
import com.intellij.util.SystemProperties;
import com.intellij.util.io.CountingGZIPInputStream;
import com.intellij.util.io.IoKt;
import com.intellij.util.io.IoService;
import kotlin.coroutines.Continuation;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -27,6 +30,20 @@ public final class NetUtils {
private NetUtils() { }
/**
* Use this function cautiously:
* <ul>
* <li>
* It checks the possibility to connect by actually connecting.
* It may be undesired in many situations.
* </li>
* <li>
* If {@code NetUtils.isLocalhost(host)} is true, it tries to listen on the socket instead of connecting.
* </li>
* </ul>
*/
@ApiStatus.Internal
@ApiStatus.Obsolete
public static boolean canConnectToSocket(String host, int port) {
if (isLocalhost(host)) {
return !canBindToLocalSocket(host, port);
@@ -36,6 +53,48 @@ public final class NetUtils {
}
}
/**
* The function is left to let the already existing code work as is.
* Please think twice before using this function in the new code.
* <p>
* This function tries to distinguish if {@code hostName} points to the loopback address, but there are many limitations:
* <ul>
* <li>
* {@code 127.0.0.2} is a valid loopback address, and {@code 127.1.2.3} too, and so on.
* However, the function detects only {@code 127.0.0.1}
* </li>
* <li>
* Although {@code localhost} resolves to {@code ::1} on macOS,
* it's not true for Ubuntu, which defines a separate host alias {@code ip6-localhost}.
* Nevertheless, the function always returns true for "localhost".
* </li>
* <li>
* {@code localhost.} with the dot at the end and {@code fgsfds.localhost} are a valid loopback addresses,
* but the function would return false.
* </li>
* <li>
* {@code 0:0:0:0:0:0:0:1}, {@code 0::0:1}, {@code 0:0::0:1} are valid IPv6 loopback addresses, but the function would return false.
* </li>
* <li>
* The user may assign any domain name for the loopback address in {@code /etc/hosts} / {@code C:\Windows\System32\drivers\etc\hosts},
* but the function won't detect it.
* </li>
* <li>
* There are public domain names that resolves to the loopback address. For example, {@code fbi.com}.
* However, the function won't detect it.
* </li>
* </ul>
* </p>
* <p>
* The only reliable way to check if {@code hostName} is a loopback address is to resolve it:
* <pre>
* InetAddress.getByName(hostName).isLoopbackAddress() // Invokes DNS lookups.
*
* </pre>
* </p>
*/
@ApiStatus.Internal
@ApiStatus.Obsolete
public static boolean isLocalhost(@NotNull @NlsSafe String hostName) {
return hostName.equalsIgnoreCase("localhost") || hostName.equals("127.0.0.1") || hostName.equals("::1");
}
@@ -53,6 +112,12 @@ public final class NetUtils {
}
}
/**
* Use this function cautiously, because it checks the possibility to connect by actually connecting.
* It may be undesired in many situations.
*/
@ApiStatus.Internal
@ApiStatus.Obsolete
public static boolean canConnectToRemoteSocket(String host, int port) {
try {
Socket socket = new Socket(host, port);
@@ -163,6 +228,7 @@ public final class NetUtils {
/**
* @deprecated use {@link #copyStreamContent(ProgressIndicator, InputStream, OutputStream, long)} instead
* @see IoKt#copyToAsync(InputStream, OutputStream, int, long, Continuation)
*/
@Deprecated
public static int copyStreamContent(@Nullable ProgressIndicator indicator,
@@ -172,6 +238,9 @@ public final class NetUtils {
return (int)copyStreamContent(indicator, inputStream, outputStream, (long)expectedContentLength);
}
/**
* @see IoKt#copyToAsync(InputStream, OutputStream, int, long, Continuation)
*/
public static long copyStreamContent(@Nullable ProgressIndicator indicator,
@NotNull InputStream inputStream,
@NotNull OutputStream outputStream,
@@ -180,6 +249,8 @@ public final class NetUtils {
}
/**
* @see IoKt#copyToAsync(InputStream, OutputStream, int, long, Continuation)
*
* @param indicator progress indicator
* @param inputStream source stream
* @param outputStream destination stream
@@ -247,6 +318,7 @@ public final class NetUtils {
return bytesWritten;
}
@ApiStatus.Internal
public static boolean isSniEnabled() {
return SystemProperties.getBooleanProperty("jsse.enableSNIExtension", true);
}
@@ -255,9 +327,12 @@ public final class NetUtils {
return IoService.getInstance().getProxySelector(pacUrlForUse);
}
@ApiStatus.Internal
public enum ValidHostInfo {
INVALID, VALID, VALID_PROXY
}
@ApiStatus.Internal
public static @NotNull ValidHostInfo isValidHost(@NotNull String host) {
try {
HostAndPort parsedHost = HostAndPort.fromString(host);