146 lines
5.2 KiB
TypeScript
Raw Normal View History

2020-12-02 18:08:49 +01:00
import "./RequireHandler";
2019-10-26 01:51:40 +02:00
2020-12-02 18:08:49 +01:00
import * as handle from "tc-native/connection";
import {NativeServerConnection} from "tc-native/connection";
2019-10-26 01:51:40 +02:00
//remote_host: "51.68.181.92",
//remote_host: "94.130.236.135",
//remote_host: "54.36.232.11", /* the beast */
//remote_host: "79.133.54.207", /* gommehd.net */
2020-12-02 18:08:49 +01:00
const target_address = "127.0.0.1";
2019-10-26 01:51:40 +02:00
const { host, port } = {
host: target_address.split(":")[0],
port: target_address.split(":").length > 1 ? parseInt(target_address.split(":")[1]) : 9987
};
2020-12-02 18:08:49 +01:00
2019-10-26 01:51:40 +02:00
class Bot {
connection: NativeServerConnection;
2020-12-02 18:08:49 +01:00
knwonChannelIds: number[] = [];
2019-10-26 01:51:40 +02:00
client_id: number;
initialized: boolean;
2020-12-02 18:08:49 +01:00
private switchInterval = [];
2019-10-26 01:51:40 +02:00
private _timeouts = [];
2020-12-02 18:08:49 +01:00
reset() {
this.connection = undefined;
for(const interval of this.switchInterval)
2019-10-26 01:51:40 +02:00
clearInterval(interval);
for(const timeouts of this._timeouts)
clearInterval(timeouts);
2020-12-02 18:08:49 +01:00
}
2019-10-26 01:51:40 +02:00
2020-12-02 18:08:49 +01:00
connect() {
this.knwonChannelIds = [];
2019-10-26 01:51:40 +02:00
this.client_id = 0;
this.initialized = false;
this.connection = handle.spawn_server_connection();
this.connection.connect({
timeout: 5000,
remote_port: port,
remote_host: host,
callback: error => {
if(error == 0) {
this.connection.send_command("clientinit", [
{
"client_key_offset": 2030434,
/*
"client_version": "1.0.0",
"client_platform": "nodejs/linux",
*/
"client_version": "3.1.8 [Build: 1516614607]",
"client_platform": "Windows",
"client_version_sign": "gDEgQf/BiOQZdAheKccM1XWcMUj2OUQqt75oFuvF2c0MQMXyv88cZQdUuckKbcBRp7RpmLInto4PIgd7mPO7BQ==",
"client_nickname": "TeaClient Native Module Test",
"client_input_hardware":true,
"client_output_hardware":true,
"client_default_channel":"",
"client_default_channel_password":"",
"client_server_password":"",
"client_meta_data":"",
"client_nickname_phonetic":"",
"client_default_token":"",
"hwid":"123,456123123123",
return_code:91
}
], []);
} else {
console.log("Bot connect failed: %o (%s) ", error, this.connection.error_message(error));
2020-12-02 18:08:49 +01:00
this.reset();
2019-10-26 01:51:40 +02:00
}
},
identity_key: "MG4DAgeAAgEgAiBC9JsqB1am6vowj2obomMyxm1GLk8qyRoxpBkAdiVYxwIgWksaSk7eyVQovZwPZBuiYHARz/xQD5zBUBK6e63V7hICIQCZ2glHe3kV62iIRKpkV2lzZGZtfBPRMbwIcU9aE1EVsg==",
teamspeak: true
});
this.connection.callback_command = (command, args, switches) => this.handle_command(command, args);
2020-12-02 18:08:49 +01:00
this.connection.callback_disconnect = () => {
this.connection = undefined;
this.reset();
}
2019-10-26 01:51:40 +02:00
}
async disconnect() {
await new Promise(resolve => this.connection.disconnect("bb", resolve));
this.connection = undefined;
2020-12-02 18:08:49 +01:00
this.reset();
2019-10-26 01:51:40 +02:00
}
private handle_command(command: string, args: any[]) {
if(command == "initserver") {
this.client_id = parseInt(args[0]["aclid"]);
} else if(command == "channellistfinished"){
this.initialized = true;
2020-12-02 18:08:49 +01:00
this.switchInterval.push(setInterval(() => this.switch_channel(), 30_000 + Math.random() * 10_000));
2019-10-26 01:51:40 +02:00
} else if(command == "channellist") {
for(const element of args) {
2020-12-02 18:08:49 +01:00
this.knwonChannelIds.push(parseInt(element["cid"]));
2019-10-26 01:51:40 +02:00
}
} else if(command == "notifychannelcreated") {
2020-12-02 18:08:49 +01:00
this.knwonChannelIds.push(parseInt(args[0]["cid"]));
2019-10-26 01:51:40 +02:00
} else if(command == "notifychanneldeleted") {
for(const arg of args) {
const channel_id = parseInt(arg["cid"]);
2020-12-02 18:08:49 +01:00
const index = this.knwonChannelIds.indexOf(channel_id);
2019-10-26 01:51:40 +02:00
if(index >= 0)
2020-12-02 18:08:49 +01:00
this.knwonChannelIds.splice(index, 1);
2019-10-26 01:51:40 +02:00
}
}
}
private switch_channel() {
2020-12-02 18:08:49 +01:00
const target_channel = this.knwonChannelIds[Math.floor((Math.random() * 100000) % this.knwonChannelIds.length)];
2019-10-26 01:51:40 +02:00
console.log("Switching to channel %d", target_channel);
this.connection.send_command("clientmove", [{clid: this.client_id, cid: target_channel}], []);
}
}
2020-12-02 18:08:49 +01:00
const bot_list: Bot[] = [];
async function connectBots() {
for(let index = 0; index < 5; index++) {
const bot = new Bot();
bot_list.push(bot);
bot.connect();
await new Promise(resolve => setTimeout(resolve, 10_000));
}
}
setInterval(() => {
bot_list.forEach(connection => {
if(connection.connection) {
connection.connection.send_voice_data(new Uint8Array([1, 2, 3]), 5, false);
} else {
connection.connect();
}
});
}, 5);
connectBots().then(undefined);