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

Commit

Permalink
restore proxy stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio committed May 3, 2023
1 parent 4554d09 commit 42e3b68
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion internal/chatgpt/request.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package chatgpt

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

typings "freechatgpt/internal/typings"

http "github.com/bogdanfinn/fhttp"
tls_client "github.com/bogdanfinn/tls-client"
)

var proxies []string

var (
jar = tls_client.NewCookieJar()
options = []tls_client.HttpClientOption{
Expand All @@ -26,10 +31,41 @@ 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() {
// Split line by :
proxy := scanner.Text()
proxy_parts := strings.Split(proxy, ":")
if len(proxy_parts) == 2 {
proxy = "socks5://" + proxy
} else if len(proxy_parts) == 4 {
proxy = "socks5://" + proxy_parts[2] + ":" + proxy_parts[3] + "@" + proxy_parts[0] + ":" + proxy_parts[1]
} else {
continue
}
proxies = append(proxies, proxy)
}
}
}

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

func SendRequest(message typings.ChatGPTRequest, 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 42e3b68

Please sign in to comment.