Commit 6a39e6b0 authored by nanahira's avatar nanahira

proxy pool

parent 7c31177e
...@@ -6,4 +6,5 @@ accounts: ...@@ -6,4 +6,5 @@ accounts:
- email: 'test@example.com' - email: 'test@example.com'
password: 'wwww' password: 'wwww'
pro: false pro: false
loginType: 'default' loginType: 'default'
\ No newline at end of file proxies: []
\ No newline at end of file
...@@ -6,6 +6,7 @@ import { AragamiModule } from 'nestjs-aragami'; ...@@ -6,6 +6,7 @@ import { AragamiModule } from 'nestjs-aragami';
import { ChatgptService } from './chatgpt/chatgpt.service'; import { ChatgptService } from './chatgpt/chatgpt.service';
import { ConversationService } from './conversation/conversation.service'; import { ConversationService } from './conversation/conversation.service';
import { AuthService } from './auth/auth.service'; import { AuthService } from './auth/auth.service';
import { ProxyPoolService } from './proxy-pool/proxy-pool.service';
@Module({ @Module({
imports: [ imports: [
...@@ -31,6 +32,6 @@ import { AuthService } from './auth/auth.service'; ...@@ -31,6 +32,6 @@ import { AuthService } from './auth/auth.service';
}), }),
], ],
controllers: [AppController], controllers: [AppController],
providers: [ChatgptService, ConversationService, AuthService], providers: [ChatgptService, ConversationService, AuthService, ProxyPoolService],
}) })
export class AppModule {} export class AppModule {}
...@@ -15,6 +15,7 @@ import { BetterLock } from 'better-lock/dist/better_lock'; ...@@ -15,6 +15,7 @@ import { BetterLock } from 'better-lock/dist/better_lock';
import _ from 'lodash'; import _ from 'lodash';
import { BlankReturnMessageDto } from '../dto/ReturnMessage.dto'; import { BlankReturnMessageDto } from '../dto/ReturnMessage.dto';
import pTimeout from 'p-timeout'; import pTimeout from 'p-timeout';
import { ProxyPoolService } from '../proxy-pool/proxy-pool.service';
interface AccountState { interface AccountState {
email: string; email: string;
...@@ -31,6 +32,7 @@ export class ChatgptService ...@@ -31,6 +32,7 @@ export class ChatgptService
constructor( constructor(
private config: ConfigService, private config: ConfigService,
private conversationService: ConversationService, private conversationService: ConversationService,
private proxyPoolService: ProxyPoolService,
) { ) {
super('ChatGPT'); super('ChatGPT');
} }
...@@ -46,6 +48,7 @@ export class ChatgptService ...@@ -46,6 +48,7 @@ export class ChatgptService
isProAccount: !!account.pro, isProAccount: !!account.pro,
isGoogleLogin: account.loginType === 'google', isGoogleLogin: account.loginType === 'google',
isMicrosoftLogin: account.loginType === 'microsoft', isMicrosoftLogin: account.loginType === 'microsoft',
proxyServer: this.proxyPoolService.getProxy(),
}); });
try { try {
await pTimeout(instance.initSession(), 600000); await pTimeout(instance.initSession(), 600000);
...@@ -59,7 +62,7 @@ export class ChatgptService ...@@ -59,7 +62,7 @@ export class ChatgptService
} catch (e) { } catch (e) {
this.error(`Failed to initialize ChatGPT API for ${account.email}`, e); this.error(`Failed to initialize ChatGPT API for ${account.email}`, e);
await Promise.all([ await Promise.all([
new Promise((r) => setTimeout(r, 60000)), new Promise((r) => setTimeout(r, 5000)),
instance.closeSession().catch(), instance.closeSession().catch(),
]); ]);
return this.initAccount(account); 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 = { ...@@ -14,6 +14,7 @@ const defaultConfig = {
accounts: [] as OpenAIAccount[], accounts: [] as OpenAIAccount[],
redisUrl: '', redisUrl: '',
token: '', token: '',
proxies: [] as string[],
}; };
export type Config = typeof defaultConfig; 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