r2/proxy/app.js
lightings 12b4e1f775 ...
2023-07-21 11:39:45 +08:00

217 lines
5.1 KiB
JavaScript

const net = require("net");
const { PomeloClient } = require("./lib/boot");
const { Stick, MaxBodyLen } = require("./lib/stick");
const { Message } = require("./Message");
const process = require("process");
const config = process.argv[2] || "default";
const proxyList = require(`./config/${config}.json`);
const newLog = function () {
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const hour = date.getHours();
const minute = date.getMinutes();
const second = date.getSeconds();
arguments[0] =
`${year}-${month}-${day} ${hour}:${minute}:${second} ` + arguments[0];
arguments.callee.oLog.apply(this, arguments);
};
const newError = function () {
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const hour = date.getHours();
const minute = date.getMinutes();
const second = date.getSeconds();
arguments[0] =
`${year}-${month}-${day} ${hour}:${minute}:${second} ` + arguments[0];
arguments.callee.oError.apply(this, arguments);
};
newLog.oLog = console.log;
newError.oError = console.error;
console.log = newLog;
console.error = newError;
for (const channel of proxyList) {
proxy(channel);
}
function proxy(config) {
console.log("proxy:", JSON.stringify(config));
const host = config.host;
const port = config.port;
const srcHost = config.srcHost;
const srcPort = config.srcPort;
let socket_id_alloc = 0;
const server = net.createServer((socket) => {
const socket_id = socket_id_alloc++;
console.log(
"client connected",
host,
port,
socket.remoteAddress,
socket.remotePort,
socket_id
);
let pomelo = new PomeloClient();
let stick = new Stick(1024);
stick.setMaxBodyLen(MaxBodyLen["32K"], true);
let connectBuffer = [];
let isReady = false;
pomelo.init(
{ host: srcHost, port: srcPort, log: true },
() => {
isReady = true;
try {
connectBuffer.forEach((data) => {
stick.putData(data);
});
} catch (e) {
console.log("putData error", e);
socket.destroy();
}
},
(route, body) => {
repPush(route, body);
}
);
pomelo.on("close", (event) => {
console.log(
host + ":" + port + "=>",
"pomelo on close " + socket_id + " " + event
);
try {
connectBuffer = null;
stick.dispose();
stick = null;
pomelo.destructor();
pomelo = null;
socket.destroy();
socket = null;
} catch (e) {
console.log(e);
}
});
pomelo.on("heartbeat timeout", () => {
console.log("pomelo heartbeat timeout");
});
socket.on("connect", () => {
console.log("connect");
});
socket.on("error", (error) => {
console.log(error);
});
socket.on("close", () => {
console.log("close " + socket_id + " from client");
try {
if (pomelo) {
pomelo.disconnect();
}
connectBuffer = null;
if (stick) {
stick.dispose();
stick = null;
}
if (pomelo) {
pomelo.destructor();
pomelo = null;
}
if (socket) {
socket.destroy();
socket = null;
}
} catch (err) {
console.log(err);
}
});
socket.on("drop", () => {
console.log("drop");
});
socket.on("data", (data) => {
try {
if (isReady) {
stick.putData(data);
} else {
connectBuffer.push(data);
}
} catch (e) {
console.log(e);
socket.destroy();
}
});
stick.onBody((msgId, data) => {
try {
const len = data.readUInt16LE(0);
let buf1 = data.subarray(2, 2 + len);
let msg = JSON.parse(buf1);
console.log(msgId, ">>>" + JSON.stringify(msg));
const account = "6516_2006_0xef59f6cc4d190a0ae576c46d4583e92b61174340";
switch (msgId) {
case Message.TYPE_REQUEST:
pomelo.request(msg.route, msg.params, (data) => {
repRequest(msg.serial, msg.route, data);
});
break;
case Message.TYPE_NOTIFY:
pomelo.notify(data.toString(), { uid: account });
break;
}
} catch (e) {
console.log(e);
}
});
const repRequest = (serial, route, data) => {
data.serial = serial;
data.route = route;
try {
let dataStr = JSON.stringify(data);
console.log("<<<", dataStr);
let rdata = stick.makeData(Message.TYPE_RESPONSE, dataStr);
socket.write(rdata);
} catch (e) {
console.log(e);
}
};
const repPush = (route, data) => {
try {
data.route = route;
const dataStr = JSON.stringify(data);
console.log("^^^", dataStr);
const rdata = stick.makeData(Message.TYPE_PUSH, dataStr);
socket.write(rdata);
} catch (e) {
console.log(e);
}
};
});
server.listen(port, host);
console.log("listening " + host + ":" + port);
}