允许http回调

This commit is contained in:
lu7213 committed 2026-11-02 10:02:29 -05:00
1 parent cfb6a9e389
commit f15f4162eb
6 files changed
+50 -10

No files matched your search

@@ -10,7 +10,7 @@ import java.net.URI;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
/** 回调专用HTTP客户端:HTTPS、解析结果逐一检查、实际连接复用该DNS结果、拒绝重定向。 */
/** 回调专用HTTP客户端:HTTPS或内网HTTP、解析结果逐一检查、拒绝重定向。 */
public final class RentalHttp {
private RentalHttp() {}
@@ -26,14 +26,15 @@ public final class RentalHttp {
public static void checkUrl(String url) {
URI uri = URI.create(url);
require(
"https".equalsIgnoreCase(uri.getScheme())
("https".equalsIgnoreCase(uri.getScheme()) || "http".equalsIgnoreCase(uri.getScheme()))
&& uri.getHost() != null
&& uri.getUserInfo() == null
&& uri.getFragment() == null
&& uri.getQuery() == null
&& (uri.getPort() == -1 || uri.getPort() == 443)
&& (uri.getPort() == -1 || ("http".equalsIgnoreCase(uri.getScheme())
? uri.getPort() >= 1 && uri.getPort() <= 65535 : uri.getPort() == 443))
&& url.length() <= 512,
"回调仅允许标准HTTPS地址");
"回调仅允许HTTPS 443或内网HTTP有效端口地址,且不能含查询串、片段或用户信息");
}
public static boolean publicAddress(InetAddress address) {
@@ -72,18 +73,31 @@ public final class RentalHttp {
* 对解析出的每个地址校验,并将同一结果用于连接,避免校验与连接使用不同DNS结果。
*/
public static OkHttpClient callbackClient(boolean allowPrivateNetwork) {
return callbackClient(allowPrivateNetwork, false);
}
/** HTTP专用客户端只连接私网,禁止代理绕过DNS校验;解析结果直接用于实际连接。 */
public static OkHttpClient callbackClient(boolean allowPrivateNetwork, boolean privateHttpOnly) {
return new OkHttpClient.Builder()
.proxy(java.net.Proxy.NO_PROXY)
.connectTimeout(5, TimeUnit.SECONDS)
.readTimeout(5, TimeUnit.SECONDS)
.callTimeout(10, TimeUnit.SECONDS)
.followRedirects(false)
.followSslRedirects(false)
.retryOnConnectionFailure(false)
.addInterceptor(chain -> {
if (!chain.request().isHttps() && (!privateHttpOnly || !allowPrivateNetwork)) {
throw new CallbackTargetRejectedException();
}
return chain.proceed(chain.request());
})
.dns(
host -> {
InetAddress[] addresses = InetAddress.getAllByName(host);
for (InetAddress address : addresses) {
if (!callbackAddressAllowed(address, allowPrivateNetwork)) {
if (!callbackAddressAllowed(address, allowPrivateNetwork)
|| (privateHttpOnly && publicAddress(address))) {
throw new CallbackTargetRejectedException();
}
}
@@ -23,6 +23,7 @@ public class RentalWorkers {
private final RentalSocket socket;
private final RentalIdentity identity;
private OkHttpClient callbacks = RentalHttp.callbackClient();
private OkHttpClient privateHttpCallbacks = RentalHttp.callbackClient(false, true);
@org.springframework.beans.factory.annotation.Value("${rental.webhook.allow-private-network:true}")
private boolean allowPrivateNetwork = true;
private long walletCursor;
@@ -40,6 +41,7 @@ public class RentalWorkers {
@javax.annotation.PostConstruct
public void validateDeliveryIntervals() {
callbacks = RentalHttp.callbackClient(allowPrivateNetwork);
privateHttpCallbacks = RentalHttp.callbackClient(allowPrivateNetwork, true);
require(dispatchIntervalMs >= 500 && dispatchIntervalMs <= 60000,
"订单调度间隔必须为500到60000毫秒");
require(pushIntervalMs >= 500 && pushIntervalMs <= 60000,
@@ -47,7 +49,7 @@ public class RentalWorkers {
LOG.info("租赁交付调度配置 dispatchIntervalMs={} pushIntervalMs={}",
dispatchIntervalMs, pushIntervalMs);
LOG.info("租赁快照定时扫描已启用 intervalMs=15000 batchSize=20,刷新ACTIVE且快照缺失或过期的钱包");
LOG.info("租赁回调扫描已启用 intervalMs=5000 batchSize=20 maxAttempts=10 allowPrivateNetwork={};HTTPS证书校验开启,拒绝重定向、回环及链路本地地址,要求HTTP200且正文严格为ok",
LOG.info("租赁回调扫描已启用 intervalMs=5000 batchSize=20 maxAttempts=10 allowPrivateNetwork={};允许内网HTTP,HTTPS仍校验证书,拒绝重定向、回环及链路本地地址,要求HTTP200且正文严格为ok",
allowPrivateNetwork);
}
@@ -341,7 +343,8 @@ public class RentalWorkers {
stage = "HTTP_EXECUTE";
LOG.info("租赁回调请求开始 webhookId={} orderId={} method=POST path={} connectTimeoutMs=5000 callTimeoutMs=10000",
id, row.get("order_id"), parsed.encodedPath());
try (Response response = callbacks.newCall(request).execute()) {
OkHttpClient client = parsed.isHttps() ? callbacks : privateHttpCallbacks;
try (Response response = client.newCall(request).execute()) {
LOG.info("租赁回调收到HTTP响应 webhookId={} httpStatus={} contentType={} contentLength={} elapsedMs={}",
id, response.code(), response.body() == null ? "" : response.body().contentType(),
response.body() == null ? -1 : response.body().contentLength(),