2023-04-02 15:15:09 +00:00
|
|
|
package chatgpt
|
|
|
|
|
|
|
|
import (
|
2023-05-03 22:36:17 +00:00
|
|
|
"bufio"
|
2023-04-02 15:15:09 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2023-05-03 22:36:17 +00:00
|
|
|
"math/rand"
|
2023-04-20 00:22:03 +00:00
|
|
|
"os"
|
2023-05-03 22:36:17 +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-05-03 22:36:17 +00:00
|
|
|
var proxies []string
|
|
|
|
|
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-05-03 22:36:17 +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() {
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2023-04-30 09:13:20 +00:00
|
|
|
func SendRequest(message typings.ChatGPTRequest, access_token string) (*http.Response, error) {
|
2023-05-03 22:36:17 +00:00
|
|
|
if http_proxy != "" && len(proxies) > 0 {
|
2023-04-22 02:30:23 +00:00
|
|
|
client.SetProxy(http_proxy)
|
|
|
|
}
|
2023-05-03 22:36:17 +00:00
|
|
|
// Take random proxy from proxies.txt
|
|
|
|
if len(proxies) > 0 {
|
|
|
|
client.SetProxy(proxies[random_int(0, len(proxies)-1)])
|
|
|
|
}
|
2023-04-12 02:31:44 +00:00
|
|
|
|
2023-05-01 08:41:48 +00:00
|
|
|
apiUrl := "https://chat.openai.com/backend-api/conversation"
|
2023-04-20 00:22:03 +00:00
|
|
|
if API_REVERSE_PROXY != "" {
|
|
|
|
apiUrl = API_REVERSE_PROXY
|
|
|
|
}
|
2023-04-12 02:31:44 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-04-12 02:31:44 +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-05-01 08:42:09 +00:00
|
|
|
request.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36")
|
2023-04-02 16:37:16 +00:00
|
|
|
request.Header.Set("Accept", "*/*")
|
|
|
|
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
|
|
|
|
}
|