Initial import of WebUp Cloud
Verify control plane / verify (push) Canceled after 0s

This commit is contained in:
WebUp Cloud committed 2026-11-07 13:31:14 +08:00
commit db82325ed7
216 files changed
+33718

No files matched your search

@@ -0,0 +1,223 @@
package run.webup.platform;
import com.fasterxml.jackson.databind.*;
import java.net.*;
import java.net.http.*;
import java.time.Duration;
import java.util.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class SandboxClient {
private final String base, token;
private final ObjectMapper json;
private final HttpClient client =
HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(5))
.followRedirects(HttpClient.Redirect.NEVER)
.build();
public SandboxClient(
@Value("${app.sandbox-url:}") String base,
@Value("${app.sandbox-token:}") String token,
ObjectMapper json) {
this.base = base;
this.token = token;
this.json = json;
}
public void check() {
if (base.isBlank() || token.length() < 32)
throw new ApiError.Failure(503, "隔离执行环境尚未配置:需要 SANDBOX_URL 与 SANDBOX_TOKEN");
URI uri = URI.create(base);
if (!Set.of("http", "https").contains(uri.getScheme())
|| uri.getHost() == null
|| uri.getUserInfo() != null
|| uri.getRawQuery() != null
|| uri.getRawFragment() != null) throw new ApiError.Failure(503, "SANDBOX_URL 配置无效");
}
public boolean configured() {
try {
check();
return true;
} catch (Exception e) {
return false;
}
}
public void probe() {
check();
try {
var request =
HttpRequest.newBuilder(URI.create(base.replaceAll("/+$", "") + "/health/ready"))
.timeout(Duration.ofSeconds(8))
.header("Authorization", "Bearer " + token)
.GET()
.build();
var response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
try (var stream = response.body()) {
byte[] bytes = stream.readNBytes(16385);
if (response.statusCode() != 200
|| bytes.length > 16384
|| !json.readTree(bytes).path("ready").asBoolean(false))
throw new ApiError.Failure(503, "隔离网关未通过健康检查");
}
} catch (ApiError.Failure f) {
throw f;
} catch (Exception e) {
if (e instanceof InterruptedException) Thread.currentThread().interrupt();
throw new ApiError.Failure(503, "无法连接隔离网关,请核对连接、凭证和集群状态");
}
}
public String execute(UUID project, ModelGateway.Call call) {
return executeBounded(project, call, 131072);
}
public JsonNode inspect(UUID project) {
try {
var result =
json.readTree(
execute(
project,
new ModelGateway.Call("inspect", "workspace_inspect", json.createObjectNode())));
if (!result.path("sourceFiles").isIntegralNumber() || !result.has("complete"))
throw new ApiError.Failure(502, "工作区源码检查未返回有效结果");
return result;
} catch (ApiError.Failure failure) {
throw failure;
} catch (Exception failure) {
throw new ApiError.Failure(502, "工作区源码检查失败");
}
}
public byte[] source(UUID project) {
try {
String result =
executeBounded(
project,
new ModelGateway.Call("source", "archive_source", json.createObjectNode()),
16000000);
JsonNode parsed = json.readTree(result);
if (!parsed.has("base64"))
throw new ApiError.Failure(422, parsed.path("error").asText("源码打包失败"));
return Base64.getDecoder().decode(parsed.get("base64").asText());
} catch (ApiError.Failure f) {
throw f;
} catch (Exception e) {
throw new ApiError.Failure(502, "源码归档格式无效");
}
}
public JsonNode reviewSnapshot(UUID project) {
return parsed(
executeBounded(
project,
new ModelGateway.Call("snapshot", "review_export", json.createObjectNode()),
16000000));
}
public JsonNode importReview(UUID workspace, JsonNode snapshot) {
var arguments = json.createObjectNode();
for (String key : List.of("archive", "archiveSha256", "sourceHash"))
arguments.set(key, snapshot.path(key));
return parsed(
executeBounded(
workspace, new ModelGateway.Call("import", "review_import", arguments), 131072, true));
}
public JsonNode validateReview(UUID workspace) {
return parsed(
executeBounded(
workspace,
new ModelGateway.Call("validate", "review_validate", json.createObjectNode()),
131072,
true));
}
private JsonNode parsed(String value) {
try {
var result = json.readTree(value);
if (result.has("error"))
throw new ApiError.Failure(502, "验收工作区返回:" + result.path("error").asText());
return result;
} catch (ApiError.Failure error) {
throw error;
} catch (Exception error) {
throw new ApiError.Failure(502, "验收工作区响应无效");
}
}
public void cleanupReview(UUID workspace) {
check();
try {
var request =
HttpRequest.newBuilder(URI.create(base.replaceAll("/+$", "") + "/reviews/" + workspace))
.timeout(Duration.ofSeconds(20))
.header("Authorization", "Bearer " + token)
.DELETE()
.build();
var response = client.send(request, HttpResponse.BodyHandlers.discarding());
if (response.statusCode() != 200) throw new ApiError.Failure(502, "验收工作区清理请求失败");
} catch (ApiError.Failure error) {
throw error;
} catch (Exception error) {
if (error instanceof InterruptedException) Thread.currentThread().interrupt();
throw new ApiError.Failure(502, "验收工作区清理结果未确认");
}
}
private String executeBounded(UUID project, ModelGateway.Call call, int limit) {
return executeBounded(project, call, limit, false);
}
private String executeBounded(UUID project, ModelGateway.Call call, int limit, boolean review) {
check();
var allowed =
review
? Set.of("review_import", "review_validate")
: Set.of(
"read_file",
"write_file",
"list_files",
"delete_file",
"run_command",
"version_save",
"version_list",
"archive_source",
"workspace_inspect",
"review_export");
if (!allowed.contains(call.name())) throw new ApiError.Failure(400, "不允许的工具");
try {
String payload =
json.writeValueAsString(Map.of("name", call.name(), "arguments", call.arguments()));
var request =
HttpRequest.newBuilder(
URI.create(
base.replaceAll("/+$", "")
+ (review ? "/reviews/" : "/workspaces/")
+ project
+ "/tools"))
.timeout(Duration.ofSeconds(150))
.header("Authorization", "Bearer " + token)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(payload))
.build();
var response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
try (var input = response.body()) {
byte[] bytes = input.readNBytes(limit + 1);
if (bytes.length > limit) throw new ApiError.Failure(502, "工具输出过大");
if (response.statusCode() != 200)
throw new ApiError.Failure(502, "隔离工具返回 HTTP " + response.statusCode());
return json.readTree(bytes).toString();
}
} catch (ApiError.Failure f) {
throw f;
} catch (Exception e) {
if (e instanceof InterruptedException) Thread.currentThread().interrupt();
throw new ApiError.Failure(502, "隔离工具调用失败,执行结果可能不确定,请核对后处理");
}
}
}