Files
usdt/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalSocket.java
T
2026-10-22 19:58:57 -04:00

377 lines
17 KiB
Java

package com.ruoyi.system.rental;
import static com.ruoyi.system.rental.RentalValues.*;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.*;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
/**
* 单入口WSS节点网关。数据库session_epoch为fencing token,旧连接关闭不能覆盖新连接。
* Socket只负责可靠交付和线索采集,不执行RPC、不把ACK或RESULT当履约成功。
*/
@Component
@ConditionalOnProperty(name = "rental.enabled", havingValue = "true")
public class RentalSocket extends TextWebSocketHandler {
private static final String PATH = "/node/v1/socket";
private final RentalStore store;
private final RentalIdentity identity;
private final RentalTasks tasks;
private final ObjectMapper json;
private final Map<String, Connection> connections = new ConcurrentHashMap<>();
private final String instance = id("INSTANCE");
public RentalSocket(
RentalStore store, RentalIdentity identity, RentalTasks tasks, ObjectMapper json) {
this.store = store;
this.identity = identity;
this.tasks = tasks;
this.json = json;
}
private static class Connection {
WebSocketSession socket;
String challenge;
String key;
String secret;
String session;
String ping;
long node;
long epoch;
long sent;
long received;
long pong = System.currentTimeMillis();
long opened = System.currentTimeMillis();
long window;
int count;
boolean ready;
}
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
if (connections.size() >= 1000) {
session.close(CloseStatus.SERVICE_OVERLOAD);
return;
}
session.setTextMessageSizeLimit(65536);
Connection c = new Connection();
c.socket =
new org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator(
session, 5000, 262144);
c.challenge = RentalCrypto.random();
connections.put(session.getId(), c);
session.sendMessage(
new TextMessage(
json.writeValueAsString(
map("type", "CHALLENGE", "challenge", c.challenge))));
}
@Override
protected void handleTextMessage(WebSocketSession socket, TextMessage message)
throws Exception {
Connection c = connections.get(socket.getId());
try {
require(c != null && message.getPayloadLength() <= 65536, "连接或帧大小无效");
long window = System.currentTimeMillis() / 1000;
if (c.window != window) {
c.window = window;
c.count = 0;
}
require(++c.count <= 30, "节点发送速率超限");
RentalJson.bounded(message.getPayload());
Map<String, Object> envelope =
json.readValue(
message.getPayload(), new TypeReference<Map<String, Object>>() {});
String body = input(envelope, "body", 60000);
RentalJson.bounded(body);
Map<String, Object> frame =
json.readValue(body, new TypeReference<Map<String, Object>>() {});
String type = input(frame, "type", 24);
String key = input(envelope, "api_key", 64);
require(
c.node != 0
|| ("AUTH".equals(type)
&& System.currentTimeMillis() - c.opened <= 5000),
"未认证连接");
Map<String, Object> credential =
identity.authenticate(
key,
"NODE",
"NODE",
number(envelope, "timestamp"),
input(envelope, "nonce", 64),
"FRAME",
PATH,
body,
input(envelope, "signature", 64));
if (c.node == 0) {
require(c.challenge.equals(text(frame, "challenge")), "认证挑战不匹配");
long nodeId = number(credential, "owner_id");
tasks.tx(
() -> {
Map<String, Object> node = store.get("node", nodeId, true);
require(
Arrays.asList("ACTIVE", "PAUSED").contains(text(node, "status"))
&& text(node, "node_no").equals(text(frame, "node_no")),
"节点未审核或已吊销");
c.node = nodeId;
c.epoch = Math.addExact(number(node, "session_epoch"), 1);
c.session = id("SESSION");
c.key = key;
c.secret = identity.secret(credential);
store.update(
"node",
nodeId,
map(
"session_id",
c.session,
"session_epoch",
c.epoch,
"server_instance_id",
instance,
"connection_status",
"SYNCING",
"connected_at",
now(),
"heartbeat_at",
now(),
"heartbeat_expires_at",
after(45),
"last_client_sequence",
0));
return null;
});
for (Connection other : connections.values()) {
if (other != c && other.node == c.node) {
other.socket.close(CloseStatus.POLICY_VIOLATION);
}
}
send(c, "AUTH_OK", map("challenge", c.challenge, "heartbeat_seconds", 15));
return;
}
require(
key.equals(c.key)
&& "CLIENT".equals(text(frame, "direction"))
&& c.session.equals(text(frame, "session_id"))
&& number(frame, "session_epoch") == c.epoch
&& number(frame, "sequence") == c.received + 1,
"过期会话或帧序号错误");
require(
store.execute(
"UPDATE rent_node SET"
+ " last_client_sequence=?,version=version+1,updated_at=?"
+ " WHERE id=? AND session_epoch=? AND"
+ " last_client_sequence=? AND status<>'REVOKED'",
c.received + 1,
now(),
c.node,
c.epoch,
c.received)
== 1,
"会话已被替换");
c.received++;
Object payloadValue = frame.get("payload");
require(payloadValue instanceof Map, "缺少消息内容");
Map<String, Object> payload = (Map<String, Object>) payloadValue;
if ("PING".equals(type)) {
send(c, "PONG", map("ping_id", text(frame, "message_id")));
} else if ("PONG".equals(type)) {
require(c.ping != null && c.ping.equals(text(payload, "ping_id")), "PONG不匹配");
c.ping = null;
c.pong = System.currentTimeMillis();
store.execute(
"UPDATE rent_node SET"
+ " heartbeat_at=?,heartbeat_expires_at=?,version=version+1,updated_at=?"
+ " WHERE id=? AND session_epoch=?",
now(),
after(45),
now(),
c.node,
c.epoch);
} else if ("SYNC_STATE".equals(type)) {
Object events = payload.get("events");
if (events instanceof List) {
require(((List<?>) events).size() <= 100, "单次恢复事件超限");
for (Object event : (List<?>) events) {
tasks.report(c.node, (Map<String, Object>) event);
}
}
send(
c,
"SYNC_ACK",
map("events", events == null ? Collections.emptyList() : events));
c.ready = !Boolean.TRUE.equals(payload.get("more"));
if (c.ready) {
store.execute(
"UPDATE rent_node SET"
+ " connection_status='ONLINE',version=version+1,updated_at=? WHERE"
+ " id=? AND session_epoch=?",
now(),
c.node,
c.epoch);
}
} else if ("TASK_EVENT".equals(type) && c.ready) {
tasks.report(c.node, payload);
send(c, "RESULT_ACK", map("event_id", text(payload, "event_id")));
} else {
throw new IllegalArgumentException("消息不允许或尚未同步");
}
} catch (Exception ex) {
// 不回传认证材料、Secret、SQL或原始异常,防止日志与网络泄密。
socket.close(CloseStatus.POLICY_VIOLATION);
}
}
private String send(Connection c, String type, Map<String, Object> payload) throws Exception {
synchronized (c) {
require(c.socket.isOpen(), "节点连接已关闭");
String message = id("MSG");
String body =
json.writeValueAsString(
map(
"type",
type,
"direction",
"SERVER",
"session_id",
c.session,
"session_epoch",
Long.toString(c.epoch),
"sequence",
Long.toString(++c.sent),
"message_id",
message,
"payload",
payload));
long timestamp = System.currentTimeMillis();
String nonce = RentalCrypto.random();
if ("PING".equals(type)) {
c.ping = message;
}
c.socket.sendMessage(
new TextMessage(
json.writeValueAsString(
map(
"api_key",
c.key,
"timestamp",
timestamp,
"nonce",
nonce,
"body",
body,
"signature",
RentalCrypto.sign(
c.secret, "NODE", c.key, timestamp, nonce,
"FRAME", PATH, body)))));
return message;
}
}
/** 由有界后台调度调用,同一逻辑任务始终使用相同delivery_message_id和冻结intent。 */
public void tick() {
for (Connection c : connections.values()) {
try {
if (c.node == 0) {
if (System.currentTimeMillis() - c.opened > 5000) {
c.socket.close(CloseStatus.POLICY_VIOLATION);
}
continue;
}
if (System.currentTimeMillis() - c.pong > 45000) {
c.socket.close(CloseStatus.SESSION_NOT_RELIABLE);
continue;
}
Map<String, Object> node = store.get("node", c.node, false);
if (number(node, "session_epoch") != c.epoch
|| "REVOKED".equals(text(node, "status"))) {
c.socket.close(CloseStatus.POLICY_VIOLATION);
continue;
}
if (c.ping == null) {
send(c, "PING", map());
}
if (!c.ready) {
continue;
}
for (Map<String, Object> task :
store.rows(
"SELECT * FROM rent_task WHERE node_id=? AND status IN"
+ " ('PENDING','ACCEPTED','UNKNOWN','BROADCASTED') AND"
+ " (last_sent_at IS NULL OR last_sent_at<?) ORDER BY id LIMIT"
+ " 10",
c.node,
after(-15))) {
Map<String, Object> credential =
store.get("credential", number(task, "credential_id"), false);
if ("REVOKED".equals(text(credential, "status"))) {
continue;
}
// PAUSED仍核对历史原任务,但不推送尚未接受的首次任务去执行。
if (!"ACTIVE".equals(text(node, "status"))
&& "PENDING".equals(text(task, "status"))) {
continue;
}
send(
c,
"TASK_PUSH",
map(
"task_no",
text(task, "task_no"),
"delivery_message_id",
text(task, "delivery_message_id"),
"api_key",
text(credential, "api_key"),
"intent",
json.writeValueAsString(
new TreeMap<>(
json.readValue(
text(task, "intent"), Map.class))),
"signature",
text(task, "signature"),
"timestamp",
time(task.get("signed_at")).toEpochMilli(),
"nonce",
text(task, "nonce")));
store.execute(
"UPDATE rent_task SET"
+ " delivery_count=delivery_count+1,last_sent_at=?,delivery_session_epoch=?,version=version+1,updated_at=?"
+ " WHERE id=?",
now(),
c.epoch,
now(),
task.get("id"));
}
} catch (Exception ex) {
try {
c.socket.close(CloseStatus.SERVER_ERROR);
} catch (Exception ignored) {
/* 关闭后由epoch保护离线更新。 */
}
}
}
}
@Override
public void afterConnectionClosed(WebSocketSession socket, CloseStatus status) {
Connection c = connections.remove(socket.getId());
if (c != null && c.node != 0) {
store.execute(
"UPDATE rent_node SET"
+ " connection_status='OFFLINE',disconnected_at=?,version=version+1,updated_at=?"
+ " WHERE id=? AND session_epoch=?",
now(),
now(),
c.node,
c.epoch);
}
}
}