This commit is contained in:
wjqserver 2025-04-07 18:51:22 +08:00
parent dcc50401c4
commit 8a50b311fc
7 changed files with 63 additions and 14 deletions

View file

@ -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 3.0.0 - 2025-04-04
--- ---
- RELEASE: Next Gen; 下一个起点; - RELEASE: Next Gen; 下一个起点;

View file

@ -1 +1 @@
25w26a 25w27a

View file

@ -32,6 +32,7 @@ type ServerConfig struct {
Port int `toml:"port"` Port int `toml:"port"`
Host string `toml:"host"` Host string `toml:"host"`
SizeLimit int `toml:"sizeLimit"` SizeLimit int `toml:"sizeLimit"`
MemLimit int64 `toml:"memLimit"`
H2C bool `toml:"H2C"` H2C bool `toml:"H2C"`
Cors string `toml:"cors"` Cors string `toml:"cors"`
Debug bool `toml:"debug"` Debug bool `toml:"debug"`
@ -86,9 +87,10 @@ type PagesConfig struct {
} }
type LogConfig struct { type LogConfig struct {
LogFilePath string `toml:"logFilePath"` LogFilePath string `toml:"logFilePath"`
MaxLogSize int `toml:"maxLogSize"` MaxLogSize int `toml:"maxLogSize"`
Level string `toml:"level"` Level string `toml:"level"`
HertZLogPath string `toml:"hertzLogPath"`
} }
/* /*
@ -179,6 +181,7 @@ func DefaultConfig() *Config {
Port: 8080, Port: 8080,
Host: "0.0.0.0", Host: "0.0.0.0",
SizeLimit: 125, SizeLimit: 125,
MemLimit: 0,
H2C: true, H2C: true,
Cors: "*", Cors: "*",
Debug: false, Debug: false,
@ -204,9 +207,10 @@ func DefaultConfig() *Config {
StaticDir: "/data/www", StaticDir: "/data/www",
}, },
Log: LogConfig{ Log: LogConfig{
LogFilePath: "/data/ghproxy/log/ghproxy.log", LogFilePath: "/data/ghproxy/log/ghproxy.log",
MaxLogSize: 10, MaxLogSize: 10,
Level: "info", Level: "info",
HertZLogPath: "/data/ghproxy/log/hertz.log",
}, },
Auth: AuthConfig{ Auth: AuthConfig{
Enabled: false, Enabled: false,

View file

@ -2,6 +2,7 @@
host = "0.0.0.0" host = "0.0.0.0"
port = 8080 port = 8080
sizeLimit = 125 # MB sizeLimit = 125 # MB
memLimit = 0 # MB
H2C = true H2C = true
cors = "*" # "*"/"" -> "*" ; "nil" -> "" ; cors = "*" # "*"/"" -> "*" ; "nil" -> "" ;
debug = false debug = false
@ -30,6 +31,7 @@ staticDir = "/data/www"
logFilePath = "/data/ghproxy/log/ghproxy.log" logFilePath = "/data/ghproxy/log/ghproxy.log"
maxLogSize = 5 # MB maxLogSize = 5 # MB
level = "info" # dump, debug, info, warn, error, none level = "info" # dump, debug, info, warn, error, none
hertzLogPath = "/data/ghproxy/log/hertz.log"
[auth] [auth]
method = "parameters" # "header" or "parameters" method = "parameters" # "header" or "parameters"

View file

@ -2,6 +2,7 @@
host = "127.0.0.1" host = "127.0.0.1"
port = 8080 port = 8080
sizeLimit = 125 # MB sizeLimit = 125 # MB
memLimit = 0 # MB
H2C = true H2C = true
cors = "*" # "*"/"" -> "*" ; "nil" -> "" ; cors = "*" # "*"/"" -> "*" ; "nil" -> "" ;
debug = false debug = false
@ -30,6 +31,7 @@ staticDir = "/usr/local/ghproxy/pages"
logFilePath = "/usr/local/ghproxy/log/ghproxy.log" logFilePath = "/usr/local/ghproxy/log/ghproxy.log"
maxLogSize = 5 # MB maxLogSize = 5 # MB
level = "info" # dump, debug, info, warn, error, none level = "info" # dump, debug, info, warn, error, none
hertzLogPath = "/usr/local/ghproxy/log/hertz.log"
[auth] [auth]
authMethod = "parameters" # "header" or "parameters" authMethod = "parameters" # "header" or "parameters"

47
main.go
View file

@ -8,6 +8,7 @@ import (
"io/fs" "io/fs"
"net/http" "net/http"
"os" "os"
"runtime/debug"
"time" "time"
"ghproxy/api" "ghproxy/api"
@ -23,6 +24,7 @@ import (
"github.com/cloudwego/hertz/pkg/app/middlewares/server/recovery" "github.com/cloudwego/hertz/pkg/app/middlewares/server/recovery"
"github.com/cloudwego/hertz/pkg/app/server" "github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/common/adaptor" "github.com/cloudwego/hertz/pkg/common/adaptor"
"github.com/cloudwego/hertz/pkg/common/hlog"
"github.com/hertz-contrib/http2/factory" "github.com/hertz-contrib/http2/factory"
) )
@ -31,6 +33,7 @@ var (
cfg *config.Config cfg *config.Config
r *server.Hertz r *server.Hertz
configfile = "/data/ghproxy/config/config.toml" configfile = "/data/ghproxy/config/config.toml"
hertZfile *os.File
cfgfile string cfgfile string
version string version string
runMode string runMode string
@ -129,7 +132,29 @@ func setupLogger(cfg *config.Config) {
fmt.Printf("Log Level: %s\n", cfg.Log.Level) fmt.Printf("Log Level: %s\n", cfg.Log.Level)
logDebug("Config File Path: ", cfgfile) logDebug("Config File Path: ", cfgfile)
logDebug("Loaded config: %v\n", cfg) 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) { func loadlist(cfg *config.Config) {
@ -315,7 +340,9 @@ func init() {
loadConfig() loadConfig()
if cfg != nil { // 在setupLogger前添加空值检查 if cfg != nil { // 在setupLogger前添加空值检查
setupLogger(cfg) setupLogger(cfg)
setupHertZLogger(cfg)
InitReq(cfg) InitReq(cfg)
setMemLimit(cfg)
loadlist(cfg) loadlist(cfg)
setupRateLimit(cfg) setupRateLimit(cfg)
@ -346,12 +373,17 @@ func main() {
addr := fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port) addr := fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port)
r := server.New( if cfg.Server.H2C {
server.WithHostPorts(addr), r = server.New(
server.WithH2C(true), server.WithHostPorts(addr),
) server.WithH2C(true),
)
r.AddProtocol("h2", factory.NewServerFactory()) r.AddProtocol("h2", factory.NewServerFactory())
} else {
r = server.New(
server.WithHostPorts(addr),
)
}
// 添加Recovery中间件 // 添加Recovery中间件
r.Use(recovery.Recovery()) r.Use(recovery.Recovery())
@ -414,5 +446,6 @@ func main() {
r.Spin() r.Spin()
defer logger.Close() defer logger.Close()
defer hertZfile.Close()
fmt.Println("Program Exit") fmt.Println("Program Exit")
} }

View file

@ -70,6 +70,7 @@ func ChunkedProxyRequest(ctx context.Context, c *app.RequestContext, u string, c
// 错误处理(404) // 错误处理(404)
if resp.StatusCode == 404 { if resp.StatusCode == 404 {
c.String(http.StatusNotFound, "File Not Found") c.String(http.StatusNotFound, "File Not Found")
//c.Status(http.StatusNotFound)
return return
} }