add header for http healthcheck

This commit is contained in:
Kaive Young 2024-03-19 18:04:26 +08:00
parent f095130823
commit 1c173839db
No known key found for this signature in database
GPG Key ID: 6270A77955E33D09
4 changed files with 19 additions and 8 deletions

View File

@ -42,6 +42,7 @@ type Monitor struct {
// For http // For http
url string url string
header http.Header header http.Header
host string
failedTimes uint64 failedTimes uint64
statusOK bool statusOK bool
statusNormalFn func() statusNormalFn func()
@ -74,8 +75,8 @@ func NewMonitor(ctx context.Context, cfg v1.HealthCheckConfig, addr string,
url = s + cfg.Path url = s + cfg.Path
} }
header := make(http.Header) header := make(http.Header)
for k, v := range cfg.Headers.Set { for _, h := range cfg.Headers {
header.Set(k, v) header.Set(h.Name, h.Value)
} }
return &Monitor{ return &Monitor{
@ -86,6 +87,7 @@ func NewMonitor(ctx context.Context, cfg v1.HealthCheckConfig, addr string,
addr: addr, addr: addr,
url: url, url: url,
header: header, header: header,
host: cfg.HostHeaderRewrite,
statusOK: false, statusOK: false,
statusNormalFn: statusNormalFn, statusNormalFn: statusNormalFn,
statusFailedFn: statusFailedFn, statusFailedFn: statusFailedFn,
@ -170,10 +172,7 @@ func (monitor *Monitor) doHTTPCheck(ctx context.Context) error {
return err return err
} }
req.Header = monitor.header req.Header = monitor.header
for k, v := range req.Header { req.Host = monitor.host
fmt.Println(k, v)
}
resp, err := http.DefaultClient.Do(req) resp, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
return err return err

View File

@ -217,7 +217,11 @@ healthCheck.intervalSeconds = 10
healthCheck.maxFailed = 3 healthCheck.maxFailed = 3
healthCheck.timeoutSeconds = 3 healthCheck.timeoutSeconds = 3
# set health check headers # set health check headers
healthCheck.headers.set.Host= = "example.com" healthCheck.headers=[
{ name = "x-from-where", value = "frp" }
]
healthCheck.hostHeaderRewrite = "example.com"
[[proxies]] [[proxies]]
name = "web02" name = "web02"
type = "https" type = "https"

View File

@ -129,3 +129,8 @@ type HTTPPluginOptions struct {
type HeaderOperations struct { type HeaderOperations struct {
Set map[string]string `json:"set,omitempty"` Set map[string]string `json:"set,omitempty"`
} }
type HTTPHeader struct {
Name string `json:"name"`
Value string `json:"value"`
}

View File

@ -99,7 +99,10 @@ type HealthCheckConfig struct {
Path string `json:"path,omitempty"` Path string `json:"path,omitempty"`
// Headers specifies the headers to send health checks to if the // Headers specifies the headers to send health checks to if the
// health check type is "http". // 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 { type DomainConfig struct {