Skip to content

Commit

Permalink
User Agent Management
Browse files Browse the repository at this point in the history
  • Loading branch information
TOR968 committed Dec 20, 2024
1 parent 782ec42 commit 1ca5d40
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 27 deletions.
59 changes: 42 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ const axios = require("axios");
const fs = require("fs").promises;
const querystring = require("querystring");
const { HttpsProxyAgent } = require("https-proxy-agent");
const { SocksProxyAgent } = require("socks-proxy-agent");
const UserAgentManager = require("./userAgentManager");
const userAgentManager = new UserAgentManager();

const BASE_URL = "https://api.pitchtalk.app/v1/api";

Expand Down Expand Up @@ -137,7 +140,7 @@ const setMaxTimer = (timer) => {
maxTimer = maxTimer < timer ? timer : maxTimer;
};

const processAccount = async (hash, proxy) => {
const processAccount = async (hash, proxy, userAgent = null) => {
try {
const parsedParams = querystring.parse(hash);
const hashData = JSON.parse(decodeURIComponent(parsedParams.user));
Expand All @@ -149,7 +152,7 @@ const processAccount = async (hash, proxy) => {
photoUrl: "",
};

const authResponse = await postRequest("/auth", authBody, proxy);
const authResponse = await postRequest("/auth", authBody, proxy, userAgent);
const accessToken = authResponse.accessToken;
if (!accessToken) {
throw new Error(authResponse?.error?.message || "Unknown error");
Expand Down Expand Up @@ -270,29 +273,39 @@ const sleep = async (ms) => {
process.stdout.clearLine(0);
process.stdout.write(`${colors.yellow}playing 100%\n`);
};

const createApiInstance = (accessToken, proxy, user) => {
const instance = axios.create({
const createApiInstance = (accessToken, proxy, user, userAgent = null) => {
const config = {
baseURL: BASE_URL,
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${accessToken}`,
origin: "https://webapp.pitchtalk.app",
priority: "u=1, i",
referer: "https://webapp.pitchtalk.app/",
"sec-ch-ua":
'"Microsoft Edge";v="129", "Not=A?Brand";v="8", "Chromium";v="129", "Microsoft Edge WebView2";v="129"',
"sec-ch-ua": '"Microsoft Edge";v="129", "Not=A?Brand";v="8", "Chromium";v="129", "Microsoft Edge WebView2";v="129"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "Windows",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-site",
"user-agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0",
"x-telegram-hash": user,
},
...(proxy && { httpsAgent: new HttpsProxyAgent(proxy) }),
});
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0",
"x-telegram-hash": user
}
};

if (userAgent) {
config.headers["User-Agent"] = userAgent;
}

if (proxy) {
if (proxy.startsWith("socks4://") || proxy.startsWith("socks5://")) {
config.httpsAgent = new SocksProxyAgent(proxy);
} else {
config.httpsAgent = new HttpsProxyAgent(proxy);
}
}

const instance = axios.create(config);

instance.interceptors.response.use(
(response) => response.data,
Expand All @@ -315,7 +328,7 @@ const getProxies = async () => {
}
};

const postRequest = async (endpoint, body = {}, proxy = null) => {
const postRequest = async (endpoint, body = {}, proxy = null, userAgent) => {
const config = {
headers: {
"Content-Type": "application/json",
Expand All @@ -333,9 +346,20 @@ const postRequest = async (endpoint, body = {}, proxy = null) => {
"user-agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0",
},
...(proxy && { httpsAgent: new HttpsProxyAgent(proxy) }),
};

if (userAgent) {
config.headers["User-Agent"] = userAgent;
}

if (proxy) {
if (proxy.startsWith("socks4://") || proxy.startsWith("socks5://")) {
config.httpsAgent = new SocksProxyAgent(proxy);
} else {
config.httpsAgent = new HttpsProxyAgent(proxy);
}
}

try {
const response = await axios.post(`${BASE_URL}${endpoint}`, body, config);
return response.data;
Expand Down Expand Up @@ -389,7 +413,8 @@ const main = async () => {
console.log(
`${colors.blue}--- Start processing account ${i + 1} | proxy: ${proxies[i] || null} ---${colors.reset} `
);
await processAccount(hashes[i].trim(), proxies[i] || null);
const userAgent = userAgentManager.getUserAgent(hashes[i]);
await processAccount(hashes[i].trim(), proxies[i] || null, userAgent);
console.log(`${colors.blue}--- Finished processing account ${i + 1} ---${colors.reset}`);
}

Expand Down
75 changes: 68 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"dependencies": {
"axios": "^1.7.7",
"https-proxy-agent": "^7.0.5"
"https-proxy-agent": "^7.0.5",
"socks-proxy-agent": "^8.0.5"
}
}
6 changes: 4 additions & 2 deletions proxy-example.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
http://username:password@host:port
http://username:password@host2:port2
http://user:pass@host:port
https://user:pass@host:port
socks4://user:pass@host:port
socks5://user:pass@host:port

1 proxy 1 user
IF YOU DO NOT NEED TO USE A PROXY, JUST DON'T FILL IN THE proxy.txt FILE.
79 changes: 79 additions & 0 deletions userAgentManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const fs = require("fs");
const path = require("path");
const querystring = require("querystring");

class UserAgentManager {
constructor(
tokensFile = "data.txt",
userAgentsFile = "user_agents.json",
userAgentsListFile = "user_agents_list.txt"
) {
this.tokensFile = tokensFile;
this.userAgentsFile = userAgentsFile;
this.userAgentsListFile = userAgentsListFile;
}

readFileLines(filePath) {
try {
return fs
.readFileSync(filePath, "utf-8")
.split("\n")
.filter((line) => line.trim() !== "");
} catch (error) {
console.error(`File reading error ${filePath}: ${error.message}`);
return [];
}
}

generateUserAgent(availableUserAgents) {
if (availableUserAgents.length === 0) {
return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36";
}

const randomIndex = Math.floor(Math.random() * availableUserAgents.length);
return availableUserAgents[randomIndex].trim();
}

extractFirstName(initData) {
try {
return JSON.parse(decodeURIComponent(querystring.parse(initData).user))?.first_name;
} catch (error) {
console.error("Name extraction error:", error);
return null;
}
}

initializeUserAgents() {
const tokens = this.readFileLines(this.tokensFile);
const availableUserAgents = this.readFileLines(this.userAgentsListFile);

let userAgents = {};
if (fs.existsSync(this.userAgentsFile)) {
try {
userAgents = JSON.parse(fs.readFileSync(this.userAgentsFile, "utf-8"));
} catch (error) {
console.warn("Failure to read existing user_agents.json, creation of a new one");
}
}

tokens.forEach((token) => {
const firstName = this.extractFirstName(token);
if (firstName && !userAgents[firstName]) {
userAgents[firstName] = this.generateUserAgent(availableUserAgents);
}
});

fs.writeFileSync(this.userAgentsFile, JSON.stringify(userAgents, null, 2), "utf-8");

return userAgents;
}

getUserAgent(token) {
const userAgents = this.initializeUserAgents();
const firstName = this.extractFirstName(token);

return firstName ? userAgents[firstName] : null;
}
}

module.exports = UserAgentManager;
2 changes: 2 additions & 0 deletions user_agents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
59 changes: 59 additions & 0 deletions user_agents_list.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:132.0) Gecko/20100101 Firefox/132.0
Mozilla/5.0 (Macintosh; Intel Mac OS X 14.7; rv:132.0) Gecko/20100101 Firefox/132.0
Mozilla/5.0 (X11; Linux i686; rv:132.0) Gecko/20100101 Firefox/132.0
Mozilla/5.0 (X11; Linux x86_64; rv:132.0) Gecko/20100101 Firefox/132.0
Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:132.0) Gecko/20100101 Firefox/132.0
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:132.0) Gecko/20100101 Firefox/132.0
Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:132.0) Gecko/20100101 Firefox/132.0
Mozilla/5.0 (Macintosh; Intel Mac OS X 14_7_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 Safari/605.1.15
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)
Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0)
Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)
Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)
Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko
Mozilla/5.0 (Windows NT 6.2; Trident/7.0; rv:11.0) like Gecko
Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko
Mozilla/5.0 (Windows NT 10.0; Trident/7.0; rv:11.0) like Gecko
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/130.0.2849.80
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/130.0.2849.80
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 OPR/114.0.0.0
Mozilla/5.0 (Windows NT 10.0; WOW64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 OPR/114.0.0.0
Mozilla/5.0 (Macintosh; Intel Mac OS X 14_7_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 OPR/114.0.0.0
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 OPR/114.0.0.0
Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Vivaldi/7.0.3495.14
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Vivaldi/7.0.3495.14
Mozilla/5.0 (Macintosh; Intel Mac OS X 14_7_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Vivaldi/7.0.3495.14
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Vivaldi/7.0.3495.14
Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Vivaldi/7.0.3495.14
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 YaBrowser/24.10.1.669 Yowser/2.5 Safari/537.36
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 YaBrowser/24.10.1.669 Yowser/2.5 Safari/537.36
Mozilla/5.0 (iPhone; CPU iPhone OS 17_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/131.0.6778.73 Mobile/15E148 Safari/604.1
Mozilla/5.0 (iPad; CPU OS 17_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/131.0.6778.73 Mobile/15E148 Safari/604.1
Mozilla/5.0 (iPod; CPU iPhone OS 17_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/131.0.6778.73 Mobile/15E148 Safari/604.1
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.6778.39 Mobile Safari/537.36
Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/132.0 Mobile/15E148 Safari/605.1.15
Mozilla/5.0 (iPad; CPU OS 14_7_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/132.0 Mobile/15E148 Safari/605.1.15
Mozilla/5.0 (iPod touch; CPU iPhone OS 14_7_1 like Mac OS X) AppleWebKit/604.5.6 (KHTML, like Gecko) FxiOS/132.0 Mobile/15E148 Safari/605.1.15
Mozilla/5.0 (Android 15; Mobile; rv:132.0) Gecko/132.0 Firefox/132.0
Mozilla/5.0 (Android 15; Mobile; LG-M255; rv:132.0) Gecko/132.0 Firefox/132.0
Mozilla/5.0 (iPhone; CPU iPhone OS 17_7_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 Mobile/15E148 Safari/604.1
Mozilla/5.0 (iPad; CPU OS 17_7_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 Mobile/15E148 Safari/604.1
Mozilla/5.0 (iPod touch; CPU iPhone 17_7_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 Mobile/15E148 Safari/604.1
Mozilla/5.0 (Linux; Android 10; HD1913) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.6778.39 Mobile Safari/537.36 EdgA/130.0.2849.80
Mozilla/5.0 (Linux; Android 10; SM-G973F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.6778.39 Mobile Safari/537.36 EdgA/130.0.2849.80
Mozilla/5.0 (Linux; Android 10; Pixel 3 XL) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.6778.39 Mobile Safari/537.36 EdgA/130.0.2849.80
Mozilla/5.0 (Linux; Android 10; ONEPLUS A6003) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.6778.39 Mobile Safari/537.36 EdgA/130.0.2849.80
Mozilla/5.0 (iPhone; CPU iPhone OS 17_7_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 EdgiOS/130.2849.80 Mobile/15E148 Safari/605.1.15
Mozilla/5.0 (Windows Mobile 10; Android 10.0; Microsoft; Lumia 950XL) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.36 Edge/40.15254.603
Mozilla/5.0 (Linux; Android 10; VOG-L29) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.6778.39 Mobile Safari/537.36 OPR/76.2.4027.7337
Mozilla/5.0 (Linux; Android 10; SM-G970F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.6778.39 Mobile Safari/537.36 OPR/76.2.4027.73374
Mozilla/5.0 (Linux; Android 10; SM-N975F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.6778.39 Mobile Safari/537.36 OPR/76.2.4027.73374
Mozilla/5.0 (iPhone; CPU iPhone OS 17_7_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 YaBrowser/24.10.5.295 Mobile/15E148 Safari/604.1
Mozilla/5.0 (iPad; CPU OS 17_7_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 YaBrowser/24.10.5.295 Mobile/15E148 Safari/605.1
Mozilla/5.0 (iPod touch; CPU iPhone 17_7_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 YaBrowser/24.10.5.295 Mobile/15E148 Safari/605.1
Mozilla/5.0 (Linux; arm_64; Android 15; SM-G965F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.6778.39 YaBrowser/24.10.4.98 Mobile Safari/537.36

0 comments on commit 1ca5d40

Please sign in to comment.