Skip to content

Commit

Permalink
Added new headers and improved ID setup
Browse files Browse the repository at this point in the history
Enhanced the Proxy function by adding three new headers: 'Oai-Language', 'Oai-Device-Id', and 'Cookie'. Also, refactored the setupID function to improve error handling and update PUID and OAIDID more efficiently. Additionally, a device id is now generated in the GetIDs function.
  • Loading branch information
leokwsw committed Apr 18, 2024
1 parent 88f5f1d commit 040a294
Showing 1 changed file with 50 additions and 28 deletions.
78 changes: 50 additions & 28 deletions api/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ func Proxy(c *gin.Context) {
}
req.Header.Set("User-Agent", UserAgent)
req.Header.Set(AuthorizationHeader, GetAccessToken(c.GetHeader(AuthorizationHeader)))
req.Header.Set("Oai-Language", Language)
req.Header.Set("Oai-Device-Id", OAIDID)
req.Header.Set("Cookie", req.Header.Get("Cookie")+"oai-did="+OAIDID)
resp, err := Client.Do(req)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, ReturnMessage(err.Error()))
Expand Down Expand Up @@ -215,42 +218,46 @@ func setupID() {

authenticator := auth.NewAuthenticator(username, password, ProxyUrl)
if err := authenticator.Begin(); err != nil {
//if err.Details == "missing access token" {
// accessToken, err := ninja.Login(username, password)
//if os.Getenv("NINJA_URL") != "" {
// if err.Details == "missing access token" {
// accessToken, err := ninja.Login(username, password)
//
// if err != nil {
// logger.Warn(fmt.Sprintf("%s: %s", refreshPuidErrorMessage, err.Details))
// return
// }
// if err != nil {
// logger.Warn(fmt.Sprintf("%s: %s", refreshPuidErrorMessage, err.Details))
// return
// }
//
// puid, oaidid := GetIDs(accessToken)
// puid, oaidid := GetIDs(accessToken)
//
// if puid == "" {
// logger.Error(refreshPuidErrorMessage)
// return
// } else {
// PUID = puid
// logger.Info(fmt.Sprintf("PUID is updated"))
// }
// if puid == "" {
// logger.Error(refreshPuidErrorMessage)
// return
// } else {
// PUID = puid
// logger.Info(fmt.Sprintf("PUID is updated"))
// }
//
// if oaidid == "" {
// logger.Warn(refreshOaididErrorMessage)
// //return
// } else {
// OAIDID = oaidid
// logger.Info(fmt.Sprintf("OAIDID is updated"))
// }
// if oaidid == "" {
// logger.Warn(refreshOaididErrorMessage)
// //return
// } else {
// OAIDID = oaidid
// logger.Info(fmt.Sprintf("OAIDID is updated"))
// }
//
// // store IMITATE_accessToken
// IMITATE_accessToken = accessToken
// // store IMITATE_accessToken
// IMITATE_accessToken = accessToken
//
// time.Sleep(time.Hour * 24 * 7)
// time.Sleep(time.Hour * 24 * 7)
//
// } else {
// logger.Warn(fmt.Sprintf("%s: %s", refreshPuidErrorMessage, err.Details))
// return
// }
//} else {
logger.Warn(fmt.Sprintf("%s: %s", refreshPuidErrorMessage, err.Details))
return
//}

}

accessToken := authenticator.GetAccessToken()
Expand All @@ -259,13 +266,22 @@ func setupID() {
return
}

puid, err := authenticator.GetPUID()
if err != nil {
puid, oaidid := GetIDs(accessToken)
if puid == "" {
logger.Error(refreshPuidErrorMessage)
return
} else {
PUID = puid
logger.Info(fmt.Sprintf("PUID is updated"))
}

PUID = puid
if oaidid == "" {
logger.Warn(refreshOaididErrorMessage)
//return
} else {
OAIDID = oaidid
logger.Info(fmt.Sprintf("OAIDID is updated"))
}

// store IMITATE_accessToken
IMITATE_accessToken = accessToken
Expand Down Expand Up @@ -310,6 +326,7 @@ func setupID() {
} else {
PUID = os.Getenv("PUID")
IMITATE_accessToken = os.Getenv("IMITATE_ACCESS_TOKEN")
OAIDID = uuid.New().String()
}
}

Expand Down Expand Up @@ -360,11 +377,16 @@ func GetIDs(accessToken string) (string, string) {
logger.Error("GetIDs: Missing access token")
return "", ""
}

// generate device id
oaidid = uuid.NewSHA1(uuid.MustParse("12345678-1234-5678-1234-567812345678"), []byte(accessToken)).String()

// Make request to https://chat.openai.com/backend-api/models
req, _ := http.NewRequest("GET", "https://chat.openai.com/backend-api/models?history_and_training_disabled=false", nil)
// Add headers
req.Header.Add(AuthorizationHeader, GetAccessToken(accessToken))
req.Header.Add("User-Agent", UserAgent)
req.Header.Set("Cookie", req.Header.Get("Cookie")+"oai-did="+oaidid)

resp, err := NewHttpClient().Do(req)
if err != nil {
Expand Down

0 comments on commit 040a294

Please sign in to comment.