This commit is contained in:
WJQSERVER 2024-10-20 18:16:30 +08:00
parent aee81ba4fd
commit 91b7cfe533
5 changed files with 23 additions and 18 deletions

View file

@ -7,6 +7,7 @@
- ADD: 尝试在核心程序内加入静态页面支持 - ADD: 尝试在核心程序内加入静态页面支持
- CHANGE: 优化日志记录 - CHANGE: 优化日志记录
- CHANGE: 去除部分无用/重复配置 - CHANGE: 去除部分无用/重复配置
- CHANGE: 规范化部分函数命名
24w18d 24w18d
--- ---

View file

@ -17,7 +17,7 @@ var (
var ( var (
logw = logger.Logw logw = logger.Logw
logInfo = logger.LogInfo logInfo = logger.LogInfo
LogWarning = logger.LogWarning logWarning = logger.LogWarning
logError = logger.LogError logError = logger.LogError
) )

View file

@ -11,7 +11,7 @@ import (
var ( var (
logw = logger.Logw logw = logger.Logw
logInfo = logger.LogInfo logInfo = logger.LogInfo
LogWarning = logger.LogWarning logWarning = logger.LogWarning
logError = logger.LogError logError = logger.LogError
) )
@ -38,13 +38,13 @@ func AuthHandler(c *gin.Context, cfg *config.Config) bool {
// 验证 token // 验证 token
if authToken == "" { if authToken == "" {
LogWarning("auth FAILED: no auth_token provided") logWarning("auth FAILED: no auth_token provided")
return false return false
} }
isValid := authToken == cfg.Auth.AuthToken isValid := authToken == cfg.Auth.AuthToken
if !isValid { if !isValid {
LogWarning("auth FAILED: invalid auth_token: %s", authToken) logWarning("auth FAILED: invalid auth_token: %s", authToken)
} }
logInfo("auth SUCCESS: %t", isValid) logInfo("auth SUCCESS: %t", isValid)

10
main.go
View file

@ -26,7 +26,7 @@ var (
var ( var (
logw = logger.Logw logw = logger.Logw
logInfo = logger.LogInfo logInfo = logger.LogInfo
LogWarning = logger.LogWarning logWarning = logger.LogWarning
logError = logger.LogError logError = logger.LogError
) )
@ -83,13 +83,17 @@ func init() {
indexPagePath := fmt.Sprintf("%s/index.html", cfg.Pages.StaticDir) indexPagePath := fmt.Sprintf("%s/index.html", cfg.Pages.StaticDir)
faviconPath := fmt.Sprintf("%s/favicon.ico", cfg.Pages.StaticDir) faviconPath := fmt.Sprintf("%s/favicon.ico", cfg.Pages.StaticDir)
// 静态index页 // 静态index页
router.StaticFile("/", indexPagePath) //router.StaticFile("/", indexPagePath)
router.GET("/", func(c *gin.Context) {
c.File(indexPagePath)
logInfo("IP:%s UA:%s METHOD:%s HTTPv:%s", c.ClientIP(), c.Request.UserAgent(), c.Request.Method, c.Request.Proto)
})
// 静态favicon.ico // 静态favicon.ico
router.StaticFile("/favicon.ico", faviconPath) router.StaticFile("/favicon.ico", faviconPath)
} else if !cfg.Pages.Enabled { } else if !cfg.Pages.Enabled {
router.GET("/", func(c *gin.Context) { router.GET("/", func(c *gin.Context) {
c.String(http.StatusForbidden, "403 Forbidden This route is not allowed to access.") c.String(http.StatusForbidden, "403 Forbidden This route is not allowed to access.")
LogWarning("Forbidden: IP:%s UA:%s METHOD:%s HTTPv:%s", c.ClientIP(), c.Request.UserAgent(), c.Request.Method, c.Request.Proto) logWarning("Forbidden: IP:%s UA:%s METHOD:%s HTTPv:%s", c.ClientIP(), c.Request.UserAgent(), c.Request.Method, c.Request.Proto)
}) })
} }

View file

@ -21,7 +21,7 @@ import (
var ( var (
logw = logger.Logw logw = logger.Logw
logInfo = logger.LogInfo logInfo = logger.LogInfo
LogWarning = logger.LogWarning logWarning = logger.LogWarning
logError = logger.LogError logError = logger.LogError
) )
@ -40,7 +40,7 @@ func NoRouteHandler(cfg *config.Config) gin.HandlerFunc {
matches := re.FindStringSubmatch(rawPath) matches := re.FindStringSubmatch(rawPath)
if len(matches) < 3 { if len(matches) < 3 {
LogWarning("Invalid URL: %s", rawPath) logWarning("Invalid URL: %s", rawPath)
c.String(http.StatusForbidden, "Invalid URL.") c.String(http.StatusForbidden, "Invalid URL.")
return return
} }
@ -51,14 +51,14 @@ func NoRouteHandler(cfg *config.Config) gin.HandlerFunc {
pathmatches := regexp.MustCompile(`^([^/]+)/([^/]+)/([^/]+)/.*`) pathmatches := regexp.MustCompile(`^([^/]+)/([^/]+)/([^/]+)/.*`)
pathParts := pathmatches.FindStringSubmatch(matches[2]) pathParts := pathmatches.FindStringSubmatch(matches[2])
if len(pathParts) < 4 { if len(pathParts) < 4 {
LogWarning("Invalid path: %s", rawPath) logWarning("Invalid path: %s", rawPath)
c.String(http.StatusForbidden, "Invalid path; expected username/repo.") c.String(http.StatusForbidden, "Invalid path; expected username/repo.")
return return
} }
username := pathParts[2] username := pathParts[2]
repo := pathParts[3] repo := pathParts[3]
LogWarning("Blacklist Check > Username: %s, Repo: %s", username, repo) logWarning("Blacklist Check > Username: %s, Repo: %s", username, repo)
fullrepo := fmt.Sprintf("%s/%s", username, repo) fullrepo := fmt.Sprintf("%s/%s", username, repo)
// 白名单检查 // 白名单检查
@ -67,7 +67,7 @@ func NoRouteHandler(cfg *config.Config) gin.HandlerFunc {
if !whitelistpass { if !whitelistpass {
errMsg := fmt.Sprintf("Whitelist Blocked repo: %s", fullrepo) errMsg := fmt.Sprintf("Whitelist Blocked repo: %s", fullrepo)
c.JSON(http.StatusForbidden, gin.H{"error": errMsg}) c.JSON(http.StatusForbidden, gin.H{"error": errMsg})
LogWarning(errMsg) logWarning(errMsg)
return return
} }
} }
@ -78,7 +78,7 @@ func NoRouteHandler(cfg *config.Config) gin.HandlerFunc {
if blacklistpass { if blacklistpass {
errMsg := fmt.Sprintf("Blacklist Blocked repo: %s", fullrepo) errMsg := fmt.Sprintf("Blacklist Blocked repo: %s", fullrepo)
c.JSON(http.StatusForbidden, gin.H{"error": errMsg}) c.JSON(http.StatusForbidden, gin.H{"error": errMsg})
LogWarning(errMsg) logWarning(errMsg)
return return
} }
} }
@ -95,7 +95,7 @@ func NoRouteHandler(cfg *config.Config) gin.HandlerFunc {
if !auth.AuthHandler(c, cfg) { if !auth.AuthHandler(c, cfg) {
c.AbortWithStatusJSON(401, gin.H{"error": "Unauthorized"}) c.AbortWithStatusJSON(401, gin.H{"error": "Unauthorized"})
LogWarning("Unauthorized request: %s", rawPath) logWarning("Unauthorized request: %s", rawPath)
return return
} }
@ -139,7 +139,7 @@ func ProxyRequest(c *gin.Context, u string, cfg *config.Config, mode string) {
defer resp.Body.Close() defer resp.Body.Close()
if err := HandleResponseSize(resp, cfg, c); err != nil { if err := HandleResponseSize(resp, cfg, c); err != nil {
LogWarning("Error handling response size: %v", err) logWarning("Error handling response size: %v", err)
return return
} }
@ -211,7 +211,7 @@ func HandleResponseSize(resp *req.Response, cfg *config.Config, c *gin.Context)
if err == nil && size > cfg.Server.SizeLimit { if err == nil && size > cfg.Server.SizeLimit {
finalURL := resp.Request.URL.String() finalURL := resp.Request.URL.String()
c.Redirect(http.StatusMovedPermanently, finalURL) c.Redirect(http.StatusMovedPermanently, finalURL)
LogWarning("Size limit exceeded: %s, Size: %d", finalURL, size) logWarning("Size limit exceeded: %s, Size: %d", finalURL, size)
return fmt.Errorf("size limit exceeded: %d", size) return fmt.Errorf("size limit exceeded: %d", size)
} }
} }
@ -268,7 +268,7 @@ func setDefaultHeaders(c *gin.Context) {
func HandleError(c *gin.Context, message string) { func HandleError(c *gin.Context, message string) {
c.String(http.StatusInternalServerError, fmt.Sprintf("server error %v", message)) c.String(http.StatusInternalServerError, fmt.Sprintf("server error %v", message))
LogWarning(message) logWarning(message)
} }
func CheckURL(u string) []string { func CheckURL(u string) []string {
@ -279,6 +279,6 @@ func CheckURL(u string) []string {
} }
} }
errMsg := fmt.Sprintf("Invalid URL: %s", u) errMsg := fmt.Sprintf("Invalid URL: %s", u)
LogWarning(errMsg) logWarning(errMsg)
return nil return nil
} }