From 5507467fb3789964bc88c63f513df6cd7ecf4f88 Mon Sep 17 00:00:00 2001 From: Kaive Young Date: Tue, 19 Mar 2024 16:18:28 +0800 Subject: [PATCH 1/3] add header for http healthcheck --- client/health/health.go | 14 ++++++++++++-- conf/frpc_full_example.toml | 5 +++++ pkg/config/v1/common.go | 5 +++++ pkg/config/v1/proxy.go | 6 ++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/client/health/health.go b/client/health/health.go index 7147f779..bc08540a 100644 --- a/client/health/health.go +++ b/client/health/health.go @@ -40,8 +40,9 @@ type Monitor struct { addr string // For http - url string - + url string + header http.Header + host string failedTimes uint64 statusOK bool statusNormalFn func() @@ -73,6 +74,11 @@ func NewMonitor(ctx context.Context, cfg v1.HealthCheckConfig, addr string, } url = s + cfg.Path } + header := make(http.Header) + for _, h := range cfg.Headers { + header.Set(h.Name, h.Value) + } + return &Monitor{ checkType: cfg.Type, interval: time.Duration(cfg.IntervalSeconds) * time.Second, @@ -80,6 +86,8 @@ func NewMonitor(ctx context.Context, cfg v1.HealthCheckConfig, addr string, maxFailedTimes: cfg.MaxFailed, addr: addr, url: url, + header: header, + host: cfg.HostHeaderRewrite, statusOK: false, statusNormalFn: statusNormalFn, statusFailedFn: statusFailedFn, @@ -163,6 +171,8 @@ func (monitor *Monitor) doHTTPCheck(ctx context.Context) error { if err != nil { return err } + req.Header = monitor.header + req.Host = monitor.host resp, err := http.DefaultClient.Do(req) if err != nil { return err diff --git a/conf/frpc_full_example.toml b/conf/frpc_full_example.toml index 59de6fa4..42f77c8c 100644 --- a/conf/frpc_full_example.toml +++ b/conf/frpc_full_example.toml @@ -216,6 +216,11 @@ healthCheck.path = "/status" healthCheck.intervalSeconds = 10 healthCheck.maxFailed = 3 healthCheck.timeoutSeconds = 3 +# set health check headers +healthCheck.headers=[ + { name = "x-from-where", value = "frp" } +] +healthCheck.hostHeaderRewrite = "example.com" [[proxies]] name = "web02" diff --git a/pkg/config/v1/common.go b/pkg/config/v1/common.go index 24ec9b0d..ddb23356 100644 --- a/pkg/config/v1/common.go +++ b/pkg/config/v1/common.go @@ -129,3 +129,8 @@ type HTTPPluginOptions struct { type HeaderOperations struct { Set map[string]string `json:"set,omitempty"` } + +type HTTPHeader struct { + Name string `json:"name"` + Value string `json:"value"` +} diff --git a/pkg/config/v1/proxy.go b/pkg/config/v1/proxy.go index c9065432..c17dd6fd 100644 --- a/pkg/config/v1/proxy.go +++ b/pkg/config/v1/proxy.go @@ -97,6 +97,12 @@ type HealthCheckConfig struct { // Path specifies the path to send health checks to if the // health check type is "http". Path string `json:"path,omitempty"` + // Headers specifies the headers to send health checks to if the + // health check type is "http". + Headers []HTTPHeader `json:"headers,omitempty"` + // HostHeaderRewrite specifies the request host to send health checks to if the + // health check type is "http". + HostHeaderRewrite string `json:"hostHeaderRewrite,omitempty"` } type DomainConfig struct { From bb7d51b0db3734908d01de6d8428ba920d741aaf Mon Sep 17 00:00:00 2001 From: Kaive Young Date: Wed, 20 Mar 2024 10:45:36 +0800 Subject: [PATCH 2/3] add header for http health check --- client/health/health.go | 4 +--- conf/frpc_full_example.toml | 1 - pkg/config/v1/proxy.go | 3 --- 3 files changed, 1 insertion(+), 7 deletions(-) diff --git a/client/health/health.go b/client/health/health.go index bc08540a..34c85da5 100644 --- a/client/health/health.go +++ b/client/health/health.go @@ -42,7 +42,6 @@ type Monitor struct { // For http url string header http.Header - host string failedTimes uint64 statusOK bool statusNormalFn func() @@ -87,7 +86,6 @@ func NewMonitor(ctx context.Context, cfg v1.HealthCheckConfig, addr string, addr: addr, url: url, header: header, - host: cfg.HostHeaderRewrite, statusOK: false, statusNormalFn: statusNormalFn, statusFailedFn: statusFailedFn, @@ -172,7 +170,7 @@ func (monitor *Monitor) doHTTPCheck(ctx context.Context) error { return err } req.Header = monitor.header - req.Host = monitor.host + req.Host = monitor.header.Get("Host") resp, err := http.DefaultClient.Do(req) if err != nil { return err diff --git a/conf/frpc_full_example.toml b/conf/frpc_full_example.toml index 42f77c8c..1e4c9a53 100644 --- a/conf/frpc_full_example.toml +++ b/conf/frpc_full_example.toml @@ -220,7 +220,6 @@ healthCheck.timeoutSeconds = 3 healthCheck.headers=[ { name = "x-from-where", value = "frp" } ] -healthCheck.hostHeaderRewrite = "example.com" [[proxies]] name = "web02" diff --git a/pkg/config/v1/proxy.go b/pkg/config/v1/proxy.go index c17dd6fd..f45c001a 100644 --- a/pkg/config/v1/proxy.go +++ b/pkg/config/v1/proxy.go @@ -100,9 +100,6 @@ type HealthCheckConfig struct { // Headers specifies the headers to send health checks to if the // health check type is "http". Headers []HTTPHeader `json:"headers,omitempty"` - // HostHeaderRewrite specifies the request host to send health checks to if the - // health check type is "http". - HostHeaderRewrite string `json:"hostHeaderRewrite,omitempty"` } type DomainConfig struct { From adb4d3d33d1e174e252acf1d55a07758a6c3d0e1 Mon Sep 17 00:00:00 2001 From: Kaive Young Date: Wed, 20 Mar 2024 10:52:09 +0800 Subject: [PATCH 3/3] add header for http health check --- client/health/health.go | 2 +- conf/frpc_full_example.toml | 2 +- pkg/config/v1/proxy.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/client/health/health.go b/client/health/health.go index 34c85da5..d298e615 100644 --- a/client/health/health.go +++ b/client/health/health.go @@ -74,7 +74,7 @@ func NewMonitor(ctx context.Context, cfg v1.HealthCheckConfig, addr string, url = s + cfg.Path } header := make(http.Header) - for _, h := range cfg.Headers { + for _, h := range cfg.HTTPHeaders { header.Set(h.Name, h.Value) } diff --git a/conf/frpc_full_example.toml b/conf/frpc_full_example.toml index 1e4c9a53..4dae6b3c 100644 --- a/conf/frpc_full_example.toml +++ b/conf/frpc_full_example.toml @@ -217,7 +217,7 @@ healthCheck.intervalSeconds = 10 healthCheck.maxFailed = 3 healthCheck.timeoutSeconds = 3 # set health check headers -healthCheck.headers=[ +healthCheck.httpheaders=[ { name = "x-from-where", value = "frp" } ] diff --git a/pkg/config/v1/proxy.go b/pkg/config/v1/proxy.go index f45c001a..9d91b585 100644 --- a/pkg/config/v1/proxy.go +++ b/pkg/config/v1/proxy.go @@ -99,7 +99,7 @@ type HealthCheckConfig struct { Path string `json:"path,omitempty"` // Headers specifies the headers to send health checks to if the // health check type is "http". - Headers []HTTPHeader `json:"headers,omitempty"` + HTTPHeaders []HTTPHeader `json:"httpHeaders,omitempty"` } type DomainConfig struct {