forked from webrulon/ChatGPT-Proxy-V1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (63 loc) · 1.35 KB
/
main.go
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
package main
import (
"os"
"time"
ratelimit "github.com/JGLTechnologies/gin-rate-limit"
"github.com/acheong08/ChatGPT-V2/internal/api"
"github.com/acheong08/ChatGPT-V2/internal/handlers"
"github.com/fvbock/endless"
"github.com/gin-gonic/gin"
)
var limit_middleware gin.HandlerFunc
var limit_store ratelimit.Store
func init() {
limit_store = ratelimit.InMemoryStore(
&ratelimit.InMemoryOptions{
Rate: time.Minute,
Limit: 40,
},
)
limit_middleware = ratelimit.RateLimiter(
limit_store,
&ratelimit.Options{
ErrorHandler: func(c *gin.Context, info ratelimit.Info) {
c.JSON(
429,
gin.H{
"message": "Too many requests",
},
)
c.Abort()
},
KeyFunc: func(c *gin.Context) string {
// Get Authorization header
return c.ClientIP()
},
},
)
}
func secret_auth(c *gin.Context) {
if os.Getenv("SECRET") == "" {
return
}
auth_header := c.GetHeader("Secret")
if auth_header != os.Getenv("SECRET") {
c.JSON(401, gin.H{"message": "Unauthorized"})
c.Abort()
return
}
}
func main() {
PORT := os.Getenv("PORT")
if PORT == "" {
PORT = "8080"
}
handler := gin.Default()
if !api.Config.Private {
handler.Use(limit_middleware)
}
handler.Use(secret_auth)
// Proxy all requests to /* to proxy if not already handled
handler.Any("/*path", handlers.Proxy)
endless.ListenAndServe(":"+PORT, handler)
}