Migrated to dockage/tor-privoxy

This commit is contained in:
minicx 2023-02-17 02:43:04 +03:00
parent a9be54929a
commit e2e6723b18
6 changed files with 66 additions and 67 deletions

View file

@ -28,12 +28,21 @@ export async function doSitesTask(telegramWorker: TelegramWorker,earnMSG: Api.Me
const skipButton=task.buttons![0][1];
const link=task.buttons![0][0].url
if (link!==undefined){
try{
await hero.goto(link, { timeoutMs: 120_000 });
} catch{
console.log(`${telegramWorker.worker.phoneNumber}:Some error while going to link`);
return
for (let tries=0;tries<3+1;tries++){
if (tries===3){
console.log(`${telegramWorker.worker.phoneNumber}:Some error while going to link`);
return
}
try{
await hero.goto(link, { timeoutMs: 120_000 });
} catch (err){
continue
} finally {
break
}
}
try {
await hero.waitForLoad("AllContentLoaded", { timeoutMs: 120_000 });
} catch (err) {

View file

@ -1,12 +1,12 @@
import Docker from "dockerode";
import { sleep } from "telegram/Helpers";
import { IPorts } from "../Helpers/IPorts";
import { getIP, HttpEconnRefusedError, TorError, UnknownError, UnkownHttpCodeError } from "../Helpers/utils";
import { getIP, HttpEconnRefusedError, timeout, TorError, UnknownError, UnkownHttpCodeError } from "../Helpers/utils";
import { DockerContainerIsBroken, DockerNoSuchContainer, DockerNotPulled } from "./DockerErrors";
import { Tor } from 'tor-control-ts';
import { startsWith } from "lodash";
export const DOCKERIMAGE = 'dperson/torproxy'
export const DOCKERIMAGE = 'dockage/tor-privoxy'
export class DockerController {
protected static dockerAPI = new Docker({ socketPath: "/var/run/docker.sock" });
@ -33,11 +33,11 @@ export class DockerController {
return true
}
}
static async createNewDocker(ports: IPorts, timeoutMS = 125_000, counter = 0): Promise<wrappedContainer> {
static async createNewDocker(ports: IPorts, timeoutMS = 300_000): Promise<wrappedContainer> {
if (await this.isPulled()) {
const options: Docker.ContainerCreateOptions = {
Image: DOCKERIMAGE,
Env: ["PASSWORD=qwerty", "LOCATION=US", "TOR_NewCircuitPeriod=50"],
// Env: ["PASSWORD=qwerty", "LOCATION=US", "TOR_NewCircuitPeriod=50"],
HostConfig: {
PortBindings: {
"8118/tcp": [
@ -57,57 +57,48 @@ export class DockerController {
],
},
},
// Healthcheck: {
// Interval: 2 * 1000000000,
// Timeout: 50 * 1000000000,
// StartPeriod: 50 * 1000000000,
// Test:['CMD_SHELL',`curl -sx localhost:8118 'https://check.torproject.org/' | grep -qm1 Congratulations`]
// },
}
const rawContainer = await this.dockerAPI.createContainer(options);
const container=new wrappedContainer(ports, rawContainer);
let container=new wrappedContainer(ports, rawContainer,(await rawContainer.inspect()).Name,{active:false,interval:0});
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 sleep(60 * 1_000);
try{
await timeout((async ()=>{
while (true){
try {
await container.getIP();
} catch {
await sleep(10_000);
continue
}
break
}
})(),timeoutMS,new DockerContainerIsBroken())
} catch (err){
if (err instanceof DockerContainerIsBroken){
console.log(err);
this.containers=this.containers.filter((_container)=>{
return _container.ports.control!==container.ports.control
})
await container.kill();
return await this.createNewDocker(ports, timeoutMS)
} else {
throw new DockerContainerIsBroken()
}
const state = (await rawContainer.inspect()).State
if (state.Health !== undefined) {
if (state.Health.Status == 'healthy') {
try {
await container.getIP()
} catch (err) {
console.log(err);
this.containers=this.containers.filter((_container)=>{
return _container.ports.http!==container.ports.http
})
await container.kill();
if (counter > 3) {
throw new DockerContainerIsBroken()
} else {
return await this.createNewDocker(ports, timeoutMS, counter + 1)
}
}
return container
} else {
if (state.Health.Status == 'unhealthy') {
this.containers=this.containers.filter((_container)=>{
return _container.ports.http!==container.ports.http
})
await container.kill();
if (counter > 3) {
throw new DockerContainerIsBroken()
} else {
return await this.createNewDocker(ports, timeoutMS, counter + 1)
}
}
}
} else {
// log what something is wrong
}
}
this.containers=this.containers.filter((_container)=>{
return _container.ports.control!==container.ports.control
})
container=new wrappedContainer(ports, rawContainer,(await rawContainer.inspect()).Name);
this.containers.push(container);
return container
} else {
throw new DockerNotPulled()
}
@ -134,11 +125,12 @@ export class DockerController {
export class wrappedContainer {
protected rawContainer: Docker.Container;
readonly ports: IPorts;
readonly name: string;
protected status: "healthy" | "unhealthy" = 'healthy';
protected currentIP: string = '';
protected interval: NodeJS.Timer | undefined;
protected tor: Tor;
constructor(ports: IPorts, rawContainer: Docker.Container, internalCheck = { active: true, interval: 60_000 }) {
constructor(ports: IPorts, rawContainer: Docker.Container,name: string, internalCheck = { active: true, interval: 60_000 }) {
this.rawContainer = rawContainer;
this.ports = ports;
if (internalCheck.active == true) {
@ -147,8 +139,9 @@ export class wrappedContainer {
this.tor = new Tor({
host: 'localhost',
port: this.ports.control,
password: 'qwerty',
password: '',
});
this.name=name;
}
async restart() {
// need check for starting
@ -229,17 +222,6 @@ export class wrappedContainer {
this.status = 'unhealthy';
return
}
const state = (await this.rawContainer.inspect()).State
if (state.Health !== undefined) {
if (state.Health.Status == 'unhealthy') {
this.restart();
this.status = state.Health.Status;
} else {
this.status = 'healthy';
}
// return // non-strict mode
}
try {
await this.getIP();

View file

@ -151,6 +151,7 @@ export async function addWorkers() {
},
});
} catch (err) {
console.log(err)
continue;
}

View file

@ -45,7 +45,7 @@ export async function processWorkers(dockerContainer: wrappedContainer, workerCo
await db.updateWorker(worker);
continue;
} else {
console.log(`Successfully connected to account ${worker.phoneNumber}`);
console.log(`${dockerContainer.name}:Successfully connected to account ${worker.phoneNumber}`);
let botEntity: Api.User;
try {
botEntity = await telegramWorker.checkFloodErrorByBotEntity();

View file

@ -1,5 +1,6 @@
import Miner from "@ulixee/miner";
import findFreePorts from "find-free-ports";
import process from "node:process";
import { sleep } from "telegram/Helpers";
import { settings } from "../Databases/JSONDatabase";
import { db } from "../Databases/SQLDatabase";
@ -15,7 +16,12 @@ export async function start() {
settings.pararels = amountWorkers
}
const ports = await findFreePorts(settings.pararels * 3);
const promises: Promise<wrappedContainer>[] = []
const promises: Promise<wrappedContainer>[] = [];
if (await DockerController.isPulled()===false){
await DockerController.pullImage();
console.log('Restart script...');
process.exit();
}
for (let i = 0; i != settings.pararels; i++) {
promises.push(DockerController.createNewDocker({
http: ports[i * 3],

View file

@ -4,6 +4,7 @@
"@ulixee/hero": "^2.0.0-alpha.16",
"@ulixee/miner": "^2.0.0-alpha.16",
"axios": "^1.2.1",
"axios-socks5-agent": "^1.0.5",
"better-sqlite3": "^8.0.1",
"cli-progress": "^3.11.2",
"dockerode": "^3.3.4",