1
0

restore proxy stuff

This commit is contained in:
Antonio 2023-05-04 06:36:17 +08:00
parent 4554d0910f
commit 42e3b688c7

View File

@ -1,9 +1,12 @@
package chatgpt
import (
"bufio"
"bytes"
"encoding/json"
"math/rand"
"os"
"strings"
typings "freechatgpt/internal/typings"
@ -11,6 +14,8 @@ import (
tls_client "github.com/bogdanfinn/tls-client"
)
var proxies []string
var (
jar = tls_client.NewCookieJar()
options = []tls_client.HttpClientOption{
@ -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 != "" {