forked from aurora-develop/aurora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
98 lines (81 loc) · 1.69 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
package main
import (
"aurora/internal/proxys"
"aurora/internal/tokens"
"bufio"
"fmt"
"net/url"
"os"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
var (
HOST string
PORT string
ACCESS_TOKENS tokens.AccessToken
ProxyIP proxys.IProxy
)
func init() {
_ = godotenv.Load(".env")
HOST = os.Getenv("SERVER_HOST")
PORT = os.Getenv("SERVER_PORT")
if HOST == "" {
HOST = "0.0.0.0"
}
if PORT == "" {
PORT = "8080"
}
checkProxy()
readAccessToken()
}
func checkProxy() {
proxies := []string{}
PROXY_URL := os.Getenv("PROXY_URL")
if PROXY_URL != "" {
proxies = append(proxies, PROXY_URL)
}
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, _ := url.Parse(proxy)
if err != nil {
fmt.Println("无法解析URL:", err)
return
}
// 如果缺少端口信息,不是完整的代理链接
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)
}
func main() {
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.OPTIONS("/v1/chat/completions", optionsHandler)
router.POST("/v1/chat/completions", nightmare)
router.GET("/v1/models", engines_handler)
router.Run(HOST + ":" + PORT)
}