mirror of
https://github.com/TheNetsky/Microsoft-Rewards-Script.git
synced 2026-01-11 02:46:17 +00:00
@@ -2,6 +2,7 @@ import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'
|
|||||||
import axiosRetry from 'axios-retry'
|
import axiosRetry from 'axios-retry'
|
||||||
import { HttpProxyAgent } from 'http-proxy-agent'
|
import { HttpProxyAgent } from 'http-proxy-agent'
|
||||||
import { HttpsProxyAgent } from 'https-proxy-agent'
|
import { HttpsProxyAgent } from 'https-proxy-agent'
|
||||||
|
import { URL } from 'url'
|
||||||
import type { AccountProxy } from '../interface/Account'
|
import type { AccountProxy } from '../interface/Account'
|
||||||
|
|
||||||
class AxiosClient {
|
class AxiosClient {
|
||||||
@@ -15,7 +16,6 @@ class AxiosClient {
|
|||||||
timeout: 20000
|
timeout: 20000
|
||||||
})
|
})
|
||||||
|
|
||||||
// Configure proxy agent if available
|
|
||||||
if (this.account.url && this.account.proxyAxios) {
|
if (this.account.url && this.account.proxyAxios) {
|
||||||
const agent = this.getAgentForProxy(this.account)
|
const agent = this.getAgentForProxy(this.account)
|
||||||
this.instance.defaults.httpAgent = agent
|
this.instance.defaults.httpAgent = agent
|
||||||
@@ -23,11 +23,10 @@ class AxiosClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
axiosRetry(this.instance, {
|
axiosRetry(this.instance, {
|
||||||
retries: 5, // Retry 5 times
|
retries: 5,
|
||||||
retryDelay: axiosRetry.exponentialDelay,
|
retryDelay: axiosRetry.exponentialDelay,
|
||||||
shouldResetTimeout: true,
|
shouldResetTimeout: true,
|
||||||
retryCondition: error => {
|
retryCondition: error => {
|
||||||
// Retry on: Network errors, 429 (Too Many Requests), 5xx server errors
|
|
||||||
if (axiosRetry.isNetworkError(error)) return true
|
if (axiosRetry.isNetworkError(error)) return true
|
||||||
if (!error.response) return true
|
if (!error.response) return true
|
||||||
|
|
||||||
@@ -38,15 +37,38 @@ class AxiosClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private getAgentForProxy(proxyConfig: AccountProxy): HttpProxyAgent<string> | HttpsProxyAgent<string> {
|
private getAgentForProxy(proxyConfig: AccountProxy): HttpProxyAgent<string> | HttpsProxyAgent<string> {
|
||||||
const { url, port } = proxyConfig
|
const { url: baseUrl, port, username, password } = proxyConfig
|
||||||
|
|
||||||
switch (true) {
|
let urlObj: URL
|
||||||
case url.startsWith('http://'):
|
try {
|
||||||
return new HttpProxyAgent(`${url}:${port}`)
|
urlObj = new URL(baseUrl)
|
||||||
case url.startsWith('https://'):
|
} catch (e) {
|
||||||
return new HttpsProxyAgent(`${url}:${port}`)
|
try {
|
||||||
|
urlObj = new URL(`http://${baseUrl}`)
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error(`Invalid proxy URL format: ${baseUrl}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const protocol = urlObj.protocol.toLowerCase()
|
||||||
|
let proxyUrl: string
|
||||||
|
|
||||||
|
if (username && password) {
|
||||||
|
urlObj.username = encodeURIComponent(username)
|
||||||
|
urlObj.password = encodeURIComponent(password)
|
||||||
|
urlObj.port = port.toString()
|
||||||
|
proxyUrl = urlObj.toString()
|
||||||
|
} else {
|
||||||
|
proxyUrl = `${protocol}//${urlObj.hostname}:${port}`
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (protocol) {
|
||||||
|
case 'http:':
|
||||||
|
return new HttpProxyAgent(proxyUrl)
|
||||||
|
case 'https:':
|
||||||
|
return new HttpsProxyAgent(proxyUrl)
|
||||||
default:
|
default:
|
||||||
throw new Error(`Unsupported proxy protocol: ${url}, only HTTP(S) is supported!`)
|
throw new Error(`Unsupported proxy protocol: ${protocol}. Only HTTP(S) is supported!`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user