feat:多机证书导入功能
This commit is contained in:
parent
db80b1dd83
commit
55a9cfbdbe
17
pom.xml
17
pom.xml
@ -82,6 +82,19 @@
|
||||
<!-- <scope>provided</scope>-->
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.snmp4j</groupId>
|
||||
<artifactId>snmp4j</artifactId>
|
||||
<version>2.8.18</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.13.1</version>
|
||||
</dependency>
|
||||
|
||||
<!--<dependency>-->
|
||||
<!--<groupId>club.fullstack</groupId>-->
|
||||
<!--<artifactId>serialize</artifactId>-->
|
||||
@ -124,8 +137,8 @@
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<source>7</source>
|
||||
<target>7</target>
|
||||
<source>8</source>
|
||||
<target>8</target>
|
||||
<compilerId>groovy-eclipse-compiler</compilerId>
|
||||
<verbose>true</verbose>
|
||||
<fork>true</fork>
|
||||
|
||||
@ -8,6 +8,8 @@ import com.sunyard.inf.customize.SydQNUCApi;
|
||||
import com.sunyard.inf.customize.SydUnionPayApi;
|
||||
import racal.sunyard.main.SydApiBuilder;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Cheney on 2017/11/11.
|
||||
* 程序调用接口
|
||||
@ -117,4 +119,6 @@ public interface SydApi extends
|
||||
public Object getBindObject();
|
||||
|
||||
public void setBindObject(Object bindObject);
|
||||
|
||||
void importCertToServers(List<String> addressList, int timeout, String cert);
|
||||
}
|
||||
|
||||
86
src/main/java/com/sunyard/entity/ServerConfig.java
Normal file
86
src/main/java/com/sunyard/entity/ServerConfig.java
Normal file
@ -0,0 +1,86 @@
|
||||
package com.sunyard.entity;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ServerConfig {
|
||||
|
||||
/**
|
||||
* 主机节点信息
|
||||
*/
|
||||
private List<ServerAddress> masterNodeList;
|
||||
|
||||
/**
|
||||
* 备机节点信息
|
||||
*/
|
||||
private List<ServerAddress> slaveNodeList;
|
||||
|
||||
|
||||
/**
|
||||
* 连接超时时间
|
||||
*/
|
||||
private Integer timeout;
|
||||
|
||||
public ServerConfig(List<ServerAddress> masterNodeList, List<ServerAddress> slaveNodeList, Integer timeout) {
|
||||
this.masterNodeList = masterNodeList;
|
||||
this.slaveNodeList = slaveNodeList;
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
public List<ServerAddress> getMasterNodeList() {
|
||||
return masterNodeList;
|
||||
}
|
||||
|
||||
public void setMasterNodeList(List<ServerAddress> masterNodeList) {
|
||||
this.masterNodeList = masterNodeList;
|
||||
}
|
||||
|
||||
public Integer getTimeout() {
|
||||
return timeout;
|
||||
}
|
||||
|
||||
public void setTimeout(Integer timeout) {
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
public List<ServerAddress> getSlaveNodeList() {
|
||||
return slaveNodeList;
|
||||
}
|
||||
|
||||
public void setSlaveNodeList(List<ServerAddress> slaveNodeList) {
|
||||
this.slaveNodeList = slaveNodeList;
|
||||
}
|
||||
|
||||
|
||||
public static class ServerAddress {
|
||||
|
||||
|
||||
private String ip;
|
||||
|
||||
private Integer port;
|
||||
|
||||
public ServerAddress(String ip, Integer port) {
|
||||
this.ip = ip;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
|
||||
public String getIp() {
|
||||
return ip;
|
||||
}
|
||||
|
||||
public void setIp(String ip) {
|
||||
this.ip = ip;
|
||||
}
|
||||
|
||||
public Integer getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(Integer port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
134
src/main/java/com/sunyard/snmp/SnmpTrapSender.java
Normal file
134
src/main/java/com/sunyard/snmp/SnmpTrapSender.java
Normal file
@ -0,0 +1,134 @@
|
||||
package com.sunyard.snmp;
|
||||
|
||||
import org.snmp4j.*;
|
||||
import org.snmp4j.mp.*;
|
||||
import org.snmp4j.security.*;
|
||||
import org.snmp4j.smi.*;
|
||||
import org.snmp4j.transport.DefaultUdpTransportMapping;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Vector;
|
||||
|
||||
public class SnmpTrapSender {
|
||||
private static volatile SnmpTrapSender instance;
|
||||
private Snmp snmp;
|
||||
private Properties config;
|
||||
|
||||
// 配置参数
|
||||
private String ip;
|
||||
private int port;
|
||||
private String enterpriseOid;
|
||||
private String community;
|
||||
private int version;
|
||||
|
||||
|
||||
private SnmpTrapSender() throws IOException {
|
||||
// loadConfig();
|
||||
ip = "172.16.18.113";
|
||||
port = 162;
|
||||
enterpriseOid = "1.3.6.1.4.1.2021.251.1";
|
||||
community = "public";
|
||||
version = 1;
|
||||
initializeSNMP();
|
||||
}
|
||||
|
||||
public static SnmpTrapSender getInstance() throws IOException {
|
||||
if (instance == null) {
|
||||
synchronized (SnmpTrapSender.class) {
|
||||
if (instance == null) {
|
||||
instance = new SnmpTrapSender();
|
||||
}
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
private void loadConfig() {
|
||||
try (InputStream input = getClass().getClassLoader().getResourceAsStream("test.properties")) {
|
||||
config = new Properties();
|
||||
config.load(input);
|
||||
|
||||
ip = config.getProperty("snmp.target.ip");
|
||||
port = Integer.parseInt(config.getProperty("snmp.target.port"));
|
||||
enterpriseOid = config.getProperty("enterprise.oid");
|
||||
community = config.getProperty("snmp.community");
|
||||
version = Integer.parseInt(config.getProperty("snmp.version"));
|
||||
|
||||
} catch (IOException | NumberFormatException e) {
|
||||
throw new RuntimeException("加载配置文件失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void loadConfig(Map<String, String> map) {
|
||||
ip = map.getOrDefault("snmp.target.ip", null);
|
||||
// port = Integer.parseInt(map.getOrDefault("snmp.target.port", "0"));
|
||||
enterpriseOid = map.getOrDefault("enterprise.oid", null);
|
||||
community = map.getOrDefault("snmp.community", null);
|
||||
// version = Integer.parseInt(map.getOrDefault("snmp.version", "1"));
|
||||
}
|
||||
|
||||
private void initializeSNMP() throws IOException {
|
||||
TransportMapping<?> transport = new DefaultUdpTransportMapping();
|
||||
snmp = new Snmp(transport);
|
||||
snmp.listen();
|
||||
}
|
||||
|
||||
public void sendTrap(String message, int severity) throws IOException {
|
||||
switch (version) {
|
||||
case SnmpConstants.version1:
|
||||
sendV1Trap(message, severity);
|
||||
break;
|
||||
case SnmpConstants.version2c:
|
||||
sendV2Trap(message, severity);
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("不支持的SNMP版本");
|
||||
}
|
||||
}
|
||||
|
||||
private void sendV1Trap(String message, int severity) throws IOException {
|
||||
CommunityTarget target = createCommunityTarget(SnmpConstants.version1);
|
||||
PDUv1 pdu = new PDUv1();
|
||||
configureCommonTrap(pdu, message, severity);
|
||||
pdu.setGenericTrap(PDUv1.ENTERPRISE_SPECIFIC);
|
||||
snmp.send(pdu, target);
|
||||
}
|
||||
|
||||
private void sendV2Trap(String message, int severity) throws IOException {
|
||||
CommunityTarget target = createCommunityTarget(SnmpConstants.version2c);
|
||||
PDU pdu = new PDU();
|
||||
configureCommonTrap(pdu, message, severity);
|
||||
pdu.setType(PDU.TRAP);
|
||||
snmp.send(pdu, target);
|
||||
}
|
||||
|
||||
|
||||
private void configureCommonTrap(PDU pdu, String message, int severity) {
|
||||
pdu.add(new VariableBinding(SnmpConstants.sysUpTime,
|
||||
new TimeTicks(System.currentTimeMillis()/1000)));
|
||||
pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID,
|
||||
new OID(enterpriseOid + ".0.1")));
|
||||
pdu.add(new VariableBinding(new OID(enterpriseOid + ".1.1"),
|
||||
new OctetString(message)));
|
||||
pdu.add(new VariableBinding(new OID(enterpriseOid + ".1.2"),
|
||||
new Integer32(severity)));
|
||||
}
|
||||
|
||||
private CommunityTarget createCommunityTarget(int version) {
|
||||
CommunityTarget target = new CommunityTarget();
|
||||
target.setCommunity(new OctetString(community));
|
||||
target.setAddress(GenericAddress.parse("udp:"+ip+"/"+port));
|
||||
target.setVersion(version);
|
||||
target.setTimeout(5000);
|
||||
target.setRetries(3);
|
||||
return target;
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
if (snmp != null) {
|
||||
snmp.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
141
src/main/java/com/sunyard/task/AsyncTaskExecutor.java
Normal file
141
src/main/java/com/sunyard/task/AsyncTaskExecutor.java
Normal file
@ -0,0 +1,141 @@
|
||||
package com.sunyard.task;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class AsyncTaskExecutor {
|
||||
|
||||
private final ExecutorService taskExecutor;
|
||||
private final ScheduledExecutorService retryScheduler;
|
||||
|
||||
public AsyncTaskExecutor(int taskThreads, int retryThreads) {
|
||||
this.taskExecutor = Executors.newFixedThreadPool(taskThreads);
|
||||
this.retryScheduler = Executors.newScheduledThreadPool(retryThreads);
|
||||
}
|
||||
|
||||
// 默认配置构造函数
|
||||
public AsyncTaskExecutor() {
|
||||
this(4, 2);
|
||||
}
|
||||
|
||||
public List<TaskOutcome<String>> execTasks(List<TaskContext<String>> taskContexts) {
|
||||
List<CompletableFuture<String>> futures =
|
||||
taskContexts.stream().map(this::retryWithDelay).collect(Collectors.toList());
|
||||
List<TaskOutcome<String>> taskOutcomes = new ArrayList<>();
|
||||
taskContexts.forEach(task ->
|
||||
retryWithDelay(task).whenCompleteAsync((result, ex) -> {
|
||||
if (ex != null) {
|
||||
// 提取原始异常
|
||||
Throwable rootCause = ex instanceof CompletionException ? ex.getCause() : ex;
|
||||
taskOutcomes.add(new TaskOutcome<>(task, rootCause));
|
||||
} else {
|
||||
taskOutcomes.add(new TaskOutcome<>(task, result));
|
||||
}
|
||||
}));
|
||||
return taskOutcomes;
|
||||
}
|
||||
|
||||
public List<TaskOutcome<String>> execBatchTasks(List<TaskContext<String>> taskContexts) {
|
||||
// 转换每个Future为明确的TaskOutcome类型
|
||||
List<CompletableFuture<TaskOutcome<String>>> futures = taskContexts.stream()
|
||||
.map(task ->
|
||||
retryWithDelay(task)
|
||||
.<TaskOutcome<String>>handle((result, ex) -> {
|
||||
if (ex != null) {
|
||||
// 提取原始异常
|
||||
Throwable cause = ex instanceof CompletionException ? ex.getCause() : ex;
|
||||
return new TaskOutcome<>(task, cause);
|
||||
} else {
|
||||
return new TaskOutcome<>(task, result);
|
||||
}
|
||||
})
|
||||
)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 等待所有任务完成
|
||||
CompletableFuture<Void> allFutures = CompletableFuture.allOf(
|
||||
futures.toArray(new CompletableFuture[0])
|
||||
);
|
||||
|
||||
// 收集处理结果
|
||||
return allFutures.thenApply(v ->
|
||||
futures.stream()
|
||||
.map(CompletableFuture::join)
|
||||
.collect(Collectors.toList())
|
||||
).join();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 带延迟的重试逻辑
|
||||
*/
|
||||
private <T> CompletableFuture<T> retryWithDelay(TaskContext<T> context) {
|
||||
return CompletableFuture.supplyAsync(context.getTask(), taskExecutor)
|
||||
.handleAsync((result, ex) -> {
|
||||
if (ex == null) {
|
||||
return CompletableFuture.completedFuture(result);
|
||||
} else {
|
||||
return handleRetry(context, ex);
|
||||
}
|
||||
}, taskExecutor)
|
||||
.thenCompose(f -> f);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 处理重试逻辑
|
||||
*/
|
||||
private <T> CompletableFuture<T> handleRetry(TaskContext<T> context, Throwable ex) {
|
||||
if (context.canRetry()) {
|
||||
context.decreaseRetry();
|
||||
System.out.printf("[重试调度] ID: %s | 延迟: %ss | 剩余重试: %d | 错误: %s%n",
|
||||
context.getTaskId(),
|
||||
context.getRetryDelay().getSeconds(),
|
||||
context.getRetriesLeft(),
|
||||
ex.getCause().getMessage());
|
||||
|
||||
// 创建延迟重试的Future
|
||||
CompletableFuture<T> delayedRetry = new CompletableFuture<>();
|
||||
retryScheduler.schedule(() -> {
|
||||
retryWithDelay(context).whenComplete((retryResult, retryEx) -> {
|
||||
if (retryEx != null) {
|
||||
delayedRetry.completeExceptionally(retryEx);
|
||||
} else {
|
||||
delayedRetry.complete(retryResult);
|
||||
}
|
||||
});
|
||||
}, context.getRetryDelay().toMillis(), TimeUnit.MILLISECONDS);
|
||||
|
||||
return delayedRetry;
|
||||
} else {
|
||||
// 重试耗尽,传递原始异常
|
||||
CompletableFuture<T> failed = new CompletableFuture<>();
|
||||
failed.completeExceptionally(ex);
|
||||
return failed;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭线程池
|
||||
*/
|
||||
public void shutdown() {
|
||||
taskExecutor.shutdown();
|
||||
retryScheduler.shutdown();
|
||||
try {
|
||||
if (!taskExecutor.awaitTermination(1, TimeUnit.SECONDS)) {
|
||||
taskExecutor.shutdownNow();
|
||||
}
|
||||
if (!retryScheduler.awaitTermination(1, TimeUnit.SECONDS)) {
|
||||
retryScheduler.shutdownNow();
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
50
src/main/java/com/sunyard/task/TaskContext.java
Normal file
50
src/main/java/com/sunyard/task/TaskContext.java
Normal file
@ -0,0 +1,50 @@
|
||||
package com.sunyard.task;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class TaskContext<T> {
|
||||
private final String taskId; // 唯一任务ID
|
||||
private final Supplier<T> task; // 原始任务逻辑
|
||||
private int retriesLeft; // 剩余重试次数
|
||||
private final Duration retryDelay; // 重试延迟
|
||||
|
||||
private final String address;
|
||||
|
||||
public TaskContext(Supplier<T> task, int maxRetries, Duration retryDelay, String address) {
|
||||
this.taskId = UUID.randomUUID().toString();
|
||||
this.task = task;
|
||||
this.retriesLeft = maxRetries;
|
||||
this.retryDelay = retryDelay;
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public String getTaskId() {
|
||||
return taskId;
|
||||
}
|
||||
|
||||
public Supplier<T> getTask() {
|
||||
return task;
|
||||
}
|
||||
|
||||
public int getRetriesLeft() {
|
||||
return retriesLeft;
|
||||
}
|
||||
|
||||
public boolean canRetry() {
|
||||
return retriesLeft > 0;
|
||||
}
|
||||
|
||||
public void decreaseRetry() {
|
||||
retriesLeft--;
|
||||
}
|
||||
|
||||
public Duration getRetryDelay() {
|
||||
return retryDelay;
|
||||
}
|
||||
}
|
||||
35
src/main/java/com/sunyard/task/TaskOutcome.java
Normal file
35
src/main/java/com/sunyard/task/TaskOutcome.java
Normal file
@ -0,0 +1,35 @@
|
||||
package com.sunyard.task;
|
||||
|
||||
public class TaskOutcome<T> {
|
||||
private final TaskContext<T> context;
|
||||
private final T result;
|
||||
private final Throwable error;
|
||||
|
||||
public TaskOutcome(TaskContext<T> context, T result) {
|
||||
this.context = context;
|
||||
this.result = result;
|
||||
this.error = null;
|
||||
}
|
||||
|
||||
public TaskOutcome(TaskContext<T> context, Throwable error) {
|
||||
this.context = context;
|
||||
this.result = null;
|
||||
this.error = error;
|
||||
}
|
||||
|
||||
public boolean isSuccess() {
|
||||
return error == null;
|
||||
}
|
||||
|
||||
public TaskContext<T> getContext() {
|
||||
return context;
|
||||
}
|
||||
|
||||
public T getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public Throwable getError() {
|
||||
return error;
|
||||
}
|
||||
}
|
||||
@ -1,11 +1,12 @@
|
||||
package racal.sunyard.main;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.sunyard.RetWrap;
|
||||
import com.sunyard.SYMEnDeLongData;
|
||||
import com.sunyard.SydApi;
|
||||
import com.sunyard.SydApiException;
|
||||
import com.sunyard.cert.Cert;
|
||||
import com.sunyard.cert.X509;
|
||||
import com.sunyard.constant.CertUsage;
|
||||
import com.sunyard.entity.Struct;
|
||||
@ -15,11 +16,16 @@ import com.sunyard.proto.Packet;
|
||||
import com.sunyard.proto.PacketSection;
|
||||
import com.sunyard.proto.Util;
|
||||
import com.sunyard.proto.section.SectionValue;
|
||||
import com.sunyard.snmp.SnmpTrapSender;
|
||||
import com.sunyard.task.TaskContext;
|
||||
import com.sunyard.task.AsyncTaskExecutor;
|
||||
import com.sunyard.task.TaskOutcome;
|
||||
import com.sunyard.trans.Alg;
|
||||
import com.sunyard.trans.FullMode;
|
||||
import com.sunyard.trans.PacketSN;
|
||||
import com.sunyard.trans.RoundMode;
|
||||
import com.sunyard.util.*;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.bouncycastle.asn1.*;
|
||||
import org.bouncycastle.asn1.cms.ContentInfo;
|
||||
import org.bouncycastle.cms.SydCmsUtil;
|
||||
@ -32,7 +38,6 @@ import racal.sunyard.main.dto.DTO7A;
|
||||
import racal.sunyard.main.proto.*;
|
||||
|
||||
import javax.security.auth.x500.X500Principal;
|
||||
import java.beans.FeatureDescriptor;
|
||||
import java.io.*;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
@ -46,7 +51,10 @@ import java.security.cert.CertificateException;
|
||||
import java.security.cert.CertificateFactory;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.Duration;
|
||||
import java.util.*;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.sunyard.util.DnUtil.verifyDn;
|
||||
import static org.bouncycastle.asn1.ASN1Encoding.DL;
|
||||
@ -1296,6 +1304,76 @@ public class SydApi4j implements SydApi {
|
||||
return dn;
|
||||
}
|
||||
|
||||
public void initSnmpTrapConfig(String config) {
|
||||
Gson gson = new Gson();
|
||||
Map<String, String> map =
|
||||
gson.fromJson(config, new TypeToken<Map<String, String>>() {}.getType());
|
||||
try {
|
||||
SnmpTrapSender.getInstance().loadConfig(map);
|
||||
} catch (IOException e) {
|
||||
throw new SydApiException("snmp配置错误", -1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void importCertToServers(List<String> addressList, int timeout, String cert) {
|
||||
if (CollectionUtils.isEmpty(addressList)) {
|
||||
throw new RuntimeException("签名服务器地址不能为空");
|
||||
}
|
||||
List<TaskContext<String>> taskContexts =
|
||||
addressList.stream().map(p -> createTaskContext(p, timeout, cert)).collect(Collectors.toList());
|
||||
AsyncTaskExecutor executor = new AsyncTaskExecutor();
|
||||
List<TaskOutcome<String>> taskOutcomes = executor.execBatchTasks(taskContexts);
|
||||
//失败的记录日志
|
||||
List<String> failures = taskOutcomes.stream()
|
||||
.filter(out -> !out.isSuccess()).map(task -> task.getContext().getAddress())
|
||||
.collect(Collectors.toList());
|
||||
if (CollectionUtils.isNotEmpty(failures)) {
|
||||
//异步发送snmp trap消息
|
||||
String result = "[" + failures.stream()
|
||||
.map(Object::toString)
|
||||
.collect(Collectors.joining(",")) + "]";
|
||||
System.out.println(result);
|
||||
String message = "servers:" + result + ",import cert:" + cert + ",failed!";
|
||||
try {
|
||||
SnmpTrapSender.getInstance().sendTrap(message, 1);
|
||||
} catch (Exception e) {
|
||||
System.out.println("消息发送失败!");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private TaskContext<String> createTaskContext(String address, int timeout, String cert) {
|
||||
Supplier<String> task = () -> importCertToServer(address, timeout, cert);
|
||||
//重试2次,间隔1s
|
||||
return new TaskContext<>(task, 2, Duration.ofSeconds(1), address);
|
||||
}
|
||||
|
||||
private String importCertToServer(String address, int timeout, String cert){
|
||||
//address 格式为 ip:port,拆分成ip和port,需要进行格式判断
|
||||
String[] addressParts = address.split(":");
|
||||
String ip = addressParts[0];
|
||||
int port = Integer.parseInt(addressParts[1]);
|
||||
String dn = null;
|
||||
SydApi4j sydApi4j = null;
|
||||
try {
|
||||
//创建sydapi4j连接对象
|
||||
sydApi4j = (SydApi4j) new SydApi4j().connect(ip, port, null, timeout);
|
||||
//导入证书
|
||||
dn = sydApi4j.importCertAndGetDN(cert);
|
||||
} catch (Exception e) {
|
||||
throw e;
|
||||
} finally {
|
||||
if (sydApi4j != null) {
|
||||
sydApi4j.disconnect();
|
||||
}
|
||||
}
|
||||
return dn;
|
||||
}
|
||||
|
||||
public boolean attachedVerifyAll(int rFlag, String sign){
|
||||
try {
|
||||
boolean sm2Cert = CertUtil.isSignedBySM2Cert(sign);
|
||||
|
||||
116
src/test/java/SimpleTest.java
Normal file
116
src/test/java/SimpleTest.java
Normal file
@ -0,0 +1,116 @@
|
||||
import com.sunyard.snmp.SnmpTrapSender;
|
||||
import com.sunyard.task.AsyncTaskExecutor;
|
||||
import com.sunyard.task.TaskContext;
|
||||
import com.sunyard.task.TaskOutcome;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.time.Duration;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class SimpleTest {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
// testt();
|
||||
testSnmp();
|
||||
}
|
||||
|
||||
public static void testt() {
|
||||
|
||||
AsyncTaskExecutor executor = new AsyncTaskExecutor();
|
||||
// 使用示例
|
||||
List<TaskContext<String>> tasks = Arrays.asList(
|
||||
createTaskContext("1", 1000, "cert"),
|
||||
createTaskContext("2", 1000, "cert"),
|
||||
createTaskContext("3", 1000, "cert"),
|
||||
createTaskContext("4", 1000, "cert")
|
||||
);
|
||||
|
||||
List<TaskOutcome<String>> outcomes = executor.execBatchTasks(tasks);
|
||||
executor.shutdown();
|
||||
// 分离成功失败
|
||||
List<String> successes = outcomes.stream()
|
||||
.filter(TaskOutcome::isSuccess)
|
||||
.map(TaskOutcome::getResult)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<TaskOutcome<String>> failures = outcomes.stream()
|
||||
.filter(out -> !out.isSuccess())
|
||||
.collect(Collectors.toList());
|
||||
String result = "[" + failures.stream()
|
||||
.map(p -> p.getContext().getAddress())
|
||||
.collect(Collectors.joining(",")) + "]";
|
||||
String message = "信雅达签名服务器:" + result + ",证书: " + "cert" + ",导入失败";
|
||||
System.out.println(message);
|
||||
System.out.println("成功节点: " + successes);
|
||||
System.out.println("失败详情: ");
|
||||
failures.forEach(f ->
|
||||
System.out.println(f.getContext().getAddress() + ": " + f.getError().getMessage())
|
||||
);
|
||||
}
|
||||
|
||||
private static TaskContext<String> createTaskContext(String address, int timeout, String cert) {
|
||||
Supplier<String> task = () -> importS(address);
|
||||
return new TaskContext<>(task, 2, Duration.ofSeconds(1), address);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void test() throws InterruptedException {
|
||||
List<String> addressList = new ArrayList<>();
|
||||
addressList.add("1");
|
||||
addressList.add("2");
|
||||
addressList.add("3");
|
||||
addressList.add("4");
|
||||
addressList.add("6");
|
||||
CompletableFuture<String>[] futures = new CompletableFuture[addressList.size()];
|
||||
for(int i = 0; i < addressList.size(); i++){
|
||||
int finalI = i;
|
||||
futures[i] = CompletableFuture.supplyAsync(() -> {
|
||||
return importS(addressList.get(finalI));
|
||||
});
|
||||
}
|
||||
// Thread.sleep(2000);
|
||||
//汇总失败的任务
|
||||
List<String> failedTasks = new ArrayList<>();
|
||||
for(int i = 0; i < futures.length; i++){
|
||||
if(futures[i].isCompletedExceptionally()){
|
||||
failedTasks.add(addressList.get(i));
|
||||
}
|
||||
}
|
||||
for (String s : failedTasks) {
|
||||
System.out.println(s);
|
||||
try {
|
||||
String path = "/Users/waner/Work" + "/" + s + ".txt";
|
||||
File file = new File(path);
|
||||
file.createNewFile();
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String importS(String s){
|
||||
// System.out.println("handle:" + s);
|
||||
if (Integer.parseInt(s) % 2 == 0) {
|
||||
System.out.println("handle1:" + s);
|
||||
throw new RuntimeException();
|
||||
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
public static void testSnmp() throws IOException {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("snmp.target.ip", "172.16.18.113");
|
||||
map.put("snmp.target.port", "162");
|
||||
map.put("enterprise.oid", "11");
|
||||
map.put("snmp.community", "public");
|
||||
map.put("snmp.version", "1");
|
||||
// SnmpTrapSender.getInstance().loadConfig(map);
|
||||
SnmpTrapSender.getInstance().sendTrap("serve ip[127.0.0.1], import cert failed", 1);
|
||||
}
|
||||
}
|
||||
54
src/test/java/SnmpTrapSenderV2c.java
Normal file
54
src/test/java/SnmpTrapSenderV2c.java
Normal file
@ -0,0 +1,54 @@
|
||||
import org.snmp4j.CommunityTarget;
|
||||
import org.snmp4j.PDU;
|
||||
import org.snmp4j.Snmp;
|
||||
import org.snmp4j.TransportMapping;
|
||||
import org.snmp4j.mp.SnmpConstants;
|
||||
import org.snmp4j.smi.Address;
|
||||
import org.snmp4j.smi.GenericAddress;
|
||||
import org.snmp4j.smi.OID;
|
||||
import org.snmp4j.smi.OctetString;
|
||||
import org.snmp4j.smi.UdpAddress;
|
||||
import org.snmp4j.smi.VariableBinding;
|
||||
import org.snmp4j.transport.DefaultUdpTransportMapping;
|
||||
|
||||
public class SnmpTrapSenderV2c {
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
// 1. 创建SNMP对象并监听
|
||||
TransportMapping<?> transport = new DefaultUdpTransportMapping();
|
||||
Snmp snmp = new Snmp(transport);
|
||||
transport.listen();
|
||||
|
||||
// 2. 创建目标地址
|
||||
Address targetAddress = new UdpAddress("172.16.18.50/162"); // 接收方的IP和端口
|
||||
CommunityTarget target = new CommunityTarget();
|
||||
target.setCommunity(new OctetString("public")); // 社区字符串
|
||||
target.setAddress(targetAddress);
|
||||
target.setVersion(SnmpConstants.version2c);
|
||||
target.setTimeout(3000);
|
||||
target.setRetries(1);
|
||||
|
||||
// 3. 创建PDU(协议数据单元)
|
||||
PDU pdu = new PDU();
|
||||
|
||||
// 必须的系统信息
|
||||
pdu.add(new VariableBinding(SnmpConstants.sysUpTime, new OctetString("12345")));
|
||||
pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, new OID("1.3.6.1.6.3.1.1.5.3")));
|
||||
|
||||
// 添加自定义变量绑定(可选)
|
||||
pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.1.0"), new OctetString("My Device")));
|
||||
pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0"), new OctetString("Critical Error")));
|
||||
|
||||
pdu.setType(PDU.TRAP);
|
||||
|
||||
// 4. 发送Trap
|
||||
snmp.send(pdu, target);
|
||||
System.out.println("SNMPv2c Trap 发送成功");
|
||||
|
||||
snmp.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,7 +1,9 @@
|
||||
package sample;
|
||||
|
||||
import com.sunyard.SydApiException;
|
||||
import com.sunyard.entity.ServerConfig;
|
||||
import com.sunyard.proto.Util;
|
||||
import com.sunyard.snmp.SnmpTrapSender;
|
||||
import com.sunyard.util.CertUtil;
|
||||
import com.sunyard.util.SYMUtil;
|
||||
import org.junit.After;
|
||||
@ -10,8 +12,10 @@ import org.junit.Test;
|
||||
import racal.sunyard.main.SydApi4j;
|
||||
import racal.sunyard.main.SydApiBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
|
||||
@ -25,21 +29,26 @@ public class Test4NongxinCW {
|
||||
|
||||
byte[] orgData = new byte[100];
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
// 通过设置debug的属性开启debug,如果不设置则默认不打开
|
||||
api = (SydApi4j) new SydApi4j().connect("172.16.17.34", 8889, null, 1000);
|
||||
|
||||
}
|
||||
|
||||
@After
|
||||
public void after() {
|
||||
try {
|
||||
this.api.disconnect();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
// @Before
|
||||
// public void before() {
|
||||
// // 通过设置debug的属性开启debug,如果不设置则默认不打开
|
||||
// api = (SydApi4j) new SydApi4j().connect("172.16.17.34", 8889, null, 1000);
|
||||
//
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void connection() {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// @After
|
||||
// public void after() {
|
||||
// try {
|
||||
// this.api.disconnect();
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
|
||||
@Test
|
||||
public void nakeSign(){
|
||||
@ -163,4 +172,38 @@ public class Test4NongxinCW {
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void importAllCert() {
|
||||
List<String> masterNodes = new ArrayList<>();
|
||||
masterNodes.add("192.168.55.80:3336");
|
||||
masterNodes.add("172.16.128.2:8889");
|
||||
masterNodes.add("172.16.128.3:8889");
|
||||
masterNodes.add("172.16.128.4:8889");
|
||||
int timeout = 1000;
|
||||
String pwd = null;
|
||||
String cert = "";
|
||||
SydApi4j sydApi4j = new SydApi4j();
|
||||
// sydApi4j.initSnmpTrapConfig("");
|
||||
sydApi4j.importCertToServers(masterNodes, timeout, cert);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void snmpTest() {
|
||||
try {
|
||||
SnmpTrapSender sender = SnmpTrapSender.getInstance();
|
||||
|
||||
// 发送不同级别的日志
|
||||
sender.sendTrap("Application started", 0);
|
||||
sender.sendTrap("High CPU usage", 1);
|
||||
sender.sendTrap("Database connection lost", 5);
|
||||
|
||||
sender.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user