1
0
This repository has been archived on 2024-02-27. You can view files and clone it, but cannot push or open issues or pull requests.
chatgpt-to-api/internal/chatgpt/request.go

102 lines
2.7 KiB
Go
Raw Normal View History

2023-04-02 15:15:09 +00:00
package chatgpt
import (
2023-04-22 02:30:23 +00:00
"bufio"
2023-04-02 15:15:09 +00:00
"bytes"
"encoding/json"
2023-04-22 02:30:23 +00:00
"math/rand"
2023-04-20 00:22:03 +00:00
"os"
2023-04-22 02:32:06 +00:00
"strings"
2023-04-02 15:15:09 +00:00
typings "freechatgpt/internal/typings"
http "github.com/bogdanfinn/fhttp"
tls_client "github.com/bogdanfinn/tls-client"
)
2023-04-22 02:30:23 +00:00
var proxies []string
2023-04-18 03:00:48 +00:00
2023-04-02 15:15:09 +00:00
var (
jar = tls_client.NewCookieJar()
options = []tls_client.HttpClientOption{
tls_client.WithTimeoutSeconds(360),
tls_client.WithClientProfile(tls_client.Chrome_110),
tls_client.WithNotFollowRedirects(),
tls_client.WithCookieJar(jar), // create cookieJar instance and pass it as argument
2023-04-20 06:24:04 +00:00
// Disable SSL verification
tls_client.WithInsecureSkipVerify(),
2023-04-02 15:15:09 +00:00
}
2023-04-22 02:30:23 +00:00
client, _ = tls_client.NewHttpClient(tls_client.NewNoopLogger(), options...)
http_proxy = os.Getenv("http_proxy")
2023-04-20 00:22:03 +00:00
API_REVERSE_PROXY = os.Getenv("API_REVERSE_PROXY")
2023-04-02 15:15:09 +00:00
)
2023-04-22 02:30:23 +00:00
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() {
2023-04-22 02:32:06 +00:00
// 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]
2023-04-22 02:32:29 +00:00
} else {
continue
2023-04-22 02:32:06 +00:00
}
2023-04-22 02:30:23 +00:00
proxies = append(proxies, proxy)
}
}
}
2023-04-18 03:00:48 +00:00
2023-04-22 02:30:23 +00:00
func random_int(min int, max int) int {
return min + rand.Intn(max-min)
}
2023-04-18 03:00:48 +00:00
2023-04-02 16:37:16 +00:00
func SendRequest(message typings.ChatGPTRequest, puid *string, access_token string) (*http.Response, error) {
2023-04-22 02:30:23 +00:00
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)])
}
2023-04-18 10:06:42 +00:00
apiUrl := "https://ai.fakeopen.com/api/conversation"
2023-04-20 00:22:03 +00:00
if API_REVERSE_PROXY != "" {
apiUrl = API_REVERSE_PROXY
}
2023-04-18 10:06:42 +00:00
println(apiUrl)
2023-04-02 15:15:09 +00:00
// JSONify the body and add it to the request
2023-04-02 16:37:16 +00:00
body_json, err := json.Marshal(message)
2023-04-02 15:15:09 +00:00
if err != nil {
2023-04-02 16:37:16 +00:00
return &http.Response{}, err
2023-04-02 15:15:09 +00:00
}
request, err := http.NewRequest(http.MethodPost, apiUrl, bytes.NewBuffer(body_json))
2023-04-02 16:37:16 +00:00
if err != nil {
return &http.Response{}, err
}
2023-04-02 15:15:09 +00:00
request.Header.Set("Content-Type", "application/json")
2023-04-02 16:37:16 +00:00
request.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
request.Header.Set("Accept", "*/*")
2023-04-02 15:15:09 +00:00
request.AddCookie(&http.Cookie{
Name: "_puid",
2023-04-02 16:37:16 +00:00
Value: *puid,
2023-04-02 15:15:09 +00:00
})
2023-04-02 16:37:16 +00:00
if access_token != "" {
request.Header.Set("Authorization", "Bearer "+access_token)
}
2023-04-02 15:15:09 +00:00
if err != nil {
return &http.Response{}, err
}
response, err := client.Do(request)
return response, err
}