proxy: support regex match in http(s) health checking

This commit is contained in:
nadoo committed 2021-12-23 00:17:28 +08:00
1 parent 71c7cd2823
commit 55ab44fc90
4 files changed
+23 -11

No files matched your search

+17 -6
View File
@@ -8,6 +8,7 @@ import (
"net"
"os"
"os/exec"
"regexp"
"strings"
"time"
@@ -44,16 +45,26 @@ func (c *tcpChecker) Check(dialer proxy.Dialer) (time.Duration, error) {
}
type httpChecker struct {
addr string
uri string
expect string
timeout time.Duration
addr string
uri string
expect string
timeout time.Duration
tlsConfig *tls.Config
serverName string
regex *regexp.Regexp
}
func newHttpChecker(addr, uri, expect string, timeout time.Duration, withTLS bool) *httpChecker {
c := &httpChecker{addr: addr, uri: uri, expect: expect, timeout: timeout}
c := &httpChecker{
addr: addr,
uri: uri,
expect: expect,
timeout: timeout,
regex: regexp.MustCompile(expect),
}
if _, p, _ := net.SplitHostPort(addr); p == "" {
if withTLS {
c.addr = net.JoinHostPort(addr, "443")
@@ -103,7 +114,7 @@ func (c *httpChecker) Check(dialer proxy.Dialer) (time.Duration, error) {
return 0, err
}
if !strings.Contains(line, c.expect) {
if !c.regex.MatchString(line) {
return 0, fmt.Errorf("expect: %s, got: %s", c.expect, line)
}