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/main.go

108 lines
2.5 KiB
Go
Raw Normal View History

2023-04-02 03:30:03 +00:00
package main
2023-04-02 05:40:07 +00:00
import (
2023-04-11 04:28:01 +00:00
"encoding/json"
2023-04-02 16:37:16 +00:00
"freechatgpt/internal/tokens"
2023-05-30 05:46:20 +00:00
"log"
"os"
2023-04-24 08:34:09 +00:00
"strings"
2023-05-30 05:46:20 +00:00
"time"
2023-05-30 05:46:20 +00:00
"github.com/acheong08/OpenAIAuth/auth"
2023-05-21 06:51:18 +00:00
"github.com/acheong08/endless"
"github.com/gin-gonic/gin"
2023-04-02 05:40:07 +00:00
)
var HOST string
var PORT string
2023-04-02 16:37:16 +00:00
var ACCESS_TOKENS tokens.AccessToken
2023-05-30 05:46:20 +00:00
var authorizations struct {
OpenAI_Email string `json:"openai_email"`
OpenAI_Password string `json:"openai_password"`
}
func init() {
2023-05-30 05:46:20 +00:00
authorizations.OpenAI_Email = os.Getenv("OPENAI_EMAIL")
authorizations.OpenAI_Password = os.Getenv("OPENAI_PASSWORD")
if authorizations.OpenAI_Email != "" && authorizations.OpenAI_Password != "" {
go func() {
for {
authenticator := auth.NewAuthenticator(authorizations.OpenAI_Email, authorizations.OpenAI_Password, os.Getenv("http_proxy"))
err := authenticator.Begin()
if err != nil {
log.Println(err)
break
}
puid, err := authenticator.GetPUID()
if err != nil {
break
}
os.Setenv("PUID", puid)
println(puid)
time.Sleep(24 * time.Hour * 7)
}
}()
}
HOST = os.Getenv("SERVER_HOST")
PORT = os.Getenv("SERVER_PORT")
if HOST == "" {
HOST = "127.0.0.1"
}
if PORT == "" {
PORT = "8080"
}
2023-04-24 08:34:09 +00:00
accessToken := os.Getenv("ACCESS_TOKENS")
if accessToken != "" {
accessTokens := strings.Split(accessToken, ",")
ACCESS_TOKENS = tokens.NewAccessToken(accessTokens)
}
2023-04-11 04:28:01 +00:00
// Check if access_tokens.json exists
if _, err := os.Stat("access_tokens.json"); os.IsNotExist(err) {
// Create the file
file, err := os.Create("access_tokens.json")
if err != nil {
panic(err)
}
defer file.Close()
} else {
// Load the tokens
file, err := os.Open("access_tokens.json")
if err != nil {
panic(err)
}
defer file.Close()
decoder := json.NewDecoder(file)
var token_list []string
err = decoder.Decode(&token_list)
if err != nil {
2023-04-11 04:29:02 +00:00
return
2023-04-11 04:28:01 +00:00
}
ACCESS_TOKENS = tokens.NewAccessToken(token_list)
}
}
func main() {
router := gin.Default()
2023-04-05 10:07:07 +00:00
router.Use(cors)
router.GET("/ping", func(c *gin.Context) {
2023-04-03 02:11:16 +00:00
c.JSON(200, gin.H{
"message": "pong",
})
})
2023-04-05 10:07:07 +00:00
admin_routes := router.Group("/admin")
admin_routes.Use(adminCheck)
/// Admin routes
2023-04-05 10:07:07 +00:00
admin_routes.PATCH("/password", passwordHandler)
2023-05-24 15:19:54 +00:00
admin_routes.PATCH("/tokens", tokensHandler)
admin_routes.PATCH("/puid", puidHandler)
2023-05-30 05:46:20 +00:00
admin_routes.PATCH("/openai", openaiHandler)
2023-04-02 14:53:33 +00:00
/// Public routes
2023-04-05 10:07:07 +00:00
router.OPTIONS("/v1/chat/completions", optionsHandler)
2023-05-24 15:19:54 +00:00
router.POST("/v1/chat/completions", Authorization, nightmare)
2023-04-11 04:28:01 +00:00
endless.ListenAndServe(HOST+":"+PORT, router)
}