Compare commits

...

6 Commits

Author SHA1 Message Date
86393d25e9 feat(ilanzou): enable local sorting 2025-07-25 17:32:16 +08:00
4df53c965c feat(cloudreve): enable local sorting 2025-07-25 17:32:00 +08:00
da8d6607cf fix(static): support logo replacement (#834 Close #754) 2025-07-25 17:12:51 +08:00
6134574dac fix(fs): rename bug (#832)
* fix(fs): rename bug

* chore

* fix bug

* .

---------

Co-authored-by: j2rong4cn <j2rong@qq.com>
2025-07-25 13:42:39 +08:00
b273232f87 refactor(log): redir utils.Log to logrus after init (#833) 2025-07-25 13:38:45 +08:00
358e4d851e refactor(log): filter (#816) 2025-07-25 11:33:27 +08:00
14 changed files with 132 additions and 187 deletions

View File

@ -48,7 +48,15 @@ the address is defined in config file`,
gin.SetMode(gin.ReleaseMode)
}
r := gin.New()
r.Use(middlewares.HTTPFilteredLogger(), gin.RecoveryWithWriter(log.StandardLogger().Out))
// gin log
if conf.Conf.Log.Filter.Enable {
r.Use(middlewares.FilteredLogger())
} else {
r.Use(gin.LoggerWithWriter(log.StandardLogger().Out))
}
r.Use(gin.RecoveryWithWriter(log.StandardLogger().Out))
server.Init(r)
var httpHandler http.Handler = r
if conf.Conf.Scheme.EnableH2c {
@ -103,7 +111,7 @@ the address is defined in config file`,
}
if conf.Conf.S3.Port != -1 && conf.Conf.S3.Enable {
s3r := gin.New()
s3r.Use(middlewares.S3FilteredLogger(), gin.RecoveryWithWriter(log.StandardLogger().Out))
s3r.Use(gin.LoggerWithWriter(log.StandardLogger().Out), gin.RecoveryWithWriter(log.StandardLogger().Out))
server.InitS3(s3r)
s3Base := fmt.Sprintf("%s:%d", conf.Conf.Scheme.Address, conf.Conf.S3.Port)
utils.Log.Infof("start S3 server @ %s", s3Base)

View File

@ -20,6 +20,7 @@ type Addition struct {
var config = driver.Config{
Name: "Cloudreve",
DefaultRoot: "/",
LocalSort: true,
}
func init() {

View File

@ -31,6 +31,7 @@ func init() {
config: driver.Config{
Name: "ILanZou",
DefaultRoot: "0",
LocalSort: true,
},
conf: Conf{
base: "https://api.ilanzou.com",
@ -48,6 +49,7 @@ func init() {
config: driver.Config{
Name: "FeijiPan",
DefaultRoot: "0",
LocalSort: true,
},
conf: Conf{
base: "https://api.feijipan.com",

View File

@ -82,6 +82,9 @@ func InitConfig() {
if !conf.Conf.Force {
confFromEnv()
}
if len(conf.Conf.Log.Filter.Filters) == 0 {
conf.Conf.Log.Filter.Enable = false
}
// convert abs path
convertAbsPath := func(path *string) {
if !filepath.IsAbs(*path) {

View File

@ -53,4 +53,5 @@ func Log() {
}
log.SetOutput(logrus.StandardLogger().Out)
utils.Log.Infof("init logrus...")
utils.Log = logrus.StandardLogger()
}

View File

@ -38,38 +38,24 @@ type Scheme struct {
}
type LogConfig struct {
Enable bool `json:"enable" env:"LOG_ENABLE"`
Name string `json:"name" env:"LOG_NAME"`
MaxSize int `json:"max_size" env:"MAX_SIZE"`
MaxBackups int `json:"max_backups" env:"MAX_BACKUPS"`
MaxAge int `json:"max_age" env:"MAX_AGE"`
Compress bool `json:"compress" env:"COMPRESS"`
Filter LogFilterConfig `json:"filter"` // Log filtering configuration (config file only, no env support)
Enable bool `json:"enable" env:"ENABLE"`
Name string `json:"name" env:"NAME"`
MaxSize int `json:"max_size" env:"MAX_SIZE"`
MaxBackups int `json:"max_backups" env:"MAX_BACKUPS"`
MaxAge int `json:"max_age" env:"MAX_AGE"`
Compress bool `json:"compress" env:"COMPRESS"`
Filter LogFilterConfig `json:"filter" envPrefix:"FILTER_"`
}
// LogFilterConfig holds configuration for log filtering
// Note: This configuration is only supported via config file, not environment variables
type LogFilterConfig struct {
// EnableFiltering controls whether log filtering is enabled
EnableFiltering bool `json:"enable_filtering"`
// FilterHealthChecks controls whether to filter health check requests
FilterHealthChecks bool `json:"filter_health_checks"`
// FilterWebDAV controls whether to filter WebDAV requests (only for HTTP server)
FilterWebDAV bool `json:"filter_webdav"`
// FilterHEADRequests controls whether to filter HEAD requests
FilterHEADRequests bool `json:"filter_head_requests"`
// CustomSkipPaths allows adding custom paths to skip
CustomSkipPaths []string `json:"custom_skip_paths"`
// CustomSkipMethods allows adding custom methods to skip
CustomSkipMethods []string `json:"custom_skip_methods"`
// CustomSkipPrefixes allows adding custom path prefixes to skip
CustomSkipPrefixes []string `json:"custom_skip_prefixes"`
Enable bool `json:"enable" env:"ENABLE"`
Filters []Filter `json:"filters"`
}
type Filter struct {
CIDR string `json:"cidr"`
Path string `json:"path"`
Method string `json:"method"`
}
type TaskConfig struct {
@ -131,7 +117,7 @@ type Config struct {
TempDir string `json:"temp_dir" env:"TEMP_DIR"`
BleveDir string `json:"bleve_dir" env:"BLEVE_DIR"`
DistDir string `json:"dist_dir"`
Log LogConfig `json:"log"`
Log LogConfig `json:"log" envPrefix:"LOG_"`
DelayedStart int `json:"delayed_start" env:"DELAYED_START"`
MaxConnections int `json:"max_connections" env:"MAX_CONNECTIONS"`
MaxConcurrency int `json:"max_concurrency" env:"MAX_CONCURRENCY"`
@ -179,13 +165,12 @@ func DefaultConfig(dataDir string) *Config {
MaxBackups: 30,
MaxAge: 28,
Filter: LogFilterConfig{
EnableFiltering: true,
FilterHealthChecks: true,
FilterWebDAV: true,
FilterHEADRequests: true,
CustomSkipPaths: []string{},
CustomSkipMethods: []string{},
CustomSkipPrefixes: []string{},
Enable: false,
Filters: []Filter{
{Path: "/ping"},
{Method: "HEAD"},
{Path: "/dav/", Method: "PROPFIND"},
},
},
},
MaxConnections: 0,

View File

@ -17,7 +17,7 @@ const (
AllowMounted = "allow_mounted"
RobotsTxt = "robots_txt"
Logo = "logo"
Logo = "logo" // multi-lines text, L1: light, EOL: dark
Favicon = "favicon"
MainColor = "main_color"

View File

@ -75,20 +75,12 @@ func EncodePath(path string, all ...bool) string {
}
func JoinBasePath(basePath, reqPath string) (string, error) {
reqPath, err := CheckRelativePath(reqPath)
if err != nil {
return "", err
}
return stdpath.Join(FixAndCleanPath(basePath), reqPath), nil
}
func CheckRelativePath(path string) (string, error) {
isRelativePath := strings.Contains(path, "..")
path = FixAndCleanPath(path)
if isRelativePath && !strings.Contains(path, "..") {
isRelativePath := strings.Contains(reqPath, "..")
reqPath = FixAndCleanPath(reqPath)
if isRelativePath && !strings.Contains(reqPath, "..") {
return "", errs.RelativePath
}
return path, nil
return stdpath.Join(FixAndCleanPath(basePath), reqPath), nil
}
func GetFullPath(mountPath, path string) string {

View File

@ -11,7 +11,6 @@ import (
"github.com/OpenListTeam/OpenList/v4/internal/model"
"github.com/OpenListTeam/OpenList/v4/internal/op"
"github.com/OpenListTeam/OpenList/v4/pkg/generic"
"github.com/OpenListTeam/OpenList/v4/pkg/utils"
"github.com/OpenListTeam/OpenList/v4/server/common"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
@ -174,7 +173,7 @@ func FsBatchRename(c *gin.Context) {
if renameObject.SrcName == "" || renameObject.NewName == "" {
continue
}
renameObject.NewName, err = utils.CheckRelativePath(renameObject.NewName)
err = checkRelativePath(renameObject.NewName)
if err != nil {
common.ErrorResp(c, err, 403)
return
@ -235,7 +234,8 @@ func FsRegexRename(c *gin.Context) {
for _, file := range files {
if srcRegexp.MatchString(file.GetName()) {
newFileName, err := utils.CheckRelativePath(srcRegexp.ReplaceAllString(file.GetName(), req.NewNameRegex))
newFileName := srcRegexp.ReplaceAllString(file.GetName(), req.NewNameRegex)
err := checkRelativePath(newFileName)
if err != nil {
common.ErrorResp(c, err, 403)
return

View File

@ -3,6 +3,7 @@ package handles
import (
"fmt"
stdpath "path"
"strings"
"github.com/OpenListTeam/OpenList/v4/internal/conf"
"github.com/OpenListTeam/OpenList/v4/internal/task"
@ -205,7 +206,7 @@ func FsRename(c *gin.Context) {
}
reqPath, err := user.JoinPath(req.Path)
if err == nil {
req.Name, err = utils.CheckRelativePath(req.Name)
err = checkRelativePath(req.Name)
}
if err != nil {
common.ErrorResp(c, err, 403)
@ -227,6 +228,13 @@ func FsRename(c *gin.Context) {
common.SuccessResp(c)
}
func checkRelativePath(path string) error {
if strings.ContainsAny(path, "/\\") || path == "" || path == "." || path == ".." {
return errs.RelativePath
}
return nil
}
type RemoveReq struct {
Dir string `json:"dir"`
Names []string `json:"names"`

View File

@ -1,55 +0,0 @@
package middlewares
import (
"github.com/OpenListTeam/OpenList/v4/internal/conf"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
// UnifiedFilteredLogger returns a filtered logger using global configuration
// serverType: "http" for main HTTP server, "s3" for S3 server
func UnifiedFilteredLogger(serverType string) gin.HandlerFunc {
config := conf.Conf.Log.Filter
if !config.EnableFiltering {
// Return standard Gin logger if filtering is disabled
return gin.LoggerWithWriter(log.StandardLogger().Out)
}
loggerConfig := FilteredLoggerConfig{
Output: log.StandardLogger().Out,
}
// Add health check paths
if config.FilterHealthChecks {
loggerConfig.SkipPaths = append(loggerConfig.SkipPaths, "/ping")
}
// Add HEAD method filtering
if config.FilterHEADRequests {
loggerConfig.SkipMethods = append(loggerConfig.SkipMethods, "HEAD")
}
// Add WebDAV filtering only for HTTP server (not for S3)
if config.FilterWebDAV && serverType == "http" {
loggerConfig.SkipPathPrefixes = append(loggerConfig.SkipPathPrefixes, "/dav/")
loggerConfig.SkipMethods = append(loggerConfig.SkipMethods, "PROPFIND")
}
// Add custom configurations
loggerConfig.SkipPaths = append(loggerConfig.SkipPaths, config.CustomSkipPaths...)
loggerConfig.SkipMethods = append(loggerConfig.SkipMethods, config.CustomSkipMethods...)
loggerConfig.SkipPathPrefixes = append(loggerConfig.SkipPathPrefixes, config.CustomSkipPrefixes...)
return FilteredLoggerWithConfig(loggerConfig)
}
// HTTPFilteredLogger returns a filtered logger for the main HTTP server
func HTTPFilteredLogger() gin.HandlerFunc {
return UnifiedFilteredLogger("http")
}
// S3FilteredLogger returns a filtered logger for the S3 server
func S3FilteredLogger() gin.HandlerFunc {
return UnifiedFilteredLogger("s3")
}

View File

@ -1,101 +1,99 @@
package middlewares
import (
"fmt"
"io"
"net/netip"
"strings"
"time"
"github.com/OpenListTeam/OpenList/v4/internal/conf"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
// FilteredLoggerConfig defines the configuration for the filtered logger
type FilteredLoggerConfig struct {
// SkipPaths is a list of URL paths to skip logging
SkipPaths []string
// SkipMethods is a list of HTTP methods to skip logging
SkipMethods []string
// SkipPathPrefixes is a list of URL path prefixes to skip logging
SkipPathPrefixes []string
// Output is the writer where logs will be written
Output io.Writer
type filter struct {
CIDR *netip.Prefix `json:"cidr,omitempty"`
Path *string `json:"path,omitempty"`
Method *string `json:"method,omitempty"`
}
// FilteredLoggerWithConfig returns a gin.HandlerFunc (middleware) that logs requests
// but skips logging for specified paths, methods, or path prefixes
func FilteredLoggerWithConfig(config FilteredLoggerConfig) gin.HandlerFunc {
if config.Output == nil {
config.Output = log.StandardLogger().Out
}
var filterList []*filter
return gin.LoggerWithConfig(gin.LoggerConfig{
Output: config.Output,
SkipPaths: config.SkipPaths,
Formatter: func(param gin.LogFormatterParams) string {
// Skip logging for health check endpoints
if shouldSkipLogging(param.Path, param.Method, config) {
return ""
func initFilterList() {
for _, s := range conf.Conf.Log.Filter.Filters {
f := new(filter)
if s.CIDR != "" {
cidr, err := netip.ParsePrefix(s.CIDR)
if err != nil {
log.Errorf("failed to parse CIDR %s: %v", s.CIDR, err)
continue
}
f.CIDR = &cidr
}
// Use a custom log format similar to Gin's default
return defaultLogFormatter(param)
},
})
if s.Path != "" {
f.Path = &s.Path
}
if s.Method != "" {
f.Method = &s.Method
}
if f.CIDR == nil && f.Path == nil && f.Method == nil {
log.Warnf("filter %s is empty, skipping", s)
continue
}
filterList = append(filterList, f)
log.Debugf("added filter: %+v", f)
}
log.Infof("Loaded %d log filters.", len(filterList))
}
func skiperDecider(c *gin.Context) bool {
// every filter need metch all condithon as filter match
// so if any condithon not metch, skip this filter
// all filters misatch, log this request
// shouldSkipLogging determines if a request should be skipped from logging
func shouldSkipLogging(path, method string, config FilteredLoggerConfig) bool {
// Check if path should be skipped
for _, skipPath := range config.SkipPaths {
if path == skipPath {
return true
for _, f := range filterList {
if f.CIDR != nil {
cip := netip.MustParseAddr(c.ClientIP())
if !f.CIDR.Contains(cip) {
continue
}
}
}
// Check if method should be skipped
for _, skipMethod := range config.SkipMethods {
if method == skipMethod {
return true
if f.Path != nil {
if (*f.Path)[0] == '/' {
// match path as prefix/exact path
if !strings.HasPrefix(c.Request.URL.Path, *f.Path) {
continue
}
} else {
// match path as relative path
if !strings.Contains(c.Request.URL.Path, "/"+*f.Path) {
continue
}
}
}
}
// Check if path prefix should be skipped
for _, skipPrefix := range config.SkipPathPrefixes {
if strings.HasPrefix(path, skipPrefix) {
return true
if f.Method != nil {
if *f.Method != c.Request.Method {
continue
}
}
}
// Special case: Skip PROPFIND requests (common in WebDAV)
if method == "PROPFIND" {
return true
}
return false
}
// defaultLogFormatter provides a default log format similar to Gin's built-in formatter
func defaultLogFormatter(param gin.LogFormatterParams) string {
var statusColor, methodColor, resetColor string
if param.IsOutputColor() {
statusColor = param.StatusCodeColor()
methodColor = param.MethodColor()
resetColor = param.ResetColor()
}
func FilteredLogger() gin.HandlerFunc {
initFilterList()
if param.Latency > time.Minute {
param.Latency = param.Latency.Truncate(time.Second)
}
return fmt.Sprintf("[GIN] %v |%s %3d %s| %13v | %15s |%s %-7s %s %#v\n%s",
param.TimeStamp.Format("2006/01/02 - 15:04:05"),
statusColor, param.StatusCode, resetColor,
param.Latency,
param.ClientIP,
methodColor, param.Method, resetColor,
param.Path,
param.ErrorMessage,
)
}
return gin.LoggerWithConfig(gin.LoggerConfig{
Output: log.StandardLogger().Out,
Skip: skiperDecider,
})
}

View File

@ -15,7 +15,7 @@ type SiteConfig struct {
func getSiteConfig() SiteConfig {
siteConfig := SiteConfig{
BasePath: conf.URL.Path,
Cdn: strings.ReplaceAll(strings.TrimSuffix(conf.Conf.Cdn, "/"), "$version", strings.TrimPrefix(conf.WebVersion, "v"),),
Cdn: strings.ReplaceAll(strings.TrimSuffix(conf.Conf.Cdn, "/"), "$version", strings.TrimPrefix(conf.WebVersion, "v")),
}
if siteConfig.BasePath != "" {
siteConfig.BasePath = utils.FixAndCleanPath(siteConfig.BasePath)

View File

@ -41,9 +41,8 @@ func replaceStrings(content string, replacements map[string]string) string {
return content
}
func initIndex() {
func initIndex(siteConfig SiteConfig) {
utils.Log.Debug("Initializing index.html...")
siteConfig := getSiteConfig()
// dist_dir is empty and cdn is not empty add web_version is empty or beta or dev
if conf.Conf.DistDir == "" && conf.Conf.Cdn != "" && (conf.WebVersion == "" || conf.WebVersion == "beta" || conf.WebVersion == "dev") {
utils.Log.Infof("Fetching index.html from CDN: %s/index.html...", conf.Conf.Cdn)
@ -89,6 +88,7 @@ func initIndex() {
func UpdateIndex() {
utils.Log.Debug("Updating index.html with settings...")
favicon := setting.GetStr(conf.Favicon)
logo := strings.Split(setting.GetStr(conf.Logo), "\n")[0]
title := setting.GetStr(conf.SiteTitle)
customizeHead := setting.GetStr(conf.CustomizeHead)
customizeBody := setting.GetStr(conf.CustomizeBody)
@ -96,6 +96,7 @@ func UpdateIndex() {
utils.Log.Debug("Applying replacements for default pages...")
replaceMap1 := map[string]string{
"https://cdn.oplist.org/gh/OpenListTeam/Logo@main/logo.svg": favicon,
"https://cdn.oplist.org/gh/OpenListTeam/Logo@main/logo.png": logo,
"Loading...": title,
"main_color: undefined": fmt.Sprintf("main_color: '%s'", mainColor),
}
@ -111,8 +112,9 @@ func UpdateIndex() {
func Static(r *gin.RouterGroup, noRoute func(handlers ...gin.HandlerFunc)) {
utils.Log.Debug("Setting up static routes...")
siteConfig := getSiteConfig()
initStatic()
initIndex()
initIndex(siteConfig)
folders := []string{"assets", "images", "streamer", "static"}
if conf.Conf.Cdn == "" {
utils.Log.Debug("Setting up static file serving...")
@ -136,7 +138,7 @@ func Static(r *gin.RouterGroup, noRoute func(handlers ...gin.HandlerFunc)) {
for _, folder := range folders {
r.GET(fmt.Sprintf("/%s/*filepath", folder), func(c *gin.Context) {
filepath := c.Param("filepath")
c.Redirect(http.StatusFound, fmt.Sprintf("%s/%s%s", conf.Conf.Cdn, folder, filepath))
c.Redirect(http.StatusFound, fmt.Sprintf("%s/%s%s", siteConfig.Cdn, folder, filepath))
})
}
}