mirror of
https://github.com/cedar2025/hysteria.git
synced 2025-06-09 14:10:00 +00:00
Merge pull request #680 from apernet/wip-masq-log
feat: log (debug) masquerade requests
This commit is contained in:
commit
9e9a820b68
@ -525,16 +525,15 @@ func (c *serverConfig) fillTrafficLogger(hyConfig *server.Config) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *serverConfig) fillMasqHandler(hyConfig *server.Config) error {
|
func (c *serverConfig) fillMasqHandler(hyConfig *server.Config) error {
|
||||||
|
var handler http.Handler
|
||||||
switch strings.ToLower(c.Masquerade.Type) {
|
switch strings.ToLower(c.Masquerade.Type) {
|
||||||
case "", "404":
|
case "", "404":
|
||||||
hyConfig.MasqHandler = http.NotFoundHandler()
|
handler = http.NotFoundHandler()
|
||||||
return nil
|
|
||||||
case "file":
|
case "file":
|
||||||
if c.Masquerade.File.Dir == "" {
|
if c.Masquerade.File.Dir == "" {
|
||||||
return configError{Field: "masquerade.file.dir", Err: errors.New("empty file directory")}
|
return configError{Field: "masquerade.file.dir", Err: errors.New("empty file directory")}
|
||||||
}
|
}
|
||||||
hyConfig.MasqHandler = http.FileServer(http.Dir(c.Masquerade.File.Dir))
|
handler = http.FileServer(http.Dir(c.Masquerade.File.Dir))
|
||||||
return nil
|
|
||||||
case "proxy":
|
case "proxy":
|
||||||
if c.Masquerade.Proxy.URL == "" {
|
if c.Masquerade.Proxy.URL == "" {
|
||||||
return configError{Field: "masquerade.proxy.url", Err: errors.New("empty proxy url")}
|
return configError{Field: "masquerade.proxy.url", Err: errors.New("empty proxy url")}
|
||||||
@ -543,7 +542,7 @@ func (c *serverConfig) fillMasqHandler(hyConfig *server.Config) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return configError{Field: "masquerade.proxy.url", Err: err}
|
return configError{Field: "masquerade.proxy.url", Err: err}
|
||||||
}
|
}
|
||||||
hyConfig.MasqHandler = &httputil.ReverseProxy{
|
handler = &httputil.ReverseProxy{
|
||||||
Rewrite: func(r *httputil.ProxyRequest) {
|
Rewrite: func(r *httputil.ProxyRequest) {
|
||||||
r.SetURL(u)
|
r.SetURL(u)
|
||||||
// SetURL rewrites the Host header,
|
// SetURL rewrites the Host header,
|
||||||
@ -557,10 +556,11 @@ func (c *serverConfig) fillMasqHandler(hyConfig *server.Config) error {
|
|||||||
w.WriteHeader(http.StatusBadGateway)
|
w.WriteHeader(http.StatusBadGateway)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
default:
|
default:
|
||||||
return configError{Field: "masquerade.type", Err: errors.New("unsupported masquerade type")}
|
return configError{Field: "masquerade.type", Err: errors.New("unsupported masquerade type")}
|
||||||
}
|
}
|
||||||
|
hyConfig.MasqHandler = &masqHandlerLogWrapper{H: handler}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Config validates the fields and returns a ready-to-use Hysteria server config
|
// Config validates the fields and returns a ready-to-use Hysteria server config
|
||||||
@ -669,3 +669,12 @@ func (l *serverLogger) UDPError(addr net.Addr, id string, sessionID uint32, err
|
|||||||
logger.Error("UDP error", zap.String("addr", addr.String()), zap.String("id", id), zap.Uint32("sessionID", sessionID), zap.Error(err))
|
logger.Error("UDP error", zap.String("addr", addr.String()), zap.String("id", id), zap.Uint32("sessionID", sessionID), zap.Error(err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type masqHandlerLogWrapper struct {
|
||||||
|
H http.Handler
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *masqHandlerLogWrapper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
logger.Debug("masquerade request", zap.String("addr", r.RemoteAddr), zap.String("method", r.Method), zap.String("host", r.Host), zap.String("url", r.URL.String()))
|
||||||
|
m.H.ServeHTTP(w, r)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user