接入租赁平台

This commit is contained in:
lu7213 committed 2026-11-05 09:20:29 -05:00
1 parent c035263bc6
commit 1c2d15b2a9
51 files changed
+3631 -15

No files matched your search

@@ -0,0 +1,107 @@
package com.ruoyi.executor;
import com.ruoyi.system.chain.ChainCommandExecutor;
import com.ruoyi.system.demo.WalletEnvelopeEncryptionDemo;
import com.ruoyi.system.demo.WalletEnvelopeEncryptionDemo.EncryptedEnvelope;
import org.junit.Test;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.*;
/**
* 独立子进程启动真实 Spring 配置、MyBatis Mapper 和解密服务。
* 使用独立 H2 与一次性测试信封;通过真实环境变量注入密码,不读本机 .env,不访问外部网络。
*/
public class ExecutorBootstrapTest {
private static final String JDBC = "jdbc:h2:mem:executorSmoke;MODE=MySQL;DATABASE_TO_UPPER=false;DB_CLOSE_DELAY=-1";
private static final String PASSWORD = "test-only-bootstrap-password";
private static final String TOKEN = "test-only-bootstrap-token-01234567890123456789";
@Test
public void isolatedApplicationStartsWithoutPaymentControllersOrSchedulers() throws Exception {
String java = System.getProperty("java.home") + "/bin/java";
String classpath = System.getProperty("surefire.test.class.path", System.getProperty("java.class.path"));
ProcessBuilder builder = new ProcessBuilder(java, "-cp", classpath, Probe.class.getName()).redirectErrorStream(true);
Map<String, String> env = builder.environment();
// 覆盖全部测试所需变量,避免继承真实执行器配置;跨机 TLS 配置也必须移除。
env.keySet().removeIf(name -> name.startsWith("CHAIN_EXECUTOR_"));
env.put("CHAIN_EXECUTOR_BIND_ADDRESS", "127.0.0.1");
env.put("CHAIN_EXECUTOR_PORT", "0");
env.put("CHAIN_EXECUTOR_ALLOWED_IPS", "127.0.0.1");
env.put("CHAIN_EXECUTOR_ALLOWED_NETWORK", "NILE");
env.put("CHAIN_EXECUTOR_BROADCAST_ENABLED", "true");
env.put("CHAIN_EXECUTOR_CONFIGURATION_PASSWORD", PASSWORD);
env.put("CHAIN_EXECUTOR_TOKEN", TOKEN);
env.put("CHAIN_EXECUTOR_DB_URL", JDBC);
env.put("CHAIN_EXECUTOR_DB_USER", "sa");
env.put("CHAIN_EXECUTOR_DB_PASSWORD", "");
Process process = builder.start();
ExecutorService reader = Executors.newSingleThreadExecutor();
try {
Future<String> captured = reader.submit(() -> {
StringBuilder text = new StringBuilder();
try (BufferedReader lines = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) {
String line;
while ((line = lines.readLine()) != null) text.append(line).append('\n');
}
return text.toString();
});
assertTrue("独立服务启动超时", process.waitFor(60, TimeUnit.SECONDS));
String output = captured.get(5, TimeUnit.SECONDS);
assertEquals(output, 0, process.exitValue());
assertTrue(output, output.contains("EXECUTOR_BOOTSTRAP_OK"));
assertFalse("密码不得进入启动输出", output.contains(PASSWORD));
} finally {
process.destroyForcibly();
reader.shutdownNow();
}
}
public static class Probe {
public static void main(String[] args) throws Exception {
JdbcTemplate jdbc = new JdbcTemplate(new DriverManagerDataSource(JDBC, "sa", ""));
jdbc.execute("create table usdt_chain_execution (execution_id varchar(36), status varchar(16), execution_mode varchar(16), created_at timestamp)");
jdbc.execute("create table usdt_config (id bigint, pay_usdt_num decimal, receive_usdt_num decimal, tron_net_type int, "
+ "version bigint, encryption_algorithm varchar, password_kdf varchar, iterations bigint, encrypted_mnemonic varchar, "
+ "mnemonic_nonce varchar, encrypted_dek varchar, dek_nonce varchar, key_salt varchar, password_salt varchar, "
+ "password_hash varchar, rotated_at timestamp, trx_num decimal, token_address varchar, usdt_balance decimal, trx_balance decimal)");
EncryptedEnvelope envelope = WalletEnvelopeEncryptionDemo.create(PASSWORD.toCharArray(), 1).envelope;
jdbc.update("insert into usdt_config (id,tron_net_type,version,encryption_algorithm,password_kdf,iterations,encrypted_mnemonic,"
+ "mnemonic_nonce,encrypted_dek,dek_nonce,key_salt,password_salt,password_hash) values (1,0,?,?,?,?,?,?,?,?,?,?,?)",
envelope.version, envelope.encryptionAlgorithm, envelope.passwordKdf, envelope.iterations,
envelope.encryptedMnemonic, envelope.mnemonicNonce, envelope.encryptedDek, envelope.dekNonce,
envelope.keySalt, envelope.passwordSalt, envelope.passwordHash);
try (ConfigurableApplicationContext context = new SpringApplicationBuilder(ChainExecutorApplication.class)
.web(WebApplicationType.NONE).properties("spring.config.name=executor")
.run("--spring.datasource.driver-class-name=org.h2.Driver", "--logging.level.root=OFF")) {
assertNotNull(context.getBean(ChainCommandExecutor.class));
assertEquals(0, context.getBeanNamesForType(com.ruoyi.system.controller.WalletAddressController.class).length);
assertEquals(0, context.getBeanNamesForType(com.ruoyi.system.chain.ChainExecutionReconciler.class).length);
assertEquals(0, context.getBeanNamesForType(com.ruoyi.system.service.PayoutProcessingService.class).length);
IntranetExecutionServer server = context.getBean(IntranetExecutionServer.class);
HttpURLConnection connection = (HttpURLConnection) new URL("http://127.0.0.1:" + server.listeningPort()
+ "/internal/v1/readiness").openConnection();
connection.setRequestProperty("Authorization", "Bearer " + TOKEN);
connection.setReadTimeout(2000);
try { assertEquals(200, connection.getResponseCode()); }
finally { connection.disconnect(); }
System.out.println("EXECUTOR_BOOTSTRAP_OK");
}
}
}
}
@@ -0,0 +1,103 @@
package com.ruoyi.executor;
import com.ruoyi.system.chain.ChainExecutionStore;
import com.ruoyi.system.chain.IntranetAddress;
import com.ruoyi.system.domain.UsdtConfig;
import com.ruoyi.system.mapper.UsdtConfigMapper;
import com.ruoyi.system.service.WalletSigningService;
import org.junit.After;
import org.junit.Test;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import java.lang.reflect.Proxy;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.*;
/** 真实回环 HTTP 集成测试;用可控解锁替身,绝不访问 TRON 或使用真实钱包密码。 */
public class IntranetExecutionServerTest {
private static final String TOKEN = "test-only-service-token-01234567890123456789";
private IntranetExecutionServer server;
private boolean passwordAccepted = true;
private final Map<String, String> environment = new HashMap<>();
@After
public void cleanup() { if (server != null) server.close(); }
@Test
public void allEndpointsRequireServiceAuthentication() throws Exception {
start("127.0.0.1");
assertEquals(403, request("GET", "/internal/v1/readiness", null, null));
assertEquals(403, request("POST", "/internal/v1/executions", "wrong", null));
assertEquals(403, request("GET", "/anything", null, null));
assertEquals(200, request("GET", "/internal/v1/readiness", TOKEN, null));
assertEquals(202, request("POST", "/internal/v1/executions", TOKEN, null));
assertEquals(404, request("GET", "/anything", TOKEN, null));
}
@Test
public void forwardedHeaderCannotBypassActualPeerWhitelist() throws Exception {
start("127.0.0.2");
assertEquals(403, request("GET", "/internal/v1/readiness", TOKEN, "127.0.0.2"));
assertEquals(403, request("POST", "/internal/v1/executions", TOKEN, "127.0.0.2"));
}
@Test
public void passwordRotationImmediatelyMakesServiceNotReady() throws Exception {
start("127.0.0.1");
passwordAccepted = false;
assertEquals(503, request("GET", "/internal/v1/readiness", TOKEN, null));
assertEquals(503, request("POST", "/internal/v1/executions", TOKEN, null));
}
@Test
public void rejectsPublicWildcardDnsAndAmbiguousAddresses() {
for (String address : new String[] {"0.0.0.0", "8.8.8.8", "localhost", "169.254.1.1", "172.32.0.1",
"192.168.001.2", "::", "::1", "10.0.0.256"}) {
try { IntranetAddress.parse(address); fail(address); }
catch (IllegalArgumentException expected) { /* fail closed */ }
}
for (String address : new String[] {"10.0.0.1", "172.16.0.1", "172.31.255.254", "192.168.0.1", "127.0.0.1"}) {
assertNotNull(IntranetAddress.parse(address));
}
}
private void start(String allowed) throws Exception {
environment.put("CHAIN_EXECUTOR_BIND_ADDRESS", "127.0.0.1");
environment.put("CHAIN_EXECUTOR_PORT", "0");
environment.put("CHAIN_EXECUTOR_ALLOWED_IPS", allowed);
environment.put("CHAIN_EXECUTOR_TOKEN", TOKEN);
environment.put("CHAIN_EXECUTOR_CONFIGURATION_PASSWORD", "test-only-password");
environment.put("CHAIN_EXECUTOR_ALLOWED_NETWORK", "NILE");
environment.put("CHAIN_EXECUTOR_BROADCAST_ENABLED", "true");
ChainExecutionStore store = new ChainExecutionStore(new DriverManagerDataSource(
"jdbc:h2:mem:server" + System.nanoTime() + ";DB_CLOSE_DELAY=-1", "sa", ""));
store.jdbc().execute("create table usdt_chain_execution(id int)");
UsdtConfig config = new UsdtConfig();
config.setTronNetType(0);
UsdtConfigMapper configs = (UsdtConfigMapper) Proxy.newProxyInstance(getClass().getClassLoader(),
new Class<?>[] {UsdtConfigMapper.class}, (proxy, method, arguments) -> config);
WalletSigningService signing = new WalletSigningService() {
@Override public void validateConfigurationPassword(String password) {
if (!passwordAccepted) throw new IllegalStateException("test rejection");
}
};
server = new IntranetExecutionServer(store, null, signing, configs, environment);
server.start();
}
private int request(String method, String path, String token, String forwarded) throws Exception {
HttpURLConnection connection = (HttpURLConnection) new URL("http://127.0.0.1:" + server.listeningPort() + path).openConnection();
connection.setConnectTimeout(2000);
connection.setReadTimeout(2000);
connection.setRequestMethod(method);
if (token != null) connection.setRequestProperty("Authorization", "Bearer " + token);
if (forwarded != null) connection.setRequestProperty("X-Forwarded-For", forwarded);
if ("POST".equals(method)) { connection.setDoOutput(true); connection.getOutputStream().close(); }
try { return connection.getResponseCode(); }
finally { connection.disconnect(); }
}
}