fix:"修复媒体下载DNS重绑定并隔离出站传输"
This commit is contained in:
1 parent
926aa64ba3
commit
32f6f67b9f
15 files changed
+791
-56
No files matched your search
+5
-49
@@ -8,8 +8,9 @@ import com.moyi.controlplane.security.SecretCipher;
|
||||
import com.moyi.controlplane.provider.credentials.UpstreamCredentialResolver;
|
||||
import com.moyi.controlplane.provider.transport.ProviderRequestException;
|
||||
import com.moyi.controlplane.provider.transport.BoundedHttpTransport;
|
||||
import com.moyi.controlplane.provider.media.MediaDownloader;
|
||||
import com.moyi.controlplane.provider.media.MediaDownloader.MediaBytes;
|
||||
import java.net.URI;
|
||||
import java.net.InetAddress;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.time.Duration;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
@@ -42,6 +43,7 @@ public class AsyncProviderClient {
|
||||
* every request otherwise selected the first upstream key forever. */
|
||||
private final ConcurrentMap<String, AtomicInteger> keyCursors = new ConcurrentHashMap<>();
|
||||
private final BoundedHttpTransport transport = new BoundedHttpTransport();
|
||||
private final MediaDownloader mediaDownloader = new MediaDownloader();
|
||||
|
||||
public AsyncProviderClient(JdbcTemplate jdbc, SecretCipher cipher, ObjectMapper mapper, Environment environment) {
|
||||
this.jdbc = jdbc; this.cipher = cipher; this.mapper = mapper; this.environment = environment;
|
||||
@@ -221,7 +223,7 @@ public class AsyncProviderClient {
|
||||
String media=mediaUrl(input);
|
||||
if(media!=null) {
|
||||
try {
|
||||
MediaBytes file=downloadMedia(media);
|
||||
MediaBytes file=mediaDownloader.download(media);
|
||||
parts.add(Map.of("inlineData",Map.of("mimeType",file.contentType(),"data",Base64.getEncoder().encodeToString(file.bytes()))));
|
||||
} catch(ApiException e) { throw e; }
|
||||
catch(Exception e) { throw new ApiException("MEDIA_FETCH_FAILED","无法读取参考图",400); }
|
||||
@@ -351,7 +353,7 @@ public class AsyncProviderClient {
|
||||
multipartField(out, boundary, entry.getKey(), String.valueOf(entry.getValue()));
|
||||
}
|
||||
if (fileField != null && media != null) {
|
||||
MediaBytes file = downloadMedia(media);
|
||||
MediaBytes file = mediaDownloader.download(media);
|
||||
multipartFile(out, boundary, fileField, file.filename(), file.contentType(), file.bytes());
|
||||
}
|
||||
out.write(("--" + boundary + "--\r\n").getBytes(java.nio.charset.StandardCharsets.UTF_8));
|
||||
@@ -400,52 +402,6 @@ public class AsyncProviderClient {
|
||||
out.write("\r\n".getBytes(java.nio.charset.StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private MediaBytes downloadMedia(String value) throws IOException, InterruptedException {
|
||||
URI uri = URI.create(value);
|
||||
if (!"https".equalsIgnoreCase(uri.getScheme()) || uri.getHost() == null || uri.getUserInfo() != null) {
|
||||
throw new ApiException("INVALID_MEDIA_URL", "媒体地址必须是可公开访问的 HTTPS URL", 400);
|
||||
}
|
||||
for (InetAddress address : InetAddress.getAllByName(uri.getHost())) {
|
||||
if (address.isAnyLocalAddress() || address.isLoopbackAddress() || address.isLinkLocalAddress()
|
||||
|| address.isSiteLocalAddress() || address.isMulticastAddress()) {
|
||||
throw new ApiException("INVALID_MEDIA_URL", "媒体地址不能指向内网或本机地址", 400);
|
||||
}
|
||||
}
|
||||
HttpRequest request = HttpRequest.newBuilder(uri).timeout(Duration.ofSeconds(15)).GET().build();
|
||||
BoundedHttpTransport.Response response;
|
||||
try { response = transport.send(request, 8 * 1024 * 1024); }
|
||||
catch (BoundedHttpTransport.Failure failure) {
|
||||
if (failure.reason() == BoundedHttpTransport.Reason.BODY_TOO_LARGE) throw new ApiException("MEDIA_TOO_LARGE", "媒体输入超过 8 MiB", 400);
|
||||
throw new ApiException("MEDIA_FETCH_FAILED", "无法在期限内读取媒体输入", 400);
|
||||
}
|
||||
if (response.statusCode() < 200 || response.statusCode() >= 300) {
|
||||
throw new ApiException("MEDIA_FETCH_FAILED", "无法读取媒体输入(HTTP " + response.statusCode() + ")", 400);
|
||||
}
|
||||
byte[] bytes = response.body();
|
||||
if (bytes.length == 0 || bytes.length > 8 * 1024 * 1024) {
|
||||
throw new ApiException("MEDIA_TOO_LARGE", "媒体输入为空或超过 8 MiB", 400);
|
||||
}
|
||||
String contentType = response.headers().firstValue("Content-Type").orElse("application/octet-stream")
|
||||
.split(";", 2)[0].trim().toLowerCase(java.util.Locale.ROOT);
|
||||
if (!contentType.startsWith("image/")) {
|
||||
throw new ApiException("MEDIA_TYPE_UNSUPPORTED", "图像编辑和视频参考图必须是 image/* 文件", 400);
|
||||
}
|
||||
String filename = "input" + extension(contentType);
|
||||
return new MediaBytes(bytes, contentType, filename);
|
||||
}
|
||||
|
||||
private String extension(String contentType) {
|
||||
return switch (contentType) {
|
||||
case "image/jpeg" -> ".jpg";
|
||||
case "image/png" -> ".png";
|
||||
case "image/webp" -> ".webp";
|
||||
case "image/gif" -> ".gif";
|
||||
default -> ".bin";
|
||||
};
|
||||
}
|
||||
|
||||
private record MediaBytes(byte[] bytes, String contentType, String filename) {}
|
||||
|
||||
private Map<String,Object> request(String method, String url, String key, Map<String,Object> body, String message) {
|
||||
boolean sending = false;
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user