Commit d5b64bd1 authored by nanahira's avatar nanahira

add preconn detect

parent 2255fe81
......@@ -226,12 +226,13 @@ export class YGOProMessagesHelper {
handlerCollection.get(translatedProto).push(handlerObj);
}
async handleBuffer(messageBuffer: Buffer, direction: string, protoFilter?: string[], params?: any, disconnectIfInvalid = false): Promise<HandleResult> {
async handleBuffer(messageBuffer: Buffer, direction: string, protoFilter?: string[], params?: any, preconnect = false): Promise<HandleResult> {
let feedback: Feedback = null;
let messageLength = 0;
let bufferProto = 0;
let datas: Buffer[] = [];
for (let l = 0; l < this.singleHandleLimit; ++l) {
const limit = preconnect ? 2 : this.singleHandleLimit;
for (let l = 0; l < limit; ++l) {
if (messageLength === 0) {
if (messageBuffer.length >= 2) {
messageLength = messageBuffer.readUInt16LE(0);
......@@ -258,7 +259,7 @@ export class YGOProMessagesHelper {
if (messageBuffer.length >= 2 + messageLength) {
const proto = this.constants[direction][bufferProto];
let cancel = proto && protoFilter && !protoFilter.includes(proto);
if (cancel && disconnectIfInvalid) {
if (cancel && preconnect) {
feedback = {
type: "INVALID_PACKET",
message: `${direction} proto not allowed`
......@@ -303,7 +304,7 @@ export class YGOProMessagesHelper {
break;
}
}
if (l === this.singleHandleLimit - 1) {
if (l === limit - 1) {
feedback = {
type: "OVERSIZE",
message: `Oversized ${direction}`
......
......@@ -1813,6 +1813,11 @@ netRequestHandler = (client) ->
ROOM_connected_ip[client.ip] = connect_count
#log.info "connect", client.ip, ROOM_connected_ip[client.ip]
if ROOM_bad_ip[client.ip] > 5 or ROOM_connected_ip[client.ip] > 10
log.info 'BAD IP', client.ip
client.destroy()
return
# server stand for the connection to ygopro server process
server = new net.Socket()
client.server = server
......@@ -1894,11 +1899,6 @@ netRequestHandler = (client) ->
SERVER_clear_disconnect(server)
return
if ROOM_bad_ip[client.ip] > 5 or ROOM_connected_ip[client.ip] > 10
log.info 'BAD IP', client.ip
CLIENT_kick(client)
return
if settings.modules.cloud_replay.enabled
client.open_cloud_replay = (replay)->
if !replay
......@@ -1944,16 +1944,16 @@ netRequestHandler = (client) ->
room.watcher.write(buffer) for buffer in handle_data.datas
else
ctos_filter = null
disconnectIfInvalid = false
preconnect = false
if settings.modules.reconnect.enabled and client.pre_reconnecting_to_room
ctos_filter = ["UPDATE_DECK"]
if !client.name
if client.name == null
ctos_filter = ["JOIN_GAME", "PLAYER_INFO"]
disconnectIfInvalid = true
preconnect = true
handle_data = await ygopro.helper.handleBuffer(ctos_buffer, "CTOS", ctos_filter, {
client: client,
server: client.server
}, disconnectIfInvalid)
}, preconnect)
if handle_data.feedback
log.warn(handle_data.feedback.message, client.name, client.ip)
if handle_data.feedback.type == "OVERSIZE" or handle_data.feedback.type == "INVALID_PACKET" or ROOM_bad_ip[client.ip] > 5
......
......@@ -2446,7 +2446,11 @@
}
ROOM_connected_ip[client.ip] = connect_count;
//log.info "connect", client.ip, ROOM_connected_ip[client.ip]
if (ROOM_bad_ip[client.ip] > 5 || ROOM_connected_ip[client.ip] > 10) {
log.info('BAD IP', client.ip);
client.destroy();
return;
}
// server stand for the connection to ygopro server process
server = new net.Socket();
client.server = server;
......@@ -2546,11 +2550,6 @@
SERVER_clear_disconnect(server);
}
});
if (ROOM_bad_ip[client.ip] > 5 || ROOM_connected_ip[client.ip] > 10) {
log.info('BAD IP', client.ip);
CLIENT_kick(client);
return;
}
if (settings.modules.cloud_replay.enabled) {
client.open_cloud_replay = async function(replay) {
var buffer, e, replay_buffer;
......@@ -2578,7 +2577,7 @@
// 客户端到服务端(ctos)协议分析
client.pre_establish_buffers = new Array();
client.on('data', async function(ctos_buffer) {
var bad_ip_count, buffer, ctos_filter, disconnectIfInvalid, handle_data, j, l, len, len1, len2, m, ref, ref1, ref2, room;
var bad_ip_count, buffer, ctos_filter, handle_data, j, l, len, len1, len2, m, preconnect, ref, ref1, ref2, room;
if (client.is_post_watcher) {
room = ROOM_all[client.rid];
if (room) {
......@@ -2607,18 +2606,18 @@
}
} else {
ctos_filter = null;
disconnectIfInvalid = false;
preconnect = false;
if (settings.modules.reconnect.enabled && client.pre_reconnecting_to_room) {
ctos_filter = ["UPDATE_DECK"];
}
if (!client.name) {
if (client.name === null) {
ctos_filter = ["JOIN_GAME", "PLAYER_INFO"];
disconnectIfInvalid = true;
preconnect = true;
}
handle_data = (await ygopro.helper.handleBuffer(ctos_buffer, "CTOS", ctos_filter, {
client: client,
server: client.server
}, disconnectIfInvalid));
}, preconnect));
if (handle_data.feedback) {
log.warn(handle_data.feedback.message, client.name, client.ip);
if (handle_data.feedback.type === "OVERSIZE" || handle_data.feedback.type === "INVALID_PACKET" || ROOM_bad_ip[client.ip] > 5) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment