Skip to content
This repository has been archived by the owner on Aug 10, 2023. It is now read-only.

Commit

Permalink
proxy loop (rate limit)
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio committed Apr 18, 2023
1 parent ed2b045 commit 937bb23
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion internal/chatgpt/request.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package chatgpt

import (
"bufio"
"bytes"
"encoding/json"
"math/rand"
"os"

typings "freechatgpt/internal/typings"
Expand All @@ -11,6 +13,8 @@ import (
tls_client "github.com/bogdanfinn/tls-client"
)

var proxies []string

var (
jar = tls_client.NewCookieJar()
options = []tls_client.HttpClientOption{
Expand All @@ -24,10 +28,32 @@ var (
API_REVERSE_PROXY = os.Getenv("API_REVERSE_PROXY")
)

func init() {
// Check for proxies.txt
if _, err := os.Stat("proxies.txt"); err == nil {
// Each line is a proxy, put in proxies array
file, _ := os.Open("proxies.txt")
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
proxy := "socks5://" + scanner.Text()
proxies = append(proxies, proxy)
}
}
}

func random_int(min int, max int) int {
return min + rand.Intn(max-min)
}

func SendRequest(message typings.ChatGPTRequest, puid *string, access_token string) (*http.Response, error) {
if http_proxy != "" {
if http_proxy != "" && len(proxies) > 0 {
client.SetProxy(http_proxy)
}
// Take random proxy from proxies.txt
if len(proxies) > 0 {
client.SetProxy(proxies[random_int(0, len(proxies)-1)])
}

apiUrl := "https://chat.openai.com/backend-api/conversation"
if API_REVERSE_PROXY != "" {
Expand Down

0 comments on commit 937bb23

Please sign in to comment.