2020-09-27 15:26:06 +08:00
|
|
|
package nodes
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
2024-07-27 15:42:50 +08:00
|
|
|
|
|
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
2020-09-27 15:26:06 +08:00
|
|
|
)
|
|
|
|
|
|
2021-04-19 19:29:32 +08:00
|
|
|
// HTTPClient HTTP客户端
|
2020-09-27 15:26:06 +08:00
|
|
|
type HTTPClient struct {
|
2024-04-17 18:24:21 +08:00
|
|
|
rawClient *http.Client
|
|
|
|
|
accessAt int64
|
|
|
|
|
isProxyProtocol bool
|
2020-09-27 15:26:06 +08:00
|
|
|
}
|
|
|
|
|
|
2021-04-19 19:29:32 +08:00
|
|
|
// NewHTTPClient 获取新客户端对象
|
2024-04-17 18:24:21 +08:00
|
|
|
func NewHTTPClient(rawClient *http.Client, isProxyProtocol bool) *HTTPClient {
|
2020-09-27 15:26:06 +08:00
|
|
|
return &HTTPClient{
|
2024-04-17 18:24:21 +08:00
|
|
|
rawClient: rawClient,
|
|
|
|
|
accessAt: fasttime.Now().Unix(),
|
|
|
|
|
isProxyProtocol: isProxyProtocol,
|
2020-09-27 15:26:06 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-19 19:29:32 +08:00
|
|
|
// RawClient 获取原始客户端对象
|
2020-09-27 15:26:06 +08:00
|
|
|
func (this *HTTPClient) RawClient() *http.Client {
|
|
|
|
|
return this.rawClient
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-19 19:29:32 +08:00
|
|
|
// UpdateAccessTime 更新访问时间
|
2020-09-27 15:26:06 +08:00
|
|
|
func (this *HTTPClient) UpdateAccessTime() {
|
2023-04-08 12:47:04 +08:00
|
|
|
this.accessAt = fasttime.Now().Unix()
|
2020-09-27 15:26:06 +08:00
|
|
|
}
|
|
|
|
|
|
2021-04-19 19:29:32 +08:00
|
|
|
// AccessTime 获取访问时间
|
2020-09-27 15:26:06 +08:00
|
|
|
func (this *HTTPClient) AccessTime() int64 {
|
|
|
|
|
return this.accessAt
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-17 18:24:21 +08:00
|
|
|
// IsProxyProtocol 判断是否为PROXY Protocol
|
|
|
|
|
func (this *HTTPClient) IsProxyProtocol() bool {
|
|
|
|
|
return this.isProxyProtocol
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-19 19:29:32 +08:00
|
|
|
// Close 关闭
|
2020-09-27 15:26:06 +08:00
|
|
|
func (this *HTTPClient) Close() {
|
|
|
|
|
this.rawClient.CloseIdleConnections()
|
|
|
|
|
}
|