Skip to content

Commit

Permalink
feat: Centralized configuration configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangsx committed May 9, 2023
1 parent 4324714 commit 2731127
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 26 deletions.
4 changes: 2 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ const router = new Router();
const errorHandler = async (ctx: Context, next: Next) => {
try {
await next();
} catch (err:any) {
} catch (err: any) {
console.error(err);
ctx.body = JSON.stringify(err);
ctx.res.end();
}
};
app.use(errorHandler);
app.use(bodyParser());
const chatModel = new ChatModelFactory({proxy: process.env.https_proxy || process.env.http_proxy});
const chatModel = new ChatModelFactory();

interface AskReq {
prompt: string;
Expand Down
1 change: 0 additions & 1 deletion model/base.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {Stream} from "stream";

export interface ChatOptions {
proxy?: string;
}

export interface Response {
Expand Down
14 changes: 5 additions & 9 deletions model/forefront/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
//@ts-ignore
import UserAgent from 'user-agents';
import tlsClient from 'tls-client';

import {Chat, ChatOptions, Request, Response, ResponseStream} from "../base";
import {CreateEmail, TempEmailType, TempMailMessage} from '../../utils/emailFactory';
import axios, {AxiosInstance, AxiosRequestConfig, CreateAxiosDefaults} from "axios";
import {AxiosInstance, AxiosRequestConfig, CreateAxiosDefaults} from "axios";
import {v4} from "uuid";
import es from "event-stream";
import {encryptWithAes256Cbc, parseJSON} from "../../utils";
import {CreateAxiosProxy, CreateTlsProxy} from "../../utils/proxyAgent";

interface ForefrontRequest extends Request {
options?: {
Expand Down Expand Up @@ -143,7 +142,7 @@ export class Forefront extends Chat {
return;
}
const [{delta: {content}}] = data.choices;
cb(null, content||'');
cb(null, content || '');
}))
return {text: stream};
} catch (e: any) {
Expand Down Expand Up @@ -174,7 +173,7 @@ export class Forefront extends Chat {
async initClient() {
let hisSession = await this.createToken();
this.session = hisSession;
this.client = axios.create({
this.client = CreateAxiosProxy({
headers: {
'authority': 'chat-server.tenant-forefront-default.knative.chi.coreweave.com',
'accept': '*/*',
Expand All @@ -199,14 +198,11 @@ export class Forefront extends Chat {
const mailbox = CreateEmail(TempEmailType.TempEmail44);
const mailAddress = await mailbox.getMailAddress();
const agent = new UserAgent().toString();
const session = new tlsClient.Session({clientIdentifier: 'chrome_108'});
const session = CreateTlsProxy({clientIdentifier: 'chrome_108'});
session.headers = {
origin: 'https://accounts.forefront.ai',
'user-agent': agent, // Replace with actual random user agent
}
if (this.options?.proxy) {
session.proxy = this.options.proxy;
}
const signEmailRes = await session.post('https://clerk.forefront.ai/v1/client/sign_ups?_clerk_js_version=4.39.0',
{data: {'email_address': mailAddress}});
const traceToken = (signEmailRes.data as any)?.response?.id;
Expand Down
11 changes: 4 additions & 7 deletions model/you/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {v4 as uuidv4} from 'uuid';
//@ts-ignore
import UserAgent from 'user-agents';
import tlsClient from 'tls-client';
import {Session} from "tls-client/dist/esm/sessions";
import {Params} from "tls-client/dist/esm/types";
import {parseJSON, toEventCB, toEventStream} from "../../utils";
import {Chat, ChatOptions, Request, Response, ResponseStream} from "../base";
import {CreateTlsProxy} from "../../utils/proxyAgent";

const userAgent = new UserAgent();

Expand Down Expand Up @@ -67,11 +67,8 @@ export class You extends Chat {

constructor(props?: ChatOptions) {
super(props);
this.session = new tlsClient.Session({clientIdentifier: 'chrome_108'});
this.session = CreateTlsProxy({clientIdentifier: 'chrome_108'});
this.session.headers = this.getHeaders();
if (this.options?.proxy) {
this.session.proxy = this.options.proxy;
}
}

private async request(req: Request) {
Expand Down Expand Up @@ -127,14 +124,14 @@ export class You extends Chat {
let obj: any;
switch (eventName) {
case 'youChatToken':
obj = parseJSON(data,{}) as any;
obj = parseJSON(data, {}) as any;
res.text += obj.youChatToken;
break;
case 'done':
resolve(res);
return;
default:
obj = parseJSON(data,{}) as any;
obj = parseJSON(data, {}) as any;
res.other[eventName] = obj;
return;
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,21 @@
"author": "xiangsx",
"license": "ISC",
"devDependencies": {
"@types/axios": "^0.14.0",
"@types/event-stream": "^4.0.0",
"@types/koa": "^2.13.6",
"@types/koa-bodyparser": "^4.3.10",
"@types/koa-router": "^7.4.4",
"@types/node": "^18.16.3",
"@types/uuid": "^9.0.1",
"@types/axios": "^0.14.0",
"ts-node": "^10.9.1",
"typescript": "^5.0.4"
},
"dependencies": {
"axios": "^1.4.0",
"event-stream": "^4.0.1",
"fake-useragent": "^1.0.1",
"http-proxy-agent": "^6.0.1",
"https-proxy-agent": "^5.0.1",
"koa": "^2.14.2",
"koa-bodyparser": "^4.4.0",
Expand Down
11 changes: 6 additions & 5 deletions utils/emailFactory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import axios, {AxiosInstance, AxiosRequestConfig, CreateAxiosDefaults} from 'axios';
import {AxiosInstance, AxiosRequestConfig, CreateAxiosDefaults} from 'axios';
import {md5, randomStr} from "./index";
import {CreateAxiosProxy} from "./proxyAgent";

export enum TempEmailType {
// need credit card https://rapidapi.com/Privatix/api/temp-mail
Expand Down Expand Up @@ -73,7 +74,7 @@ class TempMail extends BaseEmail {
if (!apikey) {
throw new Error('Need apikey for TempMail')
}
this.client = axios.create({
this.client = CreateAxiosProxy({
baseURL: 'https://privatix-temp-mail-v1.p.rapidapi.com/request/',
headers: {
'X-RapidAPI-Key': apikey,
Expand All @@ -95,7 +96,7 @@ class TempMail extends BaseEmail {
const itl = setInterval(async () => {
const response = await this.client.get(`/mail/id/${mailID}`);
if (response.data && response.data.length > 0) {
resolve(response.data.map((item:any) => ({...item, content: item.mail_html})));
resolve(response.data.map((item: any) => ({...item, content: item.mail_html})));
clearInterval(itl);
return;
}
Expand Down Expand Up @@ -130,7 +131,7 @@ class TempMail44 extends BaseEmail {
if (!apikey) {
throw new Error('Need apikey for TempMail')
}
this.client = axios.create({
this.client = CreateAxiosProxy({
baseURL: 'https://temp-mail44.p.rapidapi.com/api/v3/email/',
headers: {
'X-RapidAPI-Key': apikey,
Expand All @@ -155,7 +156,7 @@ class TempMail44 extends BaseEmail {
const itl = setInterval(async () => {
const response = await this.client.get(`/${this.address}/messages`);
if (response.data && response.data.length > 0) {
resolve(response.data.map((item:any) => ({...item, content: item.body_html})));
resolve(response.data.map((item: any) => ({...item, content: item.body_html})));
clearInterval(itl);
return;
}
Expand Down
25 changes: 25 additions & 0 deletions utils/proxyAgent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import axios, {AxiosInstance, CreateAxiosDefaults} from "axios";
import HttpsProxyAgent from "https-proxy-agent";
import {SessionConstructorOptions} from "tls-client/dist/esm/types";
import {Session} from "tls-client/dist/esm/sessions";
import tlsClient from "tls-client";

export function CreateAxiosProxy(config: CreateAxiosDefaults, proxy?: string): AxiosInstance {
const createConfig = {...config};
const useProxy = process.env.http_proxy || proxy;
if (useProxy) {
createConfig.proxy = false;
createConfig.httpAgent = HttpsProxyAgent(useProxy);
createConfig.httpsAgent = HttpsProxyAgent(useProxy);
}
return axios.create(createConfig);
}

export function CreateTlsProxy(config: SessionConstructorOptions, proxy?: string): Session {
const client = new tlsClient.Session(config);
const useProxy = process.env.http_proxy || proxy;
if (useProxy) {
client.proxy = useProxy;
}
return client;
}
17 changes: 16 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,13 @@ agent-base@6:
dependencies:
debug "4"

agent-base@^7.0.1:
version "7.0.1"
resolved "https://registry.npmjs.org/agent-base/-/agent-base-7.0.1.tgz#ec4df4e6406bdf71490ade302ea45f86bf365ea9"
integrity sha512-V9to8gr2GK7eA+xskWGAFUX/TLSQKuH2TI06c/jGLL6yLp3oEjtnqM7a5tPV9fC1rabLeAgThZeBwsYX+WWHpw==
dependencies:
debug "^4.3.4"

arg@^4.1.0:
version "4.1.3"
resolved "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
Expand Down Expand Up @@ -331,7 +338,7 @@ create-require@^1.1.0:
resolved "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==

debug@4, debug@^4.1.1, debug@^4.3.2:
debug@4, debug@^4.1.1, debug@^4.3.2, debug@^4.3.4:
version "4.3.4"
resolved "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz#1319f6579357f2338d3337d2cdd4914bb5dcc865"
integrity sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==
Expand Down Expand Up @@ -548,6 +555,14 @@ http-errors@^1.6.3, http-errors@~1.8.0:
statuses ">= 1.5.0 < 2"
toidentifier "1.0.1"

http-proxy-agent@^6.0.1:
version "6.0.1"
resolved "https://registry.npmjs.org/http-proxy-agent/-/http-proxy-agent-6.0.1.tgz#c18aa52daa625a9bd00243f57252fd44e86ff40b"
integrity sha512-rD8wrfJHbnVll9lkIpQH3vDbKON1Ssciggwydom/r89HLBXEqdMhL6wx7QF5WePDPSr0OdoztdXoojbrXadG5Q==
dependencies:
agent-base "^7.0.1"
debug "^4.3.4"

https-proxy-agent@^5.0.1:
version "5.0.1"
resolved "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-5.0.1.tgz#c59ef224a04fe8b754f3db0063a25ea30d0005d6"
Expand Down

0 comments on commit 2731127

Please sign in to comment.