forked from aurora-develop/aurora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
121 lines (101 loc) · 2.32 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"aurora/internal/proxys"
"bufio"
"embed"
"io/fs"
"log"
"log/slog"
"net/http"
"net/url"
"os"
"github.com/acheong08/endless"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
func checkProxy() *proxys.IProxy {
var proxies []string
proxyUrl := os.Getenv("PROXY_URL")
if proxyUrl != "" {
proxies = append(proxies, proxyUrl)
}
if _, err := os.Stat("proxies.txt"); err == nil {
file, _ := os.Open("proxies.txt")
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
proxy := scanner.Text()
parsedURL, err := url.Parse(proxy)
if err != nil {
slog.Warn("proxy url is invalid", "url", proxy, "err", err)
continue
}
// 如果缺少端口信息,不是完整的代理链接
if parsedURL.Port() != "" {
proxies = append(proxies, proxy)
} else {
continue
}
}
}
if len(proxies) == 0 {
proxy := os.Getenv("http_proxy")
if proxy != "" {
proxies = append(proxies, proxy)
}
}
proxyIP := proxys.NewIProxyIP(proxies)
return &proxyIP
}
//go:embed web/*
var staticFiles embed.FS
func registerRouter() *gin.Engine {
handler := NewHandle(
checkProxy(),
readAccessToken(),
)
router := gin.Default()
router.Use(cors)
router.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "Hello, world!",
})
})
router.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
router.POST("/auth/session", handler.session)
router.POST("/auth/refresh", handler.refresh)
router.OPTIONS("/v1/chat/completions", optionsHandler)
authGroup := router.Group("").Use(Authorization)
authGroup.POST("/v1/chat/completions", handler.nightmare)
authGroup.GET("/v1/models", handler.engines)
subFS, err := fs.Sub(staticFiles, "web")
if err != nil {
log.Fatal(err)
}
router.StaticFS("/web", http.FS(subFS))
return router
}
func main() {
gin.SetMode(gin.ReleaseMode)
router := registerRouter()
_ = godotenv.Load(".env")
host := os.Getenv("SERVER_HOST")
port := os.Getenv("SERVER_PORT")
tlsCert := os.Getenv("TLS_CERT")
tlsKey := os.Getenv("TLS_KEY")
if host == "" {
host = "0.0.0.0"
}
if port == "" {
port = "8080"
}
if tlsCert != "" && tlsKey != "" {
_ = endless.ListenAndServeTLS(host+":"+port, tlsCert, tlsKey, router)
} else {
_ = endless.ListenAndServe(host+":"+port, router)
}
}