140 lines
4.7 KiB
TypeScript
140 lines
4.7 KiB
TypeScript
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";
|
|
|
|
import { sleep } from "telegram/Helpers";
|
|
import { AccountInterface } from "./interfaces/databaseInterface";
|
|
import * as telegram from 'telegram';
|
|
import axios from "axios";
|
|
import ProgessBar from "progress"
|
|
|
|
|
|
|
|
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;
|
|
}
|
|
|
|
export const getIP=async function (proxyPort: number): Promise<string | null> {
|
|
// const res=await shttps.get({
|
|
// url: 'icanhazip.com',
|
|
// agentOptions: {
|
|
// socksPort: socksPort
|
|
// }
|
|
// })
|
|
// console.log(res.body)
|
|
const {data}=await axios.get('http://api.ipify.org',{
|
|
proxy:{
|
|
host:'127.0.0.1',
|
|
port:proxyPort
|
|
}
|
|
});
|
|
if (data.trim()==""){
|
|
return null
|
|
}
|
|
return data
|
|
|
|
}
|
|
export const waitNewMessages=async function(client: telegram.TelegramClient,worker: AccountInterface,chatEntity: telegram.Api.Chat | telegram.Api.User,idOfLastMessage: number,timeout: number=20): Promise<void>{
|
|
|
|
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`);
|
|
}
|
|
|
|
export const startNewTorDocker=async function(proxyPort: number,socksPort: number,controlPort: number,timeout: number=200): Promise<Docker.Container>{
|
|
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:{
|
|
Interval:1.5*1_000_000_000,
|
|
Timeout:15*1_000_000_000,
|
|
StartPeriod:50*1_000_000_000,
|
|
// Test:['CMD_SHELL',`curl -sx localhost:8118 'https://check.torproject.org/' | grep -qm1 Congratulations`]
|
|
}
|
|
|
|
}
|
|
logger.debug(`Creating container with this ports ${proxyPort} ${socksPort} ${controlPort}`);
|
|
const container=await docker.createContainer(options);
|
|
|
|
await container.start();
|
|
const start_time=(new Date());
|
|
const progressBar=new ProgessBar(`Timeout [:bar] :current/:total sec`,{
|
|
total:timeout/2,
|
|
width: 50,
|
|
complete: '❌',
|
|
incomplete: '✔️',
|
|
clear: true,
|
|
});
|
|
while ((await container.inspect())!.State.Health!.Status!='healthy'){
|
|
const state=(await container.inspect())!.State.Health!.Status
|
|
const current_time=(new Date());
|
|
if ((+current_time-+start_time)/1000>=timeout){
|
|
await container.kill();
|
|
progressBar.terminate();
|
|
throw new Error(`Docker ${container.id} is broken`);
|
|
}
|
|
if (state=='unhealthy'){
|
|
await container.kill();
|
|
progressBar.terminate();
|
|
logger.warn(`Docker ${container.id} is unhealthy.Recreating...`);
|
|
return await startNewTorDocker(proxyPort,socksPort,controlPort,timeout/2);
|
|
}
|
|
progressBar.tick();
|
|
await sleep(2000);
|
|
}
|
|
progressBar.terminate();
|
|
return container;
|
|
|
|
|
|
}
|