From bc5fb91c055eb1befdad5d5051c91290fab3d816 Mon Sep 17 00:00:00 2001 From: Kaive Young Date: Wed, 20 Mar 2024 14:58:03 +0800 Subject: [PATCH] add header for http healthcheck (#4085) --- client/health/health.go | 12 ++++++++++-- conf/frpc_full_example.toml | 4 ++++ pkg/config/v1/common.go | 5 +++++ pkg/config/v1/proxy.go | 3 +++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/client/health/health.go b/client/health/health.go index 7147f77..d298e61 100644 --- a/client/health/health.go +++ b/client/health/health.go @@ -40,8 +40,8 @@ type Monitor struct { addr string // For http - url string - + url string + header http.Header failedTimes uint64 statusOK bool statusNormalFn func() @@ -73,6 +73,11 @@ func NewMonitor(ctx context.Context, cfg v1.HealthCheckConfig, addr string, } url = s + cfg.Path } + header := make(http.Header) + for _, h := range cfg.HTTPHeaders { + header.Set(h.Name, h.Value) + } + return &Monitor{ checkType: cfg.Type, interval: time.Duration(cfg.IntervalSeconds) * time.Second, @@ -80,6 +85,7 @@ func NewMonitor(ctx context.Context, cfg v1.HealthCheckConfig, addr string, maxFailedTimes: cfg.MaxFailed, addr: addr, url: url, + header: header, statusOK: false, statusNormalFn: statusNormalFn, statusFailedFn: statusFailedFn, @@ -163,6 +169,8 @@ func (monitor *Monitor) doHTTPCheck(ctx context.Context) error { if err != nil { return err } + req.Header = monitor.header + 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 59de6fa..4dae6b3 100644 --- a/conf/frpc_full_example.toml +++ b/conf/frpc_full_example.toml @@ -216,6 +216,10 @@ healthCheck.path = "/status" healthCheck.intervalSeconds = 10 healthCheck.maxFailed = 3 healthCheck.timeoutSeconds = 3 +# set health check headers +healthCheck.httpheaders=[ + { name = "x-from-where", value = "frp" } +] [[proxies]] name = "web02" diff --git a/pkg/config/v1/common.go b/pkg/config/v1/common.go index 24ec9b0..ddb2335 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 c906543..9d91b58 100644 --- a/pkg/config/v1/proxy.go +++ b/pkg/config/v1/proxy.go @@ -97,6 +97,9 @@ 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". + HTTPHeaders []HTTPHeader `json:"httpHeaders,omitempty"` } type DomainConfig struct {