Skip to content

Commit ac69151

Browse files
Merge pull request #185 from water-sucks/add-tls-server-name-to-healthchecks
feat(healthcheck): add TLS SNI header to request when needed
2 parents 7f650bb + 8a45f6f commit ac69151

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

healthcheck/healthcheck.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type Config struct {
4848
Headers map[string]string `json:"hcHeaders"`
4949
Method string `json:"hcMethod"`
5050
Status int `json:"hcStatus"` // HTTP status code
51+
TLSServerName string `json:"hcTlsServerName"`
5152
}
5253

5354
// Target represents a health check target with its current status
@@ -70,29 +71,17 @@ type Monitor struct {
7071
targets map[int]*Target
7172
mutex sync.RWMutex
7273
callback StatusChangeCallback
73-
client *http.Client
7474
enforceCert bool
7575
}
7676

7777
// NewMonitor creates a new health check monitor
7878
func NewMonitor(callback StatusChangeCallback, enforceCert bool) *Monitor {
7979
logger.Debug("Creating new health check monitor with certificate enforcement: %t", enforceCert)
8080

81-
// Configure TLS settings based on certificate enforcement
82-
transport := &http.Transport{
83-
TLSClientConfig: &tls.Config{
84-
InsecureSkipVerify: !enforceCert,
85-
},
86-
}
87-
8881
return &Monitor{
8982
targets: make(map[int]*Target),
9083
callback: callback,
9184
enforceCert: enforceCert,
92-
client: &http.Client{
93-
Timeout: 30 * time.Second,
94-
Transport: transport,
95-
},
9685
}
9786
}
9887

@@ -388,6 +377,17 @@ func (m *Monitor) performHealthCheck(target *Target) {
388377
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(target.Config.Timeout)*time.Second)
389378
defer cancel()
390379

380+
client := &http.Client{
381+
Transport: &http.Transport{
382+
TLSClientConfig: &tls.Config{
383+
// Configure TLS settings based on certificate enforcement
384+
InsecureSkipVerify: !m.enforceCert,
385+
// Use SNI TLS header if present
386+
ServerName: target.Config.TLSServerName,
387+
},
388+
},
389+
}
390+
391391
req, err := http.NewRequestWithContext(ctx, target.Config.Method, url, nil)
392392
if err != nil {
393393
target.Status = StatusUnhealthy
@@ -402,7 +402,7 @@ func (m *Monitor) performHealthCheck(target *Target) {
402402
}
403403

404404
// Perform request
405-
resp, err := m.client.Do(req)
405+
resp, err := client.Do(req)
406406
if err != nil {
407407
target.Status = StatusUnhealthy
408408
target.LastError = fmt.Sprintf("request failed: %v", err)

0 commit comments

Comments
 (0)