2022-12-01 21:53:29 +01:00
|
|
|
import Docker from "dockerode";
|
|
|
|
import logger from "./logger";
|
|
|
|
import {database as db,settings} from './database';
|
|
|
|
// import Hero from "@ulixee/hero";
|
|
|
|
// import Miner from "@ulixee/miner";
|
2022-12-02 13:07:07 +01:00
|
|
|
|
2022-12-01 21:53:29 +01:00
|
|
|
import { sleep } from "telegram/Helpers";
|
|
|
|
import { AccountInterface } from "./interfaces/databaseInterface";
|
|
|
|
import * as telegram from 'telegram';
|
|
|
|
import axios from "axios";
|
2022-12-04 16:52:54 +01:00
|
|
|
import cliProgress from "cli-progress"
|
2022-12-02 16:41:38 +01:00
|
|
|
|
2022-12-04 16:52:54 +01:00
|
|
|
export const isBannedByClient=async function(client:telegram.TelegramClient,username: string): Promise<boolean> {
|
|
|
|
const blockedUsers=(await client.invoke(new telegram.Api.contacts.GetBlocked({}))).users as telegram.Api.User[];
|
|
|
|
for (const blockedUser of blockedUsers){
|
|
|
|
if (blockedUser.username!==undefined && blockedUser.username==username.replace('@','')){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2022-12-02 16:41:38 +01:00
|
|
|
|
|
|
|
export const getMessagesByEntity=async function(client:telegram.TelegramClient,chatEntity:telegram.Api.Chat | telegram.Api.User,entity: telegram.Api.User | telegram.Api.User,params={}){
|
|
|
|
var messages=new telegram.helpers.TotalList<telegram.Api.Message>;
|
|
|
|
for (const message of await client.getMessages(chatEntity,params)){
|
|
|
|
if ((message.sender as telegram.Api.User).username==entity.username){
|
|
|
|
messages.push(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return messages;
|
|
|
|
}
|
2022-12-03 06:37:20 +01:00
|
|
|
export const arrayWithoutElementAtIndex = function (arr: AccountInterface[] | [], index: number) {
|
|
|
|
return arr.filter(function(value, arrIndex) {
|
|
|
|
return index !== arrIndex;
|
|
|
|
});
|
|
|
|
}
|
2022-12-02 12:44:14 +01:00
|
|
|
export const getIP=async function (proxyPort: number): Promise<string | null> {
|
2022-12-06 15:11:23 +01:00
|
|
|
let data: string | PromiseLike<string | null> | null
|
|
|
|
try{
|
|
|
|
data=(await axios.get('http://api.ipify.org',{
|
|
|
|
proxy:{
|
|
|
|
host:'127.0.0.1',
|
|
|
|
port:proxyPort
|
|
|
|
},
|
|
|
|
timeout:100_000
|
|
|
|
})).data;
|
|
|
|
} catch (err){
|
|
|
|
logger.debug(err);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((await data)==null || (await data)!.trim()==""){
|
2022-12-02 12:44:14 +01:00
|
|
|
return null
|
|
|
|
}
|
2022-12-06 15:11:23 +01:00
|
|
|
return await data
|
2022-12-01 21:53:29 +01:00
|
|
|
|
|
|
|
}
|
2022-12-02 16:41:38 +01:00
|
|
|
export const waitNewMessages=async function(client: telegram.TelegramClient,worker: AccountInterface,chatEntity: telegram.Api.Chat | telegram.Api.User,idOfLastMessage: number,timeout: number=20): Promise<void>{
|
2022-12-01 21:53:29 +01:00
|
|
|
|
|
|
|
const start_time=new Date();
|
|
|
|
while ((await client.getMessages(chatEntity,{minId:idOfLastMessage})).length==0){
|
|
|
|
if ((+(new Date())-+start_time)/1000>=timeout){
|
|
|
|
logger.error(`${worker.phoneNumber} | Bot didnt answer for ${timeout}s`);
|
|
|
|
throw new Error('Is bot working?');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logger.debug(`${worker.phoneNumber} | Bot answered`);
|
|
|
|
}
|
|
|
|
|
2022-12-04 16:52:54 +01:00
|
|
|
export const startNewTorDocker=async function(proxyPort: number,socksPort: number,controlPort: number,mainProgressBar: cliProgress.MultiBar,timeout: number=200): Promise<Docker.Container>{
|
2022-12-01 21:53:29 +01:00
|
|
|
timeout*=2;
|
|
|
|
let docker = new Docker({socketPath: '/var/run/docker.sock'});
|
|
|
|
let isPulled=false;
|
|
|
|
mainLoop:
|
|
|
|
for (const image of await docker.listImages()){
|
|
|
|
const tags=image.RepoTags;
|
|
|
|
if (tags!==undefined)
|
|
|
|
for (const repoTag of tags){
|
|
|
|
if (repoTag.search('dperson/torproxy:latest')!=-1){
|
|
|
|
isPulled=true;
|
|
|
|
break mainLoop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isPulled!=true){
|
|
|
|
await docker.pull('dperson/torproxy:latest');
|
|
|
|
}
|
|
|
|
|
|
|
|
// bugy shit
|
|
|
|
const options: Docker.ContainerCreateOptions={
|
|
|
|
Image:'dperson/torproxy',
|
|
|
|
Env:[
|
|
|
|
"PASSWORD=qwerty",
|
|
|
|
"LOCATION=US",
|
|
|
|
"TOR_NewCircuitPeriod=50",
|
|
|
|
],
|
|
|
|
ExposedPorts:{},
|
|
|
|
HostConfig:{
|
|
|
|
PortBindings:{
|
|
|
|
'8118/tcp':[{
|
|
|
|
HostPort: `${proxyPort}`
|
|
|
|
}],
|
|
|
|
'9050/tcp':[{
|
|
|
|
HostPort: `${socksPort}`
|
|
|
|
}],
|
|
|
|
'9051/tcp':[{
|
|
|
|
HostPort: `${controlPort}`
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Healthcheck:{
|
2022-12-05 19:07:39 +01:00
|
|
|
Interval:2*1_000_000_000,
|
|
|
|
Timeout:50*1_000_000_000,
|
2022-12-01 21:53:29 +01:00
|
|
|
StartPeriod:50*1_000_000_000,
|
|
|
|
// Test:['CMD_SHELL',`curl -sx localhost:8118 'https://check.torproject.org/' | grep -qm1 Congratulations`]
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-12-02 12:44:14 +01:00
|
|
|
logger.debug(`Creating container with this ports ${proxyPort} ${socksPort} ${controlPort}`);
|
2022-12-01 21:53:29 +01:00
|
|
|
const container=await docker.createContainer(options);
|
|
|
|
|
|
|
|
await container.start();
|
2022-12-04 16:52:54 +01:00
|
|
|
|
|
|
|
const progressBar=mainProgressBar.create(timeout/2, 0,{status:'Starting'});
|
2022-12-01 21:53:29 +01:00
|
|
|
while ((await container.inspect())!.State.Health!.Status!='healthy'){
|
|
|
|
const state=(await container.inspect())!.State.Health!.Status
|
2022-12-04 16:52:54 +01:00
|
|
|
if (progressBar.getProgress()>=timeout){
|
2022-12-01 21:53:29 +01:00
|
|
|
await container.kill();
|
2022-12-04 16:52:54 +01:00
|
|
|
progressBar.update(timeout/2,{status:'Failed'})
|
|
|
|
progressBar.stop();
|
2022-12-01 21:53:29 +01:00
|
|
|
throw new Error(`Docker ${container.id} is broken`);
|
|
|
|
}
|
|
|
|
if (state=='unhealthy'){
|
|
|
|
await container.kill();
|
2022-12-04 16:52:54 +01:00
|
|
|
progressBar.update(timeout/2,{status:'Failed'})
|
|
|
|
progressBar.stop();
|
2022-12-01 21:53:29 +01:00
|
|
|
logger.warn(`Docker ${container.id} is unhealthy.Recreating...`);
|
2022-12-04 16:52:54 +01:00
|
|
|
return await startNewTorDocker(proxyPort,socksPort,controlPort,mainProgressBar,timeout/2);
|
2022-12-01 21:53:29 +01:00
|
|
|
}
|
2022-12-04 16:52:54 +01:00
|
|
|
progressBar.increment({status: "Starting"});;
|
2022-12-01 21:53:29 +01:00
|
|
|
await sleep(2000);
|
|
|
|
}
|
2022-12-04 16:52:54 +01:00
|
|
|
progressBar.update(0,{status:'Started'})
|
|
|
|
progressBar.stop();
|
|
|
|
|
2022-12-02 12:44:14 +01:00
|
|
|
return container;
|
2022-12-01 21:53:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
}
|