package main
import (
"fmt"
"io"
"net/http"
"regexp"
"strings"
"github.com/labstack/echo/v4"
)
const SIZE_LIMIT = 1024 * 1024 * 1024 * 999
var blobToRaw = regexp.MustCompile(`^(?:https?://)?github\.com/([^/]+)/([^/]+)/(?:blob|raw)/.*$`)
var blockedContentTypes = map[string]bool{
"text/html": true,
"application/xhtml+xml": true,
"text/xml": true,
"application/xml": true,
}
func GithubProxyHandler(c echo.Context) error {
rawPath := processPrefix(c.Param("*"))
if !isValidUrl(cfg.Rules, rawPath) {
return c.String(http.StatusForbidden, "无效请求")
}
if blobToRaw.MatchString(rawPath) {
rawPath = strings.Replace(rawPath, "/blob/", "/raw/", 1)
}
return ProxyGitHubRequest(c, rawPath)
}
func ProxyGitHubRequest(c echo.Context, u string) error {
return proxyGitHubWithRedirect(c, u, 0)
}
func proxyGitHubWithRedirect(c echo.Context, u string, redirectCount int) error {
var err error
const maxRedirects = 20
if redirectCount > maxRedirects {
return c.String(http.StatusLoopDetected, "重定向次数过多,可能存在循环重定向")
}
resp := sendRequest(c, u)
if resp.Err != nil && resp.Err != io.EOF {
return c.String(http.StatusInternalServerError, fmt.Sprintf("server error %v", resp.Err))
}
defer func() {
if err := resp.Body.Close(); err != nil {
c.Logger().Errorf("关闭代理响应体失败: %v\n", err.Error())
}
}()
if resp.ContentLength > SIZE_LIMIT {
return c.String(http.StatusRequestEntityTooLarge, fmt.Sprintf("文件过大,限制大小: %d MB", SIZE_LIMIT/(1024*1024)))
}
resp.Header.Del("Content-Security-Policy")
resp.Header.Del("Referrer-Policy")
resp.Header.Del("Strict-Transport-Security")
for key := range resp.Header {
c.Response().Header().Set(key, resp.GetHeader(key))
}
if location, err := resp.Location(); err == nil {
if isValidUrl(cfg.Rules, u) {
return c.Redirect(resp.GetStatusCode(), "/"+location.String())
} else {
if err := proxyGitHubWithRedirect(c, location.String(), redirectCount+1); err != nil {
return err
}
return nil
}
}
if c.Request().Method == "GET" {
if contentType := resp.GetContentType(); blockedContentTypes[strings.ToLower(strings.Split(contentType, ";")[0])] {
return c.JSON(http.StatusForbidden, map[string]string{
"error": "Content type not allowed",
"message": "检测到网页类型,本服务不支持加速网页,请检查您的链接是否正确。",
})
}
}
realHost := c.Request().Header.Get("X-Forwarded-Host")
if realHost == "" {
realHost = c.Request().Host
}
if !strings.HasPrefix(realHost, "http://") && !strings.HasPrefix(realHost, "https://") {
realHost = "https://" + realHost
}
var processedBody = resp.Body
var processedSize int64 = 0
if strings.HasSuffix(strings.ToLower(u), ".sh") || strings.HasSuffix(strings.ToLower(u), ".ps1") || strings.HasSuffix(strings.ToLower(u), ".py") {
isGzipCompressed := resp.GetHeader("") == "gzip"
processedBody, processedSize, err = ProcessSmart(resp.Body, isGzipCompressed, realHost)
if err != nil {
fmt.Printf("智能处理失败,回退到直接代理: %v\n", err)
processedBody = resp.Body
processedSize = 0
}
if processedSize > 0 {
c.Response().Header().Del("Content-Length")
c.Response().Header().Del("Content-Encoding")
c.Response().Header().Set("Transfer-Encoding", "chunked")
}
}
c.Response().WriteHeader(resp.GetStatusCode())
_, err = io.Copy(c.Response().Writer, processedBody)
return err
}