Spaces:
Runtime error
Runtime error
File size: 3,932 Bytes
581b6d4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
package common
import (
"WarpGPT/pkg/env"
"WarpGPT/pkg/logger"
"WarpGPT/pkg/plugins/service/proxypool"
"encoding/json"
browser "github.com/EDDYCJY/fake-useragent"
http "github.com/bogdanfinn/fhttp"
tls_client "github.com/bogdanfinn/tls-client"
"github.com/bogdanfinn/tls-client/profiles"
"github.com/gin-gonic/gin"
"io"
"fmt"
"math/rand"
"sync"
)
type Context struct {
GinContext *gin.Context
RequestUrl string
RequestClient tls_client.HttpClient
RequestBody io.ReadCloser
RequestParam string
RequestMethod string
RequestHeaders http.Header
}
type APIError struct {
AccessToken string
StatusCode int
}
func (e *APIError) Error() string {
return fmt.Sprintf("HTTP status %d, AccessToken: %s", e.StatusCode, e.AccessToken)
}
var tu sync.Mutex
type RequestUrl interface {
Generate(path string, rawquery string) string
}
func GetContextPack[T RequestUrl](ctx *gin.Context, reqUrl T) Context {
conversation := Context{}
conversation.GinContext = ctx
conversation.RequestUrl = reqUrl.Generate(ctx.Param("path"), ctx.Request.URL.RawQuery)
conversation.RequestMethod = ctx.Request.Method
conversation.RequestBody = ctx.Request.Body
conversation.RequestParam = ctx.Param("path")
conversation.RequestClient = GetHttpClient()
conversation.RequestHeaders = http.Header(ctx.Request.Header)
return conversation
}
func getUserAgent() string {
tu.Lock()
defer tu.Unlock()
return browser.Safari()
}
func GetHttpClient() tls_client.HttpClient {
jar := tls_client.NewCookieJar()
userAgent := map[int]profiles.ClientProfile{
1: profiles.Safari_15_6_1,
2: profiles.Safari_16_0,
3: profiles.Safari_IOS_15_5,
4: profiles.Safari_IOS_15_6,
5: profiles.Safari_IOS_16_0,
}
options := []tls_client.HttpClientOption{
tls_client.WithTimeoutSeconds(120),
tls_client.WithClientProfile(userAgent[rand.Intn(5)+1]),
tls_client.WithNotFollowRedirects(),
tls_client.WithCookieJar(jar),
tls_client.WithRandomTLSExtensionOrder(),
}
if env.E.ProxyPoolUrl != "" {
ip, err := proxypool.ProxyPoolInstance.GetIpInRedis()
if err != nil {
logger.Log.Warning(err.Error())
return nil
}
options = append(options, tls_client.WithProxyUrl(ip))
} else {
options = append(options, tls_client.WithProxyUrl(env.E.Proxy))
}
client, err := tls_client.NewHttpClient(tls_client.NewNoopLogger(), options...)
if err != nil {
logger.Log.Error("Error creating http client:", err)
return nil
}
return client
}
func RequestOpenAI[T any](path string, body io.Reader, accessToken string, requestMethod string) (*T, error) {
url := "https://" + env.E.OpenaiHost + path
req, err := http.NewRequest(requestMethod, url, body)
if err != nil {
logger.Log.Error("Error creating request:", err)
return nil, err
}
userAgentStr := getUserAgent()
headers := map[string]string{
"Host": env.E.OpenaiHost,
"Origin": "https://" + env.E.OpenaiHost,
"Authorization": accessToken,
"Connection": "keep-alive",
"User-Agent": userAgentStr,
"Referer": "https://" + env.E.OpenaiHost,
"Content-Type": "application/json",
"Accept": "*/*",
"sec-fetch-dest": "empty",
"sec-fetch-site": "same-origin",
}
for key, value := range headers {
req.Header.Set(key, value)
}
resp, err := GetHttpClient().Do(req)
if err != nil {
logger.Log.Error("Error sending request:", err)
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
apiError := &APIError{
AccessToken: accessToken,
StatusCode: resp.StatusCode,
}
return nil, apiError
}
var data T
readAll, err := io.ReadAll(resp.Body)
if err != nil {
logger.Log.Error("Read error:", err)
return nil, err
}
if readAll == nil {
return nil, nil
}
err = json.Unmarshal(readAll, &data)
if err != nil {
logger.Log.Error("Unmarshal error:", err)
return nil, err
}
return &data, nil
}
|