File size: 2,002 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
package main

import (
	"WarpGPT/pkg/db"
	"WarpGPT/pkg/env"
	"WarpGPT/pkg/funcaptcha"
	"WarpGPT/pkg/logger"
	"WarpGPT/pkg/plugins"
	"WarpGPT/pkg/plugins/api/arkosetoken"
	"WarpGPT/pkg/plugins/api/backendapi"
	"WarpGPT/pkg/plugins/api/officialapi"
	"WarpGPT/pkg/plugins/api/publicapi"
	"WarpGPT/pkg/plugins/api/rapi"
	"WarpGPT/pkg/plugins/api/session"
	"WarpGPT/pkg/plugins/api/unofficialapi"
	"WarpGPT/pkg/plugins/service/proxypool"
	"github.com/bogdanfinn/fhttp"
	"github.com/gin-gonic/gin"
	"strconv"
)

func CORSMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
		c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
		c.Writer.Header().Set("Access-Control-Allow-Headers", "*")
		c.Writer.Header().Set("Access-Control-Allow-Methods", "*")

		if c.Request.Method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
			return
		}

		c.Next()
	}
}
func AuthMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		apiKey := c.GetHeader("AuthKey")
		if apiKey != env.E.AuthKey {
			c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
			return
		}
		c.Next()
	}
}
func main() {
	var router = gin.Default()
	if env.E.Verify {
		router.Use(AuthMiddleware())
	}
	router.Use(CORSMiddleware())
	component := &plugins.Component{
		Engine: router,
		Db: db.DB{
			GetRedisClient: db.GetRedisClient,
		},
		Logger: logger.Log,
		Env:    &env.E,
		Auth:   funcaptcha.GetOpenAIArkoseToken,
	}
	var plugin_list []plugins.Plugin
	plugin_list = append(
		plugin_list,
		&arkosetoken.ArkoseTokenInstance,
		&session.SessionTokenInstance,
		&backendapi.BackendProcessInstance,
		&officialapi.OfficialApiProcessInstance,
		&unofficialapi.UnofficialApiProcessInstance,
		&publicapi.PublicApiProcessInstance,
		&rapi.ApiProcessInstance,
		&proxypool.ProxyPoolInstance,
	)
	for _, plugin := range plugin_list {
		plugin.Run(component)
	}
	router.Run(env.E.Host + ":" + strconv.Itoa(env.E.Port))
}