From 8a50b311fcae046be5f4eb80f1aa4b54c720934a Mon Sep 17 00:00:00 2001 From: wjqserver <114663932+WJQSERVER@users.noreply.github.com> Date: Mon, 7 Apr 2025 18:51:22 +0800 Subject: [PATCH] 25w27a --- CHANGELOG.md | 7 +++++++ DEV-VERSION | 2 +- config/config.go | 16 ++++++++++------ config/config.toml | 2 ++ deploy/config.toml | 2 ++ main.go | 47 +++++++++++++++++++++++++++++++++++++++------- proxy/chunkreq.go | 1 + 7 files changed, 63 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f7d0c6f..8b7bbb4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # 更新日志 +25w27a - 2025-04-07 +--- +- PRE-RELEASE: 此版本是v3.0.1的预发布版本,请勿在生产环境中使用; +- CHANGE: 加入`memLimit`指示gc +- CHANGE: 加入`hlog`输出路径配置 +- CHANGE: 修正H2C配置问题 + 3.0.0 - 2025-04-04 --- - RELEASE: Next Gen; 下一个起点; diff --git a/DEV-VERSION b/DEV-VERSION index ae7572d..03ab3b3 100644 --- a/DEV-VERSION +++ b/DEV-VERSION @@ -1 +1 @@ -25w26a \ No newline at end of file +25w27a \ No newline at end of file diff --git a/config/config.go b/config/config.go index 1ec7707..1bf1bdc 100644 --- a/config/config.go +++ b/config/config.go @@ -32,6 +32,7 @@ type ServerConfig struct { Port int `toml:"port"` Host string `toml:"host"` SizeLimit int `toml:"sizeLimit"` + MemLimit int64 `toml:"memLimit"` H2C bool `toml:"H2C"` Cors string `toml:"cors"` Debug bool `toml:"debug"` @@ -86,9 +87,10 @@ type PagesConfig struct { } type LogConfig struct { - LogFilePath string `toml:"logFilePath"` - MaxLogSize int `toml:"maxLogSize"` - Level string `toml:"level"` + LogFilePath string `toml:"logFilePath"` + MaxLogSize int `toml:"maxLogSize"` + Level string `toml:"level"` + HertZLogPath string `toml:"hertzLogPath"` } /* @@ -179,6 +181,7 @@ func DefaultConfig() *Config { Port: 8080, Host: "0.0.0.0", SizeLimit: 125, + MemLimit: 0, H2C: true, Cors: "*", Debug: false, @@ -204,9 +207,10 @@ func DefaultConfig() *Config { StaticDir: "/data/www", }, Log: LogConfig{ - LogFilePath: "/data/ghproxy/log/ghproxy.log", - MaxLogSize: 10, - Level: "info", + LogFilePath: "/data/ghproxy/log/ghproxy.log", + MaxLogSize: 10, + Level: "info", + HertZLogPath: "/data/ghproxy/log/hertz.log", }, Auth: AuthConfig{ Enabled: false, diff --git a/config/config.toml b/config/config.toml index ef27c6d..92b81f8 100644 --- a/config/config.toml +++ b/config/config.toml @@ -2,6 +2,7 @@ host = "0.0.0.0" port = 8080 sizeLimit = 125 # MB +memLimit = 0 # MB H2C = true cors = "*" # "*"/"" -> "*" ; "nil" -> "" ; debug = false @@ -30,6 +31,7 @@ staticDir = "/data/www" logFilePath = "/data/ghproxy/log/ghproxy.log" maxLogSize = 5 # MB level = "info" # dump, debug, info, warn, error, none +hertzLogPath = "/data/ghproxy/log/hertz.log" [auth] method = "parameters" # "header" or "parameters" diff --git a/deploy/config.toml b/deploy/config.toml index 1c7e69a..a25acf1 100644 --- a/deploy/config.toml +++ b/deploy/config.toml @@ -2,6 +2,7 @@ host = "127.0.0.1" port = 8080 sizeLimit = 125 # MB +memLimit = 0 # MB H2C = true cors = "*" # "*"/"" -> "*" ; "nil" -> "" ; debug = false @@ -30,6 +31,7 @@ staticDir = "/usr/local/ghproxy/pages" logFilePath = "/usr/local/ghproxy/log/ghproxy.log" maxLogSize = 5 # MB level = "info" # dump, debug, info, warn, error, none +hertzLogPath = "/usr/local/ghproxy/log/hertz.log" [auth] authMethod = "parameters" # "header" or "parameters" diff --git a/main.go b/main.go index aabb054..c59293b 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "io/fs" "net/http" "os" + "runtime/debug" "time" "ghproxy/api" @@ -23,6 +24,7 @@ import ( "github.com/cloudwego/hertz/pkg/app/middlewares/server/recovery" "github.com/cloudwego/hertz/pkg/app/server" "github.com/cloudwego/hertz/pkg/common/adaptor" + "github.com/cloudwego/hertz/pkg/common/hlog" "github.com/hertz-contrib/http2/factory" ) @@ -31,6 +33,7 @@ var ( cfg *config.Config r *server.Hertz configfile = "/data/ghproxy/config/config.toml" + hertZfile *os.File cfgfile string version string runMode string @@ -129,7 +132,29 @@ func setupLogger(cfg *config.Config) { fmt.Printf("Log Level: %s\n", cfg.Log.Level) logDebug("Config File Path: ", cfgfile) logDebug("Loaded config: %v\n", cfg) - logInfo("Init Completed") + logInfo("Logger Initialized Successfully") +} + +func setupHertZLogger(cfg *config.Config) { + var err error + + if cfg.Log.HertZLogPath != "" { + hertZfile, err = os.OpenFile(cfg.Log.HertZLogPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) + if err != nil { + fmt.Printf("Failed to open log file: %v\n", err) + os.Exit(1) + } + + hlog.SetOutput(hertZfile) + } + +} + +func setMemLimit(cfg *config.Config) { + if cfg.Server.MemLimit > 0 { + debug.SetMemoryLimit((cfg.Server.MemLimit) * 1024 * 1024) + logInfo("Set Memory Limit to %d MB", cfg.Server.MemLimit) + } } func loadlist(cfg *config.Config) { @@ -315,7 +340,9 @@ func init() { loadConfig() if cfg != nil { // 在setupLogger前添加空值检查 setupLogger(cfg) + setupHertZLogger(cfg) InitReq(cfg) + setMemLimit(cfg) loadlist(cfg) setupRateLimit(cfg) @@ -346,12 +373,17 @@ func main() { addr := fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port) - r := server.New( - server.WithHostPorts(addr), - server.WithH2C(true), - ) - - r.AddProtocol("h2", factory.NewServerFactory()) + if cfg.Server.H2C { + r = server.New( + server.WithHostPorts(addr), + server.WithH2C(true), + ) + r.AddProtocol("h2", factory.NewServerFactory()) + } else { + r = server.New( + server.WithHostPorts(addr), + ) + } // 添加Recovery中间件 r.Use(recovery.Recovery()) @@ -414,5 +446,6 @@ func main() { r.Spin() defer logger.Close() + defer hertZfile.Close() fmt.Println("Program Exit") } diff --git a/proxy/chunkreq.go b/proxy/chunkreq.go index dd2b1ee..0212e0d 100644 --- a/proxy/chunkreq.go +++ b/proxy/chunkreq.go @@ -70,6 +70,7 @@ func ChunkedProxyRequest(ctx context.Context, c *app.RequestContext, u string, c // 错误处理(404) if resp.StatusCode == 404 { c.String(http.StatusNotFound, "File Not Found") + //c.Status(http.StatusNotFound) return }