健康检查支持UserAgent和是否基础请求设置

This commit is contained in:
刘祥超
2021-10-19 16:31:05 +08:00
parent 1d0a66a156
commit cfb1864fd2

View File

@@ -10,8 +10,10 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeutils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
"net"
"net/http"
@@ -167,11 +169,28 @@ func (this *HealthCheckExecutor) checkNode(healthCheckConfig *serverconfigs.Heal
result.NodeAddr = "[" + result.NodeAddr + "]"
}
if len(healthCheckConfig.URL) == 0 {
healthCheckConfig.URL = "http://${host}/"
}
url := strings.ReplaceAll(healthCheckConfig.URL, "${host}", result.NodeAddr)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return err
}
if len(healthCheckConfig.UserAgent) > 0 {
req.Header.Set("User-Agent", healthCheckConfig.UserAgent)
} else {
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36")
}
key, err := nodeutils.EncryptData(result.Node.UniqueId, result.Node.Secret, maps.Map{
"onlyBasicRequest": healthCheckConfig.OnlyBasicRequest,
}, 300)
if err != nil {
return err
}
req.Header.Set(serverconfigs.HealthCheckHeaderName, key)
timeout := 5 * time.Second
if healthCheckConfig.Timeout != nil {