mirror of
https://github.com/WJQSERVER-STUDIO/ghproxy.git
synced 2026-02-03 08:11:11 +08:00
24w06b
This commit is contained in:
parent
efeb940676
commit
0899397d2e
7 changed files with 263 additions and 212 deletions
|
|
@ -1,5 +1,13 @@
|
|||
# 更新日志
|
||||
|
||||
24w06b
|
||||
---
|
||||
- CHANGE: 优化代码结构,对main函数进行模块化,提升可读性
|
||||
- CHANGE: Docker代理功能移至DEV版本内,保证稳定性
|
||||
- ADD&CHANGE: 增加Auth(用户鉴权)模块,并改进其的错误处理与日志记录
|
||||
- CHANGE: 日志模块引入goroutine协程,提升性能 (实验性功能)
|
||||
- ADD: 将主要实现分离,作为Proxy模块,并优化代码结构
|
||||
|
||||
v1.0.0
|
||||
---
|
||||
- CHANGE: 项目正式发布, 并迁移至[WJQSERVER-STUDIO/ghproxy](https://github.com/WJQSERVER-STUDIO/ghproxy)
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
24w06a
|
||||
24w06b
|
||||
33
auth/auth.go
Normal file
33
auth/auth.go
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
"ghproxy/config"
|
||||
"ghproxy/logger"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var (
|
||||
cfg *config.Config
|
||||
log = logger.Logw
|
||||
)
|
||||
|
||||
func AuthHandler(c *gin.Context) bool {
|
||||
// 如果身份验证未启用,直接返回 true
|
||||
if !cfg.Auth {
|
||||
log("auth PASS")
|
||||
return true
|
||||
}
|
||||
|
||||
// 获取 auth_token 参数
|
||||
authToken := c.Query("auth_token")
|
||||
log("auth_token: ", authToken)
|
||||
|
||||
// 验证 token
|
||||
isValid := authToken == cfg.AuthToken
|
||||
if !isValid {
|
||||
log("auth FAIL")
|
||||
}
|
||||
|
||||
return isValid
|
||||
}
|
||||
|
|
@ -94,23 +94,6 @@
|
|||
file_server
|
||||
import cache 60s 24h
|
||||
}
|
||||
|
||||
handle_errors {
|
||||
@redirects `{err.status_code} in [301, 302, 307]`
|
||||
reverse_proxy @redirects {
|
||||
header_up Location {http.response.header.Location}
|
||||
}
|
||||
}
|
||||
|
||||
route /v2* {
|
||||
reverse_proxy https://registry-1.docker.io {
|
||||
header_up Host registry-1.docker.io
|
||||
header_up X-Real-IP {remote}
|
||||
header_up X-Forwarded-For {http.request.header.X-Forwarded-For}
|
||||
header_up X-Forwarded-Proto {scheme}
|
||||
header_up Authorization {http.request.header.Authorization}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
import /data/caddy/config.d/*
|
||||
|
|
|
|||
|
|
@ -8,8 +8,12 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
var logFile *os.File
|
||||
var logger *log.Logger
|
||||
var (
|
||||
logFile *os.File
|
||||
logger *log.Logger
|
||||
logChannel = make(chan string, 100) // 创建一个缓冲通道
|
||||
quitChannel = make(chan struct{}) // 用于通知退出
|
||||
)
|
||||
|
||||
// Init 初始化日志记录器,接受日志文件路径作为参数
|
||||
func Init(logFilePath string) error {
|
||||
|
|
@ -19,17 +23,29 @@ func Init(logFilePath string) error {
|
|||
return err
|
||||
}
|
||||
logger = log.New(logFile, "", 0) // 不使用默认前缀
|
||||
|
||||
go logWorker() // 启动 goroutine 处理日志
|
||||
return nil
|
||||
}
|
||||
|
||||
// Log 直接记录日志的函数,带有时间戳
|
||||
func Log(customMessage string) {
|
||||
if logger != nil {
|
||||
timestamp := time.Now().Format("02/Jan/2006:15:04:05 -0700") // 使用自定义时间格式
|
||||
logger.Println(timestamp + " - " + customMessage)
|
||||
// logWorker 处理日志记录
|
||||
func logWorker() {
|
||||
for {
|
||||
select {
|
||||
case msg := <-logChannel:
|
||||
timestamp := time.Now().Format("02/Jan/2006:15:04:05 -0700")
|
||||
logger.Println(timestamp + " - " + msg) // 写入日志
|
||||
case <-quitChannel:
|
||||
return // 退出 goroutine
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Log 直接记录日志的函数
|
||||
func Log(customMessage string) {
|
||||
logChannel <- customMessage // 将日志消息发送到通道
|
||||
}
|
||||
|
||||
// Logw 用于格式化日志记录
|
||||
func Logw(format string, args ...interface{}) {
|
||||
message := fmt.Sprintf(format, args...) // 格式化消息
|
||||
|
|
@ -39,6 +55,9 @@ func Logw(format string, args ...interface{}) {
|
|||
// Close 关闭日志文件
|
||||
func Close() {
|
||||
if logFile != nil {
|
||||
logFile.Close()
|
||||
quitChannel <- struct{}{} // 通知日志 goroutine 退出
|
||||
if err := logFile.Close(); err != nil {
|
||||
Log("Error closing log file: " + err.Error()) // 记录关闭日志时的错误
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
188
main.go
188
main.go
|
|
@ -3,34 +3,20 @@ package main
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"ghproxy/config"
|
||||
"ghproxy/logger"
|
||||
"ghproxy/proxy"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/imroc/req/v3"
|
||||
)
|
||||
|
||||
var cfg *config.Config
|
||||
var logw = logger.Logw
|
||||
var router *gin.Engine
|
||||
|
||||
var (
|
||||
exps = []*regexp.Regexp{
|
||||
regexp.MustCompile(`^(?:https?://)?github\.com/([^/]+)/([^/]+)/(?:releases|archive)/.*`),
|
||||
regexp.MustCompile(`^(?:https?://)?github\.com/([^/]+)/([^/]+)/(?:blob|raw)/.*`),
|
||||
regexp.MustCompile(`^(?:https?://)?github\.com/([^/]+)/([^/]+)/(?:info|git-).*`),
|
||||
regexp.MustCompile(`^(?:https?://)?raw\.github(?:usercontent|)\.com/([^/]+)/([^/]+)/.+?/.+`),
|
||||
regexp.MustCompile(`^(?:https?://)?gist\.github\.com/([^/]+)/.+?/.+`),
|
||||
}
|
||||
)
|
||||
|
||||
func loadConfig() {
|
||||
var err error
|
||||
// 初始化配置
|
||||
|
|
@ -75,7 +61,7 @@ func init() {
|
|||
})
|
||||
|
||||
// 未匹配路由处理
|
||||
router.NoRoute(noRouteHandler(cfg))
|
||||
router.NoRoute(proxy.NoRouteHandler(cfg))
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
|
@ -95,173 +81,3 @@ func api(c *gin.Context) {
|
|||
"MaxResponseBodySize": cfg.SizeLimit,
|
||||
})
|
||||
}
|
||||
|
||||
func authHandler(c *gin.Context) bool {
|
||||
if cfg.Auth {
|
||||
authToken := c.Query("auth_token")
|
||||
return authToken == cfg.AuthToken
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func noRouteHandler(config *config.Config) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
rawPath := strings.TrimPrefix(c.Request.URL.RequestURI(), "/")
|
||||
re := regexp.MustCompile(`^(http:|https:)?/?/?(.*)`)
|
||||
matches := re.FindStringSubmatch(rawPath)
|
||||
|
||||
rawPath = "https://" + matches[2]
|
||||
|
||||
matches = checkURL(rawPath)
|
||||
if matches == nil {
|
||||
c.String(http.StatusForbidden, "Invalid input.")
|
||||
return
|
||||
}
|
||||
|
||||
if exps[1].MatchString(rawPath) {
|
||||
rawPath = strings.Replace(rawPath, "/blob/", "/raw/", 1)
|
||||
}
|
||||
|
||||
if !authHandler(c) {
|
||||
c.AbortWithStatusJSON(401, gin.H{"error": "Unauthorized"})
|
||||
logw("Unauthorized request: %s", rawPath)
|
||||
return
|
||||
}
|
||||
|
||||
// 日志记录
|
||||
logw("Request: %s %s", c.Request.Method, rawPath)
|
||||
logw("Matches: %v", matches)
|
||||
|
||||
// 代理请求
|
||||
switch {
|
||||
case exps[0].MatchString(rawPath), exps[1].MatchString(rawPath), exps[3].MatchString(rawPath), exps[4].MatchString(rawPath):
|
||||
logw("%s Matched - USE proxy-chrome", rawPath)
|
||||
proxyRequest(c, rawPath, config, "chrome")
|
||||
case exps[2].MatchString(rawPath):
|
||||
logw("%s Matched - USE proxy-git", rawPath)
|
||||
proxyRequest(c, rawPath, config, "git")
|
||||
default:
|
||||
c.String(http.StatusForbidden, "Invalid input.")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func proxyRequest(c *gin.Context, u string, config *config.Config, mode string) {
|
||||
method := c.Request.Method
|
||||
logw("%s Method: %s", u, method)
|
||||
|
||||
client := req.C()
|
||||
|
||||
switch mode {
|
||||
case "chrome":
|
||||
client.SetUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36").
|
||||
SetTLSFingerprintChrome().
|
||||
ImpersonateChrome()
|
||||
case "git":
|
||||
client.SetUserAgent("git/2.33.1")
|
||||
}
|
||||
|
||||
// 读取请求体
|
||||
body, err := io.ReadAll(c.Request.Body)
|
||||
if err != nil {
|
||||
handleError(c, fmt.Sprintf("Failed to read request body: %v", err))
|
||||
return
|
||||
}
|
||||
defer c.Request.Body.Close()
|
||||
|
||||
// 创建新的请求
|
||||
req := client.R().SetBody(body)
|
||||
|
||||
// 复制请求头
|
||||
for key, values := range c.Request.Header {
|
||||
for _, value := range values {
|
||||
req.SetHeader(key, value)
|
||||
}
|
||||
}
|
||||
|
||||
// 发送请求并处理响应
|
||||
resp, err := sendRequest(req, method, u)
|
||||
if err != nil {
|
||||
handleError(c, fmt.Sprintf("Failed to send request: %v", err))
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// 检查响应内容长度并处理重定向
|
||||
if err := handleResponseSize(resp, config, c); err != nil {
|
||||
logw("Error handling response size: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
copyResponseHeaders(resp, c, config)
|
||||
c.Status(resp.StatusCode)
|
||||
if _, err := io.Copy(c.Writer, resp.Body); err != nil {
|
||||
logw("Failed to copy response body: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func sendRequest(req *req.Request, method, url string) (*req.Response, error) {
|
||||
switch method {
|
||||
case "GET":
|
||||
return req.Get(url)
|
||||
case "POST":
|
||||
return req.Post(url)
|
||||
case "PUT":
|
||||
return req.Put(url)
|
||||
case "DELETE":
|
||||
return req.Delete(url)
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported method: %s", method)
|
||||
}
|
||||
}
|
||||
|
||||
func handleResponseSize(resp *req.Response, config *config.Config, c *gin.Context) error {
|
||||
contentLength := resp.Header.Get("Content-Length")
|
||||
if contentLength != "" {
|
||||
size, err := strconv.Atoi(contentLength)
|
||||
if err == nil && size > config.SizeLimit {
|
||||
finalURL := resp.Request.URL.String()
|
||||
c.Redirect(http.StatusMovedPermanently, finalURL)
|
||||
logw("Redirecting to %s due to size limit (%d bytes)", finalURL, size)
|
||||
return fmt.Errorf("response size exceeds limit")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func copyResponseHeaders(resp *req.Response, c *gin.Context, config *config.Config) {
|
||||
headersToRemove := []string{"Content-Security-Policy", "Referrer-Policy", "Strict-Transport-Security"}
|
||||
|
||||
for _, header := range headersToRemove {
|
||||
resp.Header.Del(header)
|
||||
}
|
||||
|
||||
for key, values := range resp.Header {
|
||||
for _, value := range values {
|
||||
c.Header(key, value)
|
||||
}
|
||||
}
|
||||
|
||||
if config.CORSOrigin {
|
||||
c.Header("Access-Control-Allow-Origin", "*")
|
||||
} else {
|
||||
c.Header("Access-Control-Allow-Origin", "")
|
||||
}
|
||||
}
|
||||
|
||||
func handleError(c *gin.Context, message string) {
|
||||
c.String(http.StatusInternalServerError, fmt.Sprintf("server error %v", message))
|
||||
logw(message)
|
||||
}
|
||||
|
||||
func checkURL(u string) []string {
|
||||
for _, exp := range exps {
|
||||
if matches := exp.FindStringSubmatch(u); matches != nil {
|
||||
logw("URL matched: %s, Matches: %v", u, matches[1:])
|
||||
return matches[1:]
|
||||
}
|
||||
}
|
||||
logw("Invalid URL: %s", u)
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
192
proxy/proxy.go
Normal file
192
proxy/proxy.go
Normal file
|
|
@ -0,0 +1,192 @@
|
|||
package proxy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"ghproxy/auth"
|
||||
"ghproxy/config"
|
||||
"ghproxy/logger"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/imroc/req/v3"
|
||||
)
|
||||
|
||||
var (
|
||||
exps = []*regexp.Regexp{
|
||||
regexp.MustCompile(`^(?:https?://)?github\.com/([^/]+)/([^/]+)/(?:releases|archive)/.*`),
|
||||
regexp.MustCompile(`^(?:https?://)?github\.com/([^/]+)/([^/]+)/(?:blob|raw)/.*`),
|
||||
regexp.MustCompile(`^(?:https?://)?github\.com/([^/]+)/([^/]+)/(?:info|git-).*`),
|
||||
regexp.MustCompile(`^(?:https?://)?raw\.github(?:usercontent|)\.com/([^/]+)/([^/]+)/.+?/.+`),
|
||||
regexp.MustCompile(`^(?:https?://)?gist\.github\.com/([^/]+)/.+?/.+`),
|
||||
}
|
||||
)
|
||||
|
||||
var cfg *config.Config
|
||||
var logw = logger.Logw
|
||||
|
||||
func NoRouteHandler(config *config.Config) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
rawPath := strings.TrimPrefix(c.Request.URL.RequestURI(), "/")
|
||||
re := regexp.MustCompile(`^(http:|https:)?/?/?(.*)`)
|
||||
matches := re.FindStringSubmatch(rawPath)
|
||||
|
||||
rawPath = "https://" + matches[2]
|
||||
|
||||
matches = checkURL(rawPath)
|
||||
if matches == nil {
|
||||
c.String(http.StatusForbidden, "Invalid input.")
|
||||
return
|
||||
}
|
||||
|
||||
if exps[1].MatchString(rawPath) {
|
||||
rawPath = strings.Replace(rawPath, "/blob/", "/raw/", 1)
|
||||
}
|
||||
|
||||
if !auth.AuthHandler(c) {
|
||||
c.AbortWithStatusJSON(401, gin.H{"error": "Unauthorized"})
|
||||
logw("Unauthorized request: %s", rawPath)
|
||||
return
|
||||
}
|
||||
|
||||
// 日志记录
|
||||
logw("Request: %s %s", c.Request.Method, rawPath)
|
||||
logw("Matches: %v", matches)
|
||||
|
||||
// 代理请求
|
||||
switch {
|
||||
case exps[0].MatchString(rawPath), exps[1].MatchString(rawPath), exps[3].MatchString(rawPath), exps[4].MatchString(rawPath):
|
||||
logw("%s Matched - USE proxy-chrome", rawPath)
|
||||
proxyRequest(c, rawPath, config, "chrome")
|
||||
case exps[2].MatchString(rawPath):
|
||||
logw("%s Matched - USE proxy-git", rawPath)
|
||||
proxyRequest(c, rawPath, config, "git")
|
||||
default:
|
||||
c.String(http.StatusForbidden, "Invalid input.")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func proxyRequest(c *gin.Context, u string, config *config.Config, mode string) {
|
||||
method := c.Request.Method
|
||||
logw("%s Method: %s", u, method)
|
||||
|
||||
client := req.C()
|
||||
|
||||
switch mode {
|
||||
case "chrome":
|
||||
client.SetUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36").
|
||||
SetTLSFingerprintChrome().
|
||||
ImpersonateChrome()
|
||||
case "git":
|
||||
client.SetUserAgent("git/2.33.1")
|
||||
}
|
||||
|
||||
// 读取请求体
|
||||
body, err := io.ReadAll(c.Request.Body)
|
||||
if err != nil {
|
||||
handleError(c, fmt.Sprintf("Failed to read request body: %v", err))
|
||||
return
|
||||
}
|
||||
defer c.Request.Body.Close()
|
||||
|
||||
// 创建新的请求
|
||||
req := client.R().SetBody(body)
|
||||
|
||||
// 复制请求头
|
||||
for key, values := range c.Request.Header {
|
||||
for _, value := range values {
|
||||
req.SetHeader(key, value)
|
||||
}
|
||||
}
|
||||
|
||||
// 发送请求并处理响应
|
||||
resp, err := sendRequest(req, method, u)
|
||||
if err != nil {
|
||||
handleError(c, fmt.Sprintf("Failed to send request: %v", err))
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// 检查响应内容长度并处理重定向
|
||||
if err := handleResponseSize(resp, config, c); err != nil {
|
||||
logw("Error handling response size: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
copyResponseHeaders(resp, c, config)
|
||||
c.Status(resp.StatusCode)
|
||||
if _, err := io.Copy(c.Writer, resp.Body); err != nil {
|
||||
logw("Failed to copy response body: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func sendRequest(req *req.Request, method, url string) (*req.Response, error) {
|
||||
switch method {
|
||||
case "GET":
|
||||
return req.Get(url)
|
||||
case "POST":
|
||||
return req.Post(url)
|
||||
case "PUT":
|
||||
return req.Put(url)
|
||||
case "DELETE":
|
||||
return req.Delete(url)
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported method: %s", method)
|
||||
}
|
||||
}
|
||||
|
||||
func handleResponseSize(resp *req.Response, config *config.Config, c *gin.Context) error {
|
||||
contentLength := resp.Header.Get("Content-Length")
|
||||
if contentLength != "" {
|
||||
size, err := strconv.Atoi(contentLength)
|
||||
if err == nil && size > config.SizeLimit {
|
||||
finalURL := resp.Request.URL.String()
|
||||
c.Redirect(http.StatusMovedPermanently, finalURL)
|
||||
logw("Redirecting to %s due to size limit (%d bytes)", finalURL, size)
|
||||
return fmt.Errorf("response size exceeds limit")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func copyResponseHeaders(resp *req.Response, c *gin.Context, config *config.Config) {
|
||||
headersToRemove := []string{"Content-Security-Policy", "Referrer-Policy", "Strict-Transport-Security"}
|
||||
|
||||
for _, header := range headersToRemove {
|
||||
resp.Header.Del(header)
|
||||
}
|
||||
|
||||
for key, values := range resp.Header {
|
||||
for _, value := range values {
|
||||
c.Header(key, value)
|
||||
}
|
||||
}
|
||||
|
||||
if config.CORSOrigin {
|
||||
c.Header("Access-Control-Allow-Origin", "*")
|
||||
} else {
|
||||
c.Header("Access-Control-Allow-Origin", "")
|
||||
}
|
||||
}
|
||||
|
||||
func handleError(c *gin.Context, message string) {
|
||||
c.String(http.StatusInternalServerError, fmt.Sprintf("server error %v", message))
|
||||
logw(message)
|
||||
}
|
||||
|
||||
func checkURL(u string) []string {
|
||||
for _, exp := range exps {
|
||||
if matches := exp.FindStringSubmatch(u); matches != nil {
|
||||
logw("URL matched: %s, Matches: %v", u, matches[1:])
|
||||
return matches[1:]
|
||||
}
|
||||
}
|
||||
logw("Invalid URL: %s", u)
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue