first stable dev version
This commit is contained in:
parent
a79624b86b
commit
00e60ba28f
|
@ -59,36 +59,42 @@ export class DockerController {
|
|||
},
|
||||
}
|
||||
const rawContainer = await this.dockerAPI.createContainer(options);
|
||||
const container=new wrappedContainer(ports, rawContainer);
|
||||
this.containers.push(container);
|
||||
await rawContainer.start();
|
||||
await sleep(30 * 1_000);
|
||||
const start = new Date();
|
||||
while (true) {
|
||||
if (((start.getTime() - (new Date()).getTime()) / 1_000) > timeoutMS) {
|
||||
await rawContainer.remove({ force: true, v: true });
|
||||
await container.kill();
|
||||
throw new DockerContainerIsBroken("unhealthy", (start.getTime() - (new Date()).getTime()))
|
||||
}
|
||||
const state = (await rawContainer.inspect()).State
|
||||
if (state.Health !== undefined) {
|
||||
if (state.Health.Status == 'healthy') {
|
||||
this.containers.push(new wrappedContainer(ports, rawContainer));
|
||||
|
||||
try {
|
||||
await this.containers[this.containers.length - 1].getIP()
|
||||
await container.getIP()
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
this.containers.pop();
|
||||
await rawContainer.remove({ force: true, v: true });
|
||||
this.containers=this.containers.filter((_container)=>{
|
||||
return _container.ports.http!==container.ports.http
|
||||
})
|
||||
await container.kill();
|
||||
if (counter > 3) {
|
||||
throw new DockerContainerIsBroken(state.Health.Status, (start.getTime() - (new Date()).getTime()))
|
||||
} else {
|
||||
return await this.createNewDocker(ports, timeoutMS, counter + 1)
|
||||
}
|
||||
}
|
||||
return this.containers[this.containers.length - 1]
|
||||
return container
|
||||
} else {
|
||||
|
||||
if (state.Health.Status == 'unhealthy') {
|
||||
this.containers.pop();
|
||||
await rawContainer.remove({ force: true, v: true });
|
||||
this.containers=this.containers.filter((_container)=>{
|
||||
return _container.ports.http!==container.ports.http
|
||||
})
|
||||
await container.kill();
|
||||
if (counter > 3) {
|
||||
throw new DockerContainerIsBroken(state.Health.Status, (start.getTime() - (new Date()).getTime()))
|
||||
} else {
|
||||
|
@ -145,12 +151,17 @@ export class wrappedContainer {
|
|||
}
|
||||
async restart() {
|
||||
this.status = 'unhealthy';
|
||||
await this.rawContainer.restart();
|
||||
try{
|
||||
await this.rawContainer.restart();
|
||||
} catch(err){
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
async changeIP() { //timeoutMS = 600_000
|
||||
await this.tor.connect();
|
||||
this.currentIP = await this.getIP();
|
||||
//this.currentIP = await this.getIP();
|
||||
await this.tor.signalNewnym();
|
||||
let ip = await this.getIP();
|
||||
// let counter = 0;// crutch
|
||||
|
@ -166,7 +177,7 @@ export class wrappedContainer {
|
|||
this.currentIP = ip;
|
||||
}
|
||||
|
||||
async getIP(maxTries = 60): Promise<string> { // this gets ip from internet
|
||||
async getIP(maxTries = 40): Promise<string> { // this gets ip from internet
|
||||
let counter = 0;
|
||||
while (true) {
|
||||
try {
|
||||
|
@ -250,10 +261,10 @@ export class wrappedContainer {
|
|||
}
|
||||
|
||||
}
|
||||
async getCurrentIP() { // this gets ip from local property
|
||||
async getCurrentIP() {
|
||||
if (this.currentIP === '') {
|
||||
try {
|
||||
this.currentIP = await this.getIP()
|
||||
this.currentIP = await this.getIP();
|
||||
return this.currentIP
|
||||
} catch { }
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
export interface IPorts {
|
||||
http: number,
|
||||
socks: number,
|
||||
control: number
|
||||
readonly http: number,
|
||||
readonly socks: number,
|
||||
readonly control: number
|
||||
}
|
||||
|
|
|
@ -8,8 +8,10 @@ import { UnkownHttpCodeError } from "./utils";
|
|||
|
||||
|
||||
export async function processWorkers(dockerContainer: wrappedContainer, workerController: WorkerController) {
|
||||
mainLoop:for await (const worker of workerController.interateWorkers(dockerContainer)) {
|
||||
if (worker !== undefined) {
|
||||
mainLoop:while (true){
|
||||
const iteration=(await workerController.interateWorkers(dockerContainer).next());
|
||||
if (iteration.done!==true){
|
||||
const worker=iteration.value;
|
||||
console.log(`Current worker:${worker.phoneNumber}\nBalance:${worker.balance}`);
|
||||
while (workerController.isFreeIP(worker, await dockerContainer.getCurrentIP()) === false) {
|
||||
try{
|
||||
|
@ -71,9 +73,11 @@ export async function processWorkers(dockerContainer: wrappedContainer, workerCo
|
|||
})
|
||||
}
|
||||
}
|
||||
await telegramWorker.sendMessage("me",{message:`TEST ${Math.random()}`}); // to test
|
||||
}
|
||||
} else {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,12 +1,10 @@
|
|||
import findFreePorts from "find-free-ports";
|
||||
import { sleep } from "telegram/Helpers";
|
||||
import { settings } from "../Databases/JSONDatabase";
|
||||
import { db } from "../Databases/SQLDatabase";
|
||||
import { wrappedContainer, DockerController } from "../DockerWrapper/Docker";
|
||||
import { DockerContainerIsBroken, DockerError, DockerNotPulled } from "../DockerWrapper/DockerErrors";
|
||||
import { WorkerController } from "../Worker/WorkerController";
|
||||
import { processWorkers } from "./proccessWorkers";
|
||||
import { randomInRange, UnknownError } from "./utils";
|
||||
import { UnknownError } from "./utils";
|
||||
export async function start() {
|
||||
const amountWorkers = (await db.getWorkers()).length
|
||||
if (amountWorkers < settings.pararels) {
|
||||
|
@ -14,7 +12,7 @@ export async function start() {
|
|||
settings.pararels = amountWorkers
|
||||
}
|
||||
const ports = await findFreePorts(settings.pararels * 3);
|
||||
let promises: Promise<wrappedContainer>[] = []
|
||||
const promises: Promise<wrappedContainer>[] = []
|
||||
for (let i = 0; i != settings.pararels; i++) {
|
||||
promises.push(DockerController.createNewDocker({
|
||||
http: ports[i * 3],
|
||||
|
@ -22,34 +20,22 @@ export async function start() {
|
|||
control: ports[i * 3 + 2]
|
||||
}));
|
||||
}
|
||||
const dockersPromises = await Promise.allSettled(promises);
|
||||
const dockers: wrappedContainer[] = dockersPromises.map((promise)=>{
|
||||
if (promise.status==='fulfilled'){
|
||||
return promise.value
|
||||
} else {
|
||||
throw promise.reason
|
||||
}
|
||||
})
|
||||
const dockers = await Promise.all(promises);
|
||||
|
||||
if (dockers.length != settings.pararels) {
|
||||
throw new UnknownError('There are not enough docker containers to run properly');
|
||||
} else {
|
||||
const workerController = new WorkerController();
|
||||
while (true) {
|
||||
const workerController = new WorkerController();
|
||||
await workerController.load();
|
||||
const workersPararels: Promise<void>[] = [];
|
||||
for (let i = 0; i != settings.pararels; i++) {
|
||||
workersPararels.push(processWorkers(dockers[i], workerController));
|
||||
}
|
||||
dockers.forEach((docker)=>{
|
||||
console.log(docker.ports)
|
||||
workersPararels.push(processWorkers(docker, workerController));
|
||||
})
|
||||
for (const promise of await Promise.allSettled(workersPararels)) {
|
||||
if (promise.status === "rejected") {
|
||||
if (promise.reason instanceof Error) {
|
||||
// if (promise.reason instanceof DockerContainerIsBroken) {
|
||||
// // need to do recreating of container
|
||||
// throw promise.reason as DockerContainerIsBroken;
|
||||
// } else {
|
||||
|
||||
// }
|
||||
throw promise.reason
|
||||
} else {
|
||||
throw new UnknownError("Some sort of error happened:" + promise.reason as string)
|
||||
|
|
|
@ -1,17 +1,23 @@
|
|||
import axios, { AxiosProxyConfig, AxiosResponse } from "axios";
|
||||
import { isString } from "lodash";
|
||||
import { DockerController } from "../DockerWrapper/Docker";
|
||||
import { EventEmitter } from 'node:events';
|
||||
|
||||
|
||||
process.on("beforeExit", async ()=>{
|
||||
await DockerController.killContainers();
|
||||
console.log("Containers are closed!");
|
||||
process.exit();
|
||||
})
|
||||
process.on('exit', (code) => {
|
||||
DockerController.killContainers();
|
||||
console.log('Bye bye');
|
||||
console.log('Process exit event with code: ', code);
|
||||
|
||||
});
|
||||
process.on('SIGINT', function() {
|
||||
DockerController.killContainers().then(()=>{
|
||||
process.exit();
|
||||
})
|
||||
process.on('SIGINT', async function() {
|
||||
await DockerController.killContainers();
|
||||
console.log("Containers are closed!");
|
||||
process.exit();
|
||||
});
|
||||
export class UnknownError extends Error {
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import { startsWith } from "lodash";
|
||||
import { db } from "../Databases/SQLDatabase";
|
||||
import { wrappedContainer } from "../DockerWrapper/Docker";
|
||||
import { DockerContainerIsBroken } from "../DockerWrapper/DockerErrors";
|
||||
|
@ -41,9 +42,10 @@ export class WorkerController {
|
|||
}
|
||||
}
|
||||
isFreeIP(worker: Pick<IWorker, "phoneNumber">, ip: string) {
|
||||
if (this.usedIPs.filter((usedIP) => {
|
||||
return usedIP.worker.phoneNumber !== worker.phoneNumber && usedIP.ips.find((value)=> value===ip ) === undefined
|
||||
}).length === 0) {
|
||||
const isUnique=this.usedIPs.filter((usedIP) => {
|
||||
return usedIP.ips.filter((value) => startsWith(value, ip) === true).length !== 0;
|
||||
});
|
||||
if (isUnique.length === 0) {
|
||||
const usedIP=this.usedIPs.filter((usedIP) => {
|
||||
return usedIP.worker.phoneNumber == worker.phoneNumber
|
||||
})
|
||||
|
|
|
@ -15,6 +15,8 @@
|
|||
"exclude": [
|
||||
"node_modules",
|
||||
"**/node_modules/*",
|
||||
"**/*.test.ts"
|
||||
"**/*.test.ts",
|
||||
"**/*.config.ts",
|
||||
"**/*.config.js"
|
||||
]
|
||||
}
|
Loading…
Reference in a new issue