Skip to content

Commit 6afebdb

Browse files
authored
Update main.go
1 parent ca4382b commit 6afebdb

File tree

1 file changed

+21
-49
lines changed

1 file changed

+21
-49
lines changed

ghproxy/main.go

Lines changed: 21 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"encoding/json"
55
"fmt"
6+
"github.com/gin-gonic/gin"
67
"io"
78
"net"
89
"net/http"
@@ -12,22 +13,14 @@ import (
1213
"strings"
1314
"sync"
1415
"time"
15-
16-
"github.com/gin-gonic/gin"
1716
)
1817

19-
// 常量定义
2018
const (
2119
sizeLimit = 1024 * 1024 * 1024 * 10 // 允许的文件大小,默认10GB
2220
host = "0.0.0.0" // 监听地址
2321
port = 5000 // 监听端口
2422
)
2523

26-
type Config struct {
27-
WhiteList []string `json:"whiteList"`
28-
BlackList []string `json:"blackList"`
29-
}
30-
3124
var (
3225
exps = []*regexp.Regexp{
3326
regexp.MustCompile(`^(?:https?://)?github\.com/([^/]+)/([^/]+)/(?:releases|archive)/.*$`),
@@ -45,6 +38,11 @@ var (
4538
configLock sync.RWMutex
4639
)
4740

41+
type Config struct {
42+
WhiteList []string `json:"whiteList"`
43+
BlackList []string `json:"blackList"`
44+
}
45+
4846
func main() {
4947
gin.SetMode(gin.ReleaseMode)
5048
router := gin.Default()
@@ -65,38 +63,28 @@ func main() {
6563
}
6664

6765
loadConfig()
68-
69-
// 每60分钟热重载黑白名单
7066
go func() {
71-
ticker := time.NewTicker(60 * time.Minute)
72-
defer ticker.Stop()
73-
74-
for range ticker.C {
67+
for {
68+
time.Sleep(10 * time.Minute)
7569
loadConfig()
7670
}
7771
}()
78-
7972
// 前端访问路径,默认根路径
8073
router.Static("/", "./public")
8174
router.NoRoute(handler)
8275

83-
serverAddr := fmt.Sprintf("%s:%d", host, port)
84-
if err := router.Run(serverAddr); err != nil {
76+
err := router.Run(fmt.Sprintf("%s:%d", host, port))
77+
if err != nil {
8578
fmt.Printf("Error starting server: %v\n", err)
8679
}
8780
}
8881

8982
func handler(c *gin.Context) {
9083
rawPath := strings.TrimPrefix(c.Request.URL.RequestURI(), "/")
91-
84+
9285
for strings.HasPrefix(rawPath, "/") {
9386
rawPath = strings.TrimPrefix(rawPath, "/")
9487
}
95-
// 脚本嵌套路径处理
96-
if rawPath == "perl-pe-para" {
97-
handlePerlPePara(c)
98-
return
99-
}
10088

10189
if !strings.HasPrefix(rawPath, "http") {
10290
c.String(http.StatusForbidden, "无效输入")
@@ -105,9 +93,6 @@ func handler(c *gin.Context) {
10593

10694
matches := checkURL(rawPath)
10795
if matches != nil {
108-
configLock.RLock()
109-
defer configLock.RUnlock()
110-
11196
if len(config.WhiteList) > 0 && !checkList(matches, config.WhiteList) {
11297
c.String(http.StatusForbidden, "不在白名单内,限制访问。")
11398
return
@@ -127,22 +112,8 @@ func handler(c *gin.Context) {
127112

128113
proxy(c, rawPath)
129114
}
130-
// 处理脚本嵌套相关函数
131-
func handlePerlPePara(c *gin.Context) {
132-
perlstr := "perl -pe"
133-
responseText := fmt.Sprintf(`s#(bash.*?\.sh)([^/\w\d])#\1 | %s "$(curl -L %s/perl-pe-para)" \2#g; s# (git)# https://\1#g; s#(http.*?git[^/]*?/)#%s/\1#g`, perlstr, c.Request.URL.String(), c.Request.URL.String())
134-
c.Header("Content-Type", "text/plain")
135-
c.Header("Cache-Control", "max-age=300")
136-
c.String(http.StatusOK, responseText)
137-
}
138115

139116
func proxy(c *gin.Context, u string) {
140-
// 检查是否脚本嵌套路径
141-
if strings.HasSuffix(u, "perl-pe-para") {
142-
handlePerlPePara(c)
143-
return
144-
}
145-
146117
req, err := http.NewRequest(c.Request.Method, u, c.Request.Body)
147118
if err != nil {
148119
c.String(http.StatusInternalServerError, fmt.Sprintf("server error %v", err))
@@ -161,11 +132,12 @@ func proxy(c *gin.Context, u string) {
161132
c.String(http.StatusInternalServerError, fmt.Sprintf("server error %v", err))
162133
return
163134
}
164-
defer func() {
165-
if err := resp.Body.Close(); err != nil {
166-
fmt.Printf("Error closing response body: %v\n", err)
135+
defer func(Body io.ReadCloser) {
136+
err := Body.Close()
137+
if err != nil {
138+
167139
}
168-
}()
140+
}(resp.Body)
169141

170142
if contentLength, ok := resp.Header["Content-Length"]; ok {
171143
if size, err := strconv.Atoi(contentLength[0]); err == nil && size > sizeLimit {
@@ -195,7 +167,6 @@ func proxy(c *gin.Context, u string) {
195167

196168
c.Status(resp.StatusCode)
197169
if _, err := io.Copy(c.Writer, resp.Body); err != nil {
198-
fmt.Printf("Error copying response body: %v\n", err)
199170
return
200171
}
201172
}
@@ -206,11 +177,12 @@ func loadConfig() {
206177
fmt.Printf("Error loading config: %v\n", err)
207178
return
208179
}
209-
defer func() {
210-
if err := file.Close(); err != nil {
211-
fmt.Printf("Error closing config file: %v\n", err)
180+
defer func(file *os.File) {
181+
err := file.Close()
182+
if err != nil {
183+
212184
}
213-
}()
185+
}(file)
214186

215187
var newConfig Config
216188
decoder := json.NewDecoder(file)

0 commit comments

Comments
 (0)