2023-04-02 03:30:03 +00:00
|
|
|
package main
|
2023-04-02 05:40:07 +00:00
|
|
|
|
|
|
|
import (
|
2023-05-18 03:42:54 +00:00
|
|
|
"bufio"
|
2023-04-02 16:37:16 +00:00
|
|
|
"freechatgpt/internal/tokens"
|
2023-04-02 14:19:51 +00:00
|
|
|
"os"
|
2023-04-24 08:34:09 +00:00
|
|
|
"strings"
|
2023-04-02 14:19:51 +00:00
|
|
|
|
2023-05-21 06:51:18 +00:00
|
|
|
"github.com/acheong08/endless"
|
2023-04-02 14:19:51 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2023-06-28 03:58:54 +00:00
|
|
|
"github.com/joho/godotenv"
|
2023-04-02 05:40:07 +00:00
|
|
|
)
|
2023-04-02 14:19:51 +00:00
|
|
|
|
|
|
|
var HOST string
|
|
|
|
var PORT string
|
2023-04-02 16:37:16 +00:00
|
|
|
var ACCESS_TOKENS tokens.AccessToken
|
2023-05-18 03:42:54 +00:00
|
|
|
var proxies []string
|
|
|
|
|
|
|
|
func checkProxy() {
|
2023-07-20 03:14:47 +00:00
|
|
|
// first check for proxies.txt
|
2023-05-18 03:42:54 +00:00
|
|
|
proxies = []string{}
|
|
|
|
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, ":")
|
2023-05-27 07:27:40 +00:00
|
|
|
if len(proxy_parts) > 1 {
|
2023-05-18 03:42:54 +00:00
|
|
|
proxies = append(proxies, proxy)
|
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-20 03:14:47 +00:00
|
|
|
// if no proxies, then check env http_proxy
|
|
|
|
if len(proxies) == 0 {
|
|
|
|
proxy := os.Getenv("http_proxy")
|
|
|
|
if proxy != "" {
|
|
|
|
proxies = append(proxies, proxy)
|
|
|
|
}
|
|
|
|
}
|
2023-05-18 03:42:54 +00:00
|
|
|
}
|
2023-04-02 14:19:51 +00:00
|
|
|
|
|
|
|
func init() {
|
2023-06-28 03:58:54 +00:00
|
|
|
_ = godotenv.Load(".env")
|
2023-06-16 14:49:08 +00:00
|
|
|
|
2023-04-02 14:19:51 +00:00
|
|
|
HOST = os.Getenv("SERVER_HOST")
|
|
|
|
PORT = os.Getenv("SERVER_PORT")
|
2023-06-27 16:05:19 +00:00
|
|
|
if PORT == "" {
|
|
|
|
PORT = os.Getenv("PORT")
|
|
|
|
}
|
2023-04-02 14:19:51 +00:00
|
|
|
if HOST == "" {
|
2024-02-25 19:59:37 +00:00
|
|
|
HOST = "0.0.0.0"
|
2023-04-02 14:19:51 +00:00
|
|
|
}
|
|
|
|
if PORT == "" {
|
|
|
|
PORT = "8080"
|
|
|
|
}
|
2023-05-18 03:42:54 +00:00
|
|
|
checkProxy()
|
2023-06-14 03:06:16 +00:00
|
|
|
readAccounts()
|
2023-08-04 01:44:47 +00:00
|
|
|
scheduleTokenPUID()
|
2023-04-02 14:19:51 +00:00
|
|
|
}
|
|
|
|
func main() {
|
|
|
|
router := gin.Default()
|
2023-04-05 10:07:07 +00:00
|
|
|
|
|
|
|
router.Use(cors)
|
|
|
|
|
2023-04-02 14:19:51 +00:00
|
|
|
router.GET("/ping", func(c *gin.Context) {
|
2023-04-03 02:11:16 +00:00
|
|
|
c.JSON(200, gin.H{
|
|
|
|
"message": "pong",
|
|
|
|
})
|
2023-04-02 14:19:51 +00:00
|
|
|
})
|
|
|
|
|
2023-04-05 10:07:07 +00:00
|
|
|
admin_routes := router.Group("/admin")
|
|
|
|
admin_routes.Use(adminCheck)
|
|
|
|
|
2023-04-02 14:19:51 +00:00
|
|
|
/// 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-06-16 14:49:08 +00:00
|
|
|
router.GET("/v1/engines", Authorization, engines_handler)
|
2023-06-16 15:45:27 +00:00
|
|
|
router.GET("/v1/models", Authorization, engines_handler)
|
2023-04-11 04:28:01 +00:00
|
|
|
endless.ListenAndServe(HOST+":"+PORT, router)
|
2023-04-02 14:19:51 +00:00
|
|
|
}
|