Skip to content

Commit

Permalink
release-v0.1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
aurorax-neo committed Apr 22, 2024
2 parents c1175df + 6b84d42 commit aeabdc9
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 46 deletions.
54 changes: 21 additions & 33 deletions Pool/Gpt35Pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,22 @@ func GetGpt35PoolInstance() *Gpt35Pool {
}
logger.Logger.Info(fmt.Sprint("PoolMaxCount: ", config.PoolMaxCount, ", AuthExpirationDate: ", config.AuthED, ", Init Pool..."))
// 定时刷新 Pool
go instance.timingUpdateGpt35Pool(60)
go instance.timingUpdateGpt35Pool(8)
})
return instance
}

func (G *Gpt35Pool) IsLiveGpt35(index int) bool {
//判断是否为空
if G.Gpt35s[index] == nil ||
G.Gpt35s[index].MaxUseCount <= 0 || //无可用次数
G.Gpt35s[index].ExpiresIn <= common.GetTimestampSecond(0) ||
G.Gpt35s[index].IsUpdating {
return false
}
return true
}

func (G *Gpt35Pool) GetGpt35(retry int) *chat.Gpt35 {
// 加锁
G.Lock.Lock()
Expand All @@ -64,6 +75,8 @@ func (G *Gpt35Pool) GetGpt35(retry int) *chat.Gpt35 {
G.Index = (G.Index + 1) % G.MaxCount
return &gpt35_
} else if retry > 0 { //无缓存或者缓存无效
// time
time.Sleep(time.Millisecond * 200)
// 释放锁 防止死锁
G.Lock.Unlock()
defer G.Lock.Lock()
Expand Down Expand Up @@ -94,58 +107,33 @@ func (G *Gpt35Pool) timingUpdateGpt35Pool(sec int) {
}
}

func (G *Gpt35Pool) IsLiveGpt35(index int) bool {
//判断是否为空
if G.Gpt35s[index] == nil || //空的
G.Gpt35s[index].MaxUseCount <= 0 || //无可用次数
G.Gpt35s[index].ExpiresIn <= common.GetTimestampSecond(0) ||
G.Gpt35s[index].IsUpdating {
return false
}
return true
}

func (G *Gpt35Pool) updateGpt35AtIndex(index int) bool {
func (G *Gpt35Pool) updateGpt35AtIndex(index int) {
if index < 0 || index >= len(G.Gpt35s) {
return false
}
if G.Gpt35s[index] != nil && G.Gpt35s[index].IsUpdating {
return false
return
}
if !G.IsLiveGpt35(index) {
// 标志 Gpt35 实例正在刷新
if G.Gpt35s[index] != nil {
// 标志 Gpt35 实例正在刷新
G.Gpt35s[index].IsUpdating = true
}
G.Gpt35s[index] = chat.NewGpt35()
// 标志 Gpt35 没有正在刷新
if G.Gpt35s[index] != nil {
G.Gpt35s[index].IsUpdating = false
}
return true
}
// 标志 Gpt35 没有正在刷新
if G.Gpt35s[index] != nil {
G.Gpt35s[index].IsUpdating = false
}
return false
}

func (G *Gpt35Pool) waitGpt35AtIndexUpdated(index int) {
// 加锁
G.Lock.Lock()
defer G.Lock.Unlock()
endTime := common.GetTimestampSecond(3)
// 等待 Gpt35 实例刷新完成
for {
for i := common.GetTimestampSecond(0); i < endTime; i = common.GetTimestampSecond(0) {
if G.Gpt35s[index] == nil || !G.Gpt35s[index].IsUpdating {
break
}
time.Sleep(time.Millisecond * 100)
time.Sleep(time.Millisecond * 200)
}
}

func (G *Gpt35Pool) updateGpt35Pool() {
for i := 0; i < G.MaxCount; i++ {
G.updateGpt35AtIndex(i)
time.Sleep(time.Millisecond * 8)
}
}
2 changes: 1 addition & 1 deletion RequestClient/RequestClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (

func GetInstance() *TlsClient {
clientOnce.Do(func() {
Instance = NewTlsClient(300, profiles.Safari_16_0)
Instance = NewTlsClient(300, profiles.Firefox_102)
})
return Instance
}
18 changes: 9 additions & 9 deletions chat/Gpt35.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/bogdanfinn/tls-client/profiles"
"github.com/google/uuid"
"io"
"strings"
)

const BaseUrl = "https://chat.openai.com"
Expand Down Expand Up @@ -53,15 +52,15 @@ func NewGpt35() *Gpt35 {
MaxUseCount: 1,
ExpiresIn: common.GetTimestampSecond(config.AuthED),
Session: &session{},
Ua: browser.Safari(),
Ua: browser.Firefox(),
Language: common.RandomLanguage(),
IsUpdating: false,
}
// 获取代理池
ProxyPoolInstance := ProxyPool.GetProxyPoolInstance()
// 如果代理池中有代理数大于 1 则使用 各自requestClient
if len(ProxyPoolInstance.Proxies) > 1 {
instance.RequestClient = RequestClient.NewTlsClient(300, profiles.Okhttp4Android13)
instance.RequestClient = RequestClient.NewTlsClient(300, profiles.Firefox_102)
} else {
instance.RequestClient = RequestClient.GetInstance()
}
Expand All @@ -72,18 +71,20 @@ func NewGpt35() *Gpt35 {
// 获取新的 session
err = instance.getNewSession()
if err != nil {
return nil
return &Gpt35{
MaxUseCount: 0,
ExpiresIn: 0,
IsUpdating: true,
}
}
return instance
}

func (G *Gpt35) getNewSession() error {
// 生成新的设备 ID
G.Session.OaiDeviceId = uuid.New().String()
// 设置请求体
body := strings.NewReader(`{"conversation_mode_kind":"primary_assistant"}`)
// 创建请求
request, err := G.NewRequest("POST", SessionUrl, body)
request, err := G.NewRequest("POST", SessionUrl, nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -116,7 +117,6 @@ func (G *Gpt35) NewRequest(method, url string, body io.Reader) (*fhttp.Request,
}
request.Header.Set("origin", common.GetOrigin(BaseUrl))
request.Header.Set("referer", common.GetOrigin(BaseUrl))
request.Header.Set("authority", common.GetOrigin(BaseUrl))
request.Header.Set("accept", "*/*")
request.Header.Set("cache-control", "no-cache")
request.Header.Set("content-type", "application/json")
Expand All @@ -125,8 +125,8 @@ func (G *Gpt35) NewRequest(method, url string, body io.Reader) (*fhttp.Request,
request.Header.Set("sec-fetch-dest", "empty")
request.Header.Set("sec-fetch-mode", "cors")
request.Header.Set("sec-fetch-site", "same-origin")
request.Header.Set("oai-language", "en-US")
request.Header.Set("accept-language", G.Language)
request.Header.Set("oai-language", G.Language)
request.Header.Set("User-Agent", G.Ua)
return request, nil
}
3 changes: 2 additions & 1 deletion common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ func RandomLanguage() string {
seed := time.Now().UnixNano()
rng := rand.New(rand.NewSource(seed))
// 语言列表
languages := []string{"af", "am", "ar-sa", "as", "az-Latn", "be", "bg", "bn-BD", "bn-IN", "bs", "ca", "ca-ES-valencia", "cs", "cy", "da", "de", "de-de", "el", "en-GB", "en-US", "es", "es-ES", "es-US", "es-MX", "et", "eu", "fa", "fi", "fil-Latn", "fr", "fr-FR", "fr-CA", "ga", "gd-Latn", "gl", "gu", "ha-Latn", "he", "hi", "hr", "hu", "hy", "id", "ig-Latn", "is", "it", "it-it", "ja", "ka", "kk", "km", "kn", "ko", "kok", "ku-Arab", "ky-Cyrl", "lb", "lt", "lv", "mi-Latn", "mk", "ml", "mn-Cyrl", "mr", "ms", "mt", "nb", "ne", "nl", "nl-BE", "nn", "nso", "or", "pa", "pa-Arab", "pl", "prs-Arab", "pt-BR", "pt-PT", "qut-Latn", "quz", "ro", "ru", "rw", "sd-Arab", "si", "sk", "sl", "sq", "sr-Cyrl-BA", "sr-Cyrl-RS", "sr-Latn-RS", "sv", "sw", "ta", "te", "tg-Cyrl", "th", "ti", "tk-Latn", "tn", "tr", "tt-Cyrl", "ug-Arab", "uk", "ur", "uz-Latn", "vi", "wo", "xh", "yo-Latn", "zh-Hans", "zh-Hant", "zu"}
//languages := []string{"af", "am", "ar-sa", "as", "az-Latn", "be", "bg", "bn-BD", "bn-IN", "bs", "ca", "ca-ES-valencia", "cs", "cy", "da", "de", "de-de", "el", "en-GB", "en-US", "es", "es-ES", "es-US", "es-MX", "et", "eu", "fa", "fi", "fil-Latn", "fr", "fr-FR", "fr-CA", "ga", "gd-Latn", "gl", "gu", "ha-Latn", "he", "hi", "hr", "hu", "hy", "id", "ig-Latn", "is", "it", "it-it", "ja", "ka", "kk", "km", "kn", "ko", "kok", "ku-Arab", "ky-Cyrl", "lb", "lt", "lv", "mi-Latn", "mk", "ml", "mn-Cyrl", "mr", "ms", "mt", "nb", "ne", "nl", "nl-BE", "nn", "nso", "or", "pa", "pa-Arab", "pl", "prs-Arab", "pt-BR", "pt-PT", "qut-Latn", "quz", "ro", "ru", "rw", "sd-Arab", "si", "sk", "sl", "sq", "sr-Cyrl-BA", "sr-Cyrl-RS", "sr-Latn-RS", "sv", "sw", "ta", "te", "tg-Cyrl", "th", "ti", "tk-Latn", "tn", "tr", "tt-Cyrl", "ug-Arab", "uk", "ur", "uz-Latn", "vi", "wo", "xh", "yo-Latn", "zh-Hans", "zh-Hant", "zu"}
languages := []string{"en-US,en;q=0.9"}
// 随机选择一个语言
randomIndex := rng.Intn(len(languages))
return languages[randomIndex]
Expand Down
4 changes: 2 additions & 2 deletions service/v1Chat/Gpt35.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func Gpt35(c *gin.Context, apiReq *reqmodel.ApiReq) {
response, err := ChatGpt35.RequestClient.Do(request)
if err != nil {
errStr := "RequestClient Do error"
logger.Logger.Error(fmt.Sprint(errStr, err))
logger.Logger.Error(fmt.Sprint(errStr, " ", err))
common.ErrorResponse(c, http.StatusInternalServerError, errStr, err)
return
}
Expand All @@ -65,7 +65,7 @@ func Gpt35(c *gin.Context, apiReq *reqmodel.ApiReq) {
}(response.Body)
if response.StatusCode != http.StatusOK {
errStr := "Request error"
logger.Logger.Error(fmt.Sprint(errStr, response.StatusCode))
logger.Logger.Error(fmt.Sprint(errStr, " ", response.StatusCode))
common.ErrorResponse(c, response.StatusCode, errStr, nil)
return
}
Expand Down

0 comments on commit aeabdc9

Please sign in to comment.