From 1c173839db44bbf3e7d0f69187c7827847e99e37 Mon Sep 17 00:00:00 2001 From: Kaive Young Date: Tue, 19 Mar 2024 18:04:26 +0800 Subject: [PATCH] add header for http healthcheck --- client/health/health.go | 11 +++++------ conf/frpc_full_example.toml | 6 +++++- pkg/config/v1/common.go | 5 +++++ pkg/config/v1/proxy.go | 5 ++++- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/client/health/health.go b/client/health/health.go index 0f4d5ed8..bc08540a 100644 --- a/client/health/health.go +++ b/client/health/health.go @@ -42,6 +42,7 @@ type Monitor struct { // For http url string header http.Header + host string failedTimes uint64 statusOK bool statusNormalFn func() @@ -74,8 +75,8 @@ func NewMonitor(ctx context.Context, cfg v1.HealthCheckConfig, addr string, url = s + cfg.Path } header := make(http.Header) - for k, v := range cfg.Headers.Set { - header.Set(k, v) + for _, h := range cfg.Headers { + header.Set(h.Name, h.Value) } return &Monitor{ @@ -86,6 +87,7 @@ 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, @@ -170,10 +172,7 @@ func (monitor *Monitor) doHTTPCheck(ctx context.Context) error { return err } req.Header = monitor.header - for k, v := range req.Header { - fmt.Println(k, v) - - } + 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 d70ef28f..42f77c8c 100644 --- a/conf/frpc_full_example.toml +++ b/conf/frpc_full_example.toml @@ -217,7 +217,11 @@ healthCheck.intervalSeconds = 10 healthCheck.maxFailed = 3 healthCheck.timeoutSeconds = 3 # set health check headers -healthCheck.headers.set.Host= = "example.com" +healthCheck.headers=[ + { name = "x-from-where", value = "frp" } +] +healthCheck.hostHeaderRewrite = "example.com" + [[proxies]] name = "web02" type = "https" 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 4c0d438f..c17dd6fd 100644 --- a/pkg/config/v1/proxy.go +++ b/pkg/config/v1/proxy.go @@ -99,7 +99,10 @@ 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 HeaderOperations `json:"headers,omitempty"` + 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 {