Commit 6a39e6b0 authored by nanahira's avatar nanahira

proxy pool

parent 7c31177e
......@@ -6,4 +6,5 @@ accounts:
- email: 'test@example.com'
password: 'wwww'
pro: false
loginType: 'default'
\ No newline at end of file
loginType: 'default'
proxies: []
\ No newline at end of file
......@@ -6,6 +6,7 @@ import { AragamiModule } from 'nestjs-aragami';
import { ChatgptService } from './chatgpt/chatgpt.service';
import { ConversationService } from './conversation/conversation.service';
import { AuthService } from './auth/auth.service';
import { ProxyPoolService } from './proxy-pool/proxy-pool.service';
@Module({
imports: [
......@@ -31,6 +32,6 @@ import { AuthService } from './auth/auth.service';
}),
],
controllers: [AppController],
providers: [ChatgptService, ConversationService, AuthService],
providers: [ChatgptService, ConversationService, AuthService, ProxyPoolService],
})
export class AppModule {}
......@@ -15,6 +15,7 @@ import { BetterLock } from 'better-lock/dist/better_lock';
import _ from 'lodash';
import { BlankReturnMessageDto } from '../dto/ReturnMessage.dto';
import pTimeout from 'p-timeout';
import { ProxyPoolService } from '../proxy-pool/proxy-pool.service';
interface AccountState {
email: string;
......@@ -31,6 +32,7 @@ export class ChatgptService
constructor(
private config: ConfigService,
private conversationService: ConversationService,
private proxyPoolService: ProxyPoolService,
) {
super('ChatGPT');
}
......@@ -46,6 +48,7 @@ export class ChatgptService
isProAccount: !!account.pro,
isGoogleLogin: account.loginType === 'google',
isMicrosoftLogin: account.loginType === 'microsoft',
proxyServer: this.proxyPoolService.getProxy(),
});
try {
await pTimeout(instance.initSession(), 600000);
......@@ -59,7 +62,7 @@ export class ChatgptService
} catch (e) {
this.error(`Failed to initialize ChatGPT API for ${account.email}`, e);
await Promise.all([
new Promise((r) => setTimeout(r, 60000)),
new Promise((r) => setTimeout(r, 5000)),
instance.closeSession().catch(),
]);
return this.initAccount(account);
......
import { Test, TestingModule } from '@nestjs/testing';
import { ProxyPoolService } from './proxy-pool.service';
describe('ProxyPoolService', () => {
let service: ProxyPoolService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [ProxyPoolService],
}).compile();
service = module.get<ProxyPoolService>(ProxyPoolService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
@Injectable()
export class ProxyPoolService {
private pointer = 0;
private proxies = this.config.get<string[]>('proxies');
constructor(private config: ConfigService) {}
getProxy() {
if (!this.proxies?.length) {
return;
}
if (this.pointer >= this.proxies.length) {
this.pointer = 0;
}
return this.proxies[this.pointer++];
}
}
......@@ -14,6 +14,7 @@ const defaultConfig = {
accounts: [] as OpenAIAccount[],
redisUrl: '',
token: '',
proxies: [] as string[],
};
export type Config = typeof defaultConfig;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment